var path = require('path'); var database = require(path.join(__dirname, '../../database.js')); var format = require(path.join(__dirname, '../../util/format.js')); module.exports = class DeliveryNote { async getData(params, cb) { let qryHead = `SELECT c.id clientId, c.name clientName, c.socialName, c.street, c.fi, c.postcode, c.city, p.name province, ct.country, a.street, a.city, a.postalCode, t.shipped, tg.gestdoc_id AS docId, g.odbc_date AS docDate FROM ticket t JOIN client c ON c.id = t.clientFk JOIN address a ON a.id = t.addressFk JOIN province p ON p.id = a.provinceFk JOIN country ct ON ct.id = p.countryFk LEFT JOIN vn2008.tickets_gestdoc tg ON tg.Id_Ticket = t.id LEFT JOIN vn2008.gestdoc g ON g.id = tg.gestdoc_id WHERE t.id = ?`; let qryBuys = `SELECT s.id, s.itemFk, s.concept, s.quantity, s.price, s.price - SUM(IF(tcs.tarifa_componentes_series_id = 6, sc.value, 0)) netPrice, s.discount, i.size, i.stems, ic.type taxType, i.category, it.id itemTypeId, o.code AS origin, i.inkFk, s.ticketFk, ita.tag1, ita.val1, ita.tag2, ita.val2, ita.tag3, ita.val3 FROM sale s LEFT JOIN saleComponent sc ON sc.saleFk = s.id LEFT JOIN bi.tarifa_componentes tc ON sc.componentFk = tc.Id_Componente LEFT JOIN bi.tarifa_componentes_series tcs ON tcs.tarifa_componentes_series_id = tc.tarifa_componentes_series_id LEFT JOIN item i ON i.id = s.itemFk LEFT JOIN ticket t ON t.id = s.ticketFk LEFT JOIN origin o ON o.id = i.originFk INNER JOIN supplier sp ON sp.id = t.companyFk INNER JOIN itemType it ON it.id = i.typeFk LEFT JOIN (SELECT * FROM (SELECT tt.countryFk, tcc.taxClassFk, tc.type FROM taxClassCode tcc JOIN taxCode tc ON tc.id = tcc.taxCodeFk JOIN taxType tt ON tt.id = tc.taxTypeFk WHERE tcc.effectived <= '2018-01-29 13:00:00' ORDER BY tcc.effectived DESC) tx GROUP BY tx.countryFk, tx.taxClassFk) ic ON ic.countryFk = sp.countryFk AND ic.taxClassFk = i.taxClassFk JOIN vn.itemTagArranged ita ON ita.itemFk = s.itemFk WHERE s.ticketFk = ? GROUP BY s.id ORDER BY (it.isPackaging), s.concept, s.itemFk`; let qryTax = 'CALL vn.ticketGetTaxAdd(?)'; let qryPackaging = `SELECT t.*, i.name, p.itemFk FROM vn.ticketPackaging t JOIN vn.packaging p ON p.id = t.packagingFk JOIN vn.item i ON i.id = p.itemFk WHERE t.ticketFk = ? ORDER BY itemFk`; try { let [headDetail] = await database.pool.query(qryHead, [params.ticketId]); Object.assign(this, headDetail[0]); if (!headDetail) throw new Error('No head data found'); let bodyDetail = await database.pool.query(qryBuys, [params.ticketId]); this.buys = bodyDetail[0]; if (!bodyDetail) throw new Error('No body data found'); let [ticketTax] = await database.pool.query(qryTax, [params.ticketId]); this.ticketTax = ticketTax[0]; let packaging = await database.pool.query(qryPackaging, [params.ticketId]); this.packaging = packaging[0]; this.ticketId = params.ticketId; this.shipped = format.date(this.shipped); this.docDate = format.date(this.docDate, '%d-%m-%Y %T'); this.setSubTotal(); this.formatBuys(); this.formatPackages(); this.setTotalTaxes(); this.setTotal(); this.formatTaxes(); cb(); } catch (e) { cb(e); } } get hasPackages() { if (this.packaging.length) return true; } setSubTotal() { this.subTotal = 0.00; this.buys.forEach(buy => { buy.netPrice = buy.quantity * buy.price * (1 - buy.discount / 100); this.subTotal += buy.netPrice; }); } formatBuys() { this.buys.forEach(buy => { buy.itemFk = format.code(buy.itemFk, '000000'); buy.quantity = parseInt(buy.quantity); buy.price = format.currency(buy.price); buy.discount = format.currency(buy.discount); buy.netPrice = format.currency(buy.netPrice); }); this.subTotal = format.currency(this.subTotal); } formatPackages() { this.packaging.forEach(item => { item.itemFk = format.code(item.itemFk, '000000'); }); } setTotalTaxes() { this.totalBase = 0.00; this.totalTax = 0.00; this.ticketTax.forEach(tax => { this.totalBase += parseFloat(tax.Base); this.totalTax += parseFloat(tax.tax); }); } setTotal() { this.total = format.currency(this.totalBase + this.totalTax); } formatTaxes() { this.ticketTax.forEach(tax => { tax.Base = format.currency(tax.Base); tax.vatPercent = format.percent(tax.vatPercent); tax.tax = format.currency(tax.tax); }); this.totalBase = format.currency(this.totalBase); this.totalTax = format.currency(this.totalTax); } };