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 {
|
2017-08-30 06:38:41 +00:00
|
|
|
constructor($scope, $http, vnApp, $translate) {
|
2018-07-10 12:13:47 +00:00
|
|
|
this.$scope = $scope;
|
2017-06-07 13:28:42 +00:00
|
|
|
this.$http = $http;
|
2017-06-05 07:01:21 +00:00
|
|
|
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)
|
|
|
|
};
|
2017-05-31 10:56:37 +00:00
|
|
|
}
|
2018-05-28 10:22:35 +00:00
|
|
|
|
2018-10-08 11:09:30 +00:00
|
|
|
onSubmit() {
|
|
|
|
let shouldNotify = false;
|
2018-05-28 10:22:35 +00:00
|
|
|
|
2018-10-08 11:09:30 +00:00
|
|
|
if (this.hasPaymethodChanges())
|
|
|
|
shouldNotify = true;
|
2018-05-28 10:22:35 +00:00
|
|
|
|
2018-07-10 12:13:47 +00:00
|
|
|
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!'));
|
2018-07-10 12:13:47 +00:00
|
|
|
});
|
2017-06-07 13:28:42 +00:00
|
|
|
}
|
2018-05-28 10:22:35 +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
|
|
|
|
2018-07-10 12:13:47 +00:00
|
|
|
return payMethod || iban || dueDay;
|
2017-05-31 10:56:37 +00:00
|
|
|
}
|
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) {
|
2018-11-07 08:17:07 +00:00
|
|
|
if (response == 'ACCEPT') {
|
2018-10-17 10:49:18 +00:00
|
|
|
try {
|
|
|
|
if (!this.newBankEntity.name)
|
|
|
|
throw new Error(`Name can't be empty`);
|
|
|
|
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-11-07 08:17:07 +00:00
|
|
|
}
|
2018-10-17 10:49:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-07 08:17:07 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-07 08:17:07 +00:00
|
|
|
autofillBic() {
|
2019-10-09 22:47:29 +00:00
|
|
|
if (!this.client || !this.client.iban) return;
|
2018-11-07 08:17:07 +00:00
|
|
|
|
|
|
|
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;
|
2018-11-07 08:17:07 +00:00
|
|
|
|
|
|
|
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)
|
2018-11-22 11:10:43 +00:00
|
|
|
this.client.bankEntityFk = response.data[0].id;
|
2018-11-22 14:17:35 +00:00
|
|
|
else if (!hasData)
|
|
|
|
this.client.bankEntityFk = null;
|
2018-11-07 08:17:07 +00:00
|
|
|
});
|
|
|
|
}
|
2017-05-31 10:56:37 +00:00
|
|
|
}
|
2017-08-30 06:38:41 +00:00
|
|
|
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate'];
|
2017-05-31 10:56:37 +00:00
|
|
|
|
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
|
|
|
});
|