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

103 lines
2.9 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import UserError from 'core/lib/user-error';
class Controller extends Section {
$onInit() {
this.services = [];
this.getDefaultTaxClass();
this.isDataSaved = false;
}
getDefaultTaxClass() {
let filter = {
where: {code: 'G'}
};
this.$http.get('TaxClasses/findOne', {filter})
.then(res => this.defaultTaxClass = res.data);
}
add() {
this.$.model.insert({
taxClassFk: this.defaultTaxClass.id,
quantity: 1,
ticketFk: this.$params.id
});
}
onNewServiceTypeClick(service, event) {
event.preventDefault();
this.$.newServiceType = {};
this.$.newServiceTypeDialog.show(service);
}
onNewServiceTypeAccept(service) {
if (!this.$.newServiceType.name)
throw new UserError(`Name can't be empty`);
return this.$http.post(`TicketServiceTypes`, this.$.newServiceType)
.then(res => {
this.$.typesModel.refresh();
return res;
})
.then(res => service.ticketServiceTypeFk = res.data.id);
}
onSubmit() {
this.$.watcher.check();
this.$.model.save()
.then(() => this.$.model.refresh())
.then(() => this.$.watcher.notifySaved())
.then(() => {
this.isDataSaved = true;
});
}
selectedServices() {
if (!this.services) return;
return this.services.filter(service => {
return service.checked;
});
}
selectedValidServices() {
if (!this.services) return;
const selectedServices = this.selectedServices();
return selectedServices.filter(service => {
return service.id != undefined;
});
}
hasSelectedServices() {
const selected = this.selectedServices() || [];
return selected.length > 0;
}
createRefund() {
this.$.model.save()
.then(() => {
const services = this.selectedValidServices();
if (!services) return;
const servicesIds = services.map(service => service.id);
const params = {servicesIds: servicesIds};
const query = 'Sales/refund';
this.$http.post(query, params).then(res => {
const refundTicket = res.data;
this.vnApp.showSuccess(this.$t('The following refund ticket have been created', {
ticketId: refundTicket.id
}));
this.$state.go('ticket.card.sale', {id: refundTicket.id});
});
});
}
}
ngModule.vnComponent('vnTicketService', {
template: require('./index.html'),
controller: Controller
});