itemDiary warehouseFk url param #622

This commit is contained in:
Joan Sanchez 2018-09-06 15:08:02 +02:00
parent 3de82bc8f8
commit ca97d76adf
4 changed files with 48 additions and 41 deletions

View File

@ -125,7 +125,7 @@
"item": "$ctrl.item"
}
}, {
"url" : "/diary?q",
"url" : "/diary?warehouseFk",
"state": "item.card.diary",
"component": "vn-item-diary",
"description": "Diary",

View File

@ -2,8 +2,7 @@
vn-id="model"
url="item/api/Items/getDiary"
filter="::$ctrl.filter"
data="sales"
auto-load="false">
data="sales">
</vn-crud-model>
<vn-vertical>
<vn-card pad-large>
@ -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)">
</vn-autocomplete>
</vn-horizontal>
<vn-table model="model">

View File

@ -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'),

View File

@ -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);
});
});
});