125 lines
3.9 KiB
JavaScript
Executable File
125 lines
3.9 KiB
JavaScript
Executable File
const Component = require(`${appPath}/core/component`);
|
|
const Report = require(`${appPath}/core/report`);
|
|
const reportHeader = new Component('report-header');
|
|
const reportFooter = new Component('report-footer');
|
|
const db = require(`${appPath}/core/database`);
|
|
|
|
module.exports = {
|
|
name: 'invoice',
|
|
async serverPrefetch() {
|
|
this.invoice = await this.fetchInvoice(this.invoiceId);
|
|
this.client = await this.fetchClient(this.invoiceId);
|
|
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;
|
|
|
|
if (!this.invoice)
|
|
throw new Error('Something went wrong');
|
|
},
|
|
data() {
|
|
return {totalBalance: 0.00};
|
|
},
|
|
computed: {
|
|
ticketsId() {
|
|
const tickets = this.tickets.map(ticket => ticket.id);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
return phytosanitary.filter((item, index) =>
|
|
phytosanitary.indexOf(item) == index
|
|
).join(', ');
|
|
},
|
|
taxTotal() {
|
|
const base = this.sumTotal(this.taxes, 'base');
|
|
const vat = this.sumTotal(this.taxes, 'vat');
|
|
return base + vat;
|
|
}
|
|
},
|
|
methods: {
|
|
fetchInvoice(invoiceId) {
|
|
return this.findOneFromDef('invoice', [invoiceId]);
|
|
},
|
|
fetchClient(invoiceId) {
|
|
return this.findOneFromDef('client', [invoiceId]);
|
|
},
|
|
fetchTickets(invoiceId) {
|
|
return this.rawSqlFromDef('tickets', [invoiceId]);
|
|
},
|
|
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;
|
|
},
|
|
fetchTaxes(invoiceId) {
|
|
return this.rawSqlFromDef(`taxes`, [invoiceId]);
|
|
},
|
|
fetchIntrastat(invoiceId) {
|
|
return this.rawSqlFromDef(`intrastat`, [invoiceId]);
|
|
},
|
|
fetchRectified(invoiceId) {
|
|
return this.rawSqlFromDef(`rectified`, [invoiceId]);
|
|
},
|
|
saleImport(sale) {
|
|
const price = sale.quantity * sale.price;
|
|
|
|
return price * (1 - sale.discount / 100);
|
|
},
|
|
ticketSubtotal(ticket) {
|
|
let subTotal = 0.00;
|
|
for (let sale of ticket.sales)
|
|
subTotal += this.saleImport(sale);
|
|
|
|
return subTotal;
|
|
},
|
|
sumTotal(rows, prop) {
|
|
let total = 0.00;
|
|
for (let row of rows)
|
|
total += parseFloat(row[prop]);
|
|
|
|
return total;
|
|
}
|
|
},
|
|
components: {
|
|
'report-header': reportHeader.build(),
|
|
'report-footer': reportFooter.build()
|
|
},
|
|
props: {
|
|
invoiceId: {
|
|
type: String,
|
|
required: true
|
|
}
|
|
}
|
|
};
|