salix/print/templates/reports/invoiceIn/invoiceIn.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-01-23 12:15:30 +00:00
const vnReport = require('../../../core/mixins/vn-report.js');
2022-10-17 13:13:27 +00:00
module.exports = {
name: 'invoiceIn',
2023-01-23 12:15:30 +00:00
mixins: [vnReport],
2022-10-17 13:13:27 +00:00
async serverPrefetch() {
2023-01-23 12:15:30 +00:00
this.invoice = await this.findOneFromDef('invoice', [this.id]);
this.checkMainEntity(this.invoice);
2022-10-18 12:58:38 +00:00
this.taxes = await this.fetchTaxes(this.id);
2022-10-17 13:13:27 +00:00
2023-01-23 12:15:30 +00:00
let defaultTax = await this.findOneFromDef('defaultTax');
2022-12-16 09:36:18 +00:00
if (defaultTax) {
defaultTax = Object.assign(defaultTax, {
taxableBase: 0,
vat: (this.taxTotal() * defaultTax.rate / 100)
});
this.taxes.push(defaultTax);
}
2023-01-23 12:15:30 +00:00
const entries = await this.rawSqlFromDef('entry', [this.id]);
const buys = await this.rawSqlFromDef('buy', [this.id]);
2022-10-18 12:58:38 +00:00
const map = new Map();
for (let entry of entries) {
entry.buys = [];
map.set(entry.id, entry);
}
for (let buy of buys) {
const entry = map.get(buy.entryFk);
if (entry) entry.buys.push(buy);
}
this.entries = entries;
2022-10-17 13:13:27 +00:00
},
methods: {
2022-10-19 13:15:54 +00:00
async fetchTaxes(id) {
const taxes = await this.rawSqlFromDef(`taxes`, [id]);
return this.taxVat(taxes);
2022-10-18 12:58:38 +00:00
},
buyImport(buy) {
return buy.quantity * buy.buyingValue;
},
entrySubtotal(entry) {
let subTotal = 0.00;
for (let buy of entry.buys)
subTotal += this.buyImport(buy);
return subTotal;
},
sumTotal(rows, prop) {
let total = 0.00;
for (let row of rows)
total += parseFloat(row[prop]);
return total;
2022-10-19 13:15:54 +00:00
},
taxVat(taxes) {
for (tax of taxes) {
let vat = 0;
if (tax.rate && tax.taxableBase)
vat = (tax.rate / 100) * tax.taxableBase;
tax.vat = vat;
}
return taxes;
},
taxTotal() {
const base = this.sumTotal(this.taxes, 'taxableBase');
const vat = this.sumTotal(this.taxes, 'vat');
return base + vat;
2022-10-17 13:13:27 +00:00
}
},
props: {
id: {
2022-11-02 12:55:06 +00:00
type: Number,
2022-10-17 13:13:27 +00:00
description: 'The invoice id'
}
}
};