diff --git a/models/doctype/PurchaseInvoice/PurchaseInvoice.js b/models/doctype/PurchaseInvoice/PurchaseInvoice.js index 957c7d64..bf1c447d 100644 --- a/models/doctype/PurchaseInvoice/PurchaseInvoice.js +++ b/models/doctype/PurchaseInvoice/PurchaseInvoice.js @@ -1,5 +1,5 @@ -import { getActions } from '../Transaction/Transaction'; import InvoiceTemplate from '../SalesInvoice/InvoiceTemplate.vue'; +import { getActions } from '../Transaction/Transaction'; import PurchaseInvoice from './PurchaseInvoiceDocument'; export default { @@ -61,7 +61,8 @@ export default { fieldname: 'exchangeRate', label: 'Exchange Rate', fieldtype: 'Float', - formula: (doc) => doc.getExchangeRate(), + default: 1, + formula: async (doc) => await doc.getExchangeRate(), required: true, }, { @@ -75,7 +76,7 @@ export default { fieldname: 'netTotal', label: 'Net Total', fieldtype: 'Currency', - formula: (doc) => doc.getSum('items', 'amount'), + formula: (doc) => doc.getSum('items', 'amount', false), readOnly: 1, getCurrency: (doc) => doc.currency, }, @@ -83,7 +84,7 @@ export default { fieldname: 'baseNetTotal', label: 'Net Total (Company Currency)', fieldtype: 'Currency', - formula: (doc) => doc.netTotal * doc.exchangeRate, + formula: (doc) => doc.netTotal.mul(doc.exchangeRate), readOnly: 1, }, { @@ -106,7 +107,7 @@ export default { fieldname: 'baseGrandTotal', label: 'Grand Total (Company Currency)', fieldtype: 'Currency', - formula: (doc) => doc.grandTotal * doc.exchangeRate, + formula: (doc) => doc.grandTotal.mul(doc.exchangeRate), readOnly: 1, }, { diff --git a/models/doctype/PurchaseInvoiceItem/PurchaseInvoiceItem.js b/models/doctype/PurchaseInvoiceItem/PurchaseInvoiceItem.js index 35f8eef0..dfa031e9 100644 --- a/models/doctype/PurchaseInvoiceItem/PurchaseInvoiceItem.js +++ b/models/doctype/PurchaseInvoiceItem/PurchaseInvoiceItem.js @@ -32,7 +32,7 @@ export default { label: 'Quantity', fieldtype: 'Float', required: 1, - formula: () => 1, + default: 1, }, { fieldname: 'rate', @@ -40,9 +40,9 @@ export default { fieldtype: 'Currency', required: 1, formula: async (row, doc) => { - const baseRate = (await doc.getFrom('Item', row.item, 'rate')) || 0; - const exchangeRate = doc.exchangeRate ?? 1; - return baseRate / exchangeRate; + const baseRate = + (await doc.getFrom('Item', row.item, 'rate')) || frappe.pesa(0); + return baseRate.div(doc.exchangeRate); }, getCurrency: (row, doc) => doc.currency, }, @@ -50,7 +50,7 @@ export default { fieldname: 'baseRate', label: 'Rate (Company Currency)', fieldtype: 'Currency', - formula: (row, doc) => row.rate * doc.exchangeRate, + formula: (row, doc) => row.rate.mul(doc.exchangeRate), readOnly: 1, }, { @@ -76,7 +76,7 @@ export default { label: 'Amount', fieldtype: 'Currency', readOnly: 1, - formula: (row) => row.quantity * row.rate, + formula: (row) => row.rate.mul(row.quantity), getCurrency: (row, doc) => doc.currency, }, { @@ -84,7 +84,7 @@ export default { label: 'Amount (Company Currency)', fieldtype: 'Currency', readOnly: 1, - formula: (row, doc) => row.amount * doc.exchangeRate, + formula: (row, doc) => row.amount.mul(doc.exchangeRate), }, ], }; diff --git a/models/doctype/TaxSummary/TaxSummary.js b/models/doctype/TaxSummary/TaxSummary.js index f5d1f7ba..dc130167 100644 --- a/models/doctype/TaxSummary/TaxSummary.js +++ b/models/doctype/TaxSummary/TaxSummary.js @@ -8,26 +8,26 @@ export default { label: 'Tax Account', fieldtype: 'Link', target: 'Account', - required: 1 + required: 1, }, { fieldname: 'rate', label: 'Rate', fieldtype: 'Float', - required: 1 + required: 1, }, { fieldname: 'amount', label: 'Amount', fieldtype: 'Currency', - required: 1 + required: 1, }, { fieldname: 'baseAmount', label: 'Amount (Company Currency)', fieldtype: 'Currency', - formula: (row, doc) => row.amount * doc.exchangeRate, - readOnly: 1 - } - ] + formula: (row, doc) => row.amount.mul(doc.exchangeRate), + readOnly: 1, + }, + ], }; diff --git a/models/doctype/Transaction/TransactionDocument.js b/models/doctype/Transaction/TransactionDocument.js index f4fa7bfa..3fc7747a 100644 --- a/models/doctype/Transaction/TransactionDocument.js +++ b/models/doctype/Transaction/TransactionDocument.js @@ -1,11 +1,10 @@ -import BaseDocument from 'frappejs/model/document'; import frappe from 'frappejs'; -import { round } from 'frappejs/utils/numberFormat'; +import BaseDocument from 'frappejs/model/document'; import { getExchangeRate } from '../../../accounting/exchangeRate'; export default class TransactionDocument extends BaseDocument { async getExchangeRate() { - if (!this.currency) return; + if (!this.currency) return 1.0; let accountingSettings = await frappe.getSingle('AccountingSettings'); const companyCurrency = accountingSettings.currency; @@ -14,7 +13,7 @@ export default class TransactionDocument extends BaseDocument { } return await getExchangeRate({ fromCurrency: this.currency, - toCurrency: companyCurrency + toCurrency: companyCurrency, }); } @@ -22,31 +21,30 @@ export default class TransactionDocument extends BaseDocument { let taxes = {}; for (let row of this.items) { - if (row.tax) { - let tax = await this.getTax(row.tax); - for (let d of tax.details) { - let amount = (row.amount * d.rate) / 100; - taxes[d.account] = taxes[d.account] || { - account: d.account, - rate: d.rate, - amount: 0 - }; - // collect amount - taxes[d.account].amount += amount; - } + if (!row.tax) { + continue; + } + + const tax = await this.getTax(row.tax); + for (let d of tax.details) { + taxes[d.account] = taxes[d.account] || { + account: d.account, + rate: d.rate, + amount: frappe.pesa(0), + }; + + const amount = row.amount.mul(d.rate).div(100); + taxes[d.account].amount = taxes[d.account].amount.add(amount); } } - return ( - Object.keys(taxes) - .map(account => { - let tax = taxes[account]; - tax.baseAmount = round(tax.amount * this.exchangeRate, 2); - return tax; - }) - // clear rows with 0 amount - .filter(tax => tax.amount) - ); + return Object.keys(taxes) + .map((account) => { + const tax = taxes[account]; + tax.baseAmount = tax.amount.mul(this.exchangeRate); + return tax; + }) + .filter((tax) => !tax.amount.isZero()); } async getTax(tax) { @@ -56,13 +54,8 @@ export default class TransactionDocument extends BaseDocument { } async getGrandTotal() { - let grandTotal = this.netTotal; - if (this.taxes) { - for (let row of this.taxes) { - grandTotal += row.amount; - } - } - - return grandTotal; + return (this.taxes || []) + .map(({ amount }) => amount) + .reduce((a, b) => a.add(b), this.netTotal); } -}; +}