salix/modules/claim/front/action/index.js

186 lines
5.7 KiB
JavaScript

import ngModule from '../module';
import Section from 'salix/components/section';
import './style.scss';
export default class Controller extends Section {
constructor($element, $) {
super($element, $);
this.filter = {
include: [
{relation: 'sale',
scope: {
fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'],
include: {
relation: 'ticket'
}
}
},
{relation: 'claimBeggining'},
{relation: 'claimDestination'}
]
};
this.getResolvedState();
this.maxResponsibility = 5;
}
getResolvedState() {
const query = `ClaimStates/findOne`;
const params = {
filter: {
where: {
code: 'resolved'
}
}
};
this.$http.get(query, params).then(res =>
this.resolvedStateId = res.data.id
);
}
importToNewRefundTicket() {
let query = `ClaimBeginnings/${this.$params.id}/importToNewRefundTicket`;
return this.$http.post(query).then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
showTicketDescriptor(event, ticketFk) {
this.$.ticketDescriptor.ticketFk = ticketFk;
this.$.ticketDescriptor.parent = event.target;
this.$.ticketDescriptor.show();
event.preventDefault();
}
focusLastInput() {
let inputs = document.querySelectorAll('#claimDestinationFk');
inputs[inputs.length - 1].querySelector('input').focus();
this.calculateTotals();
}
calculateTotals() {
this.claimedTotal = 0;
this.salesClaimed.forEach(sale => {
const price = sale.sale.quantity * sale.sale.price;
const discount = (sale.sale.discount * (sale.sale.quantity * sale.sale.price)) / 100;
this.claimedTotal += price - discount;
});
}
showLastTickets(event) {
let pastWeek = new Date();
pastWeek.setDate(-7);
let filter = {
include: [
{relation: 'agencyMode', fields: ['name']},
{relation: 'warehouse', fields: ['name']}
],
where: {
created: {gt: pastWeek},
clientFk: this.claim.clientFk
}
};
this.$.lastTicketsModel.filter = filter;
this.$.lastTicketsModel.refresh();
this.$.lastTicketsPopover.parent = event.target;
this.$.lastTicketsPopover.show();
}
importTicketLines(ticketFk) {
let data = {claimFk: this.$params.id, ticketFk: ticketFk};
let query = `ClaimEnds/importTicketSales`;
this.$http.post(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$.lastTicketsPopover.hide();
this.$.model.refresh();
});
}
regularize() {
const query = `Claims/${this.$params.id}/regularizeClaim`;
return this.$http.post(query).then(() => {
if (this.claim.responsibility >= Math.ceil(this.maxResponsibility) / 2)
this.$.updateGreuge.show();
else
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.card.reload();
});
}
getGreugeTypeId() {
const params = {filter: {where: {code: 'freightPickUp'}}};
const query = `GreugeTypes/findOne`;
return this.$http.get(query, {params}).then(res => {
this.greugeTypeFreightId = res.data.id;
return res;
});
}
getGreugeConfig() {
const query = `GreugeConfigs/findOne`;
return this.$http.get(query).then(res => {
this.freightPickUpPrice = res.data.freightPickUpPrice;
return res;
});
}
onUpdateGreugeResponse(response) {
if (response == 'accept') {
const promises = [];
promises.push(this.getGreugeTypeId());
promises.push(this.getGreugeConfig());
return Promise.all(promises).then(() => {
const data = {
clientFk: this.claim.clientFk,
description: this.$translate.instant('ClaimGreugeDescription', {
claimId: this.claim.id
}).toUpperCase(),
amount: this.freightPickUpPrice,
greugeTypeFk: this.greugeTypeFreightId,
ticketFk: this.claim.ticketFk
};
return this.$http.post(`Greuges/`, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.vnApp.showMessage(this.$translate.instant('Greuge inserted'));
});
});
} else
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}
showDescriptor(event, itemFk) {
this.$.descriptor.itemFk = itemFk;
this.$.descriptor.parent = event.target;
this.$.descriptor.show();
}
save(data) {
const query = `Claims/${this.$params.id}/updateClaimAction`;
this.$http.patch(query, data).then(() => {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
});
}
onSave() {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}
}
ngModule.component('vnClaimAction', {
template: require('./index.html'),
controller: Controller,
bindings: {
claim: '<'
},
require: {
card: '^vnClaimCard'
}
});