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 { constructor($element, $, vnReport) { super($element, $); this.vnReport = vnReport; } setDelivered() { const checkedTickets = this.checked; let ids = []; for (let ticket of checkedTickets) ids.push(ticket.id); this.$http.post('TicketTrackings/setDelivered', ids).then(res => { let state = res.data; for (let ticket of checkedTickets) { ticket.stateFk = state.id; ticket.state = state.name; ticket.alertLevel = state.alertLevel; ticket.alertLevelCode = state.code; } this.openDeliveryNotes(ids); }); } openDeliveryNotes(ids) { for (let id of ids) this.vnReport.show(`Tickets/${id}/delivery-note-pdf`); } 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.totalWithVat; 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; } get confirmationMessage() { if (!this.$.model) return 0; return this.$t(`Are you sure to invoice tickets`, { ticketsAmount: this.totalChecked }); } onMoreOpen() { let options = this.moreOptions.filter(o => o.always || this.isChecked); this.$.moreButton.data = options; } onMoreChange(callback) { callback.call(this); } compareDate(date) { let today = Date.vnNew(); 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.totalWithVat); if (total > 0 && total < 50) return 'warning'; } preview(ticket) { this.selectedTicket = ticket; this.$.summary.show(); } exprBuilder(param, value) { switch (param) { case 'stateFk': return {'ts.stateFk': value}; case 'salesPersonFk': return {'c.salesPersonFk': value}; case 'provinceFk': return {'a.provinceFk': value}; case 'hour': return {'z.hour': value}; case 'shipped': return {'t.shipped': { between: this.dateRange(value)} }; case 'id': case 'refFk': case 'zoneFk': case 'nickname': case 'agencyModeFk': case 'warehouseFk': return {[`t.${param}`]: value}; } } dateRange(value) { const minHour = new Date(value); minHour.setHours(0, 0, 0, 0); const maxHour = new Date(value); maxHour.setHours(23, 59, 59, 59); return [minHour, maxHour]; } clientParams() { if (this.$params.q) { const params = JSON.parse(this.$params.q); if (params.clientFk) return {clientFk: params.clientFk}; } return {}; } makeInvoice() { const ticketsIds = this.checked.map(ticket => ticket.id); return this.$http.post(`Tickets/makeInvoice`, {ticketsIds}) .then(() => this.$.model.refresh()) .then(() => this.vnApp.showSuccess(this.$t('Ticket invoiced'))); } } Controller.$inject = ['$element', '$scope', 'vnReport']; ngModule.vnComponent('vnTicketIndex', { template: require('./index.html'), controller: Controller });