From 962436b8bbd51260b81cbfdcbf3ce718d8128bf3 Mon Sep 17 00:00:00 2001 From: jgallego Date: Thu, 25 Jun 2020 17:12:53 +0200 Subject: [PATCH] 2344 ticket sale descuento usuario --- .../00-manaSpellersRequery.sql | 71 +++++++++++++++++++ db/changes/PASTE_COMMENT_REMINDER | 4 ++ modules/item/back/models/item.json | 3 + .../back/methods/ticket/updateDiscount.js | 19 ++--- 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 db/changes/10200-Desescalada/00-manaSpellersRequery.sql create mode 100644 db/changes/PASTE_COMMENT_REMINDER diff --git a/db/changes/10200-Desescalada/00-manaSpellersRequery.sql b/db/changes/10200-Desescalada/00-manaSpellersRequery.sql new file mode 100644 index 000000000..fe9407ed3 --- /dev/null +++ b/db/changes/10200-Desescalada/00-manaSpellersRequery.sql @@ -0,0 +1,71 @@ +USE `vn`; +DROP procedure IF EXISTS `manaSpellersRequery`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `manaSpellersRequery`(vWorkerFk INTEGER) +BEGIN + DECLARE vWorkerHasMana BOOLEAN; + DECLARE vFromDated DATE; + DECLARE vToDated DATE DEFAULT TIMESTAMPADD(DAY,1,CURDATE()); + DECLARE vMana INT; + DECLARE vAutoMana INT; + DECLARE vManaBank INT; + DECLARE vManaGreugeType INT; + + SELECT id INTO vMana + FROM `component` WHERE code = 'mana'; + + SELECT id INTO vAutoMana + FROM `component` WHERE code = 'autoMana'; + + SELECT id INTO vManaBank + FROM `bank` WHERE code = 'mana'; + + SELECT id INTO vManaGreugeType + FROM `greugeType` WHERE code = 'mana'; + + SELECT COUNT(*) INTO vWorkerHasMana + FROM vn.workerMana wm + LEFT JOIN vn.workerManaExcluded wme ON wm.workerFk = wme.workerFk + WHERE wm.workerFk = vWorkerFk AND wme.workerFk IS NULL; + + IF vWorkerHasMana THEN + + SELECT max(dated) INTO vFromDated + FROM clientManaCache; + + REPLACE workerMana (workerFk, amount) + SELECT vWorkerFk, sum(mana) FROM + ( + SELECT s.quantity * sc.value as mana + FROM ticket t + JOIN address a ON a.id = t.addressFk + JOIN client c ON c.id = a.clientFk + JOIN sale s ON s.ticketFk = t.id + JOIN saleComponent sc ON sc.saleFk = s.id + WHERE c.salesPersonFk = vWorkerFk AND sc.componentFk IN (vMana, vAutoMana) + AND t.shipped > vFromDated AND t.shipped < vToDated + UNION ALL + SELECT - r.amountPaid + FROM receipt r + JOIN client c ON c.id = r.clientFk + WHERE c.salesPersonFk = vWorkerFk AND bankFk = vManaBank + AND payed > vFromDated + UNION ALL + SELECT g.amount + FROM greuge g + JOIN client c ON c.id = g.clientFk + WHERE c.salesPersonFk = vWorkerFk AND g.greugeTypeFk = vManaGreugeType + AND g.shipped > vFromDated and g.shipped < CURDATE() + UNION ALL + SELECT cc.mana + FROM clientManaCache cc + JOIN client c ON c.id = cc.clientFk + WHERE c.salesPersonFk = vWorkerFk AND cc.dated = vFromDated + ) sub; + END IF; +END$$ + +DELIMITER ; + diff --git a/db/changes/PASTE_COMMENT_REMINDER b/db/changes/PASTE_COMMENT_REMINDER new file mode 100644 index 000000000..07b707920 --- /dev/null +++ b/db/changes/PASTE_COMMENT_REMINDER @@ -0,0 +1,4 @@ +/* +Hay una versión en salix que machacará toda esta función/procedimiento +avisa a ___ de los cambios que quieres hacer +*/ \ No newline at end of file diff --git a/modules/item/back/models/item.json b/modules/item/back/models/item.json index 5d2e47d2a..6198fe702 100644 --- a/modules/item/back/models/item.json +++ b/modules/item/back/models/item.json @@ -122,6 +122,9 @@ "mysql": { "columnName": "expenceFk" } + }, + "compression": { + "type": "Number" } }, "relations": { diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index 3b69c6b68..d06ac8b27 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -35,7 +35,6 @@ module.exports = Self => { }); Self.updateDiscount = async(ctx, id, salesIds, newDiscount) => { - const userId = ctx.req.accessToken.userId; const models = Self.app.models; const tx = await Self.beginTransaction({}); @@ -69,6 +68,7 @@ module.exports = Self => { if (!allFromSameTicket) throw new UserError('All sales must belong to the same ticket'); + const userId = ctx.req.accessToken.userId; const isLocked = await models.Ticket.isLocked(id); const isSalesPerson = await models.Account.hasRole(userId, 'salesPerson'); const state = await Self.app.models.TicketState.findOne({ @@ -79,18 +79,9 @@ module.exports = Self => { if (isLocked || (!isSalesPerson && alertLevel > 0)) throw new UserError(`The sales of this ticket can't be modified`); - const ticket = await models.Ticket.findById(id, { - include: { - relation: 'client', - scope: { - fields: ['salesPersonFk'] - } - }, - }, options); - const salesPersonId = ticket.client().salesPersonFk; const usesMana = await models.WorkerMana.findOne({ where: { - workerFk: salesPersonId + workerFk: userId }, fields: 'amount'}, options); @@ -115,10 +106,8 @@ module.exports = Self => { await Promise.all(promises); - if (salesPersonId) { - const query = `call vn.manaSpellersRequery(?)`; - await Self.rawSql(query, [salesPersonId], options); - } + const query = `call vn.manaSpellersRequery(?)`; + await Self.rawSql(query, [userId], options); await tx.commit(); } catch (error) {