From f82870bec674bd3e9c1573a8014a5f71575dc33b Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 19 Dec 2022 14:39:38 +0100 Subject: [PATCH 1/5] feat(item_fixed-price): recalculate rate2 and when change item keep minPrice of item --- db/changes/225001/00-priceFixed_getRate2.sql | 23 ++++++++++ .../item/back/methods/fixed-price/getRate2.js | 34 ++++++++++++++ modules/item/back/models/fixed-price.js | 1 + modules/item/front/fixed-price/index.html | 46 ++++++++++++------- modules/item/front/fixed-price/index.js | 23 +++++++++- modules/item/front/fixed-price/locale/es.yml | 2 - 6 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 db/changes/225001/00-priceFixed_getRate2.sql create mode 100644 modules/item/back/methods/fixed-price/getRate2.js diff --git a/db/changes/225001/00-priceFixed_getRate2.sql b/db/changes/225001/00-priceFixed_getRate2.sql new file mode 100644 index 000000000..cf36efb57 --- /dev/null +++ b/db/changes/225001/00-priceFixed_getRate2.sql @@ -0,0 +1,23 @@ +DROP FUNCTION IF EXISTS `vn`.`priceFixed_getRate2`; + +DELIMITER $$ +$$ +CREATE FUNCTION `vn`.`priceFixed_getRate2`(vFixedPriceFk INT, vRate3 DOUBLE) +RETURNS DOUBLE +BEGIN + + DECLARE vWarehouse INT; + DECLARE vRate2 DOUBLE; + + SELECT round(vRate3 * (1 + ((r.rate2 - r.rate3)/100)), 2) INTO vRate2 + FROM vn.rate r + JOIN vn.priceFixed p ON p.id = vFixedPriceFk + WHERE r.dated <= p.started + AND r.warehouseFk = p.warehouseFk + ORDER BY r.dated DESC + LIMIT 1; + + RETURN vRate2; + +END$$ +DELIMITER ; diff --git a/modules/item/back/methods/fixed-price/getRate2.js b/modules/item/back/methods/fixed-price/getRate2.js new file mode 100644 index 000000000..b8811f425 --- /dev/null +++ b/modules/item/back/methods/fixed-price/getRate2.js @@ -0,0 +1,34 @@ +module.exports = Self => { + Self.remoteMethod('getRate2', { + description: 'Return the rate2.', + accessType: 'READ', + accepts: [ + { + arg: 'fixedPriceId', + type: 'integer', + description: 'The fixedPrice Id', + required: true + }, + { + arg: 'rate3', + type: 'number', + description: `The price rate 3`, + required: true + } + ], + returns: { + type: 'number', + root: true + }, + http: { + path: `/getRate2`, + verb: 'GET' + } + }); + + Self.getRate2 = async(fixedPriceId, rate3) => { + const [result] = await Self.rawSql(`SELECT vn.priceFixed_getRate2(?, ?) as rate2`, + [fixedPriceId, rate3]); + return result.rate2; + }; +}; diff --git a/modules/item/back/models/fixed-price.js b/modules/item/back/models/fixed-price.js index 9c78c586f..91010805f 100644 --- a/modules/item/back/models/fixed-price.js +++ b/modules/item/back/models/fixed-price.js @@ -1,4 +1,5 @@ module.exports = Self => { require('../methods/fixed-price/filter')(Self); require('../methods/fixed-price/upsertFixedPrice')(Self); + require('../methods/fixed-price/getRate2')(Self); }; diff --git a/modules/item/front/fixed-price/index.html b/modules/item/front/fixed-price/index.html index 9498bf96f..f9d177562 100644 --- a/modules/item/front/fixed-price/index.html +++ b/modules/item/front/fixed-price/index.html @@ -41,14 +41,12 @@ Warehouse - P.P.U. + field="rate2"> + Grouping price - P.P.P. + field="rate3"> + Packing price Min price @@ -72,7 +70,7 @@ show-field="name" value-field="id" search-function="$ctrl.itemSearchFunc($search)" - on-change="$ctrl.upsertPrice(price)" + on-change="$ctrl.upsertPrice(price, true)" order="id DESC" tabindex="1"> @@ -112,18 +110,32 @@ - - + + {{price.rate2 | currency: 'EUR':2}} + + + + + - - + + {{price.rate3 | currency: 'EUR':2}} + + + + + { + const rate2 = res.data; + if (rate2) { + price.rate2 = rate2; + this.upsertPrice(price); + } + }); + } } ngModule.vnComponent('vnFixedPrice', { diff --git a/modules/item/front/fixed-price/locale/es.yml b/modules/item/front/fixed-price/locale/es.yml index 3f400336d..6bdfcb678 100644 --- a/modules/item/front/fixed-price/locale/es.yml +++ b/modules/item/front/fixed-price/locale/es.yml @@ -3,5 +3,3 @@ Search prices by item ID or code: Buscar por ID de artículo o código Search fixed prices: Buscar precios fijados Add fixed price: Añadir precio fijado This row will be removed: Esta linea se eliminará -Price By Unit: Precio Por Unidad -Price By Package: Precio Por Paquete \ No newline at end of file -- 2.40.1 From 5d2b69d063de91b8b2fa72bb54eb93dc023835e3 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 19 Dec 2022 15:06:59 +0100 Subject: [PATCH 2/5] test(item_fixedPrice): front recalculateRate2 --- .../item/back/methods/fixed-price/getRate2.js | 4 ++-- modules/item/front/fixed-price/index.js | 2 +- modules/item/front/fixed-price/index.spec.js | 20 +++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/item/back/methods/fixed-price/getRate2.js b/modules/item/back/methods/fixed-price/getRate2.js index b8811f425..e1a399be6 100644 --- a/modules/item/back/methods/fixed-price/getRate2.js +++ b/modules/item/back/methods/fixed-price/getRate2.js @@ -11,7 +11,7 @@ module.exports = Self => { }, { arg: 'rate3', - type: 'number', + type: 'object', description: `The price rate 3`, required: true } @@ -29,6 +29,6 @@ module.exports = Self => { Self.getRate2 = async(fixedPriceId, rate3) => { const [result] = await Self.rawSql(`SELECT vn.priceFixed_getRate2(?, ?) as rate2`, [fixedPriceId, rate3]); - return result.rate2; + return result; }; }; diff --git a/modules/item/front/fixed-price/index.js b/modules/item/front/fixed-price/index.js index 5a57a123e..f03d73f08 100644 --- a/modules/item/front/fixed-price/index.js +++ b/modules/item/front/fixed-price/index.js @@ -124,7 +124,7 @@ export default class Controller extends Section { }; this.$http.get(query, {params}) .then(res => { - const rate2 = res.data; + const rate2 = res.data.rate2; if (rate2) { price.rate2 = rate2; this.upsertPrice(price); diff --git a/modules/item/front/fixed-price/index.spec.js b/modules/item/front/fixed-price/index.spec.js index 94621e352..db9579444 100644 --- a/modules/item/front/fixed-price/index.spec.js +++ b/modules/item/front/fixed-price/index.spec.js @@ -85,5 +85,25 @@ describe('fixed price', () => { expect(controller.$.model.remove).toHaveBeenCalled(); }); }); + + describe('recalculateRate2()', () => { + it(`should rate2 recalculate`, () => { + jest.spyOn(controller.vnApp, 'showSuccess'); + const price = { + id: 1, + itemFk: 1, + rate2: 2, + rate3: 2 + }; + const response = {rate2: 1}; + controller.recalculateRate2(price); + + const query = `FixedPrices/getRate2?fixedPriceId=${price.id}&rate3=${price.rate3}`; + $httpBackend.expectGET(query).respond(response); + $httpBackend.flush(); + + expect(price.rate2).toEqual(response.rate2); + }); + }); }); }); -- 2.40.1 From 502a6925213db8b67db48cda2b29363fa4b61153 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 21 Dec 2022 12:55:11 +0100 Subject: [PATCH 3/5] test(item_fixedPrice_rate2): backTest and e2e --- .../00-priceFixed_getRate2.sql | 0 e2e/helpers/selectors.js | 4 +- e2e/paths/04-item/13_fixedPrice.spec.js | 6 +-- .../item/back/methods/fixed-price/getRate2.js | 15 ++++--- .../fixed-price/specs/getRate2.spec.js | 39 +++++++++++++++++++ 5 files changed, 54 insertions(+), 10 deletions(-) rename db/changes/{225001 => 225201}/00-priceFixed_getRate2.sql (100%) create mode 100644 modules/item/back/methods/fixed-price/specs/getRate2.spec.js diff --git a/db/changes/225001/00-priceFixed_getRate2.sql b/db/changes/225201/00-priceFixed_getRate2.sql similarity index 100% rename from db/changes/225001/00-priceFixed_getRate2.sql rename to db/changes/225201/00-priceFixed_getRate2.sql diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index 9dab10673..f3fb496a0 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -415,8 +415,8 @@ export default { fourthFixedPrice: 'vn-fixed-price tr:nth-child(5)', fourthItemID: 'vn-fixed-price tr:nth-child(5) vn-autocomplete[ng-model="price.itemFk"]', fourthWarehouse: 'vn-fixed-price tr:nth-child(5) vn-autocomplete[ng-model="price.warehouseFk"]', - fourthPPU: 'vn-fixed-price tr:nth-child(5) > td:nth-child(4)', - fourthPPP: 'vn-fixed-price tr:nth-child(5) > td:nth-child(5)', + fourthGroupingPrice: 'vn-fixed-price tr:nth-child(5) > td:nth-child(4)', + fourthPackingPrice: 'vn-fixed-price tr:nth-child(5) > td:nth-child(5)', fourthHasMinPrice: 'vn-fixed-price tr:nth-child(5) > td:nth-child(6) > vn-check[ng-model="price.hasMinPrice"]', fourthMinPrice: 'vn-fixed-price tr:nth-child(5) > td:nth-child(6) > vn-input-number[ng-model="price.minPrice"]', fourthStarted: 'vn-fixed-price tr:nth-child(5) vn-date-picker[ng-model="price.started"]', diff --git a/e2e/paths/04-item/13_fixedPrice.spec.js b/e2e/paths/04-item/13_fixedPrice.spec.js index fc7aac3d0..673705eff 100644 --- a/e2e/paths/04-item/13_fixedPrice.spec.js +++ b/e2e/paths/04-item/13_fixedPrice.spec.js @@ -1,7 +1,7 @@ import selectors from '../../helpers/selectors.js'; import getBrowser from '../../helpers/puppeteer'; -describe('Item fixed prices path', () => { +fdescribe('Item fixed prices path', () => { let browser; let page; beforeAll(async() => { @@ -24,8 +24,8 @@ describe('Item fixed prices path', () => { it('should fill the fixed price data', async() => { const now = new Date(); await page.autocompleteSearch(selectors.itemFixedPrice.fourthWarehouse, 'Warehouse one'); - await page.write(selectors.itemFixedPrice.fourthPPU, '1'); - await page.write(selectors.itemFixedPrice.fourthPPP, '1'); + await page.writeOnEditableTD(selectors.itemFixedPrice.fourthGroupingPrice, '1'); + await page.writeOnEditableTD(selectors.itemFixedPrice.fourthPackingPrice, '1'); await page.write(selectors.itemFixedPrice.fourthMinPrice, '1'); await page.pickDate(selectors.itemFixedPrice.fourthStarted, now); await page.pickDate(selectors.itemFixedPrice.fourthEnded, now); diff --git a/modules/item/back/methods/fixed-price/getRate2.js b/modules/item/back/methods/fixed-price/getRate2.js index e1a399be6..c90a380e3 100644 --- a/modules/item/back/methods/fixed-price/getRate2.js +++ b/modules/item/back/methods/fixed-price/getRate2.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethod('getRate2', { - description: 'Return the rate2.', + description: 'Return the rate2', accessType: 'READ', accepts: [ { @@ -11,13 +11,13 @@ module.exports = Self => { }, { arg: 'rate3', - type: 'object', + type: 'number', description: `The price rate 3`, required: true } ], returns: { - type: 'number', + type: 'object', root: true }, http: { @@ -26,9 +26,14 @@ module.exports = Self => { } }); - Self.getRate2 = async(fixedPriceId, rate3) => { + Self.getRate2 = async(fixedPriceId, rate3, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const [result] = await Self.rawSql(`SELECT vn.priceFixed_getRate2(?, ?) as rate2`, - [fixedPriceId, rate3]); + [fixedPriceId, rate3], myOptions); return result; }; }; diff --git a/modules/item/back/methods/fixed-price/specs/getRate2.spec.js b/modules/item/back/methods/fixed-price/specs/getRate2.spec.js new file mode 100644 index 000000000..2f5dd93cd --- /dev/null +++ b/modules/item/back/methods/fixed-price/specs/getRate2.spec.js @@ -0,0 +1,39 @@ +const models = require('vn-loopback/server/server').models; + +describe('getRate2()', () => { + it(`should return new rate2 if exists rate`, async() => { + const tx = await models.FixedPrice.beginTransaction({}); + + try { + const options = {transaction: tx}; + const fixedPriceId = 1; + const rate3 = 2; + const result = await models.FixedPrice.getRate2(fixedPriceId, rate3, options); + + expect(result.rate2).toEqual(1.9); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it(`should return null if not exists rate`, async() => { + const tx = await models.FixedPrice.beginTransaction({}); + + try { + const options = {transaction: tx}; + const fixedPriceId = 13; + const rate3 = 2; + const result = await models.FixedPrice.getRate2(fixedPriceId, rate3, options); + + expect(result.rate2).toEqual(null); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); -- 2.40.1 From d18e360f735f4c85659efce2d25ec65b9e5e157c Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 21 Dec 2022 13:47:43 +0100 Subject: [PATCH 4/5] unfocus e2e --- e2e/paths/04-item/13_fixedPrice.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/paths/04-item/13_fixedPrice.spec.js b/e2e/paths/04-item/13_fixedPrice.spec.js index 673705eff..40ccc009e 100644 --- a/e2e/paths/04-item/13_fixedPrice.spec.js +++ b/e2e/paths/04-item/13_fixedPrice.spec.js @@ -1,7 +1,7 @@ import selectors from '../../helpers/selectors.js'; import getBrowser from '../../helpers/puppeteer'; -fdescribe('Item fixed prices path', () => { +describe('Item fixed prices path', () => { let browser; let page; beforeAll(async() => { -- 2.40.1 From 8184749626775d0bd8c37d154f96aa94383542e0 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 4 Jan 2023 09:40:27 +0100 Subject: [PATCH 5/5] change db version folder --- db/changes/{225201 => 230201}/00-priceFixed_getRate2.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename db/changes/{225201 => 230201}/00-priceFixed_getRate2.sql (100%) diff --git a/db/changes/225201/00-priceFixed_getRate2.sql b/db/changes/230201/00-priceFixed_getRate2.sql similarity index 100% rename from db/changes/225201/00-priceFixed_getRate2.sql rename to db/changes/230201/00-priceFixed_getRate2.sql -- 2.40.1