Parcourir la source

[FIX] Corrección de cálculos para multi divisa Widget cobros de hoy

adrielso il y a 7 ans
Parent
commit
099d489534
1 fichiers modifiés avec 39 ajouts et 61 suppressions
  1. 39 61
      static/src/js/widgets/voucher_today_counters.js

+ 39 - 61
static/src/js/widgets/voucher_today_counters.js

@@ -7,22 +7,25 @@ function voucher_today_counters (widget) {
         template: 'VoucherTodayCounters',
         accountVoucher: [],
         resCompany: [],
-        currencyRate: [],
+        resCurrecy: [],
 
         events: {
             'click a': 'showCustomers',
             'click h2': 'showCustomers',
         },
+
         init: function (parent) {
             this._super(parent, {
                 width : 3,
                 height: 2
             });
         },
+
         start: function () {
             var self = this;
             self.fetchInitial();
         },
+
         fetchInitial: function () {
             var self = this;
             self.$el.find('#morosidad').block({
@@ -32,6 +35,7 @@ function voucher_today_counters (widget) {
                 }
             });
             self.$el.find('.widget-content.widget-loading').css('display','flex');
+
             self.fetchAccountVoucher().then(function (accountVoucher) {
                 return accountVoucher;
             }).then(function (accountVoucher) {
@@ -39,12 +43,9 @@ function voucher_today_counters (widget) {
                 return self.fetchResCompany();
             }).then(function (resCompany) {
                 self.resCompany = resCompany;
-                return self.fetchResCurrecy(resCompany);
+                return self.fetchResCurrecy();
             }).then(function(resCurrecy){
                 self.resCurrecy = resCurrecy;
-                return self.fetchCurrency();
-            }).then(function (currencyRate) {
-                self.currencyRate = currencyRate;
                 return self.fetchVoucherCurrency();
             });
         },
@@ -52,7 +53,7 @@ function voucher_today_counters (widget) {
         fetchAccountVoucher: function () {
             var self = this;
             var defer = $.Deferred();
-            var hoy =moment().format('YYYY-MM-DD');
+            var hoy = moment().format('YYYY-MM-DD');
             var fields = ['id', 'amount', 'currency_id', 'payment_rate_currency_id'];
             var domain = [['state', '=', 'posted'], ['type', '=', 'receipt'], ['date', '=', hoy]];
             var accountVoucher = new model.web.Model('account.voucher');
@@ -60,6 +61,7 @@ function voucher_today_counters (widget) {
             accountVoucher.query(fields).filter(domain).all().then(function (results) {
                 defer.resolve(results);
             });
+
             return defer;
         },
         //Comapnia
@@ -73,73 +75,48 @@ function voucher_today_counters (widget) {
             resCompany.query(fields).filter(domain).all().then(function (results) {
                 defer.resolve(results);
             });
-            return defer;
-        },
-        // Buscar Cambio de Monedas USD,PYG,ARG,BRL
-        fetchCurrency: function () {
-            var defer = $.Deferred();
-            var currency_Rate = new model.web.Model('res.currency.rate');
-            var fields = ['id', 'name', 'currency_id', 'rate', 'create_date'];
-            var domain = [['currency_id', 'in', [166 , 20, 7, 3]]];
-
-            currency_Rate.query(fields).filter(domain).all().then(function (results) {
-                defer.resolve(results);
-            });
 
             return defer;
         },
         // Res currecy
-        fetchResCurrecy : function(company){
+        fetchResCurrecy : function(){
             var self = this;
             var defer = $.Deferred();
-
-            var id_curency = _.flatten(_.map(company,function(map){
-                return map.currency_id[0];
-            }));
-
-            var fields =['id','name', 'symbol'];
-            var domain =[['id','in',id_curency]];
+            var fields =['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
+            var domain =[['active','=', true]];
             var resCurrecy = new model.web.Model('res.currency');
 
             resCurrecy.query(fields).filter(domain).all().then(function(results){
                 defer.resolve(results);
             });
+
             return defer;
         },
         //   voucherCurrencyRate
         fetchVoucherCurrency: function () {
             var self = this;
-            var newVoucher=[];
-            var itemvoucher;
-            var itemCurrencyRate;
+            var newVoucher = [];
+            var itemVoucher;
+            var currencyVoucher;
+            var residual = 0;
+            var company = self.resCompany.shift()
+            var currencyBase = self.getCutrrency(company.currency_id[0]).shift();
 
             for (var i = 0; i < self.accountVoucher.length; i++) {
-                itemvoucher = self.accountVoucher[i];
-                itemCurrencyRate = self.getCutrrencyRate(itemvoucher.currency_id[0]);
-                if(!itemCurrencyRate){
-                    itemCurrencyRate={};
-                    itemCurrencyRate.rate=1;
+                itemVoucher = self.accountVoucher[i];
+
+                currencyVoucher = self.getCutrrency(itemVoucher.payment_rate_currency_id[0]).shift();
+                if(!currencyVoucher){
+                    currencyVoucher = {};
+                    currencyVoucher.rate_silent = currencyBase.rate_silent;
                 }
-                newVoucher.push({   amount : itemvoucher.amount,
-                                    amount_rate : (itemvoucher.amount/itemCurrencyRate.rate)
-                                });
+
+                newVoucher.push({
+                    amount : itemVoucher.amount,
+                    amount_rate : (itemVoucher.amount * (currencyBase.rate_silent / currencyVoucher.rate_silent))
+                });
             }
-            self.fetchReduceMoveLine(newVoucher)
-        },
-        getCutrrencyRate: function (currency_id) {
-            var self = this;
-            return _.filter(self.currencyRate,function (item) {
-                return item.currency_id[0] == currency_id
-            }).shift();
-        },
-        // // Reduce Move line
-        fetchReduceMoveLine: function (newVoucher) {
-            var self = this;
-            var residual =0;
-            var moneda = self.resCurrecy.shift();
-            var company = _.map(self.resCompany, function (map) {
-                return map.currency_id[1];
-            });
+
             if (newVoucher.length > 0) {
                 residual = _.reduce(_.map(newVoucher, function (map) {
                     return map.amount_rate;
@@ -148,17 +125,18 @@ function voucher_today_counters (widget) {
                 });
             }
 
-            if (moneda.id !=166){
-                residual = accounting.formatNumber(residual, 2, ".", ",");
-            }else{
-                residual = accounting.formatNumber(residual, ",", ".");
-            }
-
             self.$el.find('.widget-content.widget-loading').css('display','none');
-            self.$el.find('.widget-content').find('a').text(residual).append(' <small>'+moneda.symbol+'</small>');
-            // self.$el.find('.widget-footer').find('span').text("Monto en "+ company);
+            self.$el.find('.widget-content').find('a').text(accounting.formatMoney(residual, currencyBase.symbol, currencyBase.decimal_places, currencyBase.thousands_separator, currencyBase.decimal_separator));
             self.$el.find('#morosidad').unblock();
         },
+        // Obtener la moneda
+        getCutrrency: function (id) {
+            var self = this;
+            return _.filter(self.resCurrecy,function (item) {
+                return item.id === id;
+            });
+        },
+        // modal
         showCustomers: function (e) {
             var hoy =moment().format('YYYY-MM-DD');