salix/modules/client/front/fiscal-data/index.js

101 lines
3.2 KiB
JavaScript

import ngModule from '../module';
import Component from 'core/lib/component';
export default class Controller extends Component {
onSubmit() {
const orgData = this.$.watcher.orgData;
delete this.client.despiteOfClient;
const hasContactData = this.client.email || this.client.phone || this.client.mobile;
if (!orgData.isTaxDataChecked && this.client.isTaxDataChecked && hasContactData)
this.checkExistingClient();
else this.save();
}
checkExistingClient() {
const findParams = [];
if (this.client.email)
findParams.push({email: this.client.email});
if (this.client.phone)
findParams.push({phone: this.client.phone});
if (this.client.mobile)
findParams.push({mobile: this.client.mobile});
const filterObj = {
where: {
and: [
{or: findParams},
{id: {neq: this.client.id}}
]
}
};
const $t = this.$translate.instant;
const filter = encodeURIComponent(JSON.stringify(filterObj));
const query = `Clients/findOne?filter=${filter}`;
this.$http.get(query).then(res => {
const params = {clientId: res.data.id};
const question = $t('Found a client with this phone or email', params, null, null, 'sanitizeParameters');
this.client.despiteOfClient = params.clientId;
this.$.confirmDuplicatedClient.question = question;
this.$.confirmDuplicatedClient.show();
}).catch(error => {
if (error.status == 404)
this.save();
});
}
checkEtChanges(orgData) {
const equalizatedHasChanged = orgData.isEqualizated != this.client.isEqualizated;
const hasToInvoiceByAddress = orgData.hasToInvoiceByAddress || this.client.hasToInvoiceByAddress;
if (equalizatedHasChanged && hasToInvoiceByAddress)
this.$.propagateIsEqualizated.show();
else if (equalizatedHasChanged)
return this.onAcceptEt();
return this.$q.resolve();
}
onAcceptEt() {
const query = `Clients/${this.client.id}/addressesPropagateRe`;
return this.$http.patch(query, {isEqualizated: this.client.isEqualizated}).then(
() => this.vnApp.showMessage(this.$translate.instant('Equivalent tax spreaded'))
);
}
onAcceptDuplication() {
this.save();
return true;
}
save() {
const orgData = this.$.watcher.orgData;
const clonedOrgData = JSON.parse(JSON.stringify(orgData));
return this.$.watcher.submit().then(
() => this.checkEtChanges(clonedOrgData));
}
onChangeEqualizated(value) {
const orgData = this.$.watcher.orgData;
if (value === true)
this.client.hasToInvoiceByAddress = false;
else if (orgData.hasToInvoiceByAddress)
this.client.hasToInvoiceByAddress = true;
this.$.$apply();
}
}
ngModule.component('vnClientFiscalData', {
template: require('./index.html'),
controller: Controller,
require: {card: '^vnClientCard'},
bindings: {
client: '<'
}
});