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

234 lines
6.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.newDestination;
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;
this.smartTableOptions = {
activeButtons: {
search: true
},
columns: [
{
field: 'claimDestinationFk',
autocomplete: {
url: 'ClaimDestinations',
showField: 'description',
valueField: 'id'
}
},
{
field: 'landed',
searchable: false
}
]
};
}
exprBuilder(param, value) {
switch (param) {
case 'itemFk':
case 'ticketFk':
case 'claimDestinationFk':
case 'quantity':
case 'price':
case 'discount':
case 'total':
return {[param]: value};
case 'concept':
return {[param]: {like: `%${value}%`}};
case 'landed':
return {[param]: {between: this.dateRange(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];
}
get checked() {
const salesClaimed = this.$.model.data || [];
const checkedSalesClaimed = [];
for (let saleClaimed of salesClaimed) {
if (saleClaimed.$checked)
checkedSalesClaimed.push(saleClaimed);
}
return checkedSalesClaimed;
}
updateDestination(saleClaimed, claimDestinationFk) {
const data = {rows: [saleClaimed], claimDestinationFk: claimDestinationFk};
this.$http.post(`Claims/updateClaimDestination`, data).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
}).catch(e => {
this.$.model.refresh();
throw e;
});
}
removeSales(saleClaimed) {
const params = {sales: [saleClaimed]};
this.$http.post(`ClaimEnds/deleteSalesClaimed`, params).then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
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.$t('Data saved!'));
});
}
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.quantity * sale.price;
const discount = (sale.discount * (sale.quantity * sale.price)) / 100;
this.claimedTotal += price - discount;
});
}
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.$t('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;
});
}
onUpdateGreugeAccept() {
const promises = [];
promises.push(this.getGreugeTypeId());
promises.push(this.getGreugeConfig());
return Promise.all(promises).then(() => {
return this.updateGreuge({
clientFk: this.claim.clientFk,
description: this.$t('ClaimGreugeDescription', {
claimId: this.claim.id
}).toUpperCase(),
amount: this.freightPickUpPrice,
greugeTypeFk: this.greugeTypeFreightId,
ticketFk: this.claim.ticketFk
});
});
}
updateGreuge(data) {
return this.$http.post(`Greuges`, data).then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.vnApp.showMessage(this.$t('Greuge added'));
});
}
save(data) {
const query = `Claims/${this.$params.id}/updateClaimAction`;
this.$http.patch(query, data)
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
onSave() {
this.vnApp.showSuccess(this.$t('Data saved!'));
}
onResponse() {
const rowsToEdit = [];
for (let row of this.checked)
rowsToEdit.push({id: row.id});
const data = {
rows: rowsToEdit,
claimDestinationFk: this.newDestination
};
const query = `Claims/updateClaimDestination`;
this.$http.post(query, data)
.then(() => {
this.$.model.refresh();
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
}
ngModule.vnComponent('vnClaimAction', {
template: require('./index.html'),
controller: Controller,
bindings: {
claim: '<'
},
require: {
card: '^vnClaimCard'
}
});