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

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-06-03 11:01:47 +00:00
import ngModule from '../module';
2017-05-23 08:58:17 +00:00
2017-06-03 11:01:47 +00:00
export default class Controller {
constructor($scope, $http, vnApp, $translate) {
this.$scope = $scope;
2017-06-07 13:28:42 +00:00
this.$http = $http;
this.vnApp = vnApp;
2018-10-17 10:49:18 +00:00
this.$translate = $translate;
}
get client() {
return this._client;
}
set client(value) {
this._client = value;
if (!value) return;
2019-02-12 12:26:52 +00:00
if (!value.bankEntityFk)
this.autofillBic();
2018-10-17 10:49:18 +00:00
this.newBankEntity = {
countryFk: Number.parseInt(value.countryFk)
};
}
2018-10-08 11:09:30 +00:00
onSubmit() {
let shouldNotify = false;
2018-10-08 11:09:30 +00:00
if (this.hasPaymethodChanges())
shouldNotify = true;
this.$scope.watcher.submit().then(() => {
2018-10-08 11:09:30 +00:00
if (shouldNotify)
2019-02-28 13:30:29 +00:00
this.vnApp.showMessage(this.$translate.instant('Notification sent!'));
});
2017-06-07 13:28:42 +00:00
}
2018-10-08 11:09:30 +00:00
hasPaymethodChanges() {
let orgData = this.$scope.watcher.orgData;
let payMethod = orgData.payMethodFk != this.client.payMethodFk;
let iban = orgData.iban != this.client.iban;
let dueDay = orgData.dueDay != this.client.dueDay;
2017-10-04 11:32:26 +00:00
return payMethod || iban || dueDay;
}
2018-10-17 10:49:18 +00:00
onBankEntityOpen() {
this.newBankEntity.name = '';
2019-03-07 08:55:22 +00:00
this.newBankEntity.id = '';
2018-10-17 10:49:18 +00:00
this.newBankEntity.bic = '';
this.$scope.$apply();
}
onBankEntityResponse(response) {
if (response == 'ACCEPT') {
2018-10-17 10:49:18 +00:00
try {
if (!this.newBankEntity.name)
throw new Error(`Name can't be empty`);
2019-03-07 08:55:22 +00:00
if (!this.newBankEntity.id)
throw new Error(`Code can't be empty`);
2018-10-17 10:49:18 +00:00
if (!this.newBankEntity.bic)
throw new Error(`Swift / BIC can't be empty`);
this.$http.post(`/client/api/BankEntities`, this.newBankEntity).then(response => {
if (response.data)
this.client.bankEntityFk = response.data.id;
});
} catch (e) {
this.vnApp.showError(this.$translate.instant(e.message));
return false;
}
}
2018-10-17 10:49:18 +00:00
return true;
}
2018-11-23 07:44:19 +00:00
get ibanCountry() {
if (!this.client || !this.client.iban) return false;
let countryCode = this.client.iban.substr(0, 2);
return countryCode;
}
autofillBic() {
if (!this.client.iban) return;
let bankEntityId = parseInt(this.client.iban.substr(4, 4));
let filter = {where: {id: bankEntityId}};
2018-11-23 07:44:19 +00:00
if (this.ibanCountry != 'ES') return;
let json = encodeURIComponent(JSON.stringify(filter));
this.$http.get(`/client/api/BankEntities?filter=${json}`).then(response => {
2018-11-22 14:17:35 +00:00
const hasData = response.data && response.data[0];
if (hasData)
this.client.bankEntityFk = response.data[0].id;
2018-11-22 14:17:35 +00:00
else if (!hasData)
this.client.bankEntityFk = null;
});
}
}
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate'];
2017-06-03 11:01:47 +00:00
ngModule.component('vnClientBillingData', {
2018-05-23 12:26:51 +00:00
template: require('./index.html'),
2017-06-03 11:01:47 +00:00
controller: Controller,
2017-05-23 08:58:17 +00:00
bindings: {
client: '<'
}
2017-06-03 11:01:47 +00:00
});