salix/modules/ticket/front/index/index.js

156 lines
4.4 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import UserError from 'core/lib/user-error';
import './style.scss';
export default class Controller extends Section {
openBalanceDialog() {
const checkedTickets = this.checked;
const description = [];
this.$.balanceCreateDialog.amountPaid = 0;
const firstTicketClientId = checkedTickets[0].clientFk;
const isSameClient = checkedTickets.every(ticket => {
return ticket.clientFk == firstTicketClientId;
});
if (!isSameClient)
throw new UserError('You cannot make a payment on account from multiple clients');
for (let ticket of checkedTickets) {
this.$.balanceCreateDialog.amountPaid += ticket.total;
this.$.balanceCreateDialog.clientFk = ticket.clientFk;
description.push(`${ticket.id}`);
}
this.$.balanceCreateDialog.description = 'Albaran: ';
this.$.balanceCreateDialog.description += description.join(', ');
this.$.balanceCreateDialog.show();
}
get checked() {
const tickets = this.$.model.data || [];
const checkedLines = [];
for (let ticket of tickets) {
if (ticket.checked)
checkedLines.push(ticket);
}
return checkedLines;
}
get totalChecked() {
return this.checked.length;
}
onMoreOpen() {
let options = this.moreOptions.filter(o => o.always || this.isChecked);
this.$.moreButton.data = options;
}
onMoreChange(callback) {
callback.call(this);
}
compareDate(date) {
let today = new Date();
today.setHours(0, 0, 0, 0);
let timeTicket = new Date(date);
timeTicket.setHours(0, 0, 0, 0);
let comparation = today - timeTicket;
if (comparation == 0)
return 'warning';
if (comparation < 0)
return 'success';
}
stateColor(ticket) {
if (ticket.alertLevelCode === 'OK')
return 'success';
else if (ticket.alertLevelCode === 'FREE')
return 'notice';
else if (ticket.alertLevel === 1)
return 'warning';
else if (ticket.alertLevel === 0)
return 'alert';
}
totalPriceColor(ticket) {
const total = parseInt(ticket.total);
if (total > 0 && total < 50)
return 'warning';
}
goToLines(ticketFk) {
let url = this.$state.href('ticket.card.sale', {id: ticketFk}, {absolute: true});
window.open(url, '_blank');
}
preview(ticket) {
this.selectedTicket = ticket;
this.$.summary.show();
}
filterBySelection(target) {
// const target = $event.target;
const model = this.$.model;
console.log(target);
const table = target.closest('.vn-table');
const headerCols = table.querySelectorAll('vn-thead vn-th');
const row = target.closest('.vn-tr');
const col = target.closest('vn-td');
const rowIndex = row.getAttribute('data-index');
const columns = Array.from(row.querySelectorAll('vn-td'));
const fieldIndex = columns.findIndex(column => column == col);
const fieldName = headerCols[fieldIndex].getAttribute('field');
const rowData = model.data[rowIndex];
const filter = {where: {}};
filter['where'][fieldName] = rowData[fieldName];
console.log(filter);
model.addFilter(filter);
}
excludeSelection($event, target) {
const model = this.$.model;
const table = target.closest('.vn-table');
const headerCols = table.querySelectorAll('vn-thead vn-th');
const row = target.closest('.vn-tr');
const col = target.closest('vn-td');
const rowIndex = row.getAttribute('data-index');
const columns = Array.from(row.querySelectorAll('vn-td'));
const fieldIndex = columns.findIndex(column => column == col);
const fieldName = headerCols[fieldIndex].getAttribute('field');
const rowData = model.data[rowIndex];
const filter = {where: {}};
filter['where'][fieldName] = {neq: rowData[fieldName]};
console.log(filter);
model.addFilter(filter);
}
removeFilter() {
this.$.model.removeFilter();
}
}
ngModule.component('vnTicketIndex', {
template: require('./index.html'),
controller: Controller
});