This commit is contained in:
parent
68f67f12d7
commit
62fe9d760c
|
@ -442,7 +442,7 @@ INSERT INTO `vn`.`ticket`(`id`, `agencyModeFk`,`warehouseFk`,`routeFk`, `shipped
|
||||||
(5, 3, 3, 3, DATE_ADD(CURDATE(), INTERVAL -3 DAY), DATE_ADD(CURDATE(), INTERVAL -3 DAY), 103, 'address 23', 123, 'T3333333', 0, DATE_ADD(CURDATE(), INTERVAL -3 DAY)),
|
(5, 3, 3, 3, DATE_ADD(CURDATE(), INTERVAL -3 DAY), DATE_ADD(CURDATE(), INTERVAL -3 DAY), 103, 'address 23', 123, 'T3333333', 0, DATE_ADD(CURDATE(), INTERVAL -3 DAY)),
|
||||||
(6, 3, 3, 4, DATE_ADD(CURDATE(), INTERVAL -2 DAY), DATE_ADD(CURDATE(), INTERVAL -2 DAY), 103, 'address 23', 123, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -2 DAY)),
|
(6, 3, 3, 4, DATE_ADD(CURDATE(), INTERVAL -2 DAY), DATE_ADD(CURDATE(), INTERVAL -2 DAY), 103, 'address 23', 123, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -2 DAY)),
|
||||||
(7, 4, 4, 4, DATE_ADD(CURDATE(), INTERVAL -1 DAY), DATE_ADD(CURDATE(), INTERVAL -1 DAY), 104, 'address 24', 124, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -1 DAY)),
|
(7, 4, 4, 4, DATE_ADD(CURDATE(), INTERVAL -1 DAY), DATE_ADD(CURDATE(), INTERVAL -1 DAY), 104, 'address 24', 124, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -1 DAY)),
|
||||||
(8, 1, 1, 4, DATE_ADD(CURDATE(), INTERVAL +1 MONTH), DATE_ADD(CURDATE(), INTERVAL +1 MONTH), 104, 'address 24', 124, NULL, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)),
|
(8, 1, 1, 4, CURDATE(), CURDATE(), 104, 'address 24', 124, NULL, 0, CURDATE()),
|
||||||
(9, 5, 5, 4, DATE_ADD(CURDATE(), INTERVAL -2 MONTH), DATE_ADD(CURDATE(), INTERVAL -2 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -2 MONTH)),
|
(9, 5, 5, 4, DATE_ADD(CURDATE(), INTERVAL -2 MONTH), DATE_ADD(CURDATE(), INTERVAL -2 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -2 MONTH)),
|
||||||
(10, 6, 5, 5, DATE_ADD(CURDATE(), INTERVAL -3 MONTH), DATE_ADD(CURDATE(), INTERVAL -3 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -3 MONTH)),
|
(10, 6, 5, 5, DATE_ADD(CURDATE(), INTERVAL -3 MONTH), DATE_ADD(CURDATE(), INTERVAL -3 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -3 MONTH)),
|
||||||
(11, 7, 1, 1, CURDATE() , CURDATE() , 101, 'address 21', 121, NULL, 0, CURDATE()),
|
(11, 7, 1, 1, CURDATE() , CURDATE() , 101, 'address 21', 121, NULL, 0, CURDATE()),
|
||||||
|
|
|
@ -1,121 +1,73 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
// #1489 updatePrice backTest
|
// #1489 updatePrice backTest
|
||||||
xdescribe('sale updatePrice()', () => {
|
describe('sale updatePrice()', () => {
|
||||||
it('should set price to 0 if the params price is undefined', async() => {
|
let originalSale;
|
||||||
let params = {
|
let originalSalesPersonMana;
|
||||||
price: undefined,
|
let createdSaleComponent;
|
||||||
id: 13,
|
let saleId = 13;
|
||||||
ticketFk: 8
|
|
||||||
};
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(0);
|
afterAll(async done => {
|
||||||
|
originalSale.save();
|
||||||
|
createdSaleComponent.destroy();
|
||||||
|
originalSalesPersonMana.save();
|
||||||
|
|
||||||
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set price as a decimal number', async() => {
|
beforeAll(async done => {
|
||||||
let params = {
|
originalSale = await app.models.Sale.findById(saleId);
|
||||||
price: 5.5,
|
originalSalesPersonMana = await app.models.WorkerMana.findById(18);
|
||||||
id: 13,
|
|
||||||
ticketFk: 8
|
|
||||||
};
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(5.5);
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should now set price as a decimal number in a string', async() => {
|
it('should throw an error if the ticket is not editable', async() => {
|
||||||
let params = {
|
let immutableSaleId = 1;
|
||||||
price: '5.5',
|
let price = 5;
|
||||||
id: 13,
|
|
||||||
ticketFk: 8
|
|
||||||
};
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(5.5);
|
await app.models.Sale.updatePrice(immutableSaleId, price)
|
||||||
|
.catch(response => {
|
||||||
|
expect(response).toEqual(new Error(`The sales of this ticket can't be modified`));
|
||||||
|
error = response;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(error).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 0 if the price is an empty string', async() => {
|
it('should return 0 if the price is an empty string', async() => {
|
||||||
let params = {
|
let price = '';
|
||||||
price: '',
|
|
||||||
id: 13,
|
await app.models.Sale.updatePrice(saleId, price);
|
||||||
ticketFk: 8
|
let saleUpdated = await app.models.Sale.findById(saleId);
|
||||||
};
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(0);
|
expect(saleUpdated.price).toEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should now set price as a decimal number in a string', async() => {
|
||||||
|
let price = '8';
|
||||||
|
|
||||||
it('should este crea mana', async() => {
|
await app.models.Sale.updatePrice(saleId, price);
|
||||||
let params = {
|
let saleUpdated = await app.models.Sale.findById(saleId);
|
||||||
price: 5.5,
|
|
||||||
id: 13,
|
|
||||||
ticketFk: 8
|
|
||||||
};
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(5.5);
|
expect(saleUpdated.price).toEqual(8);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should este no crea el mana', async() => {
|
it('should set price as a decimal number and check the sale has the mana component', async() => {
|
||||||
let params = {
|
let price = 5.5;
|
||||||
price: 5.5,
|
let manaComponentId = 37;
|
||||||
id: 13,
|
|
||||||
ticketFk: 8
|
|
||||||
};
|
|
||||||
let todosLosComponentes = await app.models.SaleComponent.find({where: {saleFk: 13}});
|
|
||||||
|
|
||||||
expect(todosLosComponentes[0].value).toEqual('');
|
await app.models.Sale.updatePrice(saleId, price);
|
||||||
expect(todosLosComponentes[1].value).toEqual('');
|
let saleUpdated = await app.models.Sale.findById(saleId);
|
||||||
expect(todosLosComponentes[2].value).toEqual('');
|
createdSaleComponent = await app.models.SaleComponent.findOne({where: {saleFk: saleId, componentFk: manaComponentId}});
|
||||||
expect(todosLosComponentes[3].value).toEqual('');
|
|
||||||
expect(todosLosComponentes[4].value).toEqual('');
|
|
||||||
|
|
||||||
await app.models.Sale.updatePrice(params);
|
|
||||||
todosLosComponentes = await app.models.SaleComponent.find({where: {saleFk: 13}});
|
|
||||||
let saleUpdated = await app.models.Sale.findById(params.id);
|
|
||||||
|
|
||||||
expect(saleUpdated.price).toEqual(5.5);
|
expect(saleUpdated.price).toEqual(5.5);
|
||||||
|
expect(createdSaleComponent.value).toEqual(4.200);
|
||||||
expect(todosLosComponentes[0].value).toEqual('');
|
|
||||||
expect(todosLosComponentes[1].value).toEqual('');
|
|
||||||
expect(todosLosComponentes[2].value).toEqual('');
|
|
||||||
expect(todosLosComponentes[3].value).toEqual('');
|
|
||||||
expect(todosLosComponentes[4].value).toEqual('');
|
|
||||||
});
|
});
|
||||||
// 2 let componentToUse;
|
|
||||||
// if (usesMana)
|
|
||||||
// componentToUse = 37;
|
|
||||||
// 3else
|
|
||||||
// componentToUse = 34;
|
|
||||||
|
|
||||||
// 4 if (saleComponent)
|
it('should check that the mana of salesPerson changed', async() => {
|
||||||
// saleComponent.updateAttributes({value: saleComponent.value + value});
|
let updatedSalesPersonMana = await app.models.WorkerMana.findById(18);
|
||||||
// 5 else {
|
|
||||||
// await Self.app.models.SaleComponent.create({
|
|
||||||
// saleFk: params.id,
|
|
||||||
// componentFk: componentToUse,
|
|
||||||
// value: value
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// it(`should throw an error if the price is undefined`, async() => {
|
expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount);
|
||||||
// let error;
|
});
|
||||||
|
|
||||||
// let params = {ticketFk: 1, price: undefined};
|
|
||||||
|
|
||||||
// await app.models.Sale.updatePrice(params)
|
|
||||||
// .catch(response => {
|
|
||||||
// expect(response).toEqual(new Error('The value should be a number'));
|
|
||||||
// error = response;
|
|
||||||
// });
|
|
||||||
|
|
||||||
// expect(error).toBeDefined();
|
|
||||||
// });
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,84 +2,94 @@ let UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('updatePrice', {
|
Self.remoteMethod('updatePrice', {
|
||||||
description: 'Changes the discount of a sale',
|
description: 'Changes the price of a sale',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [
|
||||||
arg: 'params',
|
{
|
||||||
type: 'object',
|
arg: 'id',
|
||||||
required: true,
|
description: 'The sale id',
|
||||||
description: 'sale ID, newPrice, ticketFk',
|
type: 'number',
|
||||||
http: {source: 'body'}
|
required: true,
|
||||||
}],
|
http: {source: 'path'}
|
||||||
|
}, {
|
||||||
|
arg: 'newPrice',
|
||||||
|
description: 'The new price',
|
||||||
|
type: 'number',
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
root: true
|
root: true
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/updatePrice`,
|
path: `/:id/updatePrice`,
|
||||||
verb: 'post'
|
verb: 'post'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.updatePrice = async params => {
|
Self.updatePrice = async(id, newPrice) => {
|
||||||
if (!params.price) params.price = 0;
|
let $ = Self.app.models;
|
||||||
|
let transaction = await Self.beginTransaction({});
|
||||||
|
let options = {transaction};
|
||||||
|
|
||||||
let model = Self.app.models;
|
try {
|
||||||
let manaDiscount;
|
let filter = {
|
||||||
let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}});
|
fields: ['id', 'ticketFk', 'price'],
|
||||||
let ticket = await getTicket(params);
|
include: {
|
||||||
let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'});
|
relation: 'ticket',
|
||||||
let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}});
|
scope: {
|
||||||
let componentId = buyerDiscount.id;
|
include: {
|
||||||
|
relation: 'client',
|
||||||
if (usesMana) {
|
scope: {
|
||||||
manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}});
|
fields: ['salesPersonFk']
|
||||||
componentId = manaDiscount.id;
|
}
|
||||||
}
|
},
|
||||||
let value = (params.price - currentLine.price);
|
fields: ['id', 'clientFk']
|
||||||
|
}
|
||||||
let saleComponent = await Self.app.models.SaleComponent.findOne({
|
|
||||||
where: {
|
|
||||||
componentFk: componentId,
|
|
||||||
saleFk: params.id
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (saleComponent) {
|
|
||||||
saleComponent.updateAttributes({value: saleComponent.value + value}).catch(() => {
|
|
||||||
throw new UserError(`Enter a valid number`);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await Self.app.models.SaleComponent.create({
|
|
||||||
saleFk: params.id,
|
|
||||||
componentFk: componentId,
|
|
||||||
value: value
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await currentLine.updateAttributes({price: params.price});
|
|
||||||
|
|
||||||
query = `call vn.manaSpellersRequery(?)`;
|
|
||||||
await Self.rawSql(query, [ticket[0].client().salesPersonFk]);
|
|
||||||
};
|
|
||||||
|
|
||||||
async function getTicket(params) {
|
|
||||||
let model = Self.app.models;
|
|
||||||
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk);
|
|
||||||
if (!thisTicketIsEditable)
|
|
||||||
throw new UserError(`The sales of this ticket can't be modified`);
|
|
||||||
|
|
||||||
return await model.Ticket.find({
|
|
||||||
where: {
|
|
||||||
id: params.ticketFk
|
|
||||||
},
|
|
||||||
include: [{
|
|
||||||
relation: 'client',
|
|
||||||
scope: {
|
|
||||||
fields: ['salesPersonFk']
|
|
||||||
}
|
}
|
||||||
}],
|
};
|
||||||
fields: ['id', 'clientFk']
|
let sale = await $.Sale.findById(id, filter, options);
|
||||||
});
|
|
||||||
}
|
let isEditable = await $.Ticket.isEditable(sale.ticketFk);
|
||||||
|
if (!isEditable)
|
||||||
|
throw new UserError(`The sales of this ticket can't be modified`);
|
||||||
|
|
||||||
|
let salesPerson = sale.ticket().client().salesPersonFk;
|
||||||
|
let usesMana = await $.WorkerMana.findOne({where: {workerFk: salesPerson}, fields: 'amount'}, options);
|
||||||
|
let componentCode = usesMana ? 'mana' : 'buyerDiscount';
|
||||||
|
|
||||||
|
let discount = await $.ComponentRate.findOne({where: {code: componentCode}}, options);
|
||||||
|
let componentId = discount.id;
|
||||||
|
let componentValue = newPrice - sale.price;
|
||||||
|
|
||||||
|
let where = {
|
||||||
|
componentFk: componentId,
|
||||||
|
saleFk: id
|
||||||
|
};
|
||||||
|
let saleComponent = await $.SaleComponent.findOne({where}, options);
|
||||||
|
|
||||||
|
if (saleComponent) {
|
||||||
|
await $.SaleComponent.updateAll(where, {
|
||||||
|
value: saleComponent.value + componentValue
|
||||||
|
}, options);
|
||||||
|
} else {
|
||||||
|
await $.SaleComponent.create({
|
||||||
|
saleFk: id,
|
||||||
|
componentFk: componentId,
|
||||||
|
value: componentValue
|
||||||
|
}, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
await sale.updateAttributes({price: newPrice}, options);
|
||||||
|
|
||||||
|
query = `call vn.manaSpellersRequery(?)`;
|
||||||
|
await Self.rawSql(query, [salesPerson], options);
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@ describe('ticket getSalesPersonMana()', () => {
|
||||||
it('should get the mana of a salesperson of a given ticket', async() => {
|
it('should get the mana of a salesperson of a given ticket', async() => {
|
||||||
let mana = await app.models.Ticket.getSalesPersonMana(1);
|
let mana = await app.models.Ticket.getSalesPersonMana(1);
|
||||||
|
|
||||||
expect(mana).toEqual(221);
|
expect(mana).toEqual(222);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 0 if the given ticket does not exists', async() => {
|
it('should return 0 if the given ticket does not exists', async() => {
|
||||||
|
|
|
@ -11,7 +11,12 @@
|
||||||
"type": "Number"
|
"type": "Number"
|
||||||
},
|
},
|
||||||
"saleFk": {
|
"saleFk": {
|
||||||
"id": true
|
"type": "Number",
|
||||||
|
"id": 2
|
||||||
|
},
|
||||||
|
"componentFk": {
|
||||||
|
"type": "Number",
|
||||||
|
"id": 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"relations": {
|
"relations": {
|
||||||
|
|
|
@ -260,7 +260,7 @@ class Controller {
|
||||||
|
|
||||||
updatePrice() {
|
updatePrice() {
|
||||||
if (this.editedPrice != this.sale.price) {
|
if (this.editedPrice != this.sale.price) {
|
||||||
this.$http.post(`/api/Sales/updatePrice`, {id: this.edit.id, price: this.editedPrice, ticketFk: this.ticket.id}).then(() => {
|
this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(() => {
|
||||||
this.sale.price = this.edit.price;
|
this.sale.price = this.edit.price;
|
||||||
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
|
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
|
||||||
this.$scope.model.refresh();
|
this.$scope.model.refresh();
|
||||||
|
|
|
@ -4,7 +4,7 @@ describe('workerMana getCurrentWorkerMana()', () => {
|
||||||
it('should get the mana of the logged worker', async() => {
|
it('should get the mana of the logged worker', async() => {
|
||||||
let mana = await app.models.WorkerMana.getCurrentWorkerMana({req: {accessToken: {userId: 18}}});
|
let mana = await app.models.WorkerMana.getCurrentWorkerMana({req: {accessToken: {userId: 18}}});
|
||||||
|
|
||||||
expect(mana).toEqual(221);
|
expect(mana).toEqual(222);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 0 if the user doesnt uses mana', async() => {
|
it('should return 0 if the user doesnt uses mana', async() => {
|
||||||
|
|
Loading…
Reference in New Issue