From e60d485111c3e25958ebe103f29629e81992a642 Mon Sep 17 00:00:00 2001 From: Joan Date: Fri, 23 Nov 2018 14:30:01 +0100 Subject: [PATCH] fixed infinite claim detail save #866 --- client/claim/src/action/index.spec.js | 4 ++-- client/claim/src/detail/index.html | 12 ++++++------ client/claim/src/detail/index.js | 27 +++++++++++++++------------ client/claim/src/detail/index.spec.js | 17 +++++++++-------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/client/claim/src/action/index.spec.js b/client/claim/src/action/index.spec.js index f67d0ecc4..70684ac49 100644 --- a/client/claim/src/action/index.spec.js +++ b/client/claim/src/action/index.spec.js @@ -110,7 +110,7 @@ describe('claim', () => { }); 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.vnApp, 'showSuccess'); $httpBackend.expect('POST', `claim/api/ClaimBeginnings/1/importToNewRefundTicket`).respond({}); @@ -134,7 +134,7 @@ describe('claim', () => { }); 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.vnApp, 'showSuccess'); spyOn(controller.$.lastTicketsPopover, 'hide'); diff --git a/client/claim/src/detail/index.html b/client/claim/src/detail/index.html index f9b77c470..da53aad34 100644 --- a/client/claim/src/detail/index.html +++ b/client/claim/src/detail/index.html @@ -2,7 +2,7 @@ vn-id="model" url="claim/api/ClaimBeginnings" filter="$ctrl.filter" - data="$ctrl.salesClaimed"> + data="$ctrl.salesClaimed" on-data-change="$ctrl.calculateTotals()"> @@ -32,7 +32,7 @@ - + @@ -100,8 +100,8 @@ - - {{sale.saleFk}} + + {{sale.saleFk}} {{$index}} {{sale.landed | dateTime: 'dd/MM/yyyy'}} {{sale.quantity}} {{sale.concept}} @@ -112,7 +112,7 @@ - + No results diff --git a/client/claim/src/detail/index.js b/client/claim/src/detail/index.js index 3fdc8ef20..4da589712 100644 --- a/client/claim/src/detail/index.js +++ b/client/claim/src/detail/index.js @@ -38,22 +38,24 @@ class Controller { }); } - addClaimedSale(saleFk) { - let saleToAdd = {saleFk: saleFk, claimFk: this.claim.id, quantity: 0}; + addClaimedSale(index) { + let sale = this.salesToClaim[index]; + let saleToAdd = {saleFk: sale.saleFk, claimFk: this.claim.id, quantity: 0}; let query = `claim/api/ClaimBeginnings/`; this.$http.post(query, saleToAdd).then(() => { - this.$.model.refresh(); this.$.addSales.hide(); + this.$.model.refresh(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); }); } - deleteClaimedSale(id) { - let json = encodeURIComponent(JSON.stringify(id)); - let query = `claim/api/ClaimBeginnings/${json}`; + deleteClaimedSale(index) { + let sale = this.salesClaimed[index]; + let query = `claim/api/ClaimBeginnings/${sale.id}`; this.$http.delete(query).then(() => { - this.$.model.refresh(); 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 query = `claim/api/ClaimBeginnings/`; this.$http.patch(query, params).then(() => { - this.$.model.refresh(); this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.calculateTotals(); }); } calculateTotals() { + let salesClaimed = this.$.model.data; + if (!salesClaimed) return; + 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.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); }); } diff --git a/client/claim/src/detail/index.spec.js b/client/claim/src/detail/index.spec.js index b75063d2c..b492ba29a 100644 --- a/client/claim/src/detail/index.spec.js +++ b/client/claim/src/detail/index.spec.js @@ -1,4 +1,5 @@ import './index.js'; +import {crudModel} from '../../../helpers/crudModelHelper'; describe('claim', () => { describe('Component vnClaimDetail', () => { @@ -20,8 +21,10 @@ describe('claim', () => { $state.params.id = 1; controller = $componentController('vnClaimDetail', {$state: $state}); + controller.salesToClaim = [{saleFk: 1}, {saleFk: 2}]; + controller.salesClaimed = [{id: 1}]; controller.claim = {ticketFk: 1}; - controller.$.model = {refresh: () => {}}; + controller.$.model = crudModel; controller.$.addSales = { hide: () => {}, show: () => {} @@ -50,7 +53,7 @@ describe('claim', () => { }); }); - describe('addClaimedSale(saleFk)', () => { + describe('addClaimedSale(index)', () => { it('should make a post and call refresh, hide and showSuccess', () => { spyOn(controller.$.model, 'refresh'); 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', () => { - spyOn(controller.$.model, 'refresh'); + spyOn(controller.$.model, 'remove'); spyOn(controller.vnApp, 'showSuccess'); $httpBackend.expectDELETE(`claim/api/ClaimBeginnings/1`).respond({}); - controller.deleteClaimedSale(1); + controller.deleteClaimedSale(0); $httpBackend.flush(); - expect(controller.$.model.refresh).toHaveBeenCalledWith(); + expect(controller.$.model.remove).toHaveBeenCalledWith(0); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); }); }); describe('setClaimedQuantity(id, claimedQuantity)', () => { it('should make a patch and call refresh and showSuccess', () => { - spyOn(controller.$.model, 'refresh'); spyOn(controller.vnApp, 'showSuccess'); $httpBackend.expectPATCH(`claim/api/ClaimBeginnings/`).respond({}); controller.setClaimedQuantity(1, 1); $httpBackend.flush(); - expect(controller.$.model.refresh).toHaveBeenCalledWith(); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); }); });