diff --git a/modules/item/back/models/item.json b/modules/item/back/models/item.json index dbaa3a4090..5d2e47d2a3 100644 --- a/modules/item/back/models/item.json +++ b/modules/item/back/models/item.json @@ -175,5 +175,12 @@ "model": "ItemNiche", "foreignKey": "itemFk" } + }, + "scope": { + "where": { + "name": { + "neq": "" + } + } } } \ No newline at end of file diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 41ef74c38d..b050d3579f 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -105,20 +105,22 @@ on-error-src/> - - {{sale.itemFk | zeroFill:6}} + {{sale.itemFk}} + search-function="$ctrl.itemSearchFunc($search)" + on-change="$ctrl.onChangeQuantity(sale)" + order="id DESC" + tabindex="1"> {{id}} - {{name}} @@ -137,7 +139,8 @@ + on-change="$ctrl.onChangeQuantity(sale)" + tabindex="2"> @@ -150,7 +153,7 @@ - @@ -167,7 +170,8 @@ + ng-click="$ctrl.showEditDiscountPopover($event, sale)" + ng-if="sale.id"> {{(sale.discount / 100) | percentage}} diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 42ff5f116a..c5e947d237 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -464,10 +464,12 @@ class Controller { * Updates the sale quantity for existing instance */ onChangeQuantity(sale) { + if (!sale.quantity) return; + if (!sale.id) - this.addSale(sale); - else - this.updateQuantity(sale); + return this.addSale(sale); + + this.updateQuantity(sale); } /* @@ -560,6 +562,12 @@ class Controller { this.$scope.model.refresh(); }); } + + itemSearchFunc($search) { + return /^\d+$/.test($search) + ? {id: $search} + : {name: {like: '%' + $search + '%'}}; + } } Controller.$inject = ['$scope', '$state', '$http', 'vnApp', '$translate']; diff --git a/modules/ticket/front/sale/specs/index.spec.js b/modules/ticket/front/sale/specs/index.spec.js index 2e7979faa0..3b0f04a82c 100644 --- a/modules/ticket/front/sale/specs/index.spec.js +++ b/modules/ticket/front/sale/specs/index.spec.js @@ -207,6 +207,39 @@ describe('Ticket', () => { }); }); + describe('onChangeQuantity()', () => { + it('should not call addSale() or updateQuantity() methods', () => { + jest.spyOn(controller, 'addSale'); + jest.spyOn(controller, 'updateQuantity'); + + const sale = {itemFk: 4}; + controller.onChangeQuantity(sale); + + expect(controller.addSale).not.toHaveBeenCalled(); + expect(controller.updateQuantity).not.toHaveBeenCalled(); + }); + + it('should call addSale() method', () => { + jest.spyOn(controller, 'addSale'); + + const sale = {itemFk: 4, quantity: 5}; + controller.onChangeQuantity(sale); + + expect(controller.addSale).toHaveBeenCalledWith(sale); + }); + + it('should call updateQuantity() method', () => { + jest.spyOn(controller, 'updateQuantity'); + jest.spyOn(controller, 'addSale'); + + const sale = {id: 1, itemFk: 4, quantity: 5}; + controller.onChangeQuantity(sale); + + expect(controller.addSale).not.toHaveBeenCalled(); + expect(controller.updateQuantity).toHaveBeenCalledWith(sale); + }); + }); + describe('updateQuantity()', () => { it('should make a POST query saving sale quantity', () => { jest.spyOn(controller.$scope.watcher, 'updateOriginalData');