Error when inputting commodity with no cost base
This commit is contained in:
parent
65d6a9bf05
commit
c2bfd8ca7a
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
DrCr: Web-based double-entry bookkeeping framework
|
DrCr: Web-based double-entry bookkeeping framework
|
||||||
Copyright (C) 2022–2025 Lee Yingtong Li (RunasSudo)
|
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { db } from './db.ts';
|
import { db } from './db.ts';
|
||||||
|
import { ppWithCommodity } from './display.ts';
|
||||||
|
|
||||||
export interface Amount {
|
export interface Amount {
|
||||||
quantity: number;
|
quantity: number;
|
||||||
@ -56,18 +57,20 @@ export function asCost(quantity: number, commodity: string): number {
|
|||||||
if (commodity === db.metadata.reporting_commodity) {
|
if (commodity === db.metadata.reporting_commodity) {
|
||||||
return quantity;
|
return quantity;
|
||||||
}
|
}
|
||||||
if (commodity.indexOf('{{') >= 0) {
|
if (commodity.indexOf(' {{') >= 0) {
|
||||||
// Total price
|
// Total price
|
||||||
const price = parseFloat(commodity.substring(commodity.indexOf('{{') + 2, commodity.indexOf('}}', commodity.indexOf('{{'))));
|
const price = parseFloat(commodity.substring(commodity.indexOf(' {{') + 3, commodity.indexOf('}}', commodity.indexOf(' {{'))));
|
||||||
|
|
||||||
// Multiply by Math.sign(quantity) in case the quantity is negative
|
// 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
|
// 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));
|
return Math.round(Math.sign(quantity) * price * Math.pow(10, db.metadata.dps));
|
||||||
}
|
}
|
||||||
if (commodity.indexOf('{') >= 0) {
|
if (commodity.indexOf(' {') >= 0) {
|
||||||
// Unit price
|
// Unit price
|
||||||
const price = parseFloat(commodity.substring(commodity.indexOf('{') + 1, commodity.indexOf('}', commodity.indexOf('{'))));
|
const price = parseFloat(commodity.substring(commodity.indexOf(' {') + 2, commodity.indexOf('}', commodity.indexOf(' {'))));
|
||||||
return Math.round(quantity * price);
|
return Math.round(quantity * price);
|
||||||
}
|
}
|
||||||
throw new Error('No cost base specified: ' + quantity + ' ' + commodity);
|
throw new NoCostBaseError('No cost base specified: ' + ppWithCommodity(quantity, commodity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class NoCostBaseError extends Error {}
|
||||||
|
@ -109,6 +109,7 @@
|
|||||||
|
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
import { asCost, NoCostBaseError } from '../amounts.ts';
|
||||||
import { DT_FORMAT, DeserialiseAmountError, Posting, Transaction, db, deserialiseAmount } from '../db.ts';
|
import { DT_FORMAT, DeserialiseAmountError, Posting, Transaction, db, deserialiseAmount } from '../db.ts';
|
||||||
import ComboBoxAccounts from './ComboBoxAccounts.vue';
|
import ComboBoxAccounts from './ComboBoxAccounts.vue';
|
||||||
|
|
||||||
@ -167,6 +168,18 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not in reporting commodity, check the amount specifies a cost base
|
||||||
|
try {
|
||||||
|
asCost(amount_abs.quantity, amount_abs.commodity);
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof NoCostBaseError) {
|
||||||
|
error.value = err.message;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
newTransaction.postings.push({
|
newTransaction.postings.push({
|
||||||
id: posting.id,
|
id: posting.id,
|
||||||
description: posting.description,
|
description: posting.description,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user