|
@@ -0,0 +1,284 @@
|
|
|
+function widget_ranking_morosos(widget) {
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var Qweb = openerp.web.qweb;
|
|
|
+ var model = openerp;
|
|
|
+
|
|
|
+ widget.RankingMorosos = widget.Base.extend({
|
|
|
+ template: 'RankingMorosos',
|
|
|
+
|
|
|
+ events:{
|
|
|
+ 'change #current-state' : 'UpdateChart',
|
|
|
+ },
|
|
|
+
|
|
|
+ init: function (parent) {
|
|
|
+ this._super(parent, {
|
|
|
+ width: 12,
|
|
|
+ height: 8,
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ self.fetchInitial();
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInitial: function(){
|
|
|
+ var self = this;
|
|
|
+ self.fetchDataSQL().then(function (DataSQL) {
|
|
|
+ return DataSQL;
|
|
|
+ }).then(function(DataSQL) {
|
|
|
+ self.ResPartner = DataSQL.partner;
|
|
|
+ self.ResCompany = DataSQL.company;
|
|
|
+ self.AccountInvoice = DataSQL.invoices;
|
|
|
+ return self.fetchAccountMoveLine();
|
|
|
+ }).then(function(AccountMoveLine){
|
|
|
+ self.AccountMoveLine = AccountMoveLine;
|
|
|
+ return self.MorososRanking();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchDataSQL: function() {
|
|
|
+ var data = $.get('/dashboard-RankingMorosos');
|
|
|
+ return data;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchAccountMoveLine: function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name', 'move_id', 'debit', 'credit', 'date_maturity','reconcile_ref','partner_id','amount_residual'];
|
|
|
+ var invoice_numbers = _.flatten(_.map(self.AccountInvoice, function(item){
|
|
|
+ return item.number;
|
|
|
+ }));
|
|
|
+ var domain = [
|
|
|
+ ['move_id','in',invoice_numbers],
|
|
|
+ ['debit','>',0],
|
|
|
+ ];
|
|
|
+
|
|
|
+ var AccountMoveLine = new model.web.Model('account.move.line');
|
|
|
+ AccountMoveLine.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ getAccountInvoice:function(id) {
|
|
|
+ var self = this;
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return inv.customer_id == id && inv.type == 'out_invoice';
|
|
|
+ }));
|
|
|
+ },
|
|
|
+
|
|
|
+ getResPartner:function() {
|
|
|
+ var self = this;
|
|
|
+ return _.flatten(_.filter(self.ResPartner,function (partner) {
|
|
|
+ return partner.active == true;
|
|
|
+ }));
|
|
|
+ },
|
|
|
+
|
|
|
+ getAccountMoveLine: function(number){
|
|
|
+ var self = this;
|
|
|
+ var AccountMoveLine = _.filter(self.AccountMoveLine,function(item){
|
|
|
+ return item.move_id[1] == number & item.debit > 0;
|
|
|
+ });
|
|
|
+
|
|
|
+ var x = AccountMoveLine.length;
|
|
|
+ var i = AccountMoveLine.length;
|
|
|
+ var content =_.map(AccountMoveLine, function(item2){
|
|
|
+ if(item2.date_maturity != false){
|
|
|
+ item2.dues_name= 'Cuota ' + i +' / ' + x;
|
|
|
+ i--;
|
|
|
+ return item2;
|
|
|
+ };
|
|
|
+ });
|
|
|
+ content = _.filter(content,function(item){
|
|
|
+ return item != undefined;
|
|
|
+ })
|
|
|
+ return content;
|
|
|
+ },
|
|
|
+
|
|
|
+ MorososRanking: function(){
|
|
|
+ var self = this;
|
|
|
+ var data = [];
|
|
|
+ var state = $('#current-state').val();
|
|
|
+ var ResPartner = self.getResPartner();
|
|
|
+ _.each(ResPartner, function(item){
|
|
|
+ var info = [];
|
|
|
+ var due_qty=0;
|
|
|
+ var amount=0;
|
|
|
+ var AccountInvoice = self.getAccountInvoice(item.id);
|
|
|
+ _.each(AccountInvoice, function(item){
|
|
|
+ var AccountMoveLine = self.getAccountMoveLine(item.number);
|
|
|
+ var Company = self.ResCompany[0];
|
|
|
+ _.each(AccountMoveLine, function(index){
|
|
|
+ var date;
|
|
|
+ var state = 'Vencido';
|
|
|
+ if(index.reconcile_ref){
|
|
|
+ if(index.amount_residual == 0){
|
|
|
+ state = 'Pagado';
|
|
|
+ }
|
|
|
+ if(index.amount_residual > 0){
|
|
|
+ state = 'Amortizado';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ date = moment(index.date_maturity,'YYYY-MM-DD').format('DD/MM/YYYY');
|
|
|
+ if(state != 'Pagado'){
|
|
|
+ if(index.date_maturity < moment().format('YYYY-MM-DD')){
|
|
|
+ due_qty++;
|
|
|
+ var days_of_delays = moment().diff(moment(index.date_maturity), 'days');
|
|
|
+ amount = amount + index.amount_residual;
|
|
|
+ info.push({
|
|
|
+ invoice_id : item.id,
|
|
|
+ invoice : item.number,
|
|
|
+ partner_id : item.customer_id,
|
|
|
+ date : date,
|
|
|
+ name : index.dues_name,
|
|
|
+ state : state,
|
|
|
+ days_of_delays: days_of_delays,
|
|
|
+ });
|
|
|
+ };
|
|
|
+ };
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ if(info.length > 0){
|
|
|
+ var max_days_of_delay= info.reduce((max, p) => p.days_of_delays > max ? p.days_of_delays : max, info[0].days_of_delays);
|
|
|
+ data.push({
|
|
|
+ partner_name : item.name,
|
|
|
+ due_qty:due_qty,
|
|
|
+ max_days_of_delay: max_days_of_delay ? max_days_of_delay : 0,
|
|
|
+ amount: amount,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.content = data;
|
|
|
+ self.BuildChart(data);
|
|
|
+ },
|
|
|
+
|
|
|
+ BuildChart: function (ranking){
|
|
|
+ var self = this;
|
|
|
+ var state = $('#current-state').val();
|
|
|
+ var label = [];
|
|
|
+ var body = [];
|
|
|
+ var item;
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+
|
|
|
+ ranking.sort(function(a, b){
|
|
|
+ if(state == 'day_of_delay'){
|
|
|
+ var x = a.max_days_of_delay;
|
|
|
+ var y = b.max_days_of_delay;
|
|
|
+ };
|
|
|
+ if(state == 'due_qty'){
|
|
|
+ var x = a.due_qty;
|
|
|
+ var y = b.due_qty;
|
|
|
+ }
|
|
|
+ if(state == 'amount_delay'){
|
|
|
+ var x = a.amount;
|
|
|
+ var y = b.amount;
|
|
|
+ };
|
|
|
+ if (x > y) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (x < y) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ });
|
|
|
+
|
|
|
+ for (var i = 0; i < 20; i++) {
|
|
|
+ if (ranking[i]){
|
|
|
+ item = ranking[i];
|
|
|
+ }else{
|
|
|
+ item = {};
|
|
|
+ item.partner_name = "N/A";
|
|
|
+ item.max_days_of_delay=0;
|
|
|
+ item.due_qty=0;
|
|
|
+ item.amount_=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ label.push(item.partner_name.trim());
|
|
|
+ if(state == 'day_of_delay'){
|
|
|
+ body.push(item.max_days_of_delay);
|
|
|
+ CurrencyBase.label = "Días atrasados: ";
|
|
|
+ };
|
|
|
+ if(state == 'due_qty'){
|
|
|
+ body.push(item.due_qty);
|
|
|
+ CurrencyBase.label = "Cuotas vencidas: ";
|
|
|
+ }
|
|
|
+ if(state == 'amount_delay'){
|
|
|
+ CurrencyBase.label = CurrencyBase.symbol;
|
|
|
+ body.push(item.amount);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ var name = '.ranking-morosos-chart';
|
|
|
+ var chart = new widget.DashboardChartWidget3(self);
|
|
|
+ chart.BuildBarChart3(name,label,body,CurrencyBase);
|
|
|
+ },
|
|
|
+
|
|
|
+ UpdateChart: function (){
|
|
|
+ var self = this;
|
|
|
+ var state = $('#current-state').val();
|
|
|
+ var label = [];
|
|
|
+ var body = [];
|
|
|
+ var item;
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+ var ranking = self.content;
|
|
|
+
|
|
|
+ ranking.sort(function(a, b){
|
|
|
+ if(state == 'day_of_delay'){
|
|
|
+ var x = a.max_days_of_delay;
|
|
|
+ var y = b.max_days_of_delay;
|
|
|
+ };
|
|
|
+ if(state == 'due_qty'){
|
|
|
+ var x = a.due_qty;
|
|
|
+ var y = b.due_qty;
|
|
|
+ }
|
|
|
+ if(state == 'amount_delay'){
|
|
|
+ var x = a.amount;
|
|
|
+ var y = b.amount;
|
|
|
+ };
|
|
|
+ if (x > y) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (x < y) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ });
|
|
|
+
|
|
|
+ for (var i = 0; i < 20; i++) {
|
|
|
+ if (ranking[i]){
|
|
|
+ item = ranking[i];
|
|
|
+ }else{
|
|
|
+ item = {};
|
|
|
+ item.partner_name = "N/A";
|
|
|
+ item.max_days_of_delay=0;
|
|
|
+ item.due_qty=0;
|
|
|
+ item.amount=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ label.push(item.partner_name.trim());
|
|
|
+ if(state == 'day_of_delay'){
|
|
|
+ body.push(item.max_days_of_delay);
|
|
|
+ CurrencyBase.label = "Días atrasados: ";
|
|
|
+ };
|
|
|
+ if(state == 'due_qty'){
|
|
|
+ body.push(item.due_qty);
|
|
|
+ CurrencyBase.label = "Cuotas vencidas: ";
|
|
|
+ }
|
|
|
+ if(state == 'amount_delay'){
|
|
|
+ CurrencyBase.label = CurrencyBase.symbol;
|
|
|
+ body.push(item.amount);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ $(".ranking-morosos-chart").remove();
|
|
|
+ $(".chart-container-bienstar").html('<canvas class="ranking-morosos-chart" style="padding-top:10px;width:100%"></canvas>');
|
|
|
+
|
|
|
+ var name = '.ranking-morosos-chart';
|
|
|
+ var chart = new widget.DashboardChartWidget3(self);
|
|
|
+ chart.BuildBarChart3(name,label,body,CurrencyBase);
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|