From 5531639027138950a1b73565be11ce3162c858b3 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 12 Mar 2024 09:34:12 +0100 Subject: [PATCH 1/5] feat: refs #6951 create procedure --- db/routines/vn/procedures/ticketClon.sql | 54 ++----------------- db/routines/vn/procedures/ticket_Clone.sql | 4 +- db/routines/vn/procedures/ticket_CloneAll.sql | 54 +++++++++++++++++++ 3 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 db/routines/vn/procedures/ticket_CloneAll.sql diff --git a/db/routines/vn/procedures/ticketClon.sql b/db/routines/vn/procedures/ticketClon.sql index 81328bc11..00386c6aa 100644 --- a/db/routines/vn/procedures/ticketClon.sql +++ b/db/routines/vn/procedures/ticketClon.sql @@ -1,55 +1,11 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticketClon`(vTicketFk INT, vNewShipped DATE) BEGIN - - DECLARE done INT DEFAULT FALSE; - DECLARE vNewTicketFk INT; - DECLARE vOldSaleFk INT; - DECLARE vNewSaleFk INT; - - DECLARE cur1 CURSOR FOR - SELECT id - FROM vn.sale - WHERE ticketFk = vTicketFk; - - DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; - - SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); - - CALL vn.ticket_Clone(vTicketFk, vNewTicketFk); - - UPDATE vn.ticket - SET landed = TIMESTAMPADD(DAY, DATEDIFF(vNewShipped, shipped), landed), - shipped = vNewShipped - WHERE id = vNewTicketFk; - - OPEN cur1; - - read_loop: LOOP - - FETCH cur1 INTO vOldSaleFk; - - IF done THEN - LEAVE read_loop; - END IF; - - INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed) - SELECT vNewTicketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed - FROM vn.sale - WHERE id = vOldSaleFk; - - SELECT max(id) INTO vNewSaleFk - FROM vn.sale - WHERE ticketFk = vNewTicketFk; - - INSERT INTO vn.saleComponent(saleFk, componentFk, value, isGreuge) - SELECT vNewSaleFk, componentFk, value, isGreuge - FROM vn.saleComponent - WHERE saleFk = vOldSaleFk; - - END LOOP; - CLOSE cur1; - + DECLARE vNewTicketFk INT; + + CALL ticket_CloneAll(vTicketFk, vNewShipped, TRUE, vNewTicketFk); + SELECT vNewTicketFk; + END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/ticket_Clone.sql b/db/routines/vn/procedures/ticket_Clone.sql index 7670e832e..cdddd6225 100644 --- a/db/routines/vn/procedures/ticket_Clone.sql +++ b/db/routines/vn/procedures/ticket_Clone.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_Clone`(vOriginalTicket INT, OUT vNewTicket INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_Clone`(vOriginalTicket INT, OUT vNewTicket INT,vWithWarehouse BOOLEAN) BEGIN /** * Clona el contenido de un ticket en otro @@ -32,7 +32,7 @@ BEGIN addressFk, agencyModeFk, nickname, - warehouseFk, + IF(vWithWarehouse, warehouseFk, NULL), companyFk, landed, zoneFk, diff --git a/db/routines/vn/procedures/ticket_CloneAll.sql b/db/routines/vn/procedures/ticket_CloneAll.sql new file mode 100644 index 000000000..4adb50a10 --- /dev/null +++ b/db/routines/vn/procedures/ticket_CloneAll.sql @@ -0,0 +1,54 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_CloneAll`(vTicketFk INT, vNewShipped DATE, vWithWarehouse BOOLEAN, OUT vNewTicketFk INT) +BEGIN + + DECLARE done INT DEFAULT FALSE; + DECLARE vOldSaleFk INT; + DECLARE vNewSaleFk INT; + + DECLARE cur1 CURSOR FOR + SELECT id + FROM vn.sale + WHERE ticketFk = vTicketFk; + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); + + CALL vn.ticket_Clone(vTicketFk, vNewTicketFk, vWithWarehouse); + + UPDATE vn.ticket + SET landed = TIMESTAMPADD(DAY, DATEDIFF(vNewShipped, shipped), landed), + shipped = vNewShipped + WHERE id = vNewTicketFk; + + OPEN cur1; + + read_loop: LOOP + + FETCH cur1 INTO vOldSaleFk; + + IF done THEN + LEAVE read_loop; + END IF; + + INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed) + SELECT vNewTicketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed + FROM vn.sale + WHERE id = vOldSaleFk; + + SELECT max(id) INTO vNewSaleFk + FROM vn.sale + WHERE ticketFk = vNewTicketFk; + + INSERT INTO vn.saleComponent(saleFk, componentFk, value, isGreuge) + SELECT vNewSaleFk, componentFk, value, isGreuge + FROM vn.saleComponent + WHERE saleFk = vOldSaleFk; + + END LOOP; + + CLOSE cur1; + +END$$ +DELIMITER ; From 690d93542949ff705f4b8093dbaf06297e406f84 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 15 Mar 2024 10:00:24 +0100 Subject: [PATCH 2/5] refactor: refs #6951 cloneAll --- db/routines/vn/procedures/ticket_Clone.sql | 4 +- db/routines/vn/procedures/ticket_CloneAll.sql | 73 ++++++++++--------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/db/routines/vn/procedures/ticket_Clone.sql b/db/routines/vn/procedures/ticket_Clone.sql index cdddd6225..7670e832e 100644 --- a/db/routines/vn/procedures/ticket_Clone.sql +++ b/db/routines/vn/procedures/ticket_Clone.sql @@ -1,5 +1,5 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_Clone`(vOriginalTicket INT, OUT vNewTicket INT,vWithWarehouse BOOLEAN) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_Clone`(vOriginalTicket INT, OUT vNewTicket INT) BEGIN /** * Clona el contenido de un ticket en otro @@ -32,7 +32,7 @@ BEGIN addressFk, agencyModeFk, nickname, - IF(vWithWarehouse, warehouseFk, NULL), + warehouseFk, companyFk, landed, zoneFk, diff --git a/db/routines/vn/procedures/ticket_CloneAll.sql b/db/routines/vn/procedures/ticket_CloneAll.sql index 4adb50a10..d56a47a33 100644 --- a/db/routines/vn/procedures/ticket_CloneAll.sql +++ b/db/routines/vn/procedures/ticket_CloneAll.sql @@ -2,53 +2,54 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_CloneAll`(vTicketFk INT, vNewShipped DATE, vWithWarehouse BOOLEAN, OUT vNewTicketFk INT) BEGIN - DECLARE done INT DEFAULT FALSE; + DECLARE done INT DEFAULT FALSE; DECLARE vOldSaleFk INT; - DECLARE vNewSaleFk INT; - - DECLARE cur1 CURSOR FOR + DECLARE vNewSaleFk INT; + + DECLARE cur1 CURSOR FOR SELECT id - FROM vn.sale - WHERE ticketFk = vTicketFk; - + FROM sale + WHERE ticketFk = vTicketFk; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; - - SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); - - CALL vn.ticket_Clone(vTicketFk, vNewTicketFk, vWithWarehouse); - - UPDATE vn.ticket + + SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); + + CALL ticket_Clone(vTicketFk, vNewTicketFk, vWithWarehouse); + + UPDATE ticket SET landed = TIMESTAMPADD(DAY, DATEDIFF(vNewShipped, shipped), landed), - shipped = vNewShipped - WHERE id = vNewTicketFk; - + shipped = vNewShipped, + warehouseFk = IF(vWithWarehouse, warehouseFk, NULL) + WHERE id = vNewTicketFk; + OPEN cur1; - + read_loop: LOOP - FETCH cur1 INTO vOldSaleFk; - - IF done THEN - LEAVE read_loop; - END IF; - - INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed) + FETCH cur1 INTO vOldSaleFk; + + IF done THEN + LEAVE read_loop; + END IF; + + INSERT INTO sale(ticketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed) SELECT vNewTicketFk, itemFk, quantity, concept, price, discount, priceFixed, isPriceFixed - FROM vn.sale - WHERE id = vOldSaleFk; - - SELECT max(id) INTO vNewSaleFk - FROM vn.sale - WHERE ticketFk = vNewTicketFk; - - INSERT INTO vn.saleComponent(saleFk, componentFk, value, isGreuge) + FROM sale + WHERE id = vOldSaleFk; + + SELECT max(id) INTO vNewSaleFk + FROM sale + WHERE ticketFk = vNewTicketFk; + + INSERT INTO saleComponent(saleFk, componentFk, value, isGreuge) SELECT vNewSaleFk, componentFk, value, isGreuge - FROM vn.saleComponent - WHERE saleFk = vOldSaleFk; - + FROM saleComponent + WHERE saleFk = vOldSaleFk; + END LOOP; CLOSE cur1; - + END$$ DELIMITER ; From f044809f306805ac422e5f22338003e1cc56e5c2 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 15 Mar 2024 15:45:47 +0100 Subject: [PATCH 3/5] feat: refs #6951 to create back --- db/routines/vn/procedures/ticket_CloneAll.sql | 2 +- .../00-aclTicketClone.sql | 2 + modules/ticket/back/methods/ticket/clone.js | 54 +++++++++++++++++++ modules/ticket/back/models/ticket-methods.js | 2 + modules/ticket/back/models/ticket.js | 1 - 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 db/versions/10957-goldenAnthurium/00-aclTicketClone.sql create mode 100644 modules/ticket/back/methods/ticket/clone.js diff --git a/db/routines/vn/procedures/ticket_CloneAll.sql b/db/routines/vn/procedures/ticket_CloneAll.sql index d56a47a33..a2d775a8f 100644 --- a/db/routines/vn/procedures/ticket_CloneAll.sql +++ b/db/routines/vn/procedures/ticket_CloneAll.sql @@ -15,7 +15,7 @@ BEGIN SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); - CALL ticket_Clone(vTicketFk, vNewTicketFk, vWithWarehouse); + CALL ticket_Clone(vTicketFk, vNewTicketFk); UPDATE ticket SET landed = TIMESTAMPADD(DAY, DATEDIFF(vNewShipped, shipped), landed), diff --git a/db/versions/10957-goldenAnthurium/00-aclTicketClone.sql b/db/versions/10957-goldenAnthurium/00-aclTicketClone.sql new file mode 100644 index 000000000..6387b77b0 --- /dev/null +++ b/db/versions/10957-goldenAnthurium/00-aclTicketClone.sql @@ -0,0 +1,2 @@ +INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId) + VALUES('Ticket', 'clone', 'WRITE', 'ALLOW', 'ROLE', 'administrative'); \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/clone.js b/modules/ticket/back/methods/ticket/clone.js new file mode 100644 index 000000000..535c6fc7c --- /dev/null +++ b/modules/ticket/back/methods/ticket/clone.js @@ -0,0 +1,54 @@ +module.exports = Self => { + Self.remoteMethodCtx('clone', { + description: 'clone a ticket and return the new ticket id', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'The ticket id', + http: {source: 'path'} + }, { + arg: 'shipped', + type: 'date', + }, { + arg: 'withWarehouse', + type: 'boolean', + } + ], + returns: { + type: 'number', + root: true + }, + http: { + path: `/:id/clone`, + verb: 'POST' + } + }); + + Self.clone = async(ctx, id, shipped, withWarehouse, options) => { + const myOptions = {userId: ctx.req.accessToken.userId}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const [, [{clonedTicketId}]] = await Self.rawSql(` + CALL vn.ticket_CloneAll(?, ?, ?, @clonedTicketId); + SELECT @clonedTicketId clonedTicketId;`, + [id, shipped, withWarehouse], myOptions); + + if (tx) await tx.commit(); + return clonedTicketId; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index d204a8102..0ae2ce3b4 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -46,4 +46,6 @@ module.exports = function(Self) { require('../methods/ticket/invoiceTicketsAndPdf')(Self); require('../methods/ticket/docuwareDownload')(Self); require('../methods/ticket/myLastModified')(Self); + require('../methods/ticket/addSaleByCode')(Self); + require('../methods/ticket/clone')(Self); }; diff --git a/modules/ticket/back/models/ticket.js b/modules/ticket/back/models/ticket.js index 51a8372e3..1930765fb 100644 --- a/modules/ticket/back/models/ticket.js +++ b/modules/ticket/back/models/ticket.js @@ -1,5 +1,4 @@ module.exports = Self => { require('./ticket-methods')(Self); require('../methods/ticket/state')(Self); - require('../methods/ticket/addSaleByCode')(Self); }; From 3c3dd64da142fdec27215ae16396232c996ed54a Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 18 Mar 2024 09:39:56 +0100 Subject: [PATCH 4/5] feat: refs #6951 test --- .../back/methods/ticket/specs/clone.spec.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 modules/ticket/back/methods/ticket/specs/clone.spec.js diff --git a/modules/ticket/back/methods/ticket/specs/clone.spec.js b/modules/ticket/back/methods/ticket/specs/clone.spec.js new file mode 100644 index 000000000..26114bd58 --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/clone.spec.js @@ -0,0 +1,56 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('Ticket cloning - clone function', () => { + let ctx; + let options; + let tx; + const ticketId = 1; + const shipped = Date.vnNew(); + + beforeEach(async() => { + ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'http://localhost'} + }, + args: {} + }; + + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: ctx.req + }); + + options = {transaction: tx}; + tx = await models.Ticket.beginTransaction({}); + options.transaction = tx; + }); + + afterEach(async() => { + await tx.rollback(); + }); + + it('should clone a new ticket without warehouse', async() => { + const originalTicket = await models.Ticket.findById(ticketId, null, options); + + const newTicketId = await models.Ticket.clone(ctx, ticketId, shipped, false, options); + const newTicket = await models.Ticket.findById(newTicketId, null, options); + + expect(newTicket.clientFk).toEqual(originalTicket.clientFk); + expect(newTicket.companyFk).toEqual(originalTicket.companyFk); + expect(newTicket.addressFk).toEqual(originalTicket.addressFk); + expect(newTicket.warehouseFk).toBeFalsy(); + }); + + it('should clone a new ticket with warehouse', async() => { + const originalTicket = await models.Ticket.findById(ticketId, null, options); + + const newTicketId = await models.Ticket.clone(ctx, ticketId, shipped, true, options); + const newTicket = await models.Ticket.findById(newTicketId, null, options); + + expect(newTicket.clientFk).toEqual(originalTicket.clientFk); + expect(newTicket.companyFk).toEqual(originalTicket.companyFk); + expect(newTicket.addressFk).toEqual(originalTicket.addressFk); + expect(newTicket.warehouseFk).toEqual(originalTicket.warehouseFk); + }); +}); From 0867f4d501864611adf263227f3c18d65e082ab5 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 22 Mar 2024 16:01:22 +0100 Subject: [PATCH 5/5] fix: refs #6951 var names & procedure name --- db/routines/vn/procedures/ticketClon.sql | 3 +-- .../{ticket_CloneAll.sql => ticket_cloneAll.sql} | 8 ++++---- modules/ticket/back/methods/ticket/clone.js | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) rename db/routines/vn/procedures/{ticket_CloneAll.sql => ticket_cloneAll.sql} (87%) diff --git a/db/routines/vn/procedures/ticketClon.sql b/db/routines/vn/procedures/ticketClon.sql index 00386c6aa..9144ac709 100644 --- a/db/routines/vn/procedures/ticketClon.sql +++ b/db/routines/vn/procedures/ticketClon.sql @@ -4,8 +4,7 @@ BEGIN DECLARE vNewTicketFk INT; - CALL ticket_CloneAll(vTicketFk, vNewShipped, TRUE, vNewTicketFk); - SELECT vNewTicketFk; + CALL ticket_cloneAll(vTicketFk, vNewShipped, TRUE, vNewTicketFk); END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/ticket_CloneAll.sql b/db/routines/vn/procedures/ticket_cloneAll.sql similarity index 87% rename from db/routines/vn/procedures/ticket_CloneAll.sql rename to db/routines/vn/procedures/ticket_cloneAll.sql index a2d775a8f..4b3401ed7 100644 --- a/db/routines/vn/procedures/ticket_CloneAll.sql +++ b/db/routines/vn/procedures/ticket_cloneAll.sql @@ -1,8 +1,8 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_CloneAll`(vTicketFk INT, vNewShipped DATE, vWithWarehouse BOOLEAN, OUT vNewTicketFk INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_cloneAll`(vTicketFk INT, vNewShipped DATE, vWithWarehouse BOOLEAN, OUT vNewTicketFk INT) BEGIN - DECLARE done INT DEFAULT FALSE; + DECLARE vDone BOOLEAN DEFAULT FALSE; DECLARE vOldSaleFk INT; DECLARE vNewSaleFk INT; @@ -11,7 +11,7 @@ BEGIN FROM sale WHERE ticketFk = vTicketFk; - DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; SET vNewShipped = IFNULL(vNewShipped, util.VN_CURDATE()); @@ -29,7 +29,7 @@ BEGIN FETCH cur1 INTO vOldSaleFk; - IF done THEN + IF vDone THEN LEAVE read_loop; END IF; diff --git a/modules/ticket/back/methods/ticket/clone.js b/modules/ticket/back/methods/ticket/clone.js index 535c6fc7c..93bc2a94e 100644 --- a/modules/ticket/back/methods/ticket/clone.js +++ b/modules/ticket/back/methods/ticket/clone.js @@ -40,7 +40,7 @@ module.exports = Self => { try { const [, [{clonedTicketId}]] = await Self.rawSql(` - CALL vn.ticket_CloneAll(?, ?, ?, @clonedTicketId); + CALL vn.ticket_cloneAll(?, ?, ?, @clonedTicketId); SELECT @clonedTicketId clonedTicketId;`, [id, shipped, withWarehouse], myOptions);