fixed infinite claim detail save #866

This commit is contained in:
Joan 2018-11-23 14:30:01 +01:00
parent 874b1880cf
commit e60d485111
4 changed files with 32 additions and 28 deletions

View File

@ -110,7 +110,7 @@ describe('claim', () => {
}); });
describe('importToNewRefundTicket()', () => { describe('importToNewRefundTicket()', () => {
it('should perform a post query', () => { it('should perform a post query and add lines from a new ticket', () => {
spyOn(controller.$.model, 'refresh'); spyOn(controller.$.model, 'refresh');
spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.vnApp, 'showSuccess');
$httpBackend.expect('POST', `claim/api/ClaimBeginnings/1/importToNewRefundTicket`).respond({}); $httpBackend.expect('POST', `claim/api/ClaimBeginnings/1/importToNewRefundTicket`).respond({});
@ -134,7 +134,7 @@ describe('claim', () => {
}); });
describe('importTicketLines()', () => { describe('importTicketLines()', () => {
it('should perform a post query', () => { it('should perform a post query and add lines from an existent ticket', () => {
spyOn(controller.$.model, 'refresh'); spyOn(controller.$.model, 'refresh');
spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.vnApp, 'showSuccess');
spyOn(controller.$.lastTicketsPopover, 'hide'); spyOn(controller.$.lastTicketsPopover, 'hide');

View File

@ -2,7 +2,7 @@
vn-id="model" vn-id="model"
url="claim/api/ClaimBeginnings" url="claim/api/ClaimBeginnings"
filter="$ctrl.filter" filter="$ctrl.filter"
data="$ctrl.salesClaimed"> data="$ctrl.salesClaimed" on-data-change="$ctrl.calculateTotals()">
</vn-crud-model> </vn-crud-model>
<vn-vertical> <vn-vertical>
<vn-card pad-large> <vn-card pad-large>
@ -32,7 +32,7 @@
</vn-tr> </vn-tr>
</vn-thead> </vn-thead>
<vn-tbody> <vn-tbody>
<vn-tr ng-repeat="saleClaimed in $ctrl.salesClaimed" vn-repeat-last on-last="$ctrl.calculateTotals()"> <vn-tr ng-repeat="saleClaimed in $ctrl.salesClaimed" vn-repeat-last>
<vn-td number> <vn-td number>
<span <span
ng-click="$ctrl.showDescriptor($event, saleClaimed.sale.itemFk)" ng-click="$ctrl.showDescriptor($event, saleClaimed.sale.itemFk)"
@ -66,7 +66,7 @@
margin-medium-v margin-medium-v
vn-tooltip="Remove sale" vn-tooltip="Remove sale"
icon="remove_circle_outline" icon="remove_circle_outline"
ng-click="$ctrl.deleteClaimedSale(saleClaimed.id)" ng-click="$ctrl.deleteClaimedSale($index)"
tabindex="-1"> tabindex="-1">
</vn-icon-button> </vn-icon-button>
</vn-td> </vn-td>
@ -100,8 +100,8 @@
</vn-tr> </vn-tr>
</vn-thead> </vn-thead>
<vn-tbody> <vn-tbody>
<vn-tr ng-repeat="sale in $ctrl.salesToClaim" class="clickable" ng-click="$ctrl.addClaimedSale(sale.saleFk)"> <vn-tr ng-repeat="sale in $ctrl.salesToClaim" class="clickable" ng-click="$ctrl.addClaimedSale($index)">
<vn-td number>{{sale.saleFk}}</vn-td> <vn-td number>{{sale.saleFk}} {{$index}}</vn-td>
<vn-td number>{{sale.landed | dateTime: 'dd/MM/yyyy'}}</vn-td> <vn-td number>{{sale.landed | dateTime: 'dd/MM/yyyy'}}</vn-td>
<vn-td number>{{sale.quantity}}</vn-td> <vn-td number>{{sale.quantity}}</vn-td>
<vn-td number>{{sale.concept}}</vn-td> <vn-td number>{{sale.concept}}</vn-td>
@ -112,7 +112,7 @@
</vn-td> </vn-td>
</vn-tr> </vn-tr>
</vn-tbody> </vn-tbody>
<vn-empty-rows ng-if="model.data.length === 0" translate> <vn-empty-rows ng-if="$ctrl.salesToClaim.length === 0" translate>
No results No results
</vn-empty-rows> </vn-empty-rows>
</vn-table> </vn-table>

View File

@ -38,22 +38,24 @@ class Controller {
}); });
} }
addClaimedSale(saleFk) { addClaimedSale(index) {
let saleToAdd = {saleFk: saleFk, claimFk: this.claim.id, quantity: 0}; let sale = this.salesToClaim[index];
let saleToAdd = {saleFk: sale.saleFk, claimFk: this.claim.id, quantity: 0};
let query = `claim/api/ClaimBeginnings/`; let query = `claim/api/ClaimBeginnings/`;
this.$http.post(query, saleToAdd).then(() => { this.$http.post(query, saleToAdd).then(() => {
this.$.model.refresh();
this.$.addSales.hide(); this.$.addSales.hide();
this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}); });
} }
deleteClaimedSale(id) { deleteClaimedSale(index) {
let json = encodeURIComponent(JSON.stringify(id)); let sale = this.salesClaimed[index];
let query = `claim/api/ClaimBeginnings/${json}`; let query = `claim/api/ClaimBeginnings/${sale.id}`;
this.$http.delete(query).then(() => { this.$http.delete(query).then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.model.remove(index);
this.calculateTotals();
}); });
} }
@ -61,18 +63,19 @@ class Controller {
let params = {id: id, quantity: claimedQuantity}; let params = {id: id, quantity: claimedQuantity};
let query = `claim/api/ClaimBeginnings/`; let query = `claim/api/ClaimBeginnings/`;
this.$http.patch(query, params).then(() => { this.$http.patch(query, params).then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.calculateTotals();
}); });
} }
calculateTotals() { calculateTotals() {
let salesClaimed = this.$.model.data;
if (!salesClaimed) return;
this.paidTotal = 0; this.paidTotal = 0;
this.salesClaimed.forEach(sale => {
this.paidTotal += (sale.sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.sale.quantity * sale.sale.price)) / 100);
});
this.claimedTotal = 0; this.claimedTotal = 0;
this.salesClaimed.forEach(sale => { salesClaimed.forEach(sale => {
this.paidTotal += (sale.sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.sale.quantity * sale.sale.price)) / 100);
this.claimedTotal += (sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.quantity * sale.sale.price)) / 100); this.claimedTotal += (sale.quantity * sale.sale.price) - ((sale.sale.discount * (sale.quantity * sale.sale.price)) / 100);
}); });
} }

View File

@ -1,4 +1,5 @@
import './index.js'; import './index.js';
import {crudModel} from '../../../helpers/crudModelHelper';
describe('claim', () => { describe('claim', () => {
describe('Component vnClaimDetail', () => { describe('Component vnClaimDetail', () => {
@ -20,8 +21,10 @@ describe('claim', () => {
$state.params.id = 1; $state.params.id = 1;
controller = $componentController('vnClaimDetail', {$state: $state}); controller = $componentController('vnClaimDetail', {$state: $state});
controller.salesToClaim = [{saleFk: 1}, {saleFk: 2}];
controller.salesClaimed = [{id: 1}];
controller.claim = {ticketFk: 1}; controller.claim = {ticketFk: 1};
controller.$.model = {refresh: () => {}}; controller.$.model = crudModel;
controller.$.addSales = { controller.$.addSales = {
hide: () => {}, hide: () => {},
show: () => {} show: () => {}
@ -50,7 +53,7 @@ describe('claim', () => {
}); });
}); });
describe('addClaimedSale(saleFk)', () => { describe('addClaimedSale(index)', () => {
it('should make a post and call refresh, hide and showSuccess', () => { it('should make a post and call refresh, hide and showSuccess', () => {
spyOn(controller.$.model, 'refresh'); spyOn(controller.$.model, 'refresh');
spyOn(controller.$.addSales, 'hide'); spyOn(controller.$.addSales, 'hide');
@ -65,28 +68,26 @@ describe('claim', () => {
}); });
}); });
describe('deleteClaimedSale(id)', () => { describe('deleteClaimedSale(index)', () => {
it('should make a delete and call refresh and showSuccess', () => { it('should make a delete and call refresh and showSuccess', () => {
spyOn(controller.$.model, 'refresh'); spyOn(controller.$.model, 'remove');
spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.vnApp, 'showSuccess');
$httpBackend.expectDELETE(`claim/api/ClaimBeginnings/1`).respond({}); $httpBackend.expectDELETE(`claim/api/ClaimBeginnings/1`).respond({});
controller.deleteClaimedSale(1); controller.deleteClaimedSale(0);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$.model.refresh).toHaveBeenCalledWith(); expect(controller.$.model.remove).toHaveBeenCalledWith(0);
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
}); });
}); });
describe('setClaimedQuantity(id, claimedQuantity)', () => { describe('setClaimedQuantity(id, claimedQuantity)', () => {
it('should make a patch and call refresh and showSuccess', () => { it('should make a patch and call refresh and showSuccess', () => {
spyOn(controller.$.model, 'refresh');
spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.vnApp, 'showSuccess');
$httpBackend.expectPATCH(`claim/api/ClaimBeginnings/`).respond({}); $httpBackend.expectPATCH(`claim/api/ClaimBeginnings/`).respond({});
controller.setClaimedQuantity(1, 1); controller.setClaimedQuantity(1, 1);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$.model.refresh).toHaveBeenCalledWith();
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
}); });
}); });