From f29b5dff38e4846a0e4fbd8a77c5cf573e8bed7a Mon Sep 17 00:00:00 2001 From: Gerard Date: Thu, 29 Nov 2018 16:02:54 +0100 Subject: [PATCH] #885 las comprobaciones de deuda de un cliente no son correctas --- services/db/install/dump/fixtures.sql | 2 +- services/loopback/common/methods/order/new.js | 20 +++++++++---------- .../loopback/common/methods/ticket/new.js | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/services/db/install/dump/fixtures.sql b/services/db/install/dump/fixtures.sql index 4fae22534..47c2a22f7 100644 --- a/services/db/install/dump/fixtures.sql +++ b/services/db/install/dump/fixtures.sql @@ -188,7 +188,7 @@ INSERT INTO `vn`.`client`(`id`,`name`,`fi`,`socialName`,`contact`,`street`,`city VALUES (101, 'Bruce Wayne', '84612325V', 'Batman', 'Alfred', '1007 Mountain Drive, Gotham', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'BruceWayne@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 1, NULL, 0, 0, 18, 0, 1), (102, 'Petter Parker', '87945234L', 'Spider-Man', 'Aunt May', '20 Ingram Street', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'PetterParker@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 0, 1, NULL, 0, 0, 18, 0, 1), - (103, 'Clark Kent', '06815934E', 'Super-Man', 'lois lane', '344 Clinton Street', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'ClarkKent@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 0, NULL, 0, 0, 18, 0, 1), + (103, 'Clark Kent', '06815934E', 'Super-Man', 'lois lane', '344 Clinton Street', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'ClarkKent@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 0, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 0, NULL, 0, 0, 18, 0, 1), (104, 'Tony Stark', '06089160W', 'Iron-Man', 'Pepper Potts', '10880 Malibu Point', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'TonyStark@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 0, NULL, 0, 0, 18, 0, 1), (105, 'Max Eisenhardt', '39182496H', 'Magneto', 'Rogue', 'Unknown Whereabouts', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'MaxEisenhardt@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 0, NULL, 0, 0, NULL, 0, 1), (106, 'DavidCharlesHaller', '53136686Q', 'Legion', 'Charles Xavier', 'Evil hideout', 'Silla', 46460, 1111111111, 222222222, 333333333, 1, 'DavidCharlesHaller@verdnatura.es', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 0,NULL, 10, 5,CURDATE(), 1, 5, 1, 1, 1,'0000-00-00', 1, NULL, 1, 1, 1, 0, NULL, 0, 0, 19, 0, 1), diff --git a/services/loopback/common/methods/order/new.js b/services/loopback/common/methods/order/new.js index 5dd537fe8..ae090dedd 100644 --- a/services/loopback/common/methods/order/new.js +++ b/services/loopback/common/methods/order/new.js @@ -22,28 +22,26 @@ module.exports = Self => { Self.new = async params => { let address = await Self.app.models.Address.findOne({ where: {id: params.addressFk}, - fields: ['clientFk'] + fields: ['clientFk'], + include: [ + {relation: 'client'} + ] }); let clientFk = address.clientFk; - - let client = await Self.app.models.Client.findOne({ - where: {id: clientFk}, - fields: ['isTaxDataChecked', 'isFreezed', 'isActive'] - }); - - if (client.isFreezed) + console.log(address); + if (address.client().isFreezed) throw new UserError(`You can't create an order for a frozen client`); - if (!client.isActive) + if (!address.client().isActive) throw new UserError(`You can't create an order for a inactive client`); - if (!client.isTaxDataChecked) + if (!address.client().isTaxDataChecked) throw new UserError(`You can't create an order for a client that doesn't has tax data verified`); let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`; let clientDebt = await Self.rawSql(query, [clientFk]); - if (clientDebt[0].debt > 0) + if (address.client().credit - clientDebt[0].debt <= 0) throw new UserError(`You can't create an order for a client that has a debt`); query = `CALL vn.orderListCreate(?, ?, ?, ?);`; diff --git a/services/loopback/common/methods/ticket/new.js b/services/loopback/common/methods/ticket/new.js index 0aa4aba41..76f20145d 100644 --- a/services/loopback/common/methods/ticket/new.js +++ b/services/loopback/common/methods/ticket/new.js @@ -53,7 +53,7 @@ module.exports = Self => { let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`; let clientDebt = await Self.rawSql(query, [clientFk]); - if (clientDebt[0].debt > 0) + if (address.client().credit - clientDebt[0].debt <= 0) throw new UserError(`You can't create an order for a client that has a debt`); }