|
@@ -0,0 +1,359 @@
|
|
|
+(function() {
|
|
|
+
|
|
|
+ openerp.widgetInstancePaymentTermPurchase = null;
|
|
|
+ openerp.parentInstancePaymentTermPurchase = null;
|
|
|
+ var Qweb = openerp.web.qweb;
|
|
|
+ var instance = openerp;
|
|
|
+ var instanceWeb = openerp.web;
|
|
|
+
|
|
|
+ openerp.PaymentTermPurchase = instance.Widget.extend({
|
|
|
+ template: 'eiruPaymentTerm.Purchase',
|
|
|
+
|
|
|
+ id: undefined,
|
|
|
+ order: [],
|
|
|
+ calculatorTerm: [],
|
|
|
+ termType: [],
|
|
|
+
|
|
|
+ /************
|
|
|
+ | INIT |
|
|
|
+ ************/
|
|
|
+ init: function(parent) {
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+
|
|
|
+ /************
|
|
|
+ | START |
|
|
|
+ ************/
|
|
|
+ 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');
|
|
|
+ /* CREAR */
|
|
|
+ if (e.target.accessKey === 'C')
|
|
|
+ self.$el.css('display','none');
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /*===============
|
|
|
+ | ID ORDER |
|
|
|
+ ===============*/
|
|
|
+ updateId: function(id) {
|
|
|
+ var self = this;
|
|
|
+ self.id = id;
|
|
|
+
|
|
|
+ self.$el.css('display','flex');
|
|
|
+ if (!id)
|
|
|
+ self.$el.css('display','none');
|
|
|
+ },
|
|
|
+
|
|
|
+ /*=====================
|
|
|
+ | Remover Modal |
|
|
|
+ =====================*/
|
|
|
+ removeModal: function() {
|
|
|
+ $('.expired-account-modal').remove();
|
|
|
+ $('.modal-backdrop').remove();
|
|
|
+ },
|
|
|
+
|
|
|
+ /*===============================
|
|
|
+ | Recargar la Pagina |
|
|
|
+ ===============================*/
|
|
|
+ reloadPage: function() {
|
|
|
+ openerp.parentInstancePaymentTermPurchase.reload();
|
|
|
+ },
|
|
|
+
|
|
|
+ /******************************
|
|
|
+ || Metodo Inicial ||
|
|
|
+ ******************************/
|
|
|
+ fectchInitial: function() {
|
|
|
+ var self = this;
|
|
|
+
|
|
|
+ self.fetchPurchaseOrder(self.id).then(function(order) {
|
|
|
+ return order;
|
|
|
+ }).then(function(order) {
|
|
|
+ self.order = order;
|
|
|
+ return self.fetchTypeTerm();
|
|
|
+ }).then(function(termType) {
|
|
|
+ self.termType = termType;
|
|
|
+ return self.showTaskSelected();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /*===============================
|
|
|
+ | GET Purchase Order |
|
|
|
+ ===============================*/
|
|
|
+ fetchPurchaseOrder: function(id) {
|
|
|
+ var order = new openerp.web.Model('purchase.order');
|
|
|
+ return order.call('getPurchaseOrder',[id],{
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /*=========================================
|
|
|
+ | GET Tipo de condicion de pagos |
|
|
|
+ =========================================*/
|
|
|
+ fetchTypeTerm: function() {
|
|
|
+ var typeTerm = new openerp.web.Model('account.payment.term.type');
|
|
|
+ return typeTerm.query(['id','name']).all()
|
|
|
+ },
|
|
|
+
|
|
|
+ /*===================
|
|
|
+ | Modal |
|
|
|
+ ===================*/
|
|
|
+ showTaskSelected: function() {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var state = true;
|
|
|
+ /*Curency Order*/
|
|
|
+ var saleOrder = self.order[0];
|
|
|
+ var currencyOrder = saleOrder.currency;
|
|
|
+ /*term type */
|
|
|
+ var typeTerm = self.termType;
|
|
|
+
|
|
|
+ if (!typeTerm.length){
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "No existe tipo de condiciones de pagos.");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ typeTerm.unshift({"id": '', 'name': ''})
|
|
|
+
|
|
|
+ /* Modal */
|
|
|
+ var modal = Qweb.render('eiruPaymentTerm.ModalConfigurator', {'termTypes':typeTerm});
|
|
|
+ $('.openerp_webclient_container').after(modal);
|
|
|
+ $('.expired-account-modal').modal();
|
|
|
+
|
|
|
+ var amountSale = $('.expired-account-modal').find('.amount-sale');
|
|
|
+ var symbolCurrency = $('.expired-account-modal').find('.symbol-currency');
|
|
|
+
|
|
|
+ var initialpayments = $('.expired-account-modal').find('.initial-payments-amount');
|
|
|
+ var amountResidual = $('.expired-account-modal').find('.amount-residual');
|
|
|
+
|
|
|
+ var dateSale = $('.expired-account-modal').find('.date-sale');
|
|
|
+ var dateQuota = $('.expired-account-modal').find('.date-quota');
|
|
|
+ var typeTerm = $('.expired-account-modal').find('.term-select');
|
|
|
+ var amountQuota = $('.expired-account-modal').find('.amount-quota');
|
|
|
+ /* Bottom Calcular */
|
|
|
+ var bottomCalcular = $('.expired-account-modal').find('.bt-calular');
|
|
|
+ /* table */
|
|
|
+ var tableRow = $('.expired-account-modal').find('.table-tbody');
|
|
|
+ /*Bottom Guardar */
|
|
|
+ buttonAccept = $('.expired-account-modal').find('.button-accept');
|
|
|
+ /* Value initial */
|
|
|
+ amountSale.val(instanceWeb.formatCurrency(saleOrder.amountTotal, currencyOrder));
|
|
|
+ amountResidual.val(instanceWeb.formatCurrency(saleOrder.amountTotal, currencyOrder));
|
|
|
+
|
|
|
+ symbolCurrency.text(currencyOrder.symbol);
|
|
|
+ dateSale.val(moment(saleOrder.dateOrder).format('DD/MM/YYYY'));
|
|
|
+ dateQuota.val(moment(saleOrder.dateQuota).format('DD/MM/YYYY'));
|
|
|
+
|
|
|
+ /*******************************
|
|
|
+ * FOCUSIN initialpayments *
|
|
|
+ *******************************/
|
|
|
+ initialpayments.focusin(function(e){
|
|
|
+ var amount = instanceWeb.unFormatCurrency(initialpayments.val());
|
|
|
+ if (amount === 0)
|
|
|
+ initialpayments.val('');
|
|
|
+ });
|
|
|
+ /****************************
|
|
|
+ * KEYUP initialpayments *
|
|
|
+ ****************************/
|
|
|
+ initialpayments.keyup(function(e) {
|
|
|
+ var amountPayments = instanceWeb.unFormatCurrency(initialpayments.val());
|
|
|
+ var saleAmount = instanceWeb.unFormatCurrency(amountSale.val());
|
|
|
+ if (e.key === currencyOrder.decimalSeparator && currencyOrder.decimalPlaces > 0)
|
|
|
+ return false ;
|
|
|
+
|
|
|
+ initialpayments.val(instanceWeb.formatCurrency(amountPayments, currencyOrder));
|
|
|
+ amountResidual.val(instanceWeb.formatCurrency((saleAmount - amountPayments), currencyOrder))
|
|
|
+
|
|
|
+ });
|
|
|
+ /********************************
|
|
|
+ * FOCUS OUT initialpayments *
|
|
|
+ ********************************/
|
|
|
+ initialpayments.focusout(function(e) {
|
|
|
+ var saleAmount = instanceWeb.unFormatCurrency(amountSale.val());
|
|
|
+ var amount = instanceWeb.unFormatCurrency(initialpayments.val());
|
|
|
+
|
|
|
+ if (amount > saleAmount) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "El valor de la entrega supera el valor de la venta.");
|
|
|
+ initialpayments.focus();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ amountResidual.val(instanceWeb.formatCurrency((saleAmount - amount), currencyOrder))
|
|
|
+ });
|
|
|
+
|
|
|
+ /*--------------------------
|
|
|
+ FOCUSIN amountQuota
|
|
|
+ ----------------------------*/
|
|
|
+ amountQuota.focusin(function(e) {
|
|
|
+ var amount = instanceWeb.unFormatCurrency( amountQuota.val());
|
|
|
+ if (amount === 0)
|
|
|
+ amountQuota.val('');
|
|
|
+ });
|
|
|
+ /*-----------------------
|
|
|
+ KEYUP amountQuota
|
|
|
+ ------------------------*/
|
|
|
+ amountQuota.keyup(function(e) {
|
|
|
+ if (e.key === currencyOrder.decimalSeparator && currencyOrder.decimalPlaces > 0)
|
|
|
+ return false ;
|
|
|
+
|
|
|
+ cuotaAmount = instanceWeb.unFormatCurrency(amountQuota.val());
|
|
|
+ amountQuota.val(instanceWeb.formatCurrency(cuotaAmount, currencyOrder));
|
|
|
+
|
|
|
+ });
|
|
|
+ /*---------------------------
|
|
|
+ FOCUS OUT amountQuota
|
|
|
+ ----------------------------*/
|
|
|
+ amountQuota.focusout(function(e) {
|
|
|
+ var saleAmount = instanceWeb.unFormatCurrency(amountSale.val());
|
|
|
+ var amountPayments = instanceWeb.unFormatCurrency(initialpayments.val());
|
|
|
+ var amount = instanceWeb.unFormatCurrency(amountQuota.val());
|
|
|
+
|
|
|
+ if (amount <= 0) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "El valor de la cuota debe de ser mayor que cero.");
|
|
|
+ amountQuota.focus();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (amount > (saleAmount - amountPayments)) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "El valor de la cuota no puede superar el valor total de la venta.\nVenta ="+saleAmount+"\nEntrega ="+amountPayments+"\nCuota = "+amount);
|
|
|
+ amountQuota.focus();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /*-------------------
|
|
|
+ CLICK CALCULAR
|
|
|
+ ---------------------*/
|
|
|
+ bottomCalcular.click(function(e) {
|
|
|
+ tableRow.find('tr').remove();
|
|
|
+ var htmlLine = '';
|
|
|
+ var cuotaAmount = instanceWeb.unFormatCurrency(amountQuota.val());
|
|
|
+ var amountPayments = instanceWeb.unFormatCurrency(initialpayments.val());
|
|
|
+ /* */
|
|
|
+ if(!typeTerm.val()) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "Debes de Seleccionar el tipo de plazo de pago.");
|
|
|
+ typeTerm.focus();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /* */
|
|
|
+ if (cuotaAmount <= 0) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "El valor de la cuota debe de ser mayor que cero.");
|
|
|
+ amountQuota.focus();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var termSale = {
|
|
|
+ 'orderId': self.id,
|
|
|
+ 'typeTerm': parseInt(typeTerm.val().trim()),
|
|
|
+ 'amountCuota': cuotaAmount,
|
|
|
+ 'amountPayments': amountPayments,
|
|
|
+ }
|
|
|
+
|
|
|
+ /* GENERAR LA LISTA DE CUOTA */
|
|
|
+ $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
|
|
|
+ self.calculatePaymentTerm(termSale).then(function(calculatorTerm) {
|
|
|
+ return calculatorTerm;
|
|
|
+ }).then(function(calculatorTerm) {
|
|
|
+ self.calculatorTerm = calculatorTerm;
|
|
|
+ $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
|
|
|
+ if (!!calculatorTerm.length) {
|
|
|
+ _.each(calculatorTerm, function(line) {
|
|
|
+ var dateMaturity = moment(line.date).format('DD/MM/YYYY');
|
|
|
+ var amount = instanceWeb.formatCurrency(line.amount, currencyOrder) +" "+currencyOrder.symbol;
|
|
|
+ htmlLine +='<tr><td><span>'+line.cuotaNumber+'</span></td><td><span>'+dateMaturity+'</span></td><td><span>'+amount+'</span></td></tr>';
|
|
|
+ })
|
|
|
+ tableRow.append(htmlLine);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ /*----------------------
|
|
|
+ ACEPTAR / GUARDAR
|
|
|
+ ----------------------*/
|
|
|
+ buttonAccept.click(function(e) {
|
|
|
+ if (!self.calculatorTerm.length) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "Debes de calcular las cuota antes de guardar.");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
|
|
|
+ self.accountPaymentTermConfigurator(self.calculatorTerm, self.id).then(function(termConfigurator) {
|
|
|
+ return termConfigurator;
|
|
|
+ }).then(function(termConfigurator) {
|
|
|
+ $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
|
|
|
+ self.removeModal(e);
|
|
|
+ state = termConfigurator.state;
|
|
|
+ self.reloadPage();
|
|
|
+ });
|
|
|
+
|
|
|
+ defer.resolve(state);
|
|
|
+ });
|
|
|
+ /*-----------
|
|
|
+ CERRAR
|
|
|
+ -----------*/
|
|
|
+ $('.expired-account-modal').on('hidden.bs.modal', function (e) {
|
|
|
+ defer.resolve(false);
|
|
|
+ self.removeModal(e);
|
|
|
+ });
|
|
|
+
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ /*----------------------
|
|
|
+ CALCULATOR TERM
|
|
|
+ ----------------------*/
|
|
|
+ calculatePaymentTerm: function(values) {
|
|
|
+ var order = new instance.web.Model('purchase.order');
|
|
|
+ return order.call('calculatePaymentTermPurchaseOrder',[values], {
|
|
|
+ context: new instance.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* --------------------------------------
|
|
|
+ Create / writer - Term Paymen
|
|
|
+ ---------------------------------------- */
|
|
|
+ accountPaymentTermConfigurator: function(values, id){
|
|
|
+ var term = new instance.web.Model('purchase.order');
|
|
|
+ return term.call('accountPaymentTermConfigurator',[values, id], {
|
|
|
+ context: new instance.web.CompoundContext()
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_record: function(record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+
|
|
|
+ if (this.model !== 'purchase.order')
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.parentInstancePaymentTermPurchase = this;
|
|
|
+
|
|
|
+ if (openerp.widgetInstancePaymentTermPurchase) {
|
|
|
+ openerp.widgetInstancePaymentTermPurchase.updateId(record.id);
|
|
|
+ if (this.$el.find('.button-term-configurator-purchase').length !== 0){
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.$el.find('.button-term-configurator-purchase').length !== 0 )
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.widgetInstancePaymentTermPurchase = new openerp.PaymentTermPurchase(this);
|
|
|
+ var elemento = this.$el.find('.oe_form').find('.purchase-payment-term-configurator');
|
|
|
+
|
|
|
+ openerp.widgetInstancePaymentTermPurchase.appendTo(elemento);
|
|
|
+ openerp.widgetInstancePaymentTermPurchase.updateId(record.id);
|
|
|
+ },
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+})();
|