123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- function widget_balance(widget) {
- "use strict";
- var model = openerp;
- var Qweb = openerp.web.qweb;
- widget.WidgetBalanceWidget = widget.Base.extend({
- template: 'WidgetBalance',
- modules: ['point_of_sale'],
- month: 0,
- week: 0,
- today: 0,
- data: 0,
- objective: 0,
- percentage: 0,
- events: {
- 'click .number': 'showModal',
- },
- init: function (parent) {
- this._super(parent, {
- width: 3,
- height: 2
- });
- },
- start: function () {
- var self = this;
- self.fetchInitial();
- },
- checkModel : function(model){
- var self = this;
- return _.filter(self.IrModuleModule,function(item){
- return item.name === model
- });
- },
- fetchInitial: function(){
- var self = this;
- self.fecthIrModuleModule().then(function (IrModuleModule) {
- return IrModuleModule;
- }).then(function(IrModuleModule) {
- self.IrModuleModule = IrModuleModule;
- return self.fetchResUser();
- }).then(function(ResUser) {
- self.ResUser = ResUser;
- return self.fetchAccountJournal();
- }).then(function(AccountJournal) {
- self.AccountJournal = AccountJournal;
- return self.fetchAccountInvoice();
- }).then(function(AccountInvoice) {
- self.AccountInvoice = AccountInvoice;
- return self.fetchPosOrder();
- }).then(function(PosOrder) {
- self.PosOrder = PosOrder;
- return self.fetchResCompany();
- }).then(function(ResCompany) {
- self.ResCompany = ResCompany;
- return self.fetchResCurrency();
- }).then(function(ResCurrency) {
- self.ResCurrency = ResCurrency;
- return self.fetchDashboardObjetive();
- }).then(function(DashboardObjetive) {
- self.DashboardObjetive = DashboardObjetive;
- return self.showThisMonth();
- });
- },
- /*
- IR MODULES
- */
-
- fecthIrModuleModule: function(){
- var self = this;
- var defer = $.Deferred();
- var fields = ['name','id'];
- var domain=[['state','=','installed'],['name','in',self.modules]];
- var IrModuleModule = new model.web.Model('ir.module.module');
- IrModuleModule.query(fields).filter(domain).all().then(function(results){
- defer.resolve(results);
- })
- return defer;
- },
- /*
- USER
- */
- fetchResUser: function(id) {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name','store_id'];
- var domain = [['id','=',self.session.uid]];
- var ResUser = new model.web.Model('res.users');
- ResUser.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchAccountJournal: function() {
- var self = this;
- var defer = $.Deferred();
- var store_ids = self.ResUser[0].store_id[0];
- var fields = ['id', 'name', 'store_ids','type'];
- var domain = [['type','in',['sale','purchase','sale_refund']],['store_ids','in',store_ids]];
- var AccountJournal = new model.web.Model('account.journal');
- AccountJournal.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchAccountInvoice: function() {
- var self = this;
- var defer = $.Deferred();
- var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) {
- return item.id;
- }));
- var date = moment().format('YYYY-MM-01');
- var fields = ['id','type','date_invoice','amount_total','currency_id','journal_id'];
- var domain = [['state', 'not in', ['draft','cancel']],['journal_id','in',journal_ids],['date_invoice','>',date]];
- var AccountInvoice = new model.web.Model('account.invoice');
- AccountInvoice.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchPosOrder: function() {
- var self = this;
- var defer = $.Deferred();
- var modules = self.checkModel('point_of_sale');
- if (modules.length > 0){
- var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) {
- return item.id;
- }));
- var date = moment().format('YYYY-MM-01 00:00:00');
- var fields = ['id', 'name', 'date_order', 'amount_total','sale_journal'];
- var domain = [['state', 'not in', ['draft','cancel']],['sale_journal','in',journal_ids],['date_order','>',date]];
- var PosOrder = new model.web.Model('pos.order');
- PosOrder.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- }else{
- var PosOrder = [];
- return PosOrder;
- }
- },
- fetchResCompany: function() {
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name', 'currency_id'];
- var domain = [['id', '=', self.session.company_id]];
- var ResCompany = new model.web.Model('res.company');
- ResCompany.query(fields).filter(domain).all().then(function (results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchResCurrency : function(){
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
- var domain = [['active', '=', true]];
- var ResCurrency = new model.web.Model('res.currency');
- ResCurrency.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- fetchDashboardObjetive : function(){
- var self = this;
- var defer = $.Deferred();
- var store_id = self.ResUser[0].store_id[0];
- var date_min = moment().format('YYYY-MM-01');
- var date_max = moment().add(1, 'month').format('YYYY-MM-01');
- var fields = ['id','store_id', 'date', 'sale_objetive', 'purchase_limit', 'expense_limit', 'expected_profit'];
- var domain = [['date', '>=', date_min],['date', '<', date_max],['store_id','=',store_id]];
- var DashboardObjetive = new model.web.Model('dashboard.objetive');
- DashboardObjetive.query(fields).filter(domain).all().then(function(results) {
- defer.resolve(results);
- });
- return defer;
- },
- getResCurrency: function (id) {
- var self = this;
- return _.filter(self.ResCurrency,function (item) {
- return item.id === id;
- })
- },
- /*===================
- ACCOUNT INVOICE
- ===================*/
- getTodayAccountInvoice:function(type) {
- var self = this;
- var date = moment().format('YYYY-MM-DD');
- var journals = _.filter(self.AccountJournal,function (inv) {
- return inv.type == type;
- });
- if(journals.length > 0){
- var journal_ids = _.flatten(_.map(journals, function (item) {
- return item.id;
- }));
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.type == 'in_invoice';
- }));
- }
- },
- getThisWeekAccountInvoice:function(type) {
- var self = this;
- var week = moment().week();
- var journals = _.filter(self.AccountJournal,function (inv) {
- return inv.type == type;
- });
- if(journals.length > 0){
- var journal_ids = _.flatten(_.map(journals, function (item) {
- return item.id;
- }));
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY') & _.contains(journal_ids, inv.journal_id[0]);
- }));
- }
- },
- getThisMonthAccountInvoice:function(type) {
- var self = this;
- var journals = _.filter(self.AccountJournal,function (inv) {
- return inv.type == type;
- });
- if(journals.length > 0){
- var journal_ids = _.flatten(_.map(journals, function (item) {
- return item.id;
- }));
- return _.flatten(_.filter(self.AccountInvoice,function (inv) {
- return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM') & _.contains(journal_ids, inv.journal_id[0]);
- }));
- }
- },
- /*=============
- POS ORDER
- =============*/
-
- getTodayPosOrder:function() {
- var self = this;
- var date = moment().format('YYYY-MM-DD');
- return _.flatten(_.filter(self.PosOrder,function (inv) {
- var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
- return moment(utc._d).format('YYYY-MM-DD') === date;
- }));
- },
- getThisWeekPosOrder:function() {
- var self = this;
- var week = moment().week();
- return _.flatten(_.filter(self.PosOrder,function (inv) {
- var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
- utc = moment(utc._d).format('YYYY-MM-DD');
- return moment(utc).week() === week & moment(utc).format('YYYY')=== moment().format('YYYY');
- }));
- },
- getThisMonthPosOrder:function() {
- var self = this;
- return _.flatten(_.filter(self.PosOrder,function (inv) {
- var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
- return moment(utc._d).format('YYYY-MM') === moment().format('YYYY-MM');
- }));
- },
- /*
- TODAY
- */
- showToday: function () {
- var self = this;
- var amount_order = 0;
- var sale_invoice_amount = 0;
- var sale_invoice_refund_amount = 0;
- var purchase_invoice_amount= 0;
- var balance = 0;
- var array = [];
- var order = self.getTodayPosOrder();
-
- var sale_invoice = self.getTodayAccountInvoice('sale');
- var sale_invoice_refund = self.getTodayAccountInvoice('sale_refund');
- var purchase_invoice = self.getTodayAccountInvoice('purchase');
- var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
- if(order.length > 0){
- amount_order = _.reduce(_.map(order, function (map) {
- return map.amount_total;
- }), function (memo, num) {
- return memo + num;
- });
- }
-
- /*
- SALE INVOICE
- */
-
- if(sale_invoice.length > 0){
- array = [];
- _.each(sale_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- SALE INVOICE REFUND
- */
-
- if(sale_invoice_refund.length > 0){
- array = [];
- _.each(sale_invoice_refund, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- PURCHASE INVOICE
- */
-
- if(purchase_invoice.length > 0){
- array = [];
- _.each(purchase_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- purchase_invoice_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
-
- self.today = balance;
- },
- /*
- WEEK
- */
- showThisWeek: function () {
- var self = this;
- var amount_order = 0;
- var sale_invoice_amount = 0;
- var sale_invoice_refund_amount = 0;
- var purchase_invoice_amount= 0;
- var balance = 0;
- var array = [];
- var order = self.getThisWeekPosOrder();
-
- var sale_invoice = self.getThisWeekAccountInvoice('sale');
- var sale_invoice_refund = self.getThisWeekAccountInvoice('sale_refund');
- var purchase_invoice = self.getThisWeekAccountInvoice('purchase');
-
- var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
- if(order.length > 0){
- amount_order = _.reduce(_.map(order, function (map) {
- return map.amount_total;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- SALE INVOICE
- */
-
- if(sale_invoice.length > 0){
- array = [];
- _.each(sale_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- SALE INVOICE REFUND
- */
-
- if(sale_invoice_refund.length > 0){
- array = [];
- _.each(sale_invoice_refund, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- PURCHASE INVOICE
- */
-
- if(purchase_invoice.length > 0){
- array = [];
- _.each(purchase_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- purchase_invoice_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
- self.week = balance;
- },
- /*
- MONTH
- */
- showThisMonth: function () {
- var self = this;
- if(self.DashboardObjetive.length > 0){
- self.objective = self.DashboardObjetive.shift().expected_profit;
- }else{
- self.objective = 0;
- }
- var amount_order = 0;
- var sale_invoice_amount = 0;
- var sale_invoice_refund_amount = 0;
- var purchase_invoice_amount = 0;
- var balance = 0;
- var array = [];
- var out_array = [];
- var order = self.getThisMonthPosOrder();
-
- var sale_invoice = self.getThisMonthAccountInvoice('sale');
- var sale_invoice_refund = self.getThisMonthAccountInvoice('sale_refund');
- var purchase_invoice = self.getThisMonthAccountInvoice('purchase');
- var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
-
- if(order.length > 0){
- amount_order = _.reduce(_.map(order, function (map) {
- return map.amount_total;
- }), function (memo, num) {
- return memo + num;
- });
- }
-
- /*
- SALE INVOICE
- */
-
- if(sale_invoice.length > 0){
- array = [];
- _.each(sale_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- SALE INVOICE REFUND
- */
-
- if(sale_invoice_refund.length > 0){
- array = [];
- _.each(sale_invoice_refund, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- /*
- PURCHASE INVOICE
- */
-
- if(purchase_invoice.length > 0){
- _.each(purchase_invoice, function (item) {
- var currency = self.getResCurrency(item.currency_id[0]).shift();
- out_array.push({
- amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
- })
- });
- purchase_invoice_amount = _.reduce(_.map(out_array, function (map) {
- return map.amount;
- }), function (memo, num) {
- return memo + num;
- });
- }
- balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
- self.month = balance;
- if(self.objective > 0){
- var percentage = (balance*100)/self.objective;
- }else{
- var percentage = 0;
- }
- $("#circle-balance").circliful({
- animationStep: 10,
- foregroundBorderWidth: 15,
- backgroundBorderWidth: 1,
- percent: percentage,
- fontColor: '#0288d1',
- foregroundColor: '#0288d1',
- percentageTextSize: 35,
- });
- self.percentage = percentage;
-
- self.$el.find('.widget-content').find('a').text(accounting.formatMoney(balance, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
- },
- /*
- MODAL
- */
- showModal: function (e) {
- var self = this;
- self.showThisWeek();
- self.showToday();
- var titleData = [
- {
- title: "Balance"
- }
- ];
- var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
- var modal = Qweb.render('WidgetModal', {
- modalTitle: titleData,
- objective: accounting.formatMoney(self.objective, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
- today: accounting.formatMoney(self.today, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
- week: accounting.formatMoney(self.week, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
- month: accounting.formatMoney(self.month, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
- });
- $('.openerp_webclient_container').after(modal);
- $('.widget-modal').modal()
- $('.widget-modal').on('hidden.bs.modal', function (e) {
- self.removeModal(e);
- })
- $("#myItemModal").circliful({
- animationStep: 10,
- foregroundBorderWidth: 15,
- backgroundBorderWidth: 1,
- percent: self.percentage,
- fontColor: '#0288d1',
- foregroundColor: '#0288d1',
- percentageTextSize: 35,
- });
- },
- removeModal: function (e) {
- $('.widget-modal').remove();
- $('.modal-backdrop').remove();
- },
- });
- }
|