(function() { openerp.widgetInstancePaymentsInvoices = null; openerp.parentInstancePaymentsInvoices = {}; var QWeb = openerp.web.qweb; var instanceWeb = openerp.web; openerp.EiruPaymentsInvoices = openerp.Widget.extend({ template: 'eiruPaymentsInvoices.Invoices', id: undefined, buttons: undefined, accountInvoice: [], resPartner: [], accountJournal: [], resCompany: [], resCurrency: [], currencySelect: [], ResUser: [], statementOpen: [], /*Move*/ accountMoveLine: [], moveLine:[], movePayments: [], /* bank */ resBankType: [], resBank: [], bankPayments: [], resBankChequeType: [], fieldsAllowed: [], /* account interes */ interestLine: [], interestConfig: [], /* statementConfig */ statementConfig: [], /* Module Cheque */ eiruBankPaymentsReferences: [], eiruAccountBankStatementUtility: [], eiruAccountInterest: [], /* init */ init: function(parent) { this._super(parent); this.buttons = parent.$buttons; }, /* start */ start: function () { var self = this; this.$el.click(function() { self.fetchInitial(); }); self.buttons.click(function(e) { /* C (Crear) */ if (e.target.accessKey === 'C') self.$el.css('display','none'); /* 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'); }); }, /* Actualizar Id de la visat actual */ updateId: function(id) { var self = this; self.id = id; }, /* Reload Page*/ reloadPage: function() { openerp.parentInstancePaymentsInvoices.reload(); }, /* Description: Función para remover el modal */ removeModal: function() { $('.expired-account-modal').remove(); $('.modal-backdrop').remove(); }, /* Método inicial */ fetchInitial: function() { var self = this; self.fetchAccountInvoice(self.id).then(function(accountInvoice) { return accountInvoice; }).then(function(accountInvoice) { self.accountInvoice = accountInvoice; console.log(accountInvoice); return self.fetchVerifyModule('eiru_bank_payments_references'); }).then(function(eiruBankPaymentsReferences) { self.eiruBankPaymentsReferences = eiruBankPaymentsReferences; return self.fetchResPartner(); }).then(function(resPartner) { self.resPartner = resPartner; return self.fetchAccountjournal(); }).then(function(accountJournal) { self.accountJournal = accountJournal; return self.fetchResCompany(); }).then(function(resCompany) { self.resCompany = resCompany; return self.fetchResCurrency(); }).then(function(resCurrency) { self.resCurrency = resCurrency; return self.fetchResBanktype(); }).then(function(resBankType) { self.resBankType= resBankType; return self.fetchResBankChecktype(); }).then(function(resBankChequeType) { self.resBankChequeType = resBankChequeType; return self.fetchResBank(); }).then(function(resBank) { self.resBank = resBank; return self.fetchBankPayments(); }).then(function(bankPayments) { self.bankPayments = bankPayments; return self.fetchMoveLine(); }).then(function(accountMoveLine) { self.accountMoveLine = accountMoveLine; return self.fetchVerifyModule('eiru_account_interest'); }).then(function(eiruAccountInterest) { self.eiruAccountInterest = eiruAccountInterest; return self.fetchInterestLine(); }).then(function(interestLine) { self.interestLine = interestLine; return self.fetchInterestConfig() }).then(function(interestConfig) { self.interestConfig = interestConfig; return self.fetchVerifyModule('eiru_account_bank_statement_utility'); }).then(function(eiruAccountBankStatementUtility) { self.eiruAccountBankStatementUtility = eiruAccountBankStatementUtility; return self.fetchStatementConfig(); }).then(function(statementConfig) { self.statementConfig = statementConfig; return self.fetchStatement(); }).then(function(statementOpen) { self.statementOpen = statementOpen; return self.generateMoveLine(); }).then(function(moveLine) { self.moveLine = moveLine; return self.showModalPayments(); }); }, /* Verificar modulo instalados */ fetchVerifyModule: function(module) { if (!module) return []; var moduleModule =new openerp.web.Model('ir.module.module'); var fields = ['id', 'name', 'state']; var domain = [['name', '=', module], ['state', '=', 'installed']]; return moduleModule.query(fields).filter(domain).all(); }, /* Account Invoice */ fetchAccountInvoice: function(ids) { var accountInvoice = new openerp.web.Model('account.invoice'); var fields = ['id', 'number', 'date_invoice', 'amount_total', 'residual', 'partner_id', 'currency_id', 'type' ,'seq_invoice']; var domain = [['id', '=', ids],['state', '=', 'open']]; return accountInvoice.query(fields).filter(domain).all(); }, /* Res partnes */ fetchResPartner: function() { var self = this; if (!self.accountInvoice || !self.accountInvoice.length) return; var partnerId = self.accountInvoice[0].partner_id[0]; var resPartner = new openerp.web.Model('res.partner'); var fields = ['id', 'name','commercial_partner_id']; var domain = [['id', '=', partnerId]]; return resPartner.query(fields).filter(domain).all(); }, /* Account Move Line */ fetchMoveLine: function() { var self = this; var invoiceMoveLine = new openerp.web.Model('account.invoice'); return invoiceMoveLine.call('get_moveline_invoice',[self.id], { context: new openerp.web.CompoundContext() }); }, /* Account Interest Line */ fetchInterestLine: function() { var self = this; if (!self.eiruAccountInterest.length) return []; var moveLineIDS = _.map(self.accountMoveLine, function(map) { return map.id }); var self = this; var invoiceMoveLine = new openerp.web.Model('account.invoice'); return invoiceMoveLine.call('eiru_payments_account_interest_line',[moveLineIDS], { context: new openerp.web.CompoundContext() }); }, /* GET Interets configurations */ fetchInterestConfig: function() { var self = this; if (!self.eiruAccountInterest.length) return []; var fields = ['id','name', 'lock_move_line']; var domain = [['active', '=', true]]; var accountInterestConfig = new openerp.web.Model('account.interest.config'); return accountInterestConfig.query(fields).filter(domain).all(); }, /* Res bank Paymnets */ fetchBankPayments: function() { var self = this; if (!self.eiruBankPaymentsReferences.length) return []; var invoiceBankPayments = new openerp.web.Model('account.invoice'); return invoiceBankPayments.call('get_bank_payments', { context: new openerp.web.CompoundContext() }); }, /* Account Journal Type[BANK, CASH]*/ fetchAccountjournal: function() { var fields = ['id', 'name', 'code', 'type', 'currency', 'default_debit_account_id', 'default_credit_account_id']; var domain = [['active', '=', true], ['type', 'in', ['bank', 'cash']]]; var journalSalario = new openerp.web.Model('account.journal'); return journalSalario.query(fields).filter(domain).all(); }, /* Consultar Compania */ fetchResCompany: function() { var fields = ['id','name', 'currency_id']; var domain = [['id', '=', 1]]; var resCompanyIds = new openerp.web.Model('res.company'); return resCompanyIds.query(fields).filter(domain).all(); }, /* Consultar Bank Type */ fetchResBanktype: function() { var self = this; if (!self.eiruBankPaymentsReferences.length) return []; var invoiceBankPayments = new openerp.web.Model('account.invoice'); return invoiceBankPayments.call('get_bank_payment_type', { context: new openerp.web.CompoundContext() }); }, /* Consultar Bank Check Type */ fetchResBankChecktype: function() { var self = this; if (!self.eiruBankPaymentsReferences.length) return []; var fields = ['id','name', 'code']; var resBnakCheckType = new openerp.web.Model('res.bank.cheque.type'); return resBnakCheckType.query(fields).all(); }, /*Consultar Res Bank */ fetchResBank: function() { var self = this; if (!self.eiruBankPaymentsReferences.length) return []; var fields = ['id', 'name', 'ruc']; var resBank = new openerp.web.Model('res.bank'); return resBank.query(fields).all(); }, /* Consultar Moneda Activas */ fetchResCurrency: function() { var invoicesCurrencyPayments = new openerp.web.Model('account.invoice'); return invoicesCurrencyPayments.call('get_currency_payments_invoices', { context: new openerp.web.CompoundContext() }); }, /* statement Config */ fetchStatementConfig: function() { var self = this; if (!self.eiruAccountBankStatementUtility.length) return []; var fields = ['id','name', 'active', 'import_statement_payments']; var domain = [['active', '=', true]]; var statementConfig = new openerp.web.Model('account.bank.statement.config'); return statementConfig.query(fields).filter(domain).all(); }, /* Get User */ fetchStatement: function(){ var invoiceStatement = new openerp.web.Model('account.invoice'); return invoiceStatement.call('get_statement', { context: new openerp.web.CompoundContext() }); }, /* Generar las cuotas a pagar */ generateMoveLine: function() { var self = this; var moveLine = []; var newMoveLine= []; var defer = $.Deferred(); var currencyInvoice = self.getCurrency(self.accountInvoice[0].currency_id[0]).shift(); _.each(self.accountMoveLine, function(line){ moveLine.push({ 'id': line.id, 'dateMaturity': moment(line.dateMaturity).format('DD/MM/YYYY'), 'debit': line.amountCurrency, 'residual': line.amountResidualCurrency, 'currencyAmount': line.currencyAmount, 'debitFormat': accounting.formatMoney((line.amountCurrency), currencyInvoice.symbol,currencyInvoice.decimalPlaces, currencyInvoice.thousandsSeparator, currencyInvoice.decimalSeparator), 'residualFormat': accounting.formatMoney((line.amountResidualCurrency), currencyInvoice.symbol, currencyInvoice.decimalPlaces, currencyInvoice.thousandsSeparator, currencyInvoice.decimalSeparator) }); }); newMoveLine = _.sortBy(moveLine, function(item){return new Date(item.dateMaturity.trim())}); defer.resolve(newMoveLine); return defer; }, /* Get Move Line */ getMoveLine: function(ids) { var self = this; return _.filter(self.moveLine, function(line){ return _.contains(ids, line.id); }); }, /* Get Move Line Currency */ getMoveLineCurrency: function(currencyAmount, idCurrency) { return _.filter(currencyAmount, function(currency){ return currency.id === idCurrency; }); }, /* Generar lista de cuotas seleccionadas */ generateSelectTotal:function(idSelect, idCurrency){ var self = this; var movePayments = []; var moveCurrency = []; var residual = 0; var move = self.getMoveLine(idSelect); if (!move.length){ return movePayments; } _.each(move, function(line){ moveCurrency = self.getMoveLineCurrency(line.currencyAmount, idCurrency.id).shift(); residual += moveCurrency.amountCurencyResidual; }); movePayments.push({ 'ids': idSelect, 'amountResidual': residual, 'amountResidualFormat': instanceWeb.formatCurrency(residual, idCurrency), 'currencySymbol': idCurrency.symbol, 'currencyRate': idCurrency.rate }); return movePayments; }, /* getCurrency ['filtra la moneda por id'']*/ getCurrency: function(id) { var self = this; return _.filter(self.resCurrency, function(item) { return item.id === id; }); }, /* Obtener la monedad sobre el diario seleccionada */ getCurrencySelect: function(id) { var self = this; var currency = []; if (!id) id = self.resCompany[0].currency_id[0]; currency = self.getCurrency(id); return currency; }, /* Get Journal*/ getJournal: function(code) { var self = this; return _.filter(self.accountJournal, function(item) { return item.code === code; }); }, /* GET fields filter */ getBankTypeFieldsAllowed: function(name) { var self = this; return _.contains(self.fieldsAllowed, name); }, /*Get Bnak */ getResBank: function(id) { var self= this; return _.filter(self.resBank, function(bank) { return bank.id === id; }); }, /* Select Journal Change */ getSelectedJournal: function(code) { var self = this; var currencySelect = [] var journal = self.getJournal(code).shift(); var company = self.resCompany; if (!journal) return []; if (!journal.currency) { currencySelect = self.getCurrency(company[0].currency_id[0]); }else { currencySelect = self.getCurrency(journal.currency[0]); } self.currencySelect = currencySelect; return currencySelect; }, /* verificar si existe el numero de chueque */ checkNumber: function(bankId, numberCta, number) { /** * [verificar si existe el numero de cheque en 'res.bank.paymnets' ] * :param bankId: id de banco (res_bank.id). * :param numberCta: numero de cuneta(res_bank_payments.number_cta). * :param number: numero de cheque/boleta/vale (res_bank_payments.number) * :return : [Objeto] si existe datos que satisfacer el filtro, vacio si no. */ var self = this; if (!self.bankPayments) return []; /* filtar bank Payments */ return _.filter(self.bankPayments, function(item) { return item.bankId === bankId && item.numberCta === numberCta && item.number === number; }); }, /*Obtener cajas del diario seleccionado */ getStatementeJournal: function(journalId) { var self = this; return _.filter(self.statementOpen, function(item){ return item.journalID === journalId; }); }, /* filter type bank operation */ filterTypeOperationBank: function(journal_id){ var self = this; return _.filter(self.resBankType, function(item){ return _.contains(item.journal_ids, journal_id) }); }, /* Get bank */ getTypeOperation(id){ var self = this; return _.filter(self.resBankType, function(item){ return item.id === id; }); }, /* get cheque type */ getBankChequeType: function(id){ var self= this; return _.filter(self.resBankChequeType, function(item){ return item.id === id; }); }, /* FIlter Line interest */ filterInterestLineId:function(moveId){ var self = this; return _.filter(self.interestLine, function(item) { return item.moveLineId === moveId; }); }, /** * [ showModalPayments ] */ showModalPayments: function() { var self = this; var defer =$.Deferred(); var results = true; var accountJournal = self.accountJournal; var moveSelected = []; var selectTotal = []; var currencyId = []; var idRow = []; var bankId = undefined; var numberName = 'Nº Comprobante'; var amountBalanceBank = undefined; var typePayments = undefined; accountJournal.unshift({ 'name': "", 'code': "" }); var modal = QWeb.render('eiruPaymentsInvoices.Modal',{ moveLine: self.moveLine, journal: accountJournal, }); $('.openerp_webclient_container').after(modal); $('.expired-account-modal').modal(); /* Button */ var savePaymentsButton = $('.expired-account-modal').find('.save-payments-button'); /* Table */ var tableRow = $('.expired-account-modal').find('.table-tbody').find('tr'); /* Monto total a pagar - symbol*/ var amountTotal = $('.expired-account-modal').find('.amount-total'); var amountTotalSymbol = $('.expired-account-modal').find('.amount-total-symbol'); /* fecha de operación*/ var dateOperation = $('.expired-account-modal').find('.date-operation'); /* Diario */ var journalSelect = $('.expired-account-modal').find('.journal-select'); /* Monto a pagar - symbolo */ var amountPayments = $('.expired-account-modal').find('.amount-payments'); var amountPaymentsSymbol = $('.expired-account-modal').find('.amount-payments-symbol'); /* Caja */ var statementSelect = $('.expired-account-modal').find('.statement-select'); /* Tipo de operación bancaria */ var bankTypeSelect = $('.expired-account-modal').find('.bank-type-select'); /* banking operation */ var bankingOperation = $('.expired-account-modal').find('.eiru-banking-operation'); /* Disabled paymentsAmount */ amountPayments.attr("disabled", true); /* Date Now */ dateOperation.val(moment().format("YYYY-MM-DD")); /* Desabilitar Chech de la Table */ var dateMin = undefined; var rowselect = undefined; var dateSelect = undefined; /*get currency invoices*/ var currencyInvoice = self.getCurrencySelect(self.accountInvoice[0].currency_id[0]).shift(); if (currencyInvoice) { amountTotalSymbol.text(currencyInvoice.symbol); amountPaymentsSymbol.text(currencyInvoice.symbol); } /* Disabled Chech Moveline */ _.each(tableRow, function(tr) { var ckechk = ($($(tr).children()[1]).find('.select-move-payments'))[0].checked; var dateMatu = new Date(($(tr).children()[2].textContent).trim()); if (!ckechk) { if (!dateMin) { dateMin = dateMatu.getTime(); } else { $($(tr).children()[1]).find('.select-move-payments').attr("disabled", true); } } var idMove = parseInt(($(tr).children()[0].textContent).trim()); if (!!self.interestConfig.length && self.interestConfig[0].lock_move_line) { var move = self.filterInterestLineId(idMove); var moveState = _.map(move, function(map) { return map.state }) if (_.contains(moveState, 'open')) { ($($(tr).children()[1]).find('.select-move-payments'))[0].checked = false; $($(tr).children()[1]).find('.select-move-payments').attr("disabled", true); $($(tr).children()[3]).text("Existe interés sobre la cuota vencida REF("+move[0].interest[0].name+").") } } }); /* Selecionar las cuotas */ tableRow.click(function(e) { if ($(e.target).index() !== 0) return; idRow = parseInt(($(e.target).closest('tr').children()[0].textContent).trim()); if (e.target.checked === true) { moveSelected.push(idRow); currencyId = self.getCurrencySelect(self.accountInvoice[0].currency_id[0]).shift(); if (journalSelect.val()) { currencyId = self.getSelectedJournal(journalSelect.val()).shift(); } self.movePayments = self.generateSelectTotal(moveSelected,currencyId); selectTotal = self.movePayments[0]; if (selectTotal) { /*total a pagar*/ amountTotal.val(selectTotal.amountResidualFormat); amountTotalSymbol.text(selectTotal.currencySymbol); /*Monto a pagar */ amountPayments.val(selectTotal.amountResidualFormat); amountPaymentsSymbol.text(selectTotal.currencySymbol); } dateMin = undefined; rowselect = undefined; dateSelect = undefined; _.each(tableRow, function(tr) { var dateMatu = new Date(($(tr).children()[2].textContent).trim()); var ckechk = ($($(tr).children()[1]).find('.select-move-payments'))[0].checked; if (ckechk) { if (!rowselect) { rowselect = $($(tr).children()[1]).find('.select-move-payments'); dateSelect = dateMatu.getTime(); } else { if (dateMatu.getTime() > dateSelect) { rowselect.attr("disabled", true); rowselect = $($(tr).children()[1]).find('.select-move-payments'); dateSelect = dateMatu.getTime(); } else { $($(tr).children()[1]).find('.select-move-payments').attr("disabled", true); } } } else { if (!dateMin) { dateMin = dateMatu.getTime(); var moveId = parseInt(($(tr).children()[0].textContent).trim()); $($(tr).children()[1]).find('.select-move-payments').removeAttr("disabled"); if (!!self.interestConfig.length && self.interestConfig[0].lock_move_line) { var move = self.filterInterestLineId(moveId); var moveState = _.map(move, function(map) { return map.state; }); if ((_.contains(moveState, 'open'))) { $($(tr).children()[1]).find('.select-move-payments').attr("disabled", true); } } } } }); } else { var indexRow = undefined; _.each(moveSelected, function(input, index) { if (input === idRow) { indexRow = index; } }); moveSelected.splice(indexRow,1); if (journalSelect.val()) { currencyId = self.getSelectedJournal(journalSelect.val()).shift(); }else { currencyId = self.getCurrencySelect(self.accountInvoice[0].currency_id[0]).shift(); } self.movePayments = self.generateSelectTotal(moveSelected,currencyId); selectTotal = self.movePayments[0]; if (selectTotal) { amountTotal.val(selectTotal.amountResidualFormat); amountTotalSymbol.text(selectTotal.currencySymbol); /*Monto a pagar */ amountPayments.val(selectTotal.amountResidualFormat); amountPaymentsSymbol.text(selectTotal.currencySymbol); } else { amountTotal.val(0); amountTotalSymbol.text(currencyId.symbol); /*Monto a pagar */ amountPayments.val(0); amountPaymentsSymbol.text(currencyId.symbol); } dateMin = undefined; rowselect = undefined; dateSelect = undefined; _.each(tableRow, function(tr) { var dateMatu = new Date(($(tr).children()[2].textContent).trim()); var ckechk = ($($(tr).children()[1]).find('.select-move-payments'))[0].checked; if (ckechk) { rowselect = $($(tr).children()[1]).find('.select-move-payments'); dateSelect = dateMatu.getTime(); } else { if (rowselect) rowselect.removeAttr("disabled"); if (!dateMin) { dateMin = dateMatu.getTime(); } else { $($(tr).children()[1]).find('.select-move-payments').attr("disabled", true); } } }); } }); /* Cambio en el método de pago */ journalSelect.change(function(e) { var journal = []; bankingOperation.empty(); statementSelect.val(''); if (journalSelect.val()) { journal = self.getJournal(journalSelect.val()).shift(); if (!!journal) { amountPayments.removeAttr("disabled"); /* statement Open */ if (!!self.eiruAccountBankStatementUtility.length) { if(!!self.statementConfig.length) { if (self.statementConfig[0].import_statement_payments === 'manual_import') { var statement = self.getStatementeJournal(journal.id); /* Generar lista de caja abiertas. */ $('.expired-account-modal').find('.statement-config').css('display','none'); statementSelect.empty(); if (!!statement.length) { $('.expired-account-modal').find('.statement-config').css('display','flex'); var cashboxOpen = ''; _.each(statement, function(item) { cashboxOpen += ''; }); statementSelect.append(cashboxOpen); } } } } /* Type operación bank */ typeOperationBank = self.filterTypeOperationBank(journal.id); bankTypeSelect.empty(); self.fieldsBank = []; $('.expired-account-modal').find('.type-select-bank').css('display','none'); if (!!typeOperationBank.length) { $('.expired-account-modal').find('.type-select-bank').css('display','flex'); var typeOperation = ''; _.each(typeOperationBank, function(item) { typeOperation += ''; }); bankTypeSelect.append(typeOperation); } } currencyId = self.getSelectedJournal(journalSelect.val()).shift(); } else { amountPayments.attr("disabled", true); currencyId = self.getCurrencySelect(self.accountInvoice[0].currency_id[0]).shift(); } self.movePayments = self.generateSelectTotal(moveSelected,currencyId); selectTotal = self.movePayments[0]; if (selectTotal) { /*total a pagar*/ amountTotal.val(selectTotal.amountResidualFormat); amountTotalSymbol.text(selectTotal.currencySymbol); /*Monto a pagar */ amountPayments.val(selectTotal.amountResidualFormat); amountPaymentsSymbol.text(selectTotal.currencySymbol); } }); /* Typo de pago */ bankTypeSelect.change(function(e) { bankingOperation.empty(); if (!bankTypeSelect.val()) return false; var typeBank = self.getTypeOperation(parseInt(bankTypeSelect.val())).shift(); typePayments = typeBank; var fields = ''; var cont = 0; var fieldsBank = []; _.each(typeBank.fields_allowed, function(item, index) { if (cont === 0) fields += '