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

133 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-02-17 12:00:06 +00:00
const Component = require(`${appPath}/core/component`);
2021-02-24 06:51:05 +00:00
const Report = require(`${appPath}/core/report`);
2021-02-17 12:00:06 +00:00
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
2021-02-24 06:51:05 +00:00
const db = require(`${appPath}/core/database`);
2021-02-17 12:00:06 +00:00
module.exports = {
name: 'invoice',
async serverPrefetch() {
this.invoice = await this.fetchInvoice(this.invoiceId);
2021-02-18 12:01:01 +00:00
this.client = await this.fetchClient(this.invoiceId);
2021-02-24 06:51:05 +00:00
this.taxes = await this.fetchTaxes(this.invoiceId);
this.intrastat = await this.fetchIntrastat(this.invoiceId);
this.rectified = await this.fetchRectified(this.invoiceId);
const tickets = await this.fetchTickets(this.invoiceId);
const sales = await this.fetchSales(this.invoiceId);
const map = new Map();
for (let ticket of tickets)
map.set(ticket.id, ticket);
for (let sale of sales) {
const ticket = map.get(sale.ticketFk);
if (!ticket.sales) ticket.sales = [];
ticket.sales.push(sale);
}
this.tickets = tickets;
2021-02-17 12:00:06 +00:00
if (!this.invoice)
throw new Error('Something went wrong');
},
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
serviceTotal() {
let total = 0.00;
this.services.forEach(service => {
total += parseFloat(service.price) * service.quantity;
});
return total;
} */
},
methods: {
fetchInvoice(invoiceId) {
return this.findOneFromDef('invoice', [invoiceId]);
},
2021-02-24 06:51:05 +00:00
fetchClient(invoiceId) {
return this.findOneFromDef('client', [invoiceId]);
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
fetchTickets(invoiceId) {
return this.rawSqlFromDef('tickets', [invoiceId]);
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
async fetchSales(invoiceId) {
const connection = await db.pool.getConnection();
await this.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.invoiceTickets`, connection);
await this.rawSqlFromDef('invoiceTickets', [invoiceId], connection);
const sales = this.rawSqlFromDef('sales', connection);
await this.rawSql(`DROP TEMPORARY TABLE tmp.invoiceTickets`, connection);
await connection.release();
return sales;
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
fetchTaxes(invoiceId) {
return this.rawSqlFromDef(`taxes`, [invoiceId]);
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
fetchIntrastat(invoiceId) {
return this.rawSqlFromDef(`intrastat`, [invoiceId]);
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
fetchRectified(invoiceId) {
return this.rawSqlFromDef(`rectified`, [invoiceId]);
2021-02-17 12:00:06 +00:00
},
2021-02-24 06:51:05 +00:00
subTotal(ticket) {
2021-02-17 12:00:06 +00:00
let subTotal = 0.00;
2021-02-24 06:51:05 +00:00
ticket.sales.forEach(sale => {
2021-02-17 12:00:06 +00:00
subTotal += sale.quantity * sale.price * (1 - sale.discount / 100);
});
return subTotal;
},
getTotal() {
return this.getTotalBase() + this.getTotalTax();
},
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: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: {
invoiceId: {
type: String,
required: true
}
}
};