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 = {}; this.checkedDefaulers = []; this.smartTableOptions = { activeButtons: { search: true }, columns: [ { field: 'clientFk', autocomplete: { url: 'Clients', showField: 'name', valueField: 'id' } }, { field: 'salesPersonFk', autocomplete: { url: 'Workers/activeWithInheritedRole', where: `{role: 'salesPerson'}`, searchFunction: '{firstName: $search}', showField: 'name', valueField: 'id', } }, { field: 'workerFk', autocomplete: { url: 'Workers/activeWithInheritedRole', searchFunction: '{firstName: $search}', showField: 'name', valueField: 'id', } }, { field: 'observation', searchable: false }, { field: 'created', searchable: false }, { field: 'defaulterSinced', searchable: false } ] }; this.getBalanceDueTotal(); } 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; 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`); const params = []; for (let defaulter of this.checked) { params.push({ text: this.defaulter.observation, clientFk: defaulter.clientFk }); } this.$http.post(`ClientObservations`, params) .then(() => { this.vnApp.showMessage(this.$t('Observation saved!')); this.$state.reload(); }); } exprBuilder(param, value) { switch (param) { case 'creditInsurance': case 'amount': case 'clientFk': case 'workerFk': case 'salesPersonFk': return {[`d.${param}`]: value}; } } } ngModule.vnComponent('vnClientDefaulter', { template: require('./index.html'), controller: Controller });