refs #5590 fix total invoiceOut
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Andrés 2023-06-29 10:39:07 +02:00
parent 840a88bdc1
commit 1d993c74e5
6 changed files with 81 additions and 33 deletions

View File

@ -699,12 +699,12 @@ INSERT INTO `vn`.`route`(`id`, `time`, `workerFk`, `created`, `vehicleFk`, `agen
INSERT INTO `vn`.`ticket`(`id`, `priority`, `agencyModeFk`,`warehouseFk`,`routeFk`, `shipped`, `landed`, `clientFk`,`nickname`, `addressFk`, `refFk`, `isDeleted`, `zoneFk`, `zonePrice`, `zoneBonus`, `created`) INSERT INTO `vn`.`ticket`(`id`, `priority`, `agencyModeFk`,`warehouseFk`,`routeFk`, `shipped`, `landed`, `clientFk`,`nickname`, `addressFk`, `refFk`, `isDeleted`, `zoneFk`, `zonePrice`, `zoneBonus`, `created`)
VALUES VALUES
(1 , 3, 1, 1, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1101, 'Bat cave', 121, 'T1111111', 0, 1, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)), (1 , 3, 1, 1, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1101, 'Bat cave', 121, NULL, 0, 1, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)),
(2 , 1, 1, 1, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, 'T1111111', 0, 1, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)), (2 , 1, 1, 1, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, NULL, 0, 1, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)),
(3 , 1, 7, 1, 6, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -2 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, 'T2222222', 0, 3, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH)), (3 , 1, 7, 1, 6, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -2 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, NULL, 0, 3, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH)),
(4 , 3, 2, 1, 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -3 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, 'T3333333', 0, 9, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH)), (4 , 3, 2, 1, 2, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -3 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, NULL, 0, 9, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH)),
(5 , 3, 3, 3, 3, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -4 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, 'T4444444', 0, 10, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH)), (5 , 3, 3, 3, 3, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -4 MONTH), INTERVAL +1 DAY), 1104, 'Stark tower', 124, NULL, 0, 10, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH)),
(6 , 1, 3, 3, 3, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1101, 'Mountain Drive Gotham', 1, 'A1111111', 0, 10, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)), (6 , 1, 3, 3, 3, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(DATE_ADD(util.VN_CURDATE(),INTERVAL -1 MONTH), INTERVAL +1 DAY), 1101, 'Mountain Drive Gotham', 1, NULL, 0, 10, 5, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH)),
(7 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1101, 'Mountain Drive Gotham', 1, NULL, 0, 3, 5, 1, util.VN_CURDATE()), (7 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1101, 'Mountain Drive Gotham', 1, NULL, 0, 3, 5, 1, util.VN_CURDATE()),
(8 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1101, 'Bat cave', 121, NULL, 0, 3, 5, 1, util.VN_CURDATE()), (8 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1101, 'Bat cave', 121, NULL, 0, 3, 5, 1, util.VN_CURDATE()),
(9 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1104, 'Stark tower', 124, NULL, 0, 3, 5, 1, util.VN_CURDATE()), (9 , NULL, 7, 1, 6, util.VN_CURDATE(), DATE_ADD(util.VN_CURDATE(), INTERVAL + 1 DAY), 1104, 'Stark tower', 124, NULL, 0, 3, 5, 1, util.VN_CURDATE()),
@ -2572,6 +2572,26 @@ INSERT INTO `vn`.`ticketRecalc`(`ticketFk`)
CALL `vn`.`ticket_doRecalc`(); CALL `vn`.`ticket_doRecalc`();
UPDATE `vn`.`ticket`
SET refFk = 'T1111111'
WHERE id IN (1,2);
UPDATE `vn`.`ticket`
SET refFk = 'T2222222'
WHERE id = 3;
UPDATE `vn`.`ticket`
SET refFk = 'T3333333'
WHERE id = 4;
UPDATE `vn`.`ticket`
SET refFk = 'T4444444'
WHERE id = 5;
UPDATE `vn`.`ticket`
SET refFk = 'A1111111'
WHERE id = 6;
INSERT INTO `vn`.`zoneAgencyMode`(`id`, `agencyModeFk`, `zoneFk`) INSERT INTO `vn`.`zoneAgencyMode`(`id`, `agencyModeFk`, `zoneFk`)
VALUES VALUES
(1, 1, 1), (1, 1, 1),

View File

@ -12605,7 +12605,7 @@ BEGIN
FROM myTicket t FROM myTicket t
WHERE shipped BETWEEN TIMESTAMP(vFrom) AND TIMESTAMP(vTo, '23:59:59'); WHERE shipped BETWEEN TIMESTAMP(vFrom) AND TIMESTAMP(vTo, '23:59:59');
CALL vn.ticketGetTotal; CALL vn.ticketGetTotal(NULL);
SELECT v.id, IFNULL(v.landed, v.shipped) landed, SELECT v.id, IFNULL(v.landed, v.shipped) landed,
v.shipped, v.companyFk, v.nickname, v.shipped, v.companyFk, v.nickname,
@ -47167,7 +47167,7 @@ BEGIN
ENGINE = MEMORY ENGINE = MEMORY
SELECT vTicketId ticketFk; SELECT vTicketId ticketFk;
CALL ticketGetTotal; CALL ticketGetTotal(NULL);
SELECT total INTO vTotal FROM tmp.ticketTotal; SELECT total INTO vTotal FROM tmp.ticketTotal;
@ -58494,6 +58494,13 @@ BEGIN
DECLARE vIsCEESerial BOOL DEFAULT FALSE; DECLARE vIsCEESerial BOOL DEFAULT FALSE;
DECLARE vIsCorrectInvoiceDate BOOL; DECLARE vIsCorrectInvoiceDate BOOL;
DECLARE vMaxShipped DATE; DECLARE vMaxShipped DATE;
DECLARE vDone BOOL;
DECLARE vTicketFk INT;
DECLARE vCursor CURSOR FOR
SELECT id
FROM ticketToInvoice;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SET vInvoiceDate = IFNULL(vInvoiceDate, util.VN_CURDATE()); SET vInvoiceDate = IFNULL(vInvoiceDate, util.VN_CURDATE());
@ -58579,6 +58586,20 @@ BEGIN
FROM invoiceOut FROM invoiceOut
WHERE id = vNewInvoiceId; WHERE id = vNewInvoiceId;
OPEN vCursor;
l: LOOP
SET vDone = FALSE;
FETCH vCursor INTO vTicketFk;
IF vDone THEN
LEAVE l;
END IF;
CALL ticket_recalc(vTicketFk, vTaxArea);
END LOOP;
CLOSE vCursor;
UPDATE ticket t UPDATE ticket t
JOIN tmp.ticketToInvoice ti ON ti.id = t.id JOIN tmp.ticketToInvoice ti ON ti.id = t.id
SET t.refFk = vNewRef; SET t.refFk = vNewRef;
@ -58594,10 +58615,6 @@ BEGIN
INSERT INTO ticketTracking(stateFk,ticketFk,workerFk) INSERT INTO ticketTracking(stateFk,ticketFk,workerFk)
SELECT * FROM tmp.updateInter; SELECT * FROM tmp.updateInter;
INSERT INTO ticketLog (action, userFk, originFk, description)
SELECT 'UPDATE', account.myUser_getId(), ti.id, CONCAT('Crea factura ', vNewRef)
FROM tmp.ticketToInvoice ti;
CALL invoiceExpenceMake(vNewInvoiceId); CALL invoiceExpenceMake(vNewInvoiceId);
CALL invoiceTaxMake(vNewInvoiceId,vTaxArea); CALL invoiceTaxMake(vNewInvoiceId,vTaxArea);
@ -69870,7 +69887,7 @@ DELIMITER ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;; DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ticketGetTotal`() CREATE DEFINER=`root`@`localhost` PROCEDURE `ticketGetTotal`(vTaxArea VARCHAR(25))
BEGIN BEGIN
/** /**
* Calcula el total con IVA para un conjunto de tickets. * Calcula el total con IVA para un conjunto de tickets.
@ -69878,7 +69895,7 @@ BEGIN
* @table tmp.ticket(ticketFk) Identificadores de los tickets a calcular * @table tmp.ticket(ticketFk) Identificadores de los tickets a calcular
* @return tmp.ticketTotal Total para cada ticket * @return tmp.ticketTotal Total para cada ticket
*/ */
CALL ticket_getTax(NULL); CALL ticket_getTax(vTaxArea);
DROP TEMPORARY TABLE IF EXISTS tmp.ticketTotal; DROP TEMPORARY TABLE IF EXISTS tmp.ticketTotal;
CREATE TEMPORARY TABLE tmp.ticketTotal CREATE TEMPORARY TABLE tmp.ticketTotal
@ -70029,7 +70046,7 @@ BEGIN
AND clientFk = vClientFk AND clientFk = vClientFk
AND shipped > '2001-01-01'; AND shipped > '2001-01-01';
CALL vn.ticketGetTotal; CALL vn.ticketGetTotal(NULL);
SELECT c.id, SELECT c.id,
c.name as Cliente, c.name as Cliente,
@ -71878,7 +71895,7 @@ proc: BEGIN
LEAVE myLoop; LEAVE myLoop;
END IF; END IF;
CALL ticket_recalc(vTicketFk); CALL ticket_recalc(vTicketFk, NULL);
END LOOP; END LOOP;
CLOSE cCur; CLOSE cCur;
@ -72334,14 +72351,14 @@ BEGIN
CALL addressTaxArea (); CALL addressTaxArea ();
IF vTaxArea > '' THEN IF vTaxArea IS NOT NULL THEN
UPDATE tmp.addressTaxArea UPDATE tmp.addressTaxArea
SET areaFk = vTaxArea; SET areaFk = vTaxArea;
END IF; END IF;
/* Solo se calcula la base imponible (taxableBase) y el impuesto se calculará posteriormente /* Solo se calcula la base imponible (taxableBase) y el impuesto se calculará posteriormente
* No se debería cambiar el sistema por problemas con los decimales * No se debería cambiar el sistema por problemas con los decimales
*/ */
DROP TEMPORARY TABLE IF EXISTS tmp.ticketTax; DROP TEMPORARY TABLE IF EXISTS tmp.ticketTax;
CREATE TEMPORARY TABLE tmp.ticketTax CREATE TEMPORARY TABLE tmp.ticketTax
(PRIMARY KEY (ticketFk, code, rate)) (PRIMARY KEY (ticketFk, code, rate))
@ -72349,7 +72366,7 @@ BEGIN
SELECT * FROM ( SELECT * FROM (
SELECT tmpTicket.ticketFk, SELECT tmpTicket.ticketFk,
bp.pgcFk, bp.pgcFk,
SUM(s.quantity * s.price * (100 - s.discount)/100 ) AS taxableBase, SUM(s.quantity * s.price * (100 - s.discount)/100 ) taxableBase,
pgc.rate, pgc.rate,
tc.code, tc.code,
bp.priority bp.priority
@ -72369,7 +72386,7 @@ BEGIN
JOIN pgc ON pgc.code = bp.pgcFk JOIN pgc ON pgc.code = bp.pgcFk
JOIN taxClass tc ON tc.id = bp.taxClassFk JOIN taxClass tc ON tc.id = bp.taxClassFk
GROUP BY tmpTicket.ticketFk, pgc.code, pgc.rate GROUP BY tmpTicket.ticketFk, pgc.code, pgc.rate
HAVING taxableBase != 0) t3 HAVING taxableBase <> 0) t3
ORDER BY priority; ORDER BY priority;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketServiceTax; DROP TEMPORARY TABLE IF EXISTS tmp.ticketServiceTax;
@ -72378,7 +72395,7 @@ BEGIN
ENGINE = MEMORY ENGINE = MEMORY
SELECT tt.ticketFk, SELECT tt.ticketFk,
pgc.code pgcFk, pgc.code pgcFk,
SUM(ts.quantity * ts.price) AS taxableBase, SUM(ts.quantity * ts.price) taxableBase,
pgc.rate, pgc.rate,
tc.code tc.code
FROM tmp.ticket tt FROM tmp.ticket tt
@ -72394,7 +72411,7 @@ BEGIN
JOIN pgc ON pgc.code = bp.pgcFk JOIN pgc ON pgc.code = bp.pgcFk
JOIN taxClass tc ON tc.id = bp.taxClassFk JOIN taxClass tc ON tc.id = bp.taxClassFk
GROUP BY tt.ticketFk, pgc.code GROUP BY tt.ticketFk, pgc.code
HAVING taxableBase != 0; HAVING taxableBase <> 0;
INSERT INTO tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code) INSERT INTO tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code)
SELECT ts.ticketFk, ts.pgcFk, ts.taxableBase, ts.rate, ts.code SELECT ts.ticketFk, ts.pgcFk, ts.taxableBase, ts.rate, ts.code
@ -72725,20 +72742,31 @@ DELIMITER ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;; DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ticket_recalc`(vTicketId INT) CREATE DEFINER=`root`@`localhost` PROCEDURE `ticket_recalc`(vSelf INT, vTaxArea VARCHAR(25))
BEGIN proc:BEGIN
/** /**
* Calcula y guarda el total con/sin IVA en un ticket. * Calcula y guarda el total con/sin IVA en un ticket.
* *
* @param vTicketId Identificador del ticket * @param vTicketId Identificador del ticket
*/ */
DECLARE hasInvoice BOOL;
SELECT COUNT(*) INTO hasInvoice
FROM ticket
WHERE id = vSelf
AND refFk IS NOT NULL;
IF hasInvoice THEN
LEAVE proc;
END IF;
DROP TEMPORARY TABLE IF EXISTS tmp.ticket; DROP TEMPORARY TABLE IF EXISTS tmp.ticket;
CREATE TEMPORARY TABLE tmp.ticket CREATE TEMPORARY TABLE tmp.ticket
ENGINE = MEMORY ENGINE = MEMORY
SELECT vTicketId ticketFk; SELECT vSelf ticketFk;
CALL ticketGetTotal; CALL ticketGetTotal(vTaxArea);
UPDATE ticket t UPDATE ticket t
JOIN tmp.ticketTotal tt ON tt.ticketFk = t.id JOIN tmp.ticketTotal tt ON tt.ticketFk = t.id

View File

@ -106,7 +106,7 @@ module.exports = Self => {
} }
} }
const query = `CALL vn.ticket_recalc(?)`; const query = `CALL vn.ticket_recalc(?, NULL)`;
await Self.rawSql(query, [refundTicket.id], myOptions); await Self.rawSql(query, [refundTicket.id], myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -96,7 +96,7 @@ module.exports = Self => {
await sale.updateAttributes({price: newPrice}, myOptions); await sale.updateAttributes({price: newPrice}, myOptions);
await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions); await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions);
await Self.rawSql('CALL vn.ticket_recalc(?)', [sale.ticketFk], myOptions); await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [sale.ticketFk], myOptions);
const salesPerson = sale.ticket().client().salesPersonUser(); const salesPerson = sale.ticket().client().salesPersonUser();
if (salesPerson) { if (salesPerson) {

View File

@ -85,7 +85,7 @@ module.exports = Self => {
}, myOptions); }, myOptions);
await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id], myOptions); await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id], myOptions);
await Self.rawSql('CALL vn.ticket_recalc(?)', [id], myOptions); await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [id], myOptions);
const sale = await models.Sale.findById(newSale.id, { const sale = await models.Sale.findById(newSale.id, {
include: { include: {

View File

@ -147,7 +147,7 @@ module.exports = Self => {
await Promise.all(promises); await Promise.all(promises);
await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions); await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions);
await Self.rawSql('CALL vn.ticket_recalc(?)', [id], myOptions); await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [id], myOptions);
const ticket = await models.Ticket.findById(id, { const ticket = await models.Ticket.findById(id, {
include: { include: {