2017-06-03 11:01:47 +00:00
|
|
|
import ngModule from '../module';
|
2020-03-17 10:17:50 +00:00
|
|
|
import Section from 'salix/components/section';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2020-03-17 10:17:50 +00:00
|
|
|
export default class Controller extends Section {
|
2019-06-13 11:08:11 +00:00
|
|
|
onSubmit() {
|
2019-12-31 09:38:19 +00:00
|
|
|
const orgData = this.$.watcher.orgData;
|
2020-02-04 10:25:15 +00:00
|
|
|
delete this.client.despiteOfClient;
|
2020-03-04 08:32:18 +00:00
|
|
|
|
2020-03-04 08:26:12 +00:00
|
|
|
const hasContactData = this.client.email || this.client.phone || this.client.mobile;
|
2020-03-04 10:17:35 +00:00
|
|
|
const hasChangedTaxData = !orgData.isTaxDataChecked && this.client.isTaxDataChecked;
|
|
|
|
if (hasChangedTaxData && hasContactData)
|
2020-02-04 10:25:15 +00:00
|
|
|
this.checkExistingClient();
|
|
|
|
else this.save();
|
|
|
|
}
|
2019-12-31 09:38:19 +00:00
|
|
|
|
2020-02-04 10:25:15 +00:00
|
|
|
checkExistingClient() {
|
2020-03-04 08:26:12 +00:00
|
|
|
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});
|
|
|
|
|
2020-02-04 10:25:15 +00:00
|
|
|
const filterObj = {
|
|
|
|
where: {
|
|
|
|
and: [
|
2020-03-04 08:26:12 +00:00
|
|
|
{or: findParams},
|
2020-02-04 10:25:15 +00:00
|
|
|
{id: {neq: this.client.id}}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2018-07-18 10:00:06 +00:00
|
|
|
|
2020-02-04 10:25:15 +00:00
|
|
|
const $t = this.$translate.instant;
|
|
|
|
const filter = encodeURIComponent(JSON.stringify(filterObj));
|
|
|
|
const query = `Clients/findOne?filter=${filter}`;
|
|
|
|
this.$http.get(query).then(res => {
|
2020-02-27 12:33:40 +00:00
|
|
|
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)
|
2020-03-06 12:06:01 +00:00
|
|
|
return this.save();
|
|
|
|
throw error;
|
2020-02-04 10:25:15 +00:00
|
|
|
});
|
2017-11-15 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:25:15 +00:00
|
|
|
checkEtChanges(orgData) {
|
2019-12-31 09:38:19 +00:00
|
|
|
const equalizatedHasChanged = orgData.isEqualizated != this.client.isEqualizated;
|
2020-02-04 10:25:15 +00:00
|
|
|
const hasToInvoiceByAddress = orgData.hasToInvoiceByAddress || this.client.hasToInvoiceByAddress;
|
2017-11-15 07:41:54 +00:00
|
|
|
|
2020-02-04 10:25:15 +00:00
|
|
|
if (equalizatedHasChanged && hasToInvoiceByAddress)
|
2017-11-22 07:59:21 +00:00
|
|
|
this.$.propagateIsEqualizated.show();
|
2019-12-31 09:38:19 +00:00
|
|
|
else if (equalizatedHasChanged)
|
|
|
|
return this.onAcceptEt();
|
2018-12-11 12:16:33 +00:00
|
|
|
|
2019-12-31 09:38:19 +00:00
|
|
|
return this.$q.resolve();
|
2017-11-15 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 09:38:19 +00:00
|
|
|
onAcceptEt() {
|
|
|
|
const query = `Clients/${this.client.id}/addressesPropagateRe`;
|
|
|
|
return this.$http.patch(query, {isEqualizated: this.client.isEqualizated}).then(
|
2020-07-23 14:07:08 +00:00
|
|
|
() => this.vnApp.showMessage(this.$t('Equivalent tax spreaded'))
|
2019-12-31 09:38:19 +00:00
|
|
|
);
|
2017-11-15 07:41:54 +00:00
|
|
|
}
|
2020-02-04 10:25:15 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2020-02-21 11:39:41 +00:00
|
|
|
|
|
|
|
get province() {
|
|
|
|
return this._province;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Province auto complete
|
|
|
|
set province(selection) {
|
|
|
|
this._province = selection;
|
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
if (!selection) return;
|
2020-02-21 11:39:41 +00:00
|
|
|
|
|
|
|
const country = selection.country;
|
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
if (!this.client.countryFk)
|
|
|
|
this.client.countryFk = country.id;
|
2020-02-21 11:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get town() {
|
|
|
|
return this._town;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Town auto complete
|
|
|
|
set town(selection) {
|
|
|
|
this._town = selection;
|
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
if (!selection) return;
|
2020-02-21 11:39:41 +00:00
|
|
|
|
|
|
|
const province = selection.province;
|
|
|
|
const country = province.country;
|
|
|
|
const postcodes = selection.postcodes;
|
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
if (!this.client.provinceFk)
|
|
|
|
this.client.provinceFk = province.id;
|
|
|
|
|
|
|
|
if (!this.client.countryFk)
|
|
|
|
this.client.countryFk = country.id;
|
2020-02-21 11:39:41 +00:00
|
|
|
|
|
|
|
if (postcodes.length === 1)
|
|
|
|
this.client.postcode = postcodes[0].code;
|
|
|
|
}
|
|
|
|
|
|
|
|
get postcode() {
|
|
|
|
return this._postcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Postcode auto complete
|
|
|
|
set postcode(selection) {
|
|
|
|
const oldValue = this._postcode;
|
|
|
|
this._postcode = selection;
|
2020-02-24 10:27:36 +00:00
|
|
|
|
2020-03-04 12:54:17 +00:00
|
|
|
if (!selection || !oldValue) return;
|
2020-02-21 11:39:41 +00:00
|
|
|
|
|
|
|
const town = selection.town;
|
|
|
|
const province = town.province;
|
|
|
|
const country = province.country;
|
2020-02-24 10:27:36 +00:00
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
if (!this.client.city)
|
|
|
|
this.client.city = town.name;
|
|
|
|
|
|
|
|
if (!this.client.provinceFk)
|
|
|
|
this.client.provinceFk = province.id;
|
|
|
|
|
|
|
|
if (!this.client.countryFk)
|
|
|
|
this.client.countryFk = country.id;
|
2020-02-21 11:39:41 +00:00
|
|
|
}
|
2020-02-24 10:27:36 +00:00
|
|
|
|
|
|
|
onResponse(response) {
|
|
|
|
this.client.postcode = response.code;
|
|
|
|
}
|
2017-11-15 07:41:54 +00:00
|
|
|
}
|
2019-09-02 07:11:40 +00:00
|
|
|
|
2020-07-24 12:22:30 +00:00
|
|
|
ngModule.vnComponent('vnClientFiscalData', {
|
2018-05-23 12:26:51 +00:00
|
|
|
template: require('./index.html'),
|
|
|
|
controller: Controller,
|
2018-07-03 09:13:33 +00:00
|
|
|
require: {card: '^vnClientCard'},
|
2017-01-31 13:13:06 +00:00
|
|
|
bindings: {
|
|
|
|
client: '<'
|
2017-02-07 11:58:25 +00:00
|
|
|
}
|
2017-06-03 11:01:47 +00:00
|
|
|
});
|