diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index 2aa2a07c4..b3291c25a 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -18,9 +18,12 @@ describe('ticket componentUpdate()', () => { beforeAll(async() => { const deliveryComponenet = await models.Component.findOne({where: {code: 'delivery'}}); deliveryComponentId = deliveryComponenet.id; - componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; - componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; - + componentOfSaleSeven = `SELECT value + FROM vn.saleComponent + WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; + componentOfSaleEight = `SELECT value + FROM vn.saleComponent + WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; [componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven); firstvalueBeforeChange = componentValue.value; diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index b1291a45b..a60d732b1 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -115,14 +115,38 @@ module.exports = Self => { for (let sale of sales) { const oldDiscount = sale.discount; const value = ((-sale.price * newDiscount) / 100); - const newComponent = models.SaleComponent.upsert({ - saleFk: sale.id, - value: value, - componentFk: componentId}, myOptions); + + const manaComponent = await models.Component.findOne({ + where: {code: 'mana'} + }, myOptions); + + const manaClaimComponent = await models.Component.findOne({ + where: {code: 'manaClaim'} + }, myOptions); + + const [oldComponent] = await models.SaleComponent.find({ + where: { + and: [ + {saleFk: sale.id}, + {componentFk: {inq: [manaComponent.id, manaClaimComponent.id]}} + ] + } + }, myOptions); + + let deletedComponent; + if (oldComponent) { + const filter = { + saleFk: sale.id, + componentFk: oldComponent.componentFk + }; + deletedComponent = await models.SaleComponent.destroyAll(filter, myOptions); + } + + const newComponent = await createSaleComponent(sale.id, value, componentId, myOptions); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); - promises.push(newComponent, updatedSale); + promises.push(newComponent, updatedSale, deletedComponent); const change = `${oldDiscount}% ➔ *${newDiscount}%*`; changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${change}`; @@ -165,4 +189,14 @@ module.exports = Self => { throw e; } }; + + async function createSaleComponent(saleId, value, componentId, myOptions) { + const models = Self.app.models; + + return models.SaleComponent.create({ + saleFk: saleId, + value: value, + componentFk: componentId + }, myOptions); + } }; diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 42eb10cb0..112476c30 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -300,7 +300,7 @@ ng-model="$ctrl.manaCode"> -
New price
{{$ctrl.getNewPrice() | currency: 'EUR': 2}} @@ -321,32 +321,6 @@