From ca97d76adf8a8670d32756081d2325ffcbe8750f Mon Sep 17 00:00:00 2001 From: Joan Date: Thu, 6 Sep 2018 15:08:02 +0200 Subject: [PATCH] itemDiary warehouseFk url param #622 --- client/item/routes.json | 2 +- client/item/src/diary/index.html | 10 +++--- client/item/src/diary/index.js | 53 ++++++++++++++--------------- client/item/src/diary/index.spec.js | 24 +++++++++---- 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/client/item/routes.json b/client/item/routes.json index 21399f9b56..b66d9212ec 100644 --- a/client/item/routes.json +++ b/client/item/routes.json @@ -125,7 +125,7 @@ "item": "$ctrl.item" } }, { - "url" : "/diary?q", + "url" : "/diary?warehouseFk", "state": "item.card.diary", "component": "vn-item-diary", "description": "Diary", diff --git a/client/item/src/diary/index.html b/client/item/src/diary/index.html index 3669026780..63b811b267 100644 --- a/client/item/src/diary/index.html +++ b/client/item/src/diary/index.html @@ -2,8 +2,7 @@ vn-id="model" url="item/api/Items/getDiary" filter="::$ctrl.filter" - data="sales" - auto-load="false"> + data="sales"> @@ -15,9 +14,10 @@ url="/item/api/Warehouses" show-field="name" value-field="id" - initial-data="$ctrl.filter.where.warehouseFk" - field="$ctrl.filter.where.warehouseFk" - label="Select warehouse" on-change="$ctrl.onChange(value)"> + initial-data="$ctrl.warehouseFk" + field="$ctrl.warehouseFk" + label="Select warehouse" + on-change="$ctrl.onChange(value)"> diff --git a/client/item/src/diary/index.js b/client/item/src/diary/index.js index b8fd34aecc..826211c277 100644 --- a/client/item/src/diary/index.js +++ b/client/item/src/diary/index.js @@ -2,61 +2,58 @@ import ngModule from '../module'; import './style.scss'; class Controller { - constructor($scope, $http, $state, $window, $translate) { + constructor($scope, $http, $state, $window, $translate, $stateParams) { this.$scope = $scope; this.$http = $http; this.$state = $state; + this.$stateParams = $stateParams; this.$window = $window; this.$translate = $translate; } - +/* $postLink() { if (this.item) this.filterBuilder(); } - + */ set item(value) { this._item = value; - if (value && this.$scope.model) - this.filterBuilder(); + this.filter = { + where: {itemFk: this.$stateParams.id} + }; + + if (this.$stateParams.warehouseFk) + this.warehouseFk = this.$stateParams.warehouseFk; + else if (value) + this.warehouseFk = value.itemType.warehouseFk; } get item() { return this._item; } - onChange(value) { - if (!value) return; + set warehouseFk(value) { + this._warehouseFk = value; + + this.$state.go(this.$state.current.name, { + warehouseFk: value + }); this.filter.where.warehouseFk = value; - this.$scope.model.refresh(); } -/** - * Builds a filter with default values - * and aplies query params. - */ - filterBuilder() { - this.filter = { - where: { - itemFk: this.item.id, - warehouseFk: this.item.itemType.warehouseFk - } - - }; - let where = this.filter.where; - - if (this.$state.params.q) { - let queryFilter = JSON.parse(this.$state.params.q); - where.warehouseFk = queryFilter.warehouseFk; - } + get warehouseFk() { + return this._warehouseFk; } get freeLineIndex() { let lines = this.$scope.model.data; + let currentDate = new Date(); + currentDate.setHours(0, 0, 0); + for (let i = 0; i < lines.length; i++) { - let isFutureDate = new Date(lines[i].date) > new Date(); + let isFutureDate = new Date(lines[i].date) >= currentDate; if (isFutureDate) return i; @@ -140,7 +137,7 @@ class Controller { } } -Controller.$inject = ['$scope', '$http', '$state', '$window', '$translate']; +Controller.$inject = ['$scope', '$http', '$state', '$window', '$translate', '$stateParams']; ngModule.component('vnItemDiary', { template: require('./index.html'), diff --git a/client/item/src/diary/index.spec.js b/client/item/src/diary/index.spec.js index c5c909ed50..4e17653ad3 100644 --- a/client/item/src/diary/index.spec.js +++ b/client/item/src/diary/index.spec.js @@ -3,6 +3,7 @@ import './index.js'; describe('Item', () => { describe('Component vnItemDiary', () => { let $componentController; + let $stateParams; let $scope; let controller; let $httpBackend; @@ -11,12 +12,14 @@ describe('Item', () => { angular.mock.module('item'); }); - beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { + beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$stateParams_, _$httpBackend_) => { $componentController = _$componentController_; + $stateParams = _$stateParams_; + $stateParams.id = 1; $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = $rootScope.$new(); - controller = $componentController('vnItemDiary', {$scope: $scope}); + controller = $componentController('vnItemDiary', {$scope: $scope, $stateParams}); controller.$scope.model = {}; })); @@ -71,12 +74,19 @@ describe('Item', () => { }); describe('set item()', () => { - it(`should call filterBuilder()`, () => { - spyOn(controller, 'filterBuilder'); - controller.item = {id: 1}; + it(`should set warehouseFk property based on itemType warehouseFk`, () => { + controller.item = {id: 1, itemType: {warehouseFk: 1}}; - expect(controller.filterBuilder).toHaveBeenCalledWith(); - expect(controller.item).toEqual({id: 1}); + expect(controller.warehouseFk).toEqual(1); + expect(controller.item.id).toEqual(1); + }); + + it(`should set warehouseFk property based on url query warehouseFk`, () => { + controller.$stateParams.warehouseFk = 4; + controller.item = {id: 1, itemType: {warehouseFk: 1}}; + + expect(controller.warehouseFk).toEqual(4); + expect(controller.item.id).toEqual(1); }); }); });