salix/print/templates/reports/invoice/invoice.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-09-26 11:33:27 +00:00
const Report = require(`vn-print/core/report`);
2023-01-23 12:15:30 +00:00
const vnReport = require('../../../core/mixins/vn-report.js');
2021-03-01 10:25:37 +00:00
const invoiceIncoterms = new Report('invoice-incoterms');
2021-02-17 12:00:06 +00:00
module.exports = {
name: 'invoice',
2023-01-23 12:15:30 +00:00
mixins: [vnReport],
2021-02-17 12:00:06 +00:00
async serverPrefetch() {
2023-01-23 12:15:30 +00:00
this.invoice = await this.findOneFromDef('invoice', [this.reference]);
this.checkMainEntity(this.invoice);
this.client = await this.findOneFromDef('client', [this.reference]);
this.taxes = await this.rawSqlFromDef(`taxes`, [this.reference]);
this.hasIntrastat = await this.findValueFromDef(`hasIntrastat`, [this.reference]);
this.intrastat = await this.rawSqlFromDef(`intrastat`, [
this.reference,
this.reference,
this.reference,
this.reference
]);
2023-01-23 12:15:30 +00:00
this.rectified = await this.rawSqlFromDef(`rectified`, [this.reference]);
this.hasIncoterms = await this.findValueFromDef(`hasIncoterms`, [this.reference]);
2021-02-24 06:51:05 +00:00
2023-01-23 12:15:30 +00:00
const tickets = await this.rawSqlFromDef('tickets', [this.reference]);
const sales = await this.rawSqlFromDef('sales', [this.reference, this.reference]);
2021-02-24 06:51:05 +00:00
const map = new Map();
this.ticketObservations = '';
for (let ticket of tickets) {
ticket.sales = [];
2021-02-24 06:51:05 +00:00
map.set(ticket.id, ticket);
if (ticket.description) this.ticketObservations += ticket.description + ' ';
}
this.ticketObservations = this.ticketObservations.trim();
2021-02-24 06:51:05 +00:00
for (let sale of sales) {
const ticket = map.get(sale.ticketFk);
if (ticket) ticket.sales.push(sale);
2021-02-24 06:51:05 +00:00
}
this.tickets = tickets;
2021-02-17 12:00:06 +00:00
},
data() {
return {totalBalance: 0.00};
},
computed: {
2021-02-24 06:51:05 +00:00
ticketsId() {
const tickets = this.tickets.map(ticket => ticket.id);
2021-02-17 12:00:06 +00:00
2021-02-24 06:51:05 +00:00
return tickets.join(', ');
},
botanical() {
let phytosanitary = [];
for (let ticket of this.tickets) {
for (let sale of ticket.sales) {
if (sale.botanical)
phytosanitary.push(sale.botanical);
}
}
2021-02-17 12:00:06 +00:00
2021-02-24 06:51:05 +00:00
return phytosanitary.filter((item, index) =>
phytosanitary.indexOf(item) == index
).join(', ');
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
taxTotal() {
const base = this.sumTotal(this.taxes, 'base');
const vat = this.sumTotal(this.taxes, 'vat');
return base + vat;
}
2021-02-17 12:00:06 +00:00
},
methods: {
2021-02-25 08:00:49 +00:00
saleImport(sale) {
const price = sale.quantity * sale.price;
return price * (1 - sale.discount / 100);
},
ticketSubtotal(ticket) {
2021-02-17 12:00:06 +00:00
let subTotal = 0.00;
2021-02-25 08:00:49 +00:00
for (let sale of ticket.sales)
subTotal += this.saleImport(sale);
2021-02-17 12:00:06 +00:00
return subTotal;
},
2021-02-24 06:51:05 +00:00
sumTotal(rows, prop) {
let total = 0.00;
for (let row of rows)
total += parseFloat(row[prop]);
2021-02-17 12:00:06 +00:00
2021-02-24 06:51:05 +00:00
return total;
2021-02-17 12:00:06 +00:00
}
},
components: {
2021-03-01 10:25:37 +00:00
'invoice-incoterms': invoiceIncoterms.build()
2021-02-17 12:00:06 +00:00
},
props: {
2022-09-29 05:35:20 +00:00
reference: {
2022-09-26 11:33:27 +00:00
type: String,
description: 'The invoice ref'
2021-02-17 12:00:06 +00:00
}
}
};