DrCr/src/commodities.ts

39 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-11-16 01:20:18 +11:00
/*
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 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);
}