diff --git a/db/routines/vn/procedures/ticketComponentUpdateSale.sql b/db/routines/vn/procedures/ticketComponentUpdateSale.sql index b3a6ec7165..b2d0a567af 100644 --- a/db/routines/vn/procedures/ticketComponentUpdateSale.sql +++ b/db/routines/vn/procedures/ticketComponentUpdateSale.sql @@ -54,38 +54,31 @@ BEGIN WHERE tp.id IS NULL HAVING resto <> 0) t; - IF vCode <> 'renewPrices' THEN - REPLACE INTO saleComponent(saleFk, componentFk, value) - SELECT s.id, vComponentFk, ROUND((s.price * (100 - s.discount) / 100) - SUM(sc.value), 3) dif - FROM sale s - JOIN tmp.sale tmps ON tmps.saleFk = s.id - LEFT JOIN saleComponent sc ON sc.saleFk = s.id - WHERE sc.saleFk <> vComponentFk - GROUP BY s.id - HAVING dif <> 0; - ELSE - UPDATE sale s - JOIN ticket t ON t.id = s.ticketFk - JOIN item i on i.id = s.itemFk - JOIN itemType it on it.id = i.typeFk - JOIN (SELECT SUM(sc.value) sumValue, sc.saleFk - FROM saleComponent sc - JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk - GROUP BY sc.saleFk) sc ON sc.saleFk = s.id - SET s.price = sumValue / ((100 - s.discount) / 100), - s.foreignPrice = currency_getRate(t.currencyFk, NULL) * (sumValue / ((100 - s.discount) / 100)) - WHERE it.code <> 'PRT' ; - - REPLACE INTO saleComponent(saleFk, componentFk, value) - SELECT s.id, 21, ROUND((s.price * (100 - s.discount) / 100) - SUM(value), 3) saleValue - FROM sale s - JOIN tmp.sale tmps ON tmps.saleFk = s.id - LEFT JOIN saleComponent sc ON sc.saleFk = s.id - WHERE sc.componentFk <> 21 - GROUP BY s.id - HAVING ROUND(saleValue, 4) <> 0; + IF vCode = 'renewPrices' THEN + SET vComponentFk = 21; END IF; + UPDATE sale s + JOIN ticket t ON t.id = s.ticketFk + JOIN item i on i.id = s.itemFk + JOIN itemType it on it.id = i.typeFk + JOIN (SELECT SUM(sc.value) sumValue, sc.saleFk + FROM saleComponent sc + JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk + GROUP BY sc.saleFk) sc ON sc.saleFk = s.id + SET s.price = IF(vCode <> 'renewPrices', sumValue / ((100 - s.discount) / 100), s.price), + s.foreignPrice = currency_getRate(t.currencyFk, NULL) * (sumValue / ((100 - s.discount) / 100)) + WHERE it.code <> 'PRT' ; + + REPLACE INTO saleComponent(saleFk, componentFk, value) + SELECT s.id, vComponentFk, ROUND((s.price * (100 - s.discount) / 100) - SUM(sc.value), 3) dif + FROM sale s + JOIN tmp.sale tmps ON tmps.saleFk = s.id + LEFT JOIN saleComponent sc ON sc.saleFk = s.id + WHERE sc.saleFk <> vComponentFk + GROUP BY s.id + HAVING dif <> 0; + UPDATE sale s JOIN ( SELECT SUM(sc.value) sumValue, sc.saleFk diff --git a/db/routines/vn/procedures/ticket_priceDifference.sql b/db/routines/vn/procedures/ticket_priceDifference.sql index 4e71a17eab..df9e33a790 100644 --- a/db/routines/vn/procedures/ticket_priceDifference.sql +++ b/db/routines/vn/procedures/ticket_priceDifference.sql @@ -26,6 +26,7 @@ BEGIN i.category, IFNULL(s.quantity, 0) quantity, IFNULL(s.price, 0) price, + IFNULL(s.foreignPrice, 0) foreignPrice, ROUND(SUM(tc.cost), 2) newPrice, s.id saleFk FROM vn.sale s @@ -42,10 +43,9 @@ BEGIN GROUP BY s.id ORDER BY s.id ) SELECT *, - currency_getRate(vCurrencyFk, NULL) * price foreignPrice, currency_getRate(vCurrencyFk, NULL) * newPrice newForeignPrice, quantity * (price - newPrice) difference, - currency_getRate(vCurrencyFk, NULL) * quantity * (price - newPrice) foreignDifference + quantity * (foreignPrice - currency_getRate(vCurrencyFk, NULL) * newPrice) foreignDifference FROM ticketPriceDifference; DROP TEMPORARY TABLE tmp.ticketComponentPreview; diff --git a/modules/ticket/back/methods/ticket/filter.js b/modules/ticket/back/methods/ticket/filter.js index c612234701..de98d08101 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -240,6 +240,7 @@ module.exports = Self => { t.clientFk, t.totalWithoutVat, t.totalWithVat, + t.foreignTotalWithVat, io.id invoiceOutId, a.provinceFk, p.name province, @@ -266,7 +267,8 @@ module.exports = Self => { JOIN item i2 ON i2.id = s2.itemFk WHERE s2.ticketFk = t.id ) AS packing, - c.credit + c.credit, + cu.code currencyCode FROM ticket t LEFT JOIN invoiceOut io ON t.refFk = io.ref LEFT JOIN zone z ON z.id = t.zoneFk @@ -280,6 +282,7 @@ module.exports = Self => { LEFT JOIN worker wk ON wk.id = c.salesPersonFk LEFT JOIN account.user u ON u.id = wk.id LEFT JOIN route r ON r.id = t.routeFk + JOIN currency cu ON cu.id = t.currencyFk `); if (args.orderFk) { diff --git a/modules/ticket/back/methods/ticket/summary.js b/modules/ticket/back/methods/ticket/summary.js index 6bfa478ecc..d0c9fbb50f 100644 --- a/modules/ticket/back/methods/ticket/summary.js +++ b/modules/ticket/back/methods/ticket/summary.js @@ -103,7 +103,8 @@ module.exports = Self => { scope: { fields: ['id'] } - } + }, + {relation: 'currency'} ], where: {id: ticketFk} };