From b8223aef05c3d7329e5b3bfbd6d23b36c5c98da9 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 27 Jan 2025 23:09:02 +1100 Subject: [PATCH] Fix incorrect calculation of cost basis of negative quantities of commodities specified in total price terms --- src/amounts.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/amounts.ts b/src/amounts.ts index 7f25600..ea8f828 100644 --- a/src/amounts.ts +++ b/src/amounts.ts @@ -1,6 +1,6 @@ /* DrCr: Web-based double-entry bookkeeping framework - Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo) + Copyright (C) 2022–2025 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