39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
|
/*
|
||
|
DrCr: Web-based double-entry bookkeeping framework
|
||
|
Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
||
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU Affero General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU Affero General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Affero General Public License
|
||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
import { db } from './db.ts';
|
||
|
|
||
|
export function asCost(quantity: number, commodity: string): number {
|
||
|
// Convert the amount to cost price in the reporting commodity
|
||
|
|
||
|
if (commodity === db.metadata.reporting_commodity) {
|
||
|
return quantity;
|
||
|
}
|
||
|
if (commodity.indexOf('{{') >= 0) {
|
||
|
// Total price
|
||
|
const price = parseFloat(commodity.substring(commodity.indexOf('{{') + 2, commodity.indexOf('}}', commodity.indexOf('{{'))));
|
||
|
return Math.round(price * Math.pow(10, db.metadata.dps));
|
||
|
}
|
||
|
if (commodity.indexOf('{') >= 0) {
|
||
|
// Unit price
|
||
|
const price = parseFloat(commodity.substring(commodity.indexOf('{') + 1, commodity.indexOf('}', commodity.indexOf('{'))));
|
||
|
return Math.round(quantity * price);
|
||
|
}
|
||
|
throw new Error('No cost base specified: ' + quantity + ' ' + commodity);
|
||
|
}
|