123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- openerp.eiru_payslip_payments = function(instance, local) {
- local.widgetInstance = null;
- local.parentInstance = null;
- var Qweb = openerp.web.qweb;
- local.PayslipPaymentsWidget = instance.Widget.extend({
- template: 'eiru_payslip_payments.PayslipWidget',
- id: undefined,
- payslip: [],
- payslipLine: [],
- accountJournal: [],
- company: [],
- currency: [],
- payslipNew: [],
- init: function(parent) {
- this._super(parent);
- this.buttons = parent.$buttons;
- },
- start: function() {
- var self = this;
- this.$el.click(function(){
- self.fectchInitial()
- });
- self.buttons.click(function(e) {
- /* E (Editar) */
- if (e.target.accessKey === 'E')
- self.$el.css('display','none');
- /* S (Guarrdar) */
- if (e.target.accessKey === 'S')
- self.$el.css('display','flex');
- /* D (Cancelar) */
- if (e.target.accessKey === 'D')
- self.$el.css('display','flex');
- });
- },
- updateId: function(id) {
- var self = this;
- self.id = id;
- },
- // Metodo Inicial
- fectchInitial: function() {
- var self = this;
- self.fectchHrPayslip().then(function(payslip) {
- return payslip;
- }).then(function(payslip) {
- self.payslip = payslip;
- return self.fectchHrPayslipLine(payslip);
- }).then(function(payslipLine) {
- self.payslipLine = payslipLine;
- return self.fectchJournal();
- }).then(function(accountJournal) {
- self.accountJournal = accountJournal;
- return self.fectchCompany();
- }).then(function(company) {
- self.company = company;
- return self.fectchCurency();
- }).then(function(currency) {
- self.currency = currency;
- return self.joinPayslip();
- }).then(function(payslipNew) {
- self.payslipNew = payslipNew;
- if (self.payslipNew.length <= 0) {
- instance.web.notification.do_warn("Atención","No tienes nominas que pagar");
- return
- }
- return self.asyncPaymentsPayslip();
- });
- },
- // payslip
- fectchHrPayslip: function() {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id', 'name', 'employee_id', 'date_from', 'date_to', 'move_id', 'number', 'line_ids', 'journal_id'];
- var domain = [['payslip_run_id', 'in', [self.id]], ['state', '=', 'done']];
- var payslip = new instance.web.Model('hr.payslip');
- payslip.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Hr payslip Line
- fectchHrPayslipLine: function(payslip) {
- var defer = $.Deferred();
- var payslip_id = _.map(payslip, function(map) {
- return map.id;
- });
- var fields = ['id', 'name', 'code', 'total', 'amount', 'slip_id'];
- var domain = [['slip_id', 'in', payslip_id], ['code', '=', 'NET']];
- var payslipLine = new instance.web.Model('hr.payslip.line');
- payslipLine.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Diario de Salario
- fectchJournal: function() {
- var defer = $.Deferred();
- var fields = ['id', 'name', 'code', 'type', 'currency', 'default_debit_account_id', 'default_credit_account_id'];
- var domain = [['active', '=', true], ['type', 'in', ['bank', 'cash']], ['currency', '=', false]];
- var journalSalario = new instance.web.Model('account.journal');
- journalSalario.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Company
- fectchCompany: function() {
- var defer = $.Deferred();
- var fields = ['id','name', 'currency_id'];
- var domain = [['id', '=', 1]];
- var resCompanyIds = new instance.web.Model('res.company');
- resCompanyIds.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- // Res currecy
- fectchCurency : function() {
- var defer = $.Deferred();
- var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
- var domain = [['active','=', true]];
- var resCurrecy = new instance.web.Model('res.currency');
- resCurrecy.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // get payslipLine
- getPayslipLine: function(slip_id) {
- var self = this;
- return _.filter(self.payslipLine, function(filter) {
- return filter.slip_id[0] === slip_id;
- });
- },
- // Unir Payslip con payslipLine
- joinPayslip: function() {
- var self = this;
- var defer = $.Deferred();
- var PayslipNew = [];
- var line;
- _.each(self.payslip, function(item) {
- line = self.getPayslipLine(item.id).shift();
- if (!line){
- line = {};
- line.total =0;
- }
- PayslipNew.push({
- 'id': item.id,
- 'name': item.name,
- 'employee_id': item.employee_id[0],
- 'employee': item.employee_id[1],
- 'date_from': item.date_from,
- 'date_to': item.date_to,
- 'periodo': moment(item.date_from).format('DD/MM/YYYY')+" - "+moment(item.date_to).format('DD/MM/YYYY'),
- 'number': item.number,
- 'ammout': line.total
- });
- });
- defer.resolve(PayslipNew);
- return defer;
- },
- // Moneda de la compania
- getCurrencyCompany: function(id_currency) {
- var self = this;
- return _.filter(self.currency, function(item) {
- return item.id === id_currency;
- });
- },
- // Async loop
- asyncPaymentsPayslip: function() {
- var self = this;
- var defer = $.Deferred();
- var company = self.company.shift();
- var currency = self.getCurrencyCompany(company.currency_id[0]).shift();
- self.asyncLoopFactory(self.payslipNew.length, function(loop) {
- var payslip = self.payslipNew[loop.iteration()];
- self.showPayments(payslip, currency).then(function(results) {
- self.reloadLine();
- if (results){
- loop.next();
- }
- });
- }, function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- // reloadLine
- reloadLine: function() {
- local.parentInstance.reload();
- },
- // Ejc. el Modal
- showPayments: function(payslip, currency) {
- var self = this;
- var defer = $.Deferred();
- var state = true;
- var headerModalName = [{
- name: payslip.name
- }];
- var dataPayslip = [{
- employee: payslip.employee,
- periodo: payslip.periodo,
- ref: payslip.number,
- amount: accounting.formatMoney(payslip.ammout, currency.symbol, currency.decimal_places, currency.thousands_separator, currency.decimal_separator)
- }];
- var modal = Qweb.render('EiruPayslipPaymentsModal', {
- data: dataPayslip,
- dataName: headerModalName,
- journal: self.accountJournal
- });
- $('.openerp_webclient_container').after(modal);
- $('.expired-account-modal').modal();
- // Referencia de Pago\
- var journal_ref = $('.expired-account-modal').find('.journal-ref');
- var journal = $('.expired-account-modal').find('.current-journal');
- // Click Cerrar
- $('.expired-account-modal').on('hidden.bs.modal', function (e) {
- defer.resolve(false);
- self.removeModal(e);
- });
- // clcik boton pagar
- var contenido = $('.expired-account-modal').find('.payments-payslip');
- contenido.click(function(e) {
- self.paymentsPayslip(payslip,journal.val(), journal_ref.val()).then(function(insert) {
- return insert;
- }).then(function(journal) {
- if (!journal)
- state = false;
- defer.resolve(state);
- })
- self.removeModal(e);
- });
- return defer;
- },
- // Remover
- removeModal: function() {
- $('.expired-account-modal').remove();
- $('.modal-backdrop').remove();
- },
- // Pagara la Nomina
- paymentsPayslip: function(payslip, journal, journal_ref) {
- var defer = $.Deferred();
- var HrPayslip = new instance.web.Model('hr.payslip');
- HrPayslip.call('create_from_prayslip',[
- {
- id: payslip.id,
- journal_id: journal,
- journal_ref: journal_ref
- }
- ],{
- context: new instance.web.CompoundContext()
- }).then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- /* ---------------------------------------------------------------------
- * Description: Async loop util v2 written by Robert Gauto.
- * --------------------------------------------------------------------*/
- asyncLoopFactory: function (iterations, func, callback) {
- var index = 0;
- var done = false;
- var loop = {
- next: function() {
- if (done) {
- return;
- }
- if (index < iterations) {
- index++;
- func(loop);
- } else {
- done = true;
- callback();
- }
- },
- iteration: function () {
- return index - 1;
- },
- break: function () {
- done = true;
- callback();
- }
- };
- loop.next();
- return loop;
- },
- });
- if (instance.web && instance.web.FormView) {
- instance.web.FormView.include({
- load_record: function(record) {
- this._super.apply(this, arguments);
- if (this.model !== 'hr.payslip.run')
- return;
- local.parentInstance = this;
- if (local.widgetInstance) {
- local.widgetInstance.updateId(record.id);
- }
- if (this.$el.find('.payslip-payments').length !== 0 )
- return;
- local.widgetInstance = new local.PayslipPaymentsWidget(this);
- var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
- elemento = elemento.find('.oe_right.oe_button_box.eiru-payslip-payments');
- local.widgetInstance.appendTo(elemento);
- local.widgetInstance.updateId(record.id);
- },
- });
- }
- }
|