Tarea #352 ticket.lines Modificar descuento

This commit is contained in:
gerard 2018-06-28 15:21:04 +02:00
parent 54ee27dcef
commit accd66f63a
5 changed files with 250 additions and 3 deletions

View File

@ -0,0 +1,24 @@
<vn-horizontal pad-medium class="header">
<h5>MANÁ: {{$ctrl.mana | currency:' €':0}}</h5>
</vn-horizontal>
<div pad-medium>
<vn-textfield
label="Discount"
model="$ctrl.newDiscount"
type="number"
on-change="$ctrl.updateDiscount()">
<t-right-icons>
<span class="filter">%</span>
</t-right-icons>
</vn-textfield>
<div class="simulator">
<p class="simulatorTitle" translate>New price</p>
<p>{{($ctrl.edit[0].quantity * $ctrl.edit[0].price)
- (($ctrl.newDiscount * ($ctrl.edit[0].quantity * $ctrl.edit[0].price))/100)
| currency:' €':2}}</p>
<vn-button
label="Save"
ng-click="$ctrl.updateDiscount()">
</vn-button>
</div>
</div>

View File

@ -0,0 +1,71 @@
import ngModule from '../module';
class Controller {
constructor($scope, $http, $state, vnApp, $translate) {
this.$scope = $scope;
this.$http = $http;
this.$state = $state;
this.vnApp = vnApp;
this.translate = $translate;
}
set edit(value) {
this._edit = value;
this.clear();
this.setNewDiscount();
}
get edit() {
return this._edit;
}
set bulk(value) {
this._bulk = value;
this.setNewDiscount();
}
get bulk() {
return this._bulk;
}
setNewDiscount() {
if (!this.newDiscount && this.edit[0])
this.newDiscount = this.edit[0].discount;
}
updateDiscount() {
let editLines = [];
let modified = false;
for (let i = 0; i < this.edit.length; i++) {
if (this.newDiscount != this.edit[0].discount || this.bulk) {
editLines.push({id: this.edit[i].id, discount: this.newDiscount, ticketFk: this.$state.params.id});
modified = true;
}
}
if (modified) {
this.$http.post(`/ticket/api/Sales/updateDiscount`, {editLines}).then(() => {
this.hide();
});
} else {
this.vnApp.showError(this.translate.instant('There is no changes to save'));
}
this.clear();
}
clear() {
this.newDiscount = null;
}
}
Controller.$inject = ['$scope', '$http', '$state', 'vnApp', '$translate'];
ngModule.component('vnTicketSaleEditDiscount', {
template: require('./editDiscount.html'),
controller: Controller,
bindings: {
edit: '<?',
mana: '<?',
bulk: '<?',
hide: '&'
}
});

View File

@ -0,0 +1,89 @@
import './editDiscount.js';
describe('Ticket', () => {
describe('Component vnTicketSaleEditDiscount', () => {
let $componentController;
let controller;
let $httpBackend;
let $state;
let $scope;
beforeEach(() => {
angular.mock.module('ticket');
});
beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_, $rootScope) => {
$componentController = _$componentController_;
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
$scope = $rootScope.$new();
$scope.index = {model: {instances: [{id: 1}, {id: 2}]}, accept: () => {
return {
then: () => {}
};
}};
$state = _$state_;
$state.params.id = 1;
controller = $componentController('vnTicketSaleEditDiscount', {$scope: $scope, $state: $state});
controller._edit = [{id: 3, discount: 15}];
controller.hide = () => {};
}));
describe('edit() setter', () => {
it('should set _edit value and call setNewDiscount', () => {
spyOn(controller, 'setNewDiscount');
controller.edit = {id: 1};
expect(controller.edit).toEqual({id: 1});
expect(controller.setNewDiscount).toHaveBeenCalledWith();
});
});
describe('bulk() setter', () => {
it('should set _bulk value and call setNewDiscount', () => {
spyOn(controller, 'setNewDiscount');
controller.bulk = true;
expect(controller.bulk).toEqual(true);
expect(controller.setNewDiscount).toHaveBeenCalledWith();
});
});
describe('setNewDiscount()', () => {
it('should set NewDiscount to edit[0].discount value if it doesnt exists', () => {
controller.edit = [{discount: 1}];
controller.setNewDiscount();
expect(controller.newDiscount).toEqual(1);
});
});
describe('updateDiscount()', () => {
it('should make a query if the discount value has been modified or the bulk value is true', () => {
controller.bulk = true;
controller.newDiscount = 15;
$httpBackend.expectPOST(`/ticket/api/Sales/updateDiscount`).respond();
controller.updateDiscount();
$httpBackend.flush();
});
it('should call vnApp.showError if the discount value hasnt has been modified and the bulk value is false', () => {
controller.bulk = false;
spyOn(controller.vnApp, "showError");
controller.updateDiscount();
expect(controller.vnApp.showError).toHaveBeenCalledWith('There is no changes to save');
});
});
describe('clear()', () => {
it('should set newDiscount to null', () => {
controller.clear();
expect(controller.newDiscount).toEqual(null);
});
});
});
});

View File

@ -0,0 +1,62 @@
module.exports = Self => {
Self.remoteMethod('updateDiscount', {
description: 'Changes the discount of a sale',
accessType: '',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: 'sale ID, newDiscount, price',
http: {source: 'body'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/updateDiscount`,
verb: 'post'
}
});
Self.updateDiscount = async params => {
let model = Self.app.models;
let thisTicketIsEditable = await model.Ticket.isEditable(params.editLines[0].ticketFk);
if (!thisTicketIsEditable)
throw new Error(`The sales of this ticket can't be modified`);
let ticket = await model.Ticket.find({
where: {
id: params.editLines[0].ticketFk
},
include: [{
relation: 'client',
scope: {
fields: ['salesPersonFk']
}
}],
fields: ['id', 'clientFk']
});
let componentToUse;
let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'});
if (usesMana)
componentToUse = 37;
else
componentToUse = 34;
for (let i = 0; i < params.editLines.length; i++) {
let currentLine = await model.Sale.findOne({where: {id: params.editLines[i].id}, fields: 'price'});
let value = (-currentLine.price * params.editLines[i].discount / 100);
await model.SaleComponent.upsert({saleFk: params.editLines[i].id, value: value, componentFk: componentToUse});
await model.Sale.update({id: params.editLines[i].id}, {discount: params.editLines[i].discount});
}
query = `
call vn.manaSpellersRequery(?)`;
await Self.rawSql(query, [ticket[0].client().salesPersonFk]);
};
};

View File

@ -4,8 +4,9 @@ module.exports = Self => {
require('../methods/sale/priceDifference')(Self);
require('../methods/sale/crudSale')(Self);
require('../methods/sale/moveToTicket')(Self);
require('../methods/sale/moveToNewTicket')(Self);
require('../methods/sale/removes')(Self);
// require('../methods/sale/updateDiscount')(Self);
// require('../methods/sale/updatePrice')(Self);
// require('../methods/sale/updateQuantity')(Self);
require('../methods/sale/updateDiscount')(Self);
require('../methods/sale/updatePrice')(Self);
require('../methods/sale/updateQuantity')(Self);
};