salix/modules/item/front/diary/index.js

169 lines
4.5 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
get item() {
return this._item;
}
set item(value) {
this._item = value;
this.filter = {
where: {itemFk: this.$params.id}
};
this.$.$applyAsync(() => {
if (this.$params.warehouseFk)
this.warehouseFk = this.$params.warehouseFk;
else if (value)
this.warehouseFk = value.itemType.warehouseFk;
if (this.$params.ticketFk)
this.ticketFk = this.$params.ticketFk;
});
}
set warehouseFk(value) {
if (value && value != this._warehouseFk) {
this._warehouseFk = value;
this.$state.go(this.$state.current.name, {
warehouseFk: value
});
this.filter.where.warehouseFk = value;
this.$.model.refresh();
}
}
get warehouseFk() {
return this._warehouseFk;
}
get freeLineIndex() {
let lines = this.$.model.data;
let minDate = new Date();
minDate.setHours(0, 0, 0, 0);
let maxDate = new Date();
maxDate.setHours(23, 59, 59, 59);
for (let i = 0; i < lines.length; i++) {
const dated = new Date(lines[i].date);
let isForFuture = dated > maxDate;
let isForToday = (dated >= minDate && dated <= maxDate);
if (isForFuture || isForToday)
return i;
}
}
get onPreparationLineIndex() {
let lines = this.$.model.data;
for (let i = this.freeLineIndex; i >= 0; i--) {
let line = lines[i];
let currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
let isPastDate = new Date(lines[i].date) < currentDate;
let isPicked = line.alertLevel == 1 && line.isPicked;
if ((isPicked) || line.alertLevel > 1 || isPastDate)
return i + 1;
}
}
get givenTicketIndex() {
let lines = this.$.model.data;
for (let i = lines.length - 1; i > 0; i--) {
let line = lines[i];
if (line.origin == this.ticketFk)
return i;
}
}
scrollToLine() {
let body = this.$window.document.body;
let selectedTicketLineIndex = this.givenTicketIndex;
let lineIndex = this.onPreparationLineIndex;
let lines = body.querySelector('vn-tbody').children;
if (lineIndex == undefined || !lines.length) return;
let onPreparationLine = lines[lineIndex];
let balance = onPreparationLine.uerySelector('.balanceSpan');
balance.classList.add('message');
balance.title = this.$translate.instant('Visble quantity');
let headerOffset = body.qurySelector('vn-topbar').getBoundingClientRect();
let headerHeight = headerOffset.height;
let offsetTop;
if (this.ticketFk) {
let selectedTicketLine = lines[selectedTicketLineIndex];
let id = selectedTicketLine.querySelector('[name=origin]');
id.classList.add('message');
offsetTop = selectedTicketLine.offsetTop - headerHeight;
} else
offsetTop = onPreparationLine.offsetTop - headerHeight;
this.$window.scrollTo(0, offsetTop);
this.ticketFk = null;
}
/**
* Compares a date with the current one
* @param {Object} date - Date to compare
* @return {Boolean} - Returns true if the two dates equals
*/
isToday(date) {
let today = new Date();
today.setHours(0, 0, 0, 0);
let comparedDate = new Date(date);
comparedDate.setHours(0, 0, 0, 0);
if (!(today - comparedDate))
return true;
}
showDescriptor(event, sale) {
if (!sale.isTicket) return;
this.$.descriptor.ticketFk = sale.origin;
this.$.descriptor.parent = event.target;
this.$.descriptor.show();
event.preventDefault();
}
showClientDescriptor(event, sale) {
if (!sale.isTicket) return;
this.$.clientDescriptor.clientFk = sale.clientFk;
this.$.clientDescriptor.parent = event.target;
this.$.clientDescriptor.show();
event.preventDefault();
}
onDescriptorLoad() {
this.$.popover.relocate();
}
}
ngModule.component('vnItemDiary', {
template: require('./index.html'),
controller: Controller,
bindings: {
item: '<'
}
});