Merge pull request '4616-ticket.sale_mana' (!1088) from 4616-ticket.sale_mana into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1088
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Vicent Llopis 2022-11-02 13:48:18 +00:00
commit c01b1c06a7
5 changed files with 66 additions and 60 deletions

View File

@ -18,9 +18,12 @@ describe('ticket componentUpdate()', () => {
beforeAll(async() => { beforeAll(async() => {
const deliveryComponenet = await models.Component.findOne({where: {code: 'delivery'}}); const deliveryComponenet = await models.Component.findOne({where: {code: 'delivery'}});
deliveryComponentId = deliveryComponenet.id; deliveryComponentId = deliveryComponenet.id;
componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; componentOfSaleSeven = `SELECT value
componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; 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); [componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven);
firstvalueBeforeChange = componentValue.value; firstvalueBeforeChange = componentValue.value;

View File

@ -115,14 +115,38 @@ module.exports = Self => {
for (let sale of sales) { for (let sale of sales) {
const oldDiscount = sale.discount; const oldDiscount = sale.discount;
const value = ((-sale.price * newDiscount) / 100); const value = ((-sale.price * newDiscount) / 100);
const newComponent = models.SaleComponent.upsert({
saleFk: sale.id, const manaComponent = await models.Component.findOne({
value: value, where: {code: 'mana'}
componentFk: componentId}, myOptions); }, 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); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions);
promises.push(newComponent, updatedSale); promises.push(newComponent, updatedSale, deletedComponent);
const change = `${oldDiscount}% ➔ *${newDiscount}%*`; const change = `${oldDiscount}% ➔ *${newDiscount}%*`;
changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${change}`; changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${change}`;
@ -165,4 +189,14 @@ module.exports = Self => {
throw e; 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);
}
}; };

View File

@ -300,7 +300,7 @@
ng-model="$ctrl.manaCode"> ng-model="$ctrl.manaCode">
</vn-radio> </vn-radio>
</vn-vertical> </vn-vertical>
<div class="simulator"> <div class="simulator" ng-show="$ctrl.edit.sale">
<p class="simulatorTitle" translate>New price</p> <p class="simulatorTitle" translate>New price</p>
<p> <p>
<strong>{{$ctrl.getNewPrice() | currency: 'EUR': 2}}</strong> <strong>{{$ctrl.getNewPrice() | currency: 'EUR': 2}}</strong>
@ -321,32 +321,6 @@
</div> </div>
</vn-popover> </vn-popover>
<!-- Multiple discount dialog -->
<vn-dialog vn-id="editDiscountDialog"
on-open="$ctrl.getMana()"
message="Edit discount">
<tpl-body>
<vn-spinner class="vn-pa-xs"
ng-if="$ctrl.edit.mana == null"
enable="true">
</vn-spinner>
<div ng-if="$ctrl.edit.mana != null">
<div class="vn-pa-md">
<vn-input-number vn-focus
label="Discount"
ng-model="$ctrl.edit.discount"
on-change="$ctrl.changeMultipleDiscount()"
clear-disabled="true"
suffix="%">
</vn-input-number>
</div>
<section class="header vn-pa-md">
<span>Mana: <strong>{{::$ctrl.edit.mana | currency: 'EUR': 0}}</strong></span>
</section>
</div>
</tpl-body>
</vn-dialog>
<!-- Transfer Popover --> <!-- Transfer Popover -->
<vn-popover vn-id="transfer"> <vn-popover vn-id="transfer">
<div class="vn-pa-lg transfer"> <div class="vn-pa-lg transfer">
@ -490,7 +464,7 @@
</vn-item> </vn-item>
<vn-item translate <vn-item translate
name="discount" name="discount"
ng-click="$ctrl.showEditDiscountDialog($event)"> ng-click="$ctrl.showEditDiscountPopover($event, sale)">
Update discount Update discount
</vn-item> </vn-item>
<vn-item translate <vn-item translate

View File

@ -243,26 +243,21 @@ class Controller extends Section {
showEditDiscountPopover(event, sale) { showEditDiscountPopover(event, sale) {
if (this.isLocked) return; if (this.isLocked) return;
if (sale) {
this.edit = { this.edit = {
discount: sale.discount, discount: sale.discount,
sale: sale sale: sale
}; };
} else {
this.edit = {
discount: null,
sales: this.selectedValidSales()
};
}
this.$.editDiscount.show(event); this.$.editDiscount.show(event);
} }
showEditDiscountDialog(event) {
if (this.isLocked) return;
this.edit = {
discount: null,
sales: this.selectedValidSales()
};
this.$.editDiscountDialog.show(event);
}
changeDiscount() { changeDiscount() {
const sale = this.edit.sale; const sale = this.edit.sale;
const newDiscount = this.edit.discount; const newDiscount = this.edit.discount;
@ -278,11 +273,10 @@ class Controller extends Section {
const hasChanges = sales.some(sale => { const hasChanges = sales.some(sale => {
return sale.discount != newDiscount; return sale.discount != newDiscount;
}); });
if (newDiscount != null && hasChanges) if (newDiscount != null && hasChanges)
this.updateDiscount(sales); this.updateDiscount(sales);
this.$.editDiscountDialog.hide(); this.$.editDiscount.hide();
} }
updateDiscount(sales) { updateDiscount(sales) {
@ -303,7 +297,7 @@ class Controller extends Section {
} }
getNewPrice() { getNewPrice() {
if (this.edit) { if (this.edit.sale) {
const sale = this.edit.sale; const sale = this.edit.sale;
let newDiscount = sale.discount; let newDiscount = sale.discount;
let newPrice = this.edit.price || sale.price; let newPrice = this.edit.price || sale.price;
@ -505,7 +499,8 @@ class Controller extends Section {
} }
save() { save() {
this.changeDiscount(); if (this.edit.sale) this.changeDiscount();
if (this.edit.sales) this.changeMultipleDiscount();
} }
cancel() { cancel() {

View File

@ -393,7 +393,7 @@ describe('Ticket', () => {
secondSelectedSale.checked = true; secondSelectedSale.checked = true;
const expectedSales = [firstSelectedSale, secondSelectedSale]; const expectedSales = [firstSelectedSale, secondSelectedSale];
controller.$.editDiscountDialog = {hide: jest.fn()}; controller.$.editDiscount = {hide: jest.fn()};
controller.edit = { controller.edit = {
discount: 10, discount: 10,
sales: expectedSales sales: expectedSales
@ -402,7 +402,7 @@ describe('Ticket', () => {
controller.changeMultipleDiscount(); controller.changeMultipleDiscount();
expect(controller.updateDiscount).toHaveBeenCalledWith(expectedSales); expect(controller.updateDiscount).toHaveBeenCalledWith(expectedSales);
expect(controller.$.editDiscountDialog.hide).toHaveBeenCalledWith(); expect(controller.$.editDiscount.hide).toHaveBeenCalledWith();
}); });
it('should not call to the updateDiscount() method and then to the editDiscountDialog hide() method', () => { it('should not call to the updateDiscount() method and then to the editDiscountDialog hide() method', () => {
@ -417,7 +417,7 @@ describe('Ticket', () => {
secondSelectedSale.discount = 10; secondSelectedSale.discount = 10;
const expectedSales = [firstSelectedSale, secondSelectedSale]; const expectedSales = [firstSelectedSale, secondSelectedSale];
controller.$.editDiscountDialog = {hide: jest.fn()}; controller.$.editDiscount = {hide: jest.fn()};
controller.edit = { controller.edit = {
discount: 10, discount: 10,
sales: expectedSales sales: expectedSales
@ -426,7 +426,7 @@ describe('Ticket', () => {
controller.changeMultipleDiscount(); controller.changeMultipleDiscount();
expect(controller.updateDiscount).not.toHaveBeenCalledWith(expectedSales); expect(controller.updateDiscount).not.toHaveBeenCalledWith(expectedSales);
expect(controller.$.editDiscountDialog.hide).toHaveBeenCalledWith(); expect(controller.$.editDiscount.hide).toHaveBeenCalledWith();
}); });
}); });