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-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',
|
|
|
|
async serverPrefetch() {
|
2022-07-21 08:05:37 +00:00
|
|
|
this.invoice = await this.fetchInvoice(this.refFk);
|
|
|
|
this.client = await this.fetchClient(this.refFk);
|
|
|
|
this.taxes = await this.fetchTaxes(this.refFk);
|
2022-06-29 12:15:22 +00:00
|
|
|
this.intrastat = await this.fetchIntrastat(this.refFk);
|
2022-07-21 08:05:37 +00:00
|
|
|
this.rectified = await this.fetchRectified(this.refFk);
|
|
|
|
this.hasIncoterms = await this.fetchHasIncoterms(this.refFk);
|
2021-02-24 06:51:05 +00:00
|
|
|
|
2022-07-21 08:05:37 +00:00
|
|
|
const tickets = await this.fetchTickets(this.refFk);
|
|
|
|
const sales = await this.fetchSales(this.refFk);
|
2021-02-24 06:51:05 +00:00
|
|
|
|
|
|
|
const map = new Map();
|
|
|
|
|
2021-10-04 06:41:34 +00:00
|
|
|
for (let ticket of tickets) {
|
|
|
|
ticket.sales = [];
|
|
|
|
|
2021-02-24 06:51:05 +00:00
|
|
|
map.set(ticket.id, ticket);
|
2021-10-04 06:41:34 +00:00
|
|
|
}
|
2021-02-24 06:51:05 +00:00
|
|
|
|
|
|
|
for (let sale of sales) {
|
|
|
|
const ticket = map.get(sale.ticketFk);
|
2022-02-16 08:53:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
methods: {
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchInvoice(refFk) {
|
|
|
|
return this.findOneFromDef('invoice', [refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchClient(refFk) {
|
|
|
|
return this.findOneFromDef('client', [refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchTickets(refFk) {
|
|
|
|
return this.rawSqlFromDef('tickets', [refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchSales(refFk) {
|
|
|
|
return this.rawSqlFromDef('sales', [refFk, refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchTaxes(refFk) {
|
|
|
|
return this.rawSqlFromDef(`taxes`, [refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-06-29 12:15:22 +00:00
|
|
|
fetchIntrastat(refFk) {
|
|
|
|
return this.rawSqlFromDef(`intrastat`, [refFk, refFk, refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchRectified(refFk) {
|
|
|
|
return this.rawSqlFromDef(`rectified`, [refFk]);
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
2022-07-21 08:05:37 +00:00
|
|
|
fetchHasIncoterms(refFk) {
|
|
|
|
return this.findValueFromDef(`hasIncoterms`, [refFk]);
|
2021-03-01 10:25:37 +00:00
|
|
|
},
|
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: {
|
|
|
|
'report-header': reportHeader.build(),
|
2021-03-01 10:25:37 +00:00
|
|
|
'report-footer': reportFooter.build(),
|
|
|
|
'invoice-incoterms': invoiceIncoterms.build()
|
2021-02-17 12:00:06 +00:00
|
|
|
},
|
|
|
|
props: {
|
2022-06-29 12:15:22 +00:00
|
|
|
refFk: {
|
|
|
|
type: String
|
2021-02-17 12:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|