2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00

fix: update PurchaseInvoice to use pesa

This commit is contained in:
18alantom 2021-12-27 17:04:34 +05:30
parent 4385a93a77
commit 1e6113e92f
4 changed files with 48 additions and 54 deletions

View File

@ -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,
},
{

View File

@ -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),
},
],
};

View File

@ -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,
},
],
};

View File

@ -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);
}
};
}