diff --git a/modules/item/back/methods/fixed-price/specs/upsertFixedPrice.spec.js b/modules/item/back/methods/fixed-price/specs/upsertFixedPrice.spec.js index 68eacfbad..59defa738 100644 --- a/modules/item/back/methods/fixed-price/specs/upsertFixedPrice.spec.js +++ b/modules/item/back/methods/fixed-price/specs/upsertFixedPrice.spec.js @@ -72,4 +72,45 @@ describe('upsertFixedPrice()', () => { throw e; } }); + + it(`should recalculate rate2 if change rate3`, async() => { + const tx = await models.FixedPrice.beginTransaction({}); + + const tomorrow = new Date(now); + tomorrow.setDate(tomorrow.getDate() + 1); + + const rate2 = 2; + const firstRate3 = 1; + const secondRate3 = 2; + try { + const options = {transaction: tx}; + const ctx = {args: { + id: undefined, + itemFk: 1, + warehouseFk: 1, + started: tomorrow, + ended: tomorrow, + rate2: rate2, + rate3: firstRate3, + minPrice: 0, + hasMinPrice: false + }}; + + // create new fixed price + const newFixedPrice = await models.FixedPrice.upsertFixedPrice(ctx, options); + + // change rate3 to same fixed price id + ctx.args.id = newFixedPrice.id; + ctx.args.rate3 = secondRate3; + + const result = await models.FixedPrice.upsertFixedPrice(ctx, options); + + expect(result.rate2).not.toEqual(rate2); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); diff --git a/modules/item/back/methods/fixed-price/upsertFixedPrice.js b/modules/item/back/methods/fixed-price/upsertFixedPrice.js index ad35ec3a1..c56c434ad 100644 --- a/modules/item/back/methods/fixed-price/upsertFixedPrice.js +++ b/modules/item/back/methods/fixed-price/upsertFixedPrice.js @@ -72,6 +72,16 @@ module.exports = Self => { try { delete args.ctx; // removed unwanted data + + if (args.id) { + const beforeFixedPrice = await models.FixedPrice.findById(args.id, {fields: ['rate3']}, myOptions); + const [result] = await Self.rawSql(`SELECT vn.priceFixed_getRate2(?, ?) as rate2`, + [args.id, args.rate3], myOptions); + + if (beforeFixedPrice.rate3 != args.rate3 && result.rate2) + args.rate2 = result.rate2; + } + const fixedPrice = await models.FixedPrice.upsert(args, myOptions); const targetItem = await models.Item.findById(args.itemFk, null, myOptions); diff --git a/modules/item/front/fixed-price/index.html b/modules/item/front/fixed-price/index.html index f9d177562..ce7cefe7a 100644 --- a/modules/item/front/fixed-price/index.html +++ b/modules/item/front/fixed-price/index.html @@ -131,7 +131,7 @@ class="dense" vn-focus ng-model="price.rate3" - on-change="$ctrl.upsertPrice(price); $ctrl.recalculateRate2(price)" + on-change="$ctrl.upsertPrice(price);" step="0.01"s> diff --git a/modules/item/front/fixed-price/index.js b/modules/item/front/fixed-price/index.js index f03d73f08..df2989043 100644 --- a/modules/item/front/fixed-price/index.js +++ b/modules/item/front/fixed-price/index.js @@ -113,24 +113,6 @@ export default class Controller extends Section { return {[param]: value}; } } - - recalculateRate2(price) { - if (!price.id || !price.rate3) return; - - const query = 'FixedPrices/getRate2'; - const params = { - fixedPriceId: price.id, - rate3: price.rate3 - }; - this.$http.get(query, {params}) - .then(res => { - const rate2 = res.data.rate2; - if (rate2) { - price.rate2 = rate2; - this.upsertPrice(price); - } - }); - } } ngModule.vnComponent('vnFixedPrice', { diff --git a/modules/item/front/fixed-price/index.spec.js b/modules/item/front/fixed-price/index.spec.js index db9579444..94621e352 100644 --- a/modules/item/front/fixed-price/index.spec.js +++ b/modules/item/front/fixed-price/index.spec.js @@ -85,25 +85,5 @@ 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); - }); - }); }); });