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

286 lines
7.7 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
class Controller extends Section {
constructor($element, $, vnReport, vnEmail) {
super($element, $);
this.vnReport = vnReport;
this.vnEmail = vnEmail;
}
get ticketId() {
return this._ticketId;
}
set ticketId(value) {
this._ticketId = value;
this.id = value;
}
get id() {
return this._id;
}
set id(value) {
this._id = value;
if (!value) return;
this.loadData().then(() => {
if (this.$params.sendSMS) {
this.showSMSDialog({
message: this.$params.message
});
}
});
}
get isInvoiced() {
return this.ticket.refFk !== null;
}
get isTicketModule() {
return this.$state.getCurrentPath()[1].state.name === 'ticket';
}
get hasInvoicing() {
return this.aclService.hasAny(['invoicing']);
}
loadData() {
const filter = {
include: [{
relation: 'address',
},
{
relation: 'client',
scope: {
fields: [
'salesPersonFk',
'name',
'isActive',
'isFreezed',
'isTaxDataChecked',
'credit',
'email',
'phone',
'mobile'
],
include: {
relation: 'salesPersonUser',
scope: {
fields: ['id', 'name']
}
}
}
},
{
relation: 'invoiceOut'
}]
};
return this.$http.get(`Tickets/${this.ticketId}`, {filter})
.then(res => this.ticket = res.data)
.then(() => {
this.isTicketEditable();
this.hasDocuware();
});
}
reload() {
return this.loadData().then(() => {
if (this.parentReload)
this.parentReload();
});
}
isTicketEditable() {
if (!this.ticket) return;
this.$http.get(`Tickets/${this.id}/isEditable`).then(res => {
this.isEditable = res.data;
});
}
addTurn(day) {
const 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!'));
});
}
showPdfDeliveryNote(type) {
this.vnReport.show('delivery-note', {
recipientId: this.ticket.client.id,
ticketId: this.id,
type: type
});
}
hasDocuware() {
const params = {
fileCabinet: 'deliveryClient',
dialog: 'findTicket'
};
this.$http.post(`Docuwares/${this.id}/checkFile`, params)
.then(res => this.hasDocuwareFile = res.data);
}
showCsvDeliveryNote() {
this.vnReport.showCsv('delivery-note', {
recipientId: this.ticket.client.id,
ticketId: this.id,
});
}
sendPdfDeliveryNote($data) {
return this.vnEmail.send('delivery-note', {
recipientId: this.ticket.client.id,
recipient: $data.email,
ticketId: this.id
});
}
sendCsvDeliveryNote($data) {
return this.vnEmail.sendCsv('delivery-note', {
recipientId: this.ticket.client.id,
recipient: $data.email,
ticketId: this.id
});
}
deleteTicket() {
return this.$http.post(`Tickets/${this.id}/setDeleted`)
.then(() => this.reload())
.then(() => {
const isInsideTicket = this.$state.current.name.startsWith('ticket');
if (isInsideTicket)
this.$state.go('ticket.index');
this.vnApp.showSuccess(this.$t('Ticket deleted. You can undo this action within the first hour'));
});
}
get canRestoreTicket() {
const isDeleted = this.ticket.isDeleted;
const now = new Date();
const maxDate = new Date(this.ticket.updated);
maxDate.setHours(maxDate.getHours() + 1);
return isDeleted && (now <= maxDate);
}
restoreTicket() {
return this.$http.post(`Tickets/${this.id}/restore`)
.then(() => this.reload())
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
showChangeShipped() {
this.newShipped = this.ticket.shipped;
this.$.changeShippedDialog.show();
}
changeShipped() {
const data = {shipped: this.newShipped};
return this.$http.post(`Tickets/${this.id}/updateEditableTicket`, data)
.then(() => this.reload())
.then(() => this.vnApp.showSuccess(this.$t('Shipped hour updated')));
}
sendImportSms() {
const params = {
ticketId: this.id,
created: this.ticket.updated
};
this.showSMSDialog({
message: this.$t('Minimum is needed', params)
});
}
sendPaymentSms() {
this.showSMSDialog({
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({
ticketId: this.id,
destinationFk: this.ticket.clientFk,
destination: phone
}, params);
this.$.sms.open();
}
makeInvoice() {
const params = {ticketsIds: [this.id]};
return this.$http.post(`Tickets/makeInvoice`, params)
.then(() => this.reload())
.then(() => this.vnApp.showSuccess(this.$t('Ticket invoiced')));
}
createPdfInvoice() {
const invoiceId = this.ticket.invoiceOut.id;
return this.$http.post(`InvoiceOuts/${invoiceId}/createPdf`)
.then(() => this.reload())
.then(() => {
const snackbarMessage = this.$t(
`The invoice PDF document has been regenerated`);
this.vnApp.showSuccess(snackbarMessage);
});
}
recalculateComponents() {
return this.$http.post(`Tickets/${this.id}/recalculateComponents`)
.then(() => this.reload())
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
async refund() {
const filter = {
where: {ticketFk: this.id}
};
const sales = await this.$http.get('Sales', {filter});
this.sales = sales.data;
const ticketServices = await this.$http.get('TicketServices', {filter});
this.services = ticketServices.data;
const params = {
sales: this.sales,
services: this.services
};
const query = `Sales/refund`;
return this.$http.post(query, params).then(res => {
this.$state.go('ticket.card.sale', {id: res.data});
});
}
}
Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];
ngModule.vnComponent('vnTicketDescriptorMenu', {
template: require('./index.html'),
controller: Controller,
bindings: {
ticketId: '<',
parentReload: '&'
}
});