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

245 lines
6.7 KiB
JavaScript

import ngModule from '../module';
import Descriptor from 'salix/components/descriptor';
class Controller extends Descriptor {
get ticket() {
return this.entity;
}
set ticket(value) {
this.entity = value;
}
get entity() {
return super.entity;
}
set entity(value) {
super.entity = value;
this.canStowaway();
this.isTicketEditable();
if (value && this.$params.sendSMS)
this.showSMSDialog();
}
get isInvoiced() {
return this.ticket.refFk !== null;
}
get isTicketModule() {
return this.$state.getCurrentPath()[1].state.name === 'ticket';
}
get shouldShowDeleteStowaway() {
if (!this.ticket || !this.isTicketModule)
return false;
return this.ticket.stowaway || this.ticket.ship;
}
get filter() {
if (this.ticket)
return JSON.stringify({clientFk: this.ticket.clientFk});
return null;
}
isTicketEditable() {
if (!this.ticket) return;
this.$http.get(`Tickets/${this.id}/isEditable`).then(res => {
this.isEditable = res.data;
});
}
showChangeShipped() {
this.newShipped = this.ticket.shipped;
this.$.changeShippedDialog.show();
}
changeShipped() {
let data = {
shipped: this.newShipped
};
return this.$http.post(`Tickets/${this.id}/updateEditableTicket`, data)
.then(() => this.cardReload())
.then(() => this.vnApp.showSuccess(this.$t('Shipped hour updated')));
}
goToTicket(ticketId) {
this.$state.go('ticket.card.sale', {id: ticketId}, {absolute: true});
}
addTurn(day) {
let params = {
ticketFk: this.id,
weekDay: day,
agencyModeFk: this.ticket.agencyModeFk
};
return this.$http.patch(`TicketWeeklies`, params)
.then(() => {
this.$.addTurn.hide();
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
deleteTicket() {
return this.$http.post(`Tickets/${this.id}/setDeleted`)
.then(() => {
this.$state.go('ticket.index');
this.vnApp.showSuccess(this.$t('Ticket deleted'));
});
}
canStowaway() {
this.canShowStowaway = false;
if (!this.isTicketModule || !this.ticket) return;
this.$http.get(`Tickets/${this.id}/canHaveStowaway`)
.then(res => this.canShowStowaway = !!res.data);
}
deleteStowaway() {
return this.$http.post(`Tickets/${this.id}/deleteStowaway`)
.then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.cardReload();
});
}
showDeliveryNote() {
this.vnReport.show('delivery-note', {
recipientId: this.ticket.client.id,
ticketId: this.id,
});
}
sendDeliveryNote() {
return this.vnEmail.send('delivery-note', {
recipientId: this.ticket.client.id,
recipient: this.ticket.client.email,
ticketId: this.id
});
}
sendImportSms() {
const params = {
ticketId: this.id,
created: this.ticket.created
};
this.showSMSDialog({
message: this.$params.message || this.$t('Minimum is needed', params)
});
}
sendPaymentSms() {
this.showSMSDialog({
message: this.$params.message || this.$t('Make a payment')
});
}
showSMSDialog(params) {
const address = this.ticket.address;
const client = this.ticket.client;
const phone = this.$params.phone
|| address.mobile
|| address.phone
|| client.mobile
|| client.phone;
this.newSMS = Object.assign({
destinationFk: this.ticket.clientFk,
destination: phone
}, params);
this.$.sms.open();
}
makeInvoice() {
return this.$http.post(`Tickets/${this.id}/makeInvoice`)
.then(() => {
this.vnApp.showSuccess(this.$t('Ticket invoiced'));
this.$state.reload();
});
}
regenerateInvoice() {
const invoiceId = this.ticket.invoiceOut.id;
return this.$http.post(`InvoiceOuts/${invoiceId}/regenerate`)
.then(() => {
const snackbarMessage = this.$t(
`Invoice sent for a regeneration, will be available in a few minutes`);
this.vnApp.showSuccess(snackbarMessage);
});
}
recalculateComponents() {
return this.$http.post(`Tickets/${this.id}/recalculateComponents`)
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
cardReload() {
// Prevents error when not defined
}
loadData() {
const filter = {
include: [
{
relation: 'warehouse',
scope: {
fields: ['name']
}
}, {
relation: 'agencyMode',
scope: {
fields: ['name']
}
}, {
relation: 'client',
scope: {
fields: [
'salesPersonFk',
'name',
'isActive',
'isFreezed',
'isTaxDataChecked'
],
include: {
relation: 'salesPerson',
scope: {
fields: ['userFk'],
include: {
relation: 'user',
scope: {
fields: ['nickname']
}
}
}
}
}
}, {
relation: 'ticketState',
scope: {
fields: ['stateFk'],
include: {
relation: 'state',
fields: ['id', 'name'],
}
}
}
]
};
return this.getData(`Tickets/${this.id}`, {filter})
.then(res => this.entity = res.data);
}
}
ngModule.vnComponent('vnTicketDescriptor', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticket: '<',
cardReload: '&'
}
});