salix/print/templates/reports/delivery-note/delivery-note.js

107 lines
3.3 KiB
JavaScript
Executable File

const config = require(`${appPath}/core/config`);
const Component = require(`${appPath}/core/component`);
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
const md5 = require('md5');
module.exports = {
name: 'delivery-note',
async serverPrefetch() {
this.client = await this.fetchClient(this.ticketId);
this.ticket = await this.fetchTicket(this.ticketId);
this.sales = await this.fetchSales(this.ticketId);
this.address = await this.fetchAddress(this.ticketId);
this.services = await this.fetchServices(this.ticketId);
this.taxes = await this.fetchTaxes(this.ticketId);
this.packagings = await this.fetchPackagings(this.ticketId);
this.signature = await this.fetchSignature(this.ticketId);
if (!this.ticket)
throw new Error('Something went wrong');
},
data() {
return {totalBalance: 0.00};
},
computed: {
dmsPath() {
const pathHash = md5(this.signature.id.toString()).substring(0, 3);
const hostPath = `file:///${config.storage.root}/${pathHash}`;
if (this.signature && this.signature.id)
return `${hostPath}/${this.signature.id}.png`;
},
serviceTotal() {
let total = 0.00;
this.services.forEach(service => {
total += parseFloat(service.price) * service.quantity;
});
return total;
}
},
methods: {
fetchClient(ticketId) {
return this.findOneFromDef('client', [ticketId]);
},
fetchTicket(ticketId) {
return this.findOneFromDef('ticket', [ticketId]);
},
fetchAddress(ticketId) {
return this.findOneFromDef(`address`, [ticketId]);
},
fetchSignature(ticketId) {
return this.findOneFromDef('signature', [ticketId]);
},
fetchTaxes(ticketId) {
return this.findOneFromDef(`taxes`, [ticketId]);
},
fetchSales(ticketId) {
return this.rawSqlFromDef('sales', [ticketId]);
},
fetchPackagings(ticketId) {
return this.rawSqlFromDef('packagings', [ticketId]);
},
fetchServices(ticketId) {
return this.rawSqlFromDef('services', [ticketId]);
},
getSubTotal() {
let subTotal = 0.00;
this.sales.forEach(sale => {
subTotal += sale.quantity * sale.price * (1 - sale.discount / 100);
});
return subTotal;
},
getTotalBase() {
let totalBase = 0.00;
this.taxes.forEach(tax => {
totalBase += parseFloat(tax.Base);
});
return totalBase;
},
getTotalTax() {
let totalTax = 0.00;
this.taxes.forEach(tax => {
totalTax += parseFloat(tax.tax);
});
return totalTax;
},
getTotal() {
return this.getTotalBase() + this.getTotalTax();
}
},
components: {
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
},
props: {
ticketId: {
type: String,
required: true
}
}
};