import ngModule from '../module'; import Section from 'salix/components/section'; export default class Controller extends Section { constructor($element, $) { super($element, $); this.$http.get('NewWorkerConfigs/findOne').then(res => { return this.worker = Object.assign({}, res.data); }); } onSubmit() { return this.$.watcher.submit().then(json => { this.$state.go('worker.card.basicData', {id: json.data.id}); }); } autofillBic() { if (!this.worker || !this.worker.iban) return; let bankEntityId = parseInt(this.worker.iban.substr(4, 4)); let filter = {where: {id: bankEntityId}}; if (this.ibanCountry != 'ES') return; this.$http.get(`BankEntities`, {filter}).then(response => { const hasData = response.data && response.data[0]; if (hasData) this.worker.bankEntityFk = response.data[0].id; else if (!hasData) this.worker.bankEntityFk = null; }); } generateCodeUser() { if (!this.worker.firstName || !this.worker.lastNames) return; const totalName = this.worker.firstName.concat(' ' + this.worker.lastNames).toLowerCase(); const totalNameArray = totalName.split(' '); let newCode = ''; for (let part of totalNameArray) newCode += part.charAt(0); this.worker.code = newCode.toUpperCase().slice(0, 3); this.worker.name = totalNameArray[0] + newCode.slice(1); } get province() { return this._province; } // Province auto complete set province(selection) { this._province = selection; } get town() { return this._town; } // Town auto complete set town(selection) { this._town = selection; if (!selection) return; const province = selection.province; const postcodes = selection.postcodes; if (!this.client.provinceFk) this.client.provinceFk = province.id; if (postcodes.length === 1) this.client.postcode = postcodes[0].code; } get postcode() { return this._postcode; } // Postcode auto complete set postcode(selection) { this._postcode = selection; if (!selection) return; const town = selection.town; const province = town.province; if (!this.client.city) this.client.city = town.name; if (!this.client.provinceFk) this.client.provinceFk = province.id; } onResponse(response) { this.client.postcode = response.code; this.client.city = response.city; this.client.provinceFk = response.provinceFk; } } Controller.$inject = ['$element', '$scope']; ngModule.vnComponent('vnWorkerCreate', { template: require('./index.html'), controller: Controller });