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

Fix Invoice

This commit is contained in:
Revant Nandgaonkar 2018-03-19 20:38:10 +05:30
parent edb0924375
commit 4d3122b569
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,72 @@
const BaseDocument = require('frappejs/model/document');
const frappe = require('frappejs');
module.exports = class Invoice extends BaseDocument {
async getRowTax(row) {
if (row.tax) {
let tax = await this.getTax(row.tax);
let taxAmount = [];
for (let d of (tax.details || [])) {
taxAmount.push({account: d.account, rate: d.rate, amount: row.amount * d.rate / 100});
}
return JSON.stringify(taxAmount);
} else {
return '';
}
}
async getTax(tax) {
if (!this._taxes) this._taxes = {};
if (!this._taxes[tax]) this._taxes[tax] = await frappe.getDoc('Tax', tax);
return this._taxes[tax];
}
makeTaxSummary() {
if (!this.taxes) this.taxes = [];
// reset tax amount
this.taxes.map(d => { d.amount = 0; d.rate = 0; });
// calculate taxes
for (let row of this.items) {
if (row.taxAmount) {
let taxAmount = JSON.parse(row.taxAmount);
for (let rowTaxDetail of taxAmount) {
let found = false;
// check if added in summary
for (let taxDetail of this.taxes) {
if (taxDetail.account === rowTaxDetail.account) {
taxDetail.rate = rowTaxDetail.rate;
taxDetail.amount += rowTaxDetail.amount;
found = true;
}
}
// add new row
if (!found) {
this.taxes.push({
account: rowTaxDetail.account,
rate: rowTaxDetail.rate,
amount: rowTaxDetail.amount
});
}
}
}
}
// clear no taxes
this.taxes = this.taxes.filter(d => d.amount);
}
getGrandTotal() {
this.makeTaxSummary();
let grandTotal = this.netTotal;
if (this.taxes) {
for (let row of this.taxes) {
grandTotal += row.amount;
}
}
return grandTotal;
}
}

View File

@ -0,0 +1,13 @@
const BaseList = require('frappejs/client/view/list');
const frappe = require('frappejs');
module.exports = class InvoiceList extends BaseList {
getFields() {
return ['name', 'customer', 'grandTotal', 'submitted'];
}
getRowHTML(data) {
return `<div class="col-3">${this.getNameHTML(data)}</div>
<div class="col-4 text-muted">${data.customer}</div>
<div class="col-4 text-muted text-right">${frappe.format(data.grandTotal, "Currency")}</div>`;
}
}