|
@@ -0,0 +1,265 @@
|
|
|
+(function() {
|
|
|
+
|
|
|
+ openerp.widgetInstanceInterestInvoice = null;
|
|
|
+ openerp.parentInstanceInterestInvoice = {};
|
|
|
+ var QWeb = openerp.web.qweb;
|
|
|
+ var instanceWeb = openerp.web;
|
|
|
+
|
|
|
+ openerp.BankPaymentsInterestInvoice = openerp.Widget.extend({
|
|
|
+ template: 'eiruAccountInterest.Invoice',
|
|
|
+ id: undefined,
|
|
|
+ buttons: undefined,
|
|
|
+ accountInterest: [],
|
|
|
+ accountInterestline: [],
|
|
|
+ /* init */
|
|
|
+ init: function(parent) {
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+ /* start */
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ this.$el.click(function(){
|
|
|
+ self.fetchInitial();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* Check state*/
|
|
|
+ checkState: function(id, state) {
|
|
|
+ var self = this;
|
|
|
+ self.id = id;
|
|
|
+ },
|
|
|
+ /* Reload Page*/
|
|
|
+ reloadPage: function() {
|
|
|
+ openerp.parentInstanceInterestInvoice.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.fetchAccountInterest(self.id).then(function(accountInterest) {
|
|
|
+ return accountInterest;
|
|
|
+ }).then(function(accountInterest) {
|
|
|
+ self.accountInterest = accountInterest;
|
|
|
+ return self.fetchAccountInterestLine(self.id)
|
|
|
+ }).then(function(accountInterestline) {
|
|
|
+ self.accountInterestline = accountInterestline
|
|
|
+ return self.showModalPayments();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* Get account.interest */
|
|
|
+ fetchAccountInterest: function(id) {
|
|
|
+ var accountInterest = new instanceWeb.Model('account.interest');
|
|
|
+ return accountInterest.call('get_account_interest', [id], {
|
|
|
+ context: new instanceWeb.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* GET account.interest.line */
|
|
|
+ fetchAccountInterestLine: function(id) {
|
|
|
+ var interestLine = new openerp.web.Model('account.interest.line');
|
|
|
+ var fields = ['id','interest_id','amount_interest','date_maturity','expired_days','state'];
|
|
|
+ var domain = [['interest_id', '=', id],['state', '=', 'open']];
|
|
|
+ return interestLine.query(fields).filter(domain).order_by('date_maturity').all();
|
|
|
+ },
|
|
|
+
|
|
|
+ /* Modal */
|
|
|
+ showModalPayments: function() {
|
|
|
+ var self = this;
|
|
|
+ var line = [];
|
|
|
+ var results = true;
|
|
|
+ var defer =$.Deferred();
|
|
|
+ var interest = self.accountInterest[0];
|
|
|
+ var currency = !!interest.currency.length ? interest.currency[0] : [];
|
|
|
+ var insteresPayments = [];
|
|
|
+
|
|
|
+ _.each(self.accountInterestline, function(item){
|
|
|
+ line.push({
|
|
|
+ 'id' : item.id,
|
|
|
+ 'amount_interest': item.amount_interest,
|
|
|
+ 'amountInterestformat': instanceWeb.formatCurrency(item.amount_interest, currency),
|
|
|
+ 'date_maturity': item.date_maturity,
|
|
|
+ 'dateMaturity': moment(item.date_maturity).format("DD-MM-YYYY"),
|
|
|
+ 'expired_days': item.expired_days,
|
|
|
+ 'state': item.state,
|
|
|
+ });
|
|
|
+ })
|
|
|
+
|
|
|
+ var modal = QWeb.render('eiruAccountInterest.ModaleInvoice',{
|
|
|
+ 'line': line
|
|
|
+ });
|
|
|
+
|
|
|
+ $('.openerp_webclient_container').after(modal);
|
|
|
+ $('.expired-account-modal').modal();
|
|
|
+
|
|
|
+ var tableRow = $('.expired-account-modal').find('.table-tbody').find('tr');
|
|
|
+ /* Detalles */
|
|
|
+ var amountSymbolInterest = $('.expired-account-modal').find('.amount-symbol-interest');
|
|
|
+ var amountInterest = $('.expired-account-modal').find('.amount-interest');
|
|
|
+ var amountInterésDiscount = $('.expired-account-modal').find('.amount-interés-discount');
|
|
|
+ var amountInterésPayments = $('.expired-account-modal').find('.amount-interés-payments');
|
|
|
+ /* button Crear Factura */
|
|
|
+ var buttonAccept = $('.expired-account-modal').find('.button-accept');
|
|
|
+
|
|
|
+ amountSymbolInterest.text(currency.symbol)
|
|
|
+
|
|
|
+ tableRow.keyup(function(e) {
|
|
|
+ if (($(e.target).index() !== 0) && ($(e.target)[0].className !== 'interes-payments-discount'))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (e.key === currency.decimalSeparator && currency.decimalPlaces > 0)
|
|
|
+ return false ;
|
|
|
+
|
|
|
+ var discount = $($(e.target).closest('tr').children()[5]).find('.interes-payments-discount');
|
|
|
+ var amount = instanceWeb.unFormatCurrency( discount.val());
|
|
|
+ amount = instanceWeb.formatCurrency(amount, currency)
|
|
|
+
|
|
|
+ discount.val(amount);
|
|
|
+ discount.css('border-color','#ccc');
|
|
|
+ });
|
|
|
+
|
|
|
+ tableRow.focusout(function(e) {
|
|
|
+ if (($(e.target).index() !== 0) ||(($(e.target).index() === 0) && ($(e.target)[0].className === 'select-interes-payments')))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ var interest = ($(e.target).closest('tr').children()[4].textContent).trim();
|
|
|
+ var discount = $($(e.target).closest('tr').children()[5]).find('.interes-payments-discount');
|
|
|
+ var total = $($(e.target).closest('tr').children()[6]);
|
|
|
+
|
|
|
+ var amountInterst = instanceWeb.unFormatCurrency(interest);
|
|
|
+ var amountDiscount = instanceWeb.unFormatCurrency(discount.val());
|
|
|
+
|
|
|
+ if (amountInterst < amountDiscount) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "El monto del descuento supera el interés calculado.");
|
|
|
+ amountDiscount = 0;
|
|
|
+ discount.val(instanceWeb.formatCurrency(amountDiscount, currency));
|
|
|
+ discount.css('border-color','red');
|
|
|
+ }
|
|
|
+
|
|
|
+ idRow = parseInt(($(e.target).closest('tr').children()[0].textContent).trim());
|
|
|
+ _.each(insteresPayments, function(item) {
|
|
|
+ if (item.id === idRow)
|
|
|
+ item.discount = amountDiscount;
|
|
|
+ });
|
|
|
+
|
|
|
+ total.text(instanceWeb.formatCurrency((amountInterst - amountDiscount),currency));
|
|
|
+ self.reloadAmountInterest(insteresPayments, amountInterest, amountInterésDiscount, amountInterésPayments, currency)
|
|
|
+ });
|
|
|
+
|
|
|
+ /* Selecionar las cuotas */
|
|
|
+ tableRow.click(function(e) {
|
|
|
+
|
|
|
+ if (($(e.target).index() !== 0) ||(($(e.target).index() === 0) && ($(e.target)[0].className !== 'select-interes-payments')))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ var discount = $($(e.target).closest('tr').children()[5]).find('.interes-payments-discount');
|
|
|
+ idRow = parseInt(($(e.target).closest('tr').children()[0].textContent).trim());
|
|
|
+
|
|
|
+ if (e.target.checked === true) {
|
|
|
+ idRow = parseInt(($(e.target).closest('tr').children()[0].textContent).trim());
|
|
|
+ var interest = ($(e.target).closest('tr').children()[4].textContent).trim();
|
|
|
+
|
|
|
+ $(e.target).closest('tr').addClass('selected-interest');
|
|
|
+ discount.removeAttr("disabled");
|
|
|
+ discount.addClass('interest-discount-in');
|
|
|
+
|
|
|
+ insteresPayments.push({
|
|
|
+ 'id': idRow,
|
|
|
+ 'interes': instanceWeb.unFormatCurrency(interest),
|
|
|
+ 'discount': instanceWeb.unFormatCurrency(discount.val())
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+ var indexSplice= undefined;
|
|
|
+ _.each(insteresPayments, function(item, index){
|
|
|
+ if (item.id === idRow)
|
|
|
+ indexSplice = index
|
|
|
+ })
|
|
|
+ insteresPayments.splice(indexSplice,1);
|
|
|
+
|
|
|
+ $(e.target).closest('tr').removeClass('selected-interest');
|
|
|
+ discount.attr("disabled", true);
|
|
|
+ discount.removeClass('interest-discount-in');
|
|
|
+ discount.val(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ self.reloadAmountInterest(insteresPayments, amountInterest, amountInterésDiscount, amountInterésPayments, currency)
|
|
|
+
|
|
|
+ });
|
|
|
+ buttonAccept.click(function(e) {
|
|
|
+
|
|
|
+ if (!insteresPayments.length){
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "Debes seleccionar al menos una linea del interés, para generar la factura.");
|
|
|
+ }
|
|
|
+
|
|
|
+ self.cretaIvoiceInterest(insteresPayments).then(function(resultsInvoice) {
|
|
|
+ return resultsInvoice;
|
|
|
+ }).then(function(resultsInvoice) {
|
|
|
+ if (!resultsInvoice.state) {
|
|
|
+ results = resultsInvoice.state;
|
|
|
+ instanceWeb.notification.do_warn("Atencion", message);
|
|
|
+ }
|
|
|
+ self.reloadPage();
|
|
|
+ self.removeModal(e);
|
|
|
+ });
|
|
|
+
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+
|
|
|
+ /* Cerrar */
|
|
|
+ $('.expired-account-modal').on('hidden.bs.modal', function(e) {
|
|
|
+ results = false;
|
|
|
+ defer.resolve(results);
|
|
|
+ self.removeModal(e);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ /* */
|
|
|
+ reloadAmountInterest: function( interestSelected, interest, discount,payments, currency){
|
|
|
+ var discountAmount = _.reduce(_.map(interestSelected, function(item){return item.discount}), function(memo, num){ return memo + num; }, 0);
|
|
|
+ var interestAmount = _.reduce(_.map(interestSelected, function(item){return item.interes}), function(memo, num){ return memo + num; }, 0);
|
|
|
+ interest.val(instanceWeb.formatCurrency(interestAmount, currency));
|
|
|
+ discount.val(instanceWeb.formatCurrency(discountAmount, currency));
|
|
|
+ payments.val(instanceWeb.formatCurrency((interestAmount - discountAmount), currency));
|
|
|
+ },
|
|
|
+
|
|
|
+ /*createI Invoice */
|
|
|
+ cretaIvoiceInterest: function(insteresPayments){
|
|
|
+ var self = this;
|
|
|
+ var accountInterest = new instanceWeb.Model('account.interest');
|
|
|
+ return accountInterest.call('create_account_invoice_interest', [self.id, insteresPayments], {
|
|
|
+ context: new instanceWeb.CompoundContext()
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (openerp.web && openerp.web.FormView) {
|
|
|
+ openerp.web.FormView.include({
|
|
|
+ load_record: function(record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+
|
|
|
+ if (this.model !== 'account.interest')
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.parentInstanceInterestInvoice = this;
|
|
|
+
|
|
|
+ if (openerp.widgetInstanceInterestInvoice) {
|
|
|
+ openerp.widgetInstanceInterestInvoice.checkState(record.id, record.state);
|
|
|
+ if (this.$el.find('.button-interes-invoice').length !== 0)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.$el.find('.button-interes-invoice').length !== 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.widgetInstanceInterestInvoice = new openerp.BankPaymentsInterestInvoice(this);
|
|
|
+ var element =this.$el.find('.oe_form').find('.account-interes-invoice');
|
|
|
+
|
|
|
+ openerp.widgetInstanceInterestInvoice.appendTo(element[0]);
|
|
|
+ openerp.widgetInstanceInterestInvoice.checkState(record.id, record.state);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+})();
|