Fix incorrect calculation of cost basis of negative quantities of commodities specified in total price terms

This commit is contained in:
RunasSudo 2025-01-27 23:09:02 +11:00
parent 226ef6dac0
commit b8223aef05
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -1,6 +1,6 @@
/*
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 20222025 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
@ -58,7 +58,10 @@ export function asCost(quantity: number, commodity: string): number {
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));
// Multiply by Math.sign(quantity) in case the quantity is negative
// FIXME: This yields unexpected results when trying to deduct a partial amount from a commodity specified in total price terms
return Math.round(Math.sign(quantity) * price * Math.pow(10, db.metadata.dps));
}
if (commodity.indexOf('{') >= 0) {
// Unit price