2344 ticket sale descuento usuario
gitea/salix/pipeline/head This commit has test failures Details

This commit is contained in:
Javi Gallego 2020-06-25 17:12:53 +02:00
parent cd5ec02e42
commit 962436b8bb
4 changed files with 82 additions and 15 deletions

View File

@ -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 ;

View File

@ -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
*/

View File

@ -122,6 +122,9 @@
"mysql": {
"columnName": "expenceFk"
}
},
"compression": {
"type": "Number"
}
},
"relations": {

View File

@ -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) {