123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- function ranking_customer(widget) {
- "use strict";
- var model = openerp;
- var Qweb = openerp.web.qweb;
- widget.RankingCustomer = widget.Base.extend({
- template: 'RankingCustomer',
- init: function (parent) {
- this._super(parent, {
- width: 6,
- height: 4
- });
- },
- start: function () {
- var self = this;
- self.fetchInitial();
- },
- fetchInitial: function(){
- var self = this;
- self.fetchDataSQL().then(function (DataSQL) {
- return DataSQL;
- }).then(function(DataSQL) {
- self.ResCompany = DataSQL.company;
- self.AccountInvoice = DataSQL.invoices;
- self.PosOrder = DataSQL.orders;
- self.ResPartner = DataSQL.partners;
- return self.CustomerRanking();
- });
- },
- fetchDataSQL: function() {
- var data = $.get('/dashboard-RankingCustomer');
- return data;
- },
- getPosOrder:function(id) {
- var self = this;
- return _.flatten(_.filter(self.PosOrder,function (inv) {
- return inv.customer_id === id;
- }));
- },
- getAccountInvoice:function(id, type) {
- var self = this;
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return inv.customer_id === id && inv.type == type;
- }));
- },
- CustomerRanking: function() {
- var self = this;
- var ranking = [];
- var ResPartner = self.ResPartner;
- _.each(ResPartner, function (item) {
- var PosOrder = self.getPosOrder(item.id);
- var AccountInvoice = self.getAccountInvoice(item.id,'out_invoice');
- var AccountInvoiceRefund = self.getAccountInvoice(item.id,'out_refund');
- var pos_order_amount = _.reduce(_.map(PosOrder,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- var account_invoice_amount = _.reduce(_.map(AccountInvoice,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- var account_invoice_refund_amount = _.reduce(_.map(AccountInvoiceRefund,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- var amount = (pos_order_amount + account_invoice_amount) - account_invoice_refund_amount;
- if(amount > 0 ){
- ranking.push({
- name: item.name,
- amount: amount,
- });
- }
- });
- ranking.sort(function(a, b) {
- return b.amount - a.amount
- });
- self.BuildChart(ranking);
- },
- BuildChart: function (ranking){
- var self = this;
- var label = [];
- var body = [];
- var rank = 10;
- var item;
- var CurrencyBase = self.ResCompany[0].currency_id;
- for (var i = 0; i < rank; i++) {
- if (ranking[i]){
- item = ranking[i];
- }else{
- item = {};
- item.name = "N/A";
- item.amount = 0;
- }
- label.push(item.name)
- body.push(item.amount);
- }
- var name = '.ranking-horizontalBar-chart';
- var chart = new widget.DashboardChartWidget(self);
- chart.BuildHorizontalBarChart(name,CurrencyBase,label,body);
- }
- });
- }
|