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

133 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-10-31 11:43:04 +00:00
const config = require(`${appPath}/core/config`);
const Component = require(`${appPath}/core/component`);
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
2019-07-23 11:41:28 +00:00
const md5 = require('md5');
2020-10-19 06:10:32 +00:00
const fs = require('fs-extra');
2019-02-05 06:58:05 +00:00
module.exports = {
2019-10-31 11:43:04 +00:00
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);
2019-02-05 06:58:05 +00:00
2019-10-31 11:43:04 +00:00
if (!this.ticket)
throw new Error('Something went wrong');
2019-02-05 06:58:05 +00:00
},
data() {
return {totalBalance: 0.00};
},
2019-02-06 09:53:12 +00:00
computed: {
dmsPath() {
2020-10-19 06:10:32 +00:00
if (!this.signature) return;
2019-07-23 11:41:28 +00:00
2020-10-19 06:10:32 +00:00
const hash = md5(this.signature.id.toString()).substring(0, 3);
const file = `${config.storage.root}/${hash}/${this.signature.id}.png`;
if (!fs.existsSync(file)) return null;
2020-10-19 06:10:32 +00:00
const src = fs.readFileSync(file);
const base64 = Buffer.from(src, 'utf8').toString('base64');
return `data:image/png;base64, ${base64}`;
},
serviceTotal() {
let total = 0.00;
this.services.forEach(service => {
total += parseFloat(service.price) * service.quantity;
});
return total;
}
},
2019-02-05 06:58:05 +00:00
methods: {
2019-10-31 11:43:04 +00:00
fetchClient(ticketId) {
2020-09-28 11:54:02 +00:00
return this.findOneFromDef('client', [ticketId]);
2019-02-05 06:58:05 +00:00
},
2019-10-31 11:43:04 +00:00
fetchTicket(ticketId) {
2020-09-28 11:54:02 +00:00
return this.findOneFromDef('ticket', [ticketId]);
},
2019-10-31 11:43:04 +00:00
fetchAddress(ticketId) {
2020-09-28 11:54:02 +00:00
return this.findOneFromDef(`address`, [ticketId]);
},
2020-09-25 12:45:00 +00:00
fetchSignature(ticketId) {
2020-09-28 11:54:02 +00:00
return this.findOneFromDef('signature', [ticketId]);
2019-02-05 06:58:05 +00:00
},
2019-10-31 11:43:04 +00:00
fetchTaxes(ticketId) {
2020-09-28 11:54:02 +00:00
return this.findOneFromDef(`taxes`, [ticketId]);
2020-09-25 12:45:00 +00:00
},
fetchSales(ticketId) {
2020-09-28 11:54:02 +00:00
return this.rawSqlFromDef('sales', [ticketId]);
},
2019-10-31 11:43:04 +00:00
fetchPackagings(ticketId) {
2020-09-28 11:54:02 +00:00
return this.rawSqlFromDef('packagings', [ticketId]);
},
2019-10-31 11:43:04 +00:00
fetchServices(ticketId) {
2020-09-28 11:54:02 +00:00
return this.rawSqlFromDef('services', [ticketId]);
},
2020-09-25 12:45:00 +00:00
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();
2020-12-16 08:24:11 +00:00
},
getBotanical() {
let phytosanitary = [];
this.sales.forEach(sale => {
if (sale.botanical)
phytosanitary.push(sale.botanical);
2020-12-16 08:24:11 +00:00
});
return phytosanitary.filter((item, index) =>
phytosanitary.indexOf(item) == index
).join(', ');
2022-03-23 08:27:10 +00:00
},
footerType() {
const translatedType = this.$t(this.type);
return `${translatedType} ${this.ticketId}`;
2020-09-25 12:45:00 +00:00
}
2019-02-05 06:58:05 +00:00
},
components: {
2019-10-31 11:43:04 +00:00
'report-header': reportHeader.build(),
'report-footer': reportFooter.build()
2019-02-05 06:58:05 +00:00
},
2019-10-31 11:43:04 +00:00
props: {
ticketId: {
2022-01-17 11:44:43 +00:00
type: [Number, String],
2019-10-31 11:43:04 +00:00
required: true
},
type: {
type: String,
required: true
2019-10-31 11:43:04 +00:00
}
}
2019-02-05 06:58:05 +00:00
};