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

115 lines
3.1 KiB
JavaScript

import ngModule from '../module';
export default class Controller {
constructor($scope, $http, vnApp, $translate) {
this.$scope = $scope;
this.$http = $http;
this.vnApp = vnApp;
this.$translate = $translate;
}
get client() {
return this._client;
}
set client(value) {
this._client = value;
if (!value) return;
this.newBankEntity = {
countryFk: Number.parseInt(value.countryFk)
};
}
onSubmit() {
let shouldNotify = false;
if (this.hasPaymethodChanges())
shouldNotify = true;
this.$scope.watcher.submit().then(() => {
if (shouldNotify)
this.notifyChanges();
});
}
notifyChanges() {
this.$http.get(`/mailer/notification/payment-update/${this.client.id}`).then(
() => this.vnApp.showMessage(this.$translate.instant('Notification sent!'))
);
}
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;
return payMethod || iban || dueDay;
}
onBankEntityOpen() {
this.newBankEntity.name = '';
this.newBankEntity.bic = '';
this.$scope.$apply();
}
onBankEntityResponse(response) {
if (response == 'ACCEPT') {
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;
}
}
return true;
}
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}};
if (this.ibanCountry != 'ES') return;
let json = encodeURIComponent(JSON.stringify(filter));
this.$http.get(`/client/api/BankEntities?filter=${json}`).then(response => {
const hasData = response.data && response.data[0];
if (hasData)
this.client.bankEntityFk = response.data[0].id;
else if (!hasData)
this.client.bankEntityFk = null;
});
}
}
Controller.$inject = ['$scope', '$http', 'vnApp', '$translate'];
ngModule.component('vnClientBillingData', {
template: require('./index.html'),
controller: Controller,
bindings: {
client: '<'
}
});