salix/modules/client/front/defaulter/index.js

200 lines
5.7 KiB
JavaScript
Raw Permalink Normal View History

import ngModule from '../module';
import Section from 'salix/components/section';
import UserError from 'core/lib/user-error';
export default class Controller extends Section {
constructor($element, $) {
super($element, $);
this.defaulter = {};
2023-06-27 08:27:39 +00:00
this.defaulters = [];
this.checkedDefaulers = [];
this.smartTableOptions = {
activeButtons: {
search: true
},
columns: [
{
field: 'clientFk',
autocomplete: {
url: 'Clients',
showField: 'name',
valueField: 'id'
}
2023-05-23 09:26:31 +00:00
}, {
field: 'salesPersonFk',
autocomplete: {
url: 'Workers/activeWithInheritedRole',
where: `{role: 'salesPerson'}`,
searchFunction: '{firstName: $search}',
showField: 'name',
valueField: 'id',
}
2023-05-23 09:26:31 +00:00
}, {
field: 'countryFk',
2023-05-23 09:26:31 +00:00
autocomplete: {
2023-07-13 08:22:41 +00:00
url: 'Countries',
2023-05-23 09:26:31 +00:00
showField: 'country',
valueField: 'id'
2023-05-23 09:26:31 +00:00
}
2023-05-23 09:32:26 +00:00
}, {
field: 'payMethodFk',
autocomplete: {
showField: 'name',
valueField: 'id'
}
},
{
field: 'workerFk',
autocomplete: {
url: 'Workers/activeWithInheritedRole',
searchFunction: '{firstName: $search}',
showField: 'name',
valueField: 'id',
}
},
{
field: 'observation',
2022-03-01 10:43:35 +00:00
searchable: false
},
{
field: 'created',
datepicker: true
},
{
field: 'defaulterSinced',
datepicker: true
}
]
};
this.getBalanceDueTotal();
}
2023-06-27 08:27:39 +00:00
set defaulters(value) {
if (!value || !value.length) return;
for (let defaulter of value)
2023-07-03 09:45:05 +00:00
defaulter.isWorker = defaulter.businessTypeFk === 'worker';
2023-06-27 08:27:39 +00:00
this._defaulters = value;
}
get defaulters() {
return this._defaulters;
}
get checked() {
const clients = this.$.model.data || [];
const checkedLines = [];
for (let defaulter of clients) {
if (defaulter.checked)
checkedLines.push(defaulter);
}
return checkedLines;
}
saveChecked(clientId) {
this.checkedDefaulers = this.checkedDefaulers.includes(clientId) ?
this.checkedDefaulers.filter(id => id !== clientId) : [...this.checkedDefaulers, clientId];
}
reCheck() {
if (!this.$.model.data || !this.checkedDefaulers.length) return;
this.$.model.data.forEach(defaulter => {
defaulter.checked = this.checkedDefaulers.includes(defaulter.clientFk);
});
}
getBalanceDueTotal() {
this.$http.get('Defaulters/filter')
.then(res => {
if (!res.data) return 0;
this.balanceDueTotal = res.data.reduce(
(accumulator, currentValue) => {
return accumulator + (currentValue['amount'] || 0);
}, 0);
});
}
chipColor(date) {
const day = 24 * 60 * 60 * 1000;
2023-01-16 14:18:24 +00:00
const today = Date.vnNew();
today.setHours(0, 0, 0, 0);
const observationShipped = new Date(date);
observationShipped.setHours(0, 0, 0, 0);
const difference = today - observationShipped;
if (difference > (day * 20))
return 'alert';
if (difference > (day * 10))
return 'warning';
}
onResponse() {
if (!this.defaulter.observation)
throw new UserError(`The message can't be empty`);
2022-02-01 07:49:56 +00:00
const params = [];
for (let defaulter of this.checked) {
2022-02-01 07:49:56 +00:00
params.push({
text: this.defaulter.observation,
clientFk: defaulter.clientFk
2022-02-01 07:49:56 +00:00
});
}
2022-02-01 07:49:56 +00:00
this.$http.post(`ClientObservations`, params) .then(() => {
this.vnApp.showSuccess(this.$t('Observation saved!'));
this.sendMail();
2022-02-01 07:49:56 +00:00
this.$state.reload();
});
}
sendMail() {
const params = {
defaulters: this.checked,
2023-05-23 09:26:31 +00:00
observation: this.defaulter.observation,
};
this.$http.post(`Defaulters/observationEmail`, params);
}
exprBuilder(param, value) {
switch (param) {
case 'creditInsurance':
case 'amount':
case 'clientFk':
case 'workerFk':
case 'countryFk':
2023-05-23 11:23:46 +00:00
case 'payMethod':
case 'salesPersonFk':
return {[`d.${param}`]: value};
case 'created':
return {'d.created': {
between: this.dateRange(value)}
};
case 'defaulterSinced':
return {'d.defaulterSinced': {
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];
}
}
2022-02-25 09:34:17 +00:00
ngModule.vnComponent('vnClientDefaulter', {
template: require('./index.html'),
controller: Controller
});