widget_balance.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. function widget_balance(widget) {
  2. "use strict";
  3. var model = openerp;
  4. var Qweb = openerp.web.qweb;
  5. widget.WidgetBalanceWidget = widget.Base.extend({
  6. template: 'WidgetBalance',
  7. modules: ['point_of_sale'],
  8. month: 0,
  9. week: 0,
  10. today: 0,
  11. data: 0,
  12. objective: 0,
  13. percentage: 0,
  14. events: {
  15. 'click .number': 'showModal',
  16. },
  17. init: function (parent) {
  18. this._super(parent, {
  19. width: 3,
  20. height: 2
  21. });
  22. },
  23. start: function () {
  24. var self = this;
  25. self.fetchInitial();
  26. },
  27. checkModel : function(model){
  28. var self = this;
  29. return _.filter(self.IrModuleModule,function(item){
  30. return item.name === model
  31. });
  32. },
  33. fetchInitial: function(){
  34. var self = this;
  35. self.fecthIrModuleModule().then(function (IrModuleModule) {
  36. return IrModuleModule;
  37. }).then(function(IrModuleModule) {
  38. self.IrModuleModule = IrModuleModule;
  39. return self.fetchResUser();
  40. }).then(function(ResUser) {
  41. self.ResUser = ResUser;
  42. return self.fetchAccountJournal();
  43. }).then(function(AccountJournal) {
  44. self.AccountJournal = AccountJournal;
  45. return self.fetchAccountInvoice();
  46. }).then(function(AccountInvoice) {
  47. self.AccountInvoice = AccountInvoice;
  48. return self.fetchPosOrder();
  49. }).then(function(PosOrder) {
  50. self.PosOrder = PosOrder;
  51. return self.fetchResCompany();
  52. }).then(function(ResCompany) {
  53. self.ResCompany = ResCompany;
  54. return self.fetchResCurrency();
  55. }).then(function(ResCurrency) {
  56. self.ResCurrency = ResCurrency;
  57. return self.fetchDashboardObjetive();
  58. }).then(function(DashboardObjetive) {
  59. self.DashboardObjetive = DashboardObjetive;
  60. return self.showThisMonth();
  61. });
  62. },
  63. /*
  64. IR MODULES
  65. */
  66. fecthIrModuleModule: function(){
  67. var self = this;
  68. var defer = $.Deferred();
  69. var fields = ['name','id'];
  70. var domain=[['state','=','installed'],['name','in',self.modules]];
  71. var IrModuleModule = new model.web.Model('ir.module.module');
  72. IrModuleModule.query(fields).filter(domain).all().then(function(results){
  73. defer.resolve(results);
  74. })
  75. return defer;
  76. },
  77. /*
  78. USER
  79. */
  80. fetchResUser: function(id) {
  81. var self = this;
  82. var defer = $.Deferred();
  83. var fields = ['id','name','store_id'];
  84. var domain = [['id','=',self.session.uid]];
  85. var ResUser = new model.web.Model('res.users');
  86. ResUser.query(fields).filter(domain).all().then(function (results) {
  87. defer.resolve(results);
  88. });
  89. return defer;
  90. },
  91. fetchAccountJournal: function() {
  92. var self = this;
  93. var defer = $.Deferred();
  94. var store_ids = self.ResUser[0].store_id[0];
  95. var fields = ['id', 'name', 'store_ids','type'];
  96. var domain = [['type','in',['sale','purchase','sale_refund']],['store_ids','in',store_ids]];
  97. var AccountJournal = new model.web.Model('account.journal');
  98. AccountJournal.query(fields).filter(domain).all().then(function(results) {
  99. defer.resolve(results);
  100. });
  101. return defer;
  102. },
  103. fetchAccountInvoice: function() {
  104. var self = this;
  105. var defer = $.Deferred();
  106. var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) {
  107. return item.id;
  108. }));
  109. var date = moment().format('YYYY-MM-01');
  110. var fields = ['id','type','date_invoice','amount_total','currency_id','journal_id'];
  111. var domain = [['state', 'not in', ['draft','cancel']],['journal_id','in',journal_ids],['date_invoice','>',date]];
  112. var AccountInvoice = new model.web.Model('account.invoice');
  113. AccountInvoice.query(fields).filter(domain).all().then(function(results) {
  114. defer.resolve(results);
  115. });
  116. return defer;
  117. },
  118. fetchPosOrder: function() {
  119. var self = this;
  120. var defer = $.Deferred();
  121. var modules = self.checkModel('point_of_sale');
  122. if (modules.length > 0){
  123. var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) {
  124. return item.id;
  125. }));
  126. var date = moment().format('YYYY-MM-01 00:00:00');
  127. var fields = ['id', 'name', 'date_order', 'amount_total','sale_journal'];
  128. var domain = [['state', 'not in', ['draft','cancel']],['sale_journal','in',journal_ids],['date_order','>',date]];
  129. var PosOrder = new model.web.Model('pos.order');
  130. PosOrder.query(fields).filter(domain).all().then(function(results) {
  131. defer.resolve(results);
  132. });
  133. return defer;
  134. }else{
  135. var PosOrder = [];
  136. return PosOrder;
  137. }
  138. },
  139. fetchResCompany: function() {
  140. var self = this;
  141. var defer = $.Deferred();
  142. var fields = ['id','name', 'currency_id'];
  143. var domain = [['id', '=', self.session.company_id]];
  144. var ResCompany = new model.web.Model('res.company');
  145. ResCompany.query(fields).filter(domain).all().then(function (results) {
  146. defer.resolve(results);
  147. });
  148. return defer;
  149. },
  150. fetchResCurrency : function(){
  151. var self = this;
  152. var defer = $.Deferred();
  153. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  154. var domain = [['active', '=', true]];
  155. var ResCurrency = new model.web.Model('res.currency');
  156. ResCurrency.query(fields).filter(domain).all().then(function(results) {
  157. defer.resolve(results);
  158. });
  159. return defer;
  160. },
  161. fetchDashboardObjetive : function(){
  162. var self = this;
  163. var defer = $.Deferred();
  164. var store_id = self.ResUser[0].store_id[0];
  165. var date_min = moment().format('YYYY-MM-01');
  166. var date_max = moment().add(1, 'month').format('YYYY-MM-01');
  167. var fields = ['id','store_id', 'date', 'sale_objetive', 'purchase_limit', 'expense_limit', 'expected_profit'];
  168. var domain = [['date', '>=', date_min],['date', '<', date_max],['store_id','=',store_id]];
  169. var DashboardObjetive = new model.web.Model('dashboard.objetive');
  170. DashboardObjetive.query(fields).filter(domain).all().then(function(results) {
  171. defer.resolve(results);
  172. });
  173. return defer;
  174. },
  175. getResCurrency: function (id) {
  176. var self = this;
  177. return _.filter(self.ResCurrency,function (item) {
  178. return item.id === id;
  179. })
  180. },
  181. /*===================
  182. ACCOUNT INVOICE
  183. ===================*/
  184. getTodayAccountInvoice:function(type) {
  185. var self = this;
  186. var date = moment().format('YYYY-MM-DD');
  187. var journals = _.filter(self.AccountJournal,function (inv) {
  188. return inv.type == type;
  189. });
  190. if(journals.length > 0){
  191. var journal_ids = _.flatten(_.map(journals, function (item) {
  192. return item.id;
  193. }));
  194. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  195. return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.type == 'in_invoice';
  196. }));
  197. }
  198. },
  199. getThisWeekAccountInvoice:function(type) {
  200. var self = this;
  201. var week = moment().week();
  202. var journals = _.filter(self.AccountJournal,function (inv) {
  203. return inv.type == type;
  204. });
  205. if(journals.length > 0){
  206. var journal_ids = _.flatten(_.map(journals, function (item) {
  207. return item.id;
  208. }));
  209. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  210. return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY') & _.contains(journal_ids, inv.journal_id[0]);
  211. }));
  212. }
  213. },
  214. getThisMonthAccountInvoice:function(type) {
  215. var self = this;
  216. var journals = _.filter(self.AccountJournal,function (inv) {
  217. return inv.type == type;
  218. });
  219. if(journals.length > 0){
  220. var journal_ids = _.flatten(_.map(journals, function (item) {
  221. return item.id;
  222. }));
  223. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  224. return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM') & _.contains(journal_ids, inv.journal_id[0]);
  225. }));
  226. }
  227. },
  228. /*=============
  229. POS ORDER
  230. =============*/
  231. getTodayPosOrder:function() {
  232. var self = this;
  233. var date = moment().format('YYYY-MM-DD');
  234. return _.flatten(_.filter(self.PosOrder,function (inv) {
  235. var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
  236. return moment(utc._d).format('YYYY-MM-DD') === date;
  237. }));
  238. },
  239. getThisWeekPosOrder:function() {
  240. var self = this;
  241. var week = moment().week();
  242. return _.flatten(_.filter(self.PosOrder,function (inv) {
  243. var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
  244. utc = moment(utc._d).format('YYYY-MM-DD');
  245. return moment(utc).week() === week & moment(utc).format('YYYY')=== moment().format('YYYY');
  246. }));
  247. },
  248. getThisMonthPosOrder:function() {
  249. var self = this;
  250. return _.flatten(_.filter(self.PosOrder,function (inv) {
  251. var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
  252. return moment(utc._d).format('YYYY-MM') === moment().format('YYYY-MM');
  253. }));
  254. },
  255. /*
  256. TODAY
  257. */
  258. showToday: function () {
  259. var self = this;
  260. var amount_order = 0;
  261. var sale_invoice_amount = 0;
  262. var sale_invoice_refund_amount = 0;
  263. var purchase_invoice_amount= 0;
  264. var balance = 0;
  265. var array = [];
  266. var order = self.getTodayPosOrder();
  267. var sale_invoice = self.getTodayAccountInvoice('sale');
  268. var sale_invoice_refund = self.getTodayAccountInvoice('sale_refund');
  269. var purchase_invoice = self.getTodayAccountInvoice('purchase');
  270. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  271. if(order.length > 0){
  272. amount_order = _.reduce(_.map(order, function (map) {
  273. return map.amount_total;
  274. }), function (memo, num) {
  275. return memo + num;
  276. });
  277. }
  278. /*
  279. SALE INVOICE
  280. */
  281. if(sale_invoice.length > 0){
  282. array = [];
  283. _.each(sale_invoice, function (item) {
  284. var currency = self.getResCurrency(item.currency_id[0]).shift();
  285. array.push({
  286. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  287. })
  288. });
  289. sale_invoice_amount = _.reduce(_.map(array, function (map) {
  290. return map.amount;
  291. }), function (memo, num) {
  292. return memo + num;
  293. });
  294. }
  295. /*
  296. SALE INVOICE REFUND
  297. */
  298. if(sale_invoice_refund.length > 0){
  299. array = [];
  300. _.each(sale_invoice_refund, function (item) {
  301. var currency = self.getResCurrency(item.currency_id[0]).shift();
  302. array.push({
  303. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  304. })
  305. });
  306. sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
  307. return map.amount;
  308. }), function (memo, num) {
  309. return memo + num;
  310. });
  311. }
  312. /*
  313. PURCHASE INVOICE
  314. */
  315. if(purchase_invoice.length > 0){
  316. array = [];
  317. _.each(purchase_invoice, function (item) {
  318. var currency = self.getResCurrency(item.currency_id[0]).shift();
  319. array.push({
  320. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  321. })
  322. });
  323. purchase_invoice_amount = _.reduce(_.map(array, function (map) {
  324. return map.amount;
  325. }), function (memo, num) {
  326. return memo + num;
  327. });
  328. }
  329. balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
  330. self.today = balance;
  331. },
  332. /*
  333. WEEK
  334. */
  335. showThisWeek: function () {
  336. var self = this;
  337. var amount_order = 0;
  338. var sale_invoice_amount = 0;
  339. var sale_invoice_refund_amount = 0;
  340. var purchase_invoice_amount= 0;
  341. var balance = 0;
  342. var array = [];
  343. var order = self.getThisWeekPosOrder();
  344. var sale_invoice = self.getThisWeekAccountInvoice('sale');
  345. var sale_invoice_refund = self.getThisWeekAccountInvoice('sale_refund');
  346. var purchase_invoice = self.getThisWeekAccountInvoice('purchase');
  347. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  348. if(order.length > 0){
  349. amount_order = _.reduce(_.map(order, function (map) {
  350. return map.amount_total;
  351. }), function (memo, num) {
  352. return memo + num;
  353. });
  354. }
  355. /*
  356. SALE INVOICE
  357. */
  358. if(sale_invoice.length > 0){
  359. array = [];
  360. _.each(sale_invoice, function (item) {
  361. var currency = self.getResCurrency(item.currency_id[0]).shift();
  362. array.push({
  363. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  364. })
  365. });
  366. sale_invoice_amount = _.reduce(_.map(array, function (map) {
  367. return map.amount;
  368. }), function (memo, num) {
  369. return memo + num;
  370. });
  371. }
  372. /*
  373. SALE INVOICE REFUND
  374. */
  375. if(sale_invoice_refund.length > 0){
  376. array = [];
  377. _.each(sale_invoice_refund, function (item) {
  378. var currency = self.getResCurrency(item.currency_id[0]).shift();
  379. array.push({
  380. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  381. })
  382. });
  383. sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
  384. return map.amount;
  385. }), function (memo, num) {
  386. return memo + num;
  387. });
  388. }
  389. /*
  390. PURCHASE INVOICE
  391. */
  392. if(purchase_invoice.length > 0){
  393. array = [];
  394. _.each(purchase_invoice, function (item) {
  395. var currency = self.getResCurrency(item.currency_id[0]).shift();
  396. array.push({
  397. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  398. })
  399. });
  400. purchase_invoice_amount = _.reduce(_.map(array, function (map) {
  401. return map.amount;
  402. }), function (memo, num) {
  403. return memo + num;
  404. });
  405. }
  406. balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
  407. self.week = balance;
  408. },
  409. /*
  410. MONTH
  411. */
  412. showThisMonth: function () {
  413. var self = this;
  414. if(self.DashboardObjetive.length > 0){
  415. self.objective = self.DashboardObjetive.shift().expected_profit;
  416. }else{
  417. self.objective = 0;
  418. }
  419. var amount_order = 0;
  420. var sale_invoice_amount = 0;
  421. var sale_invoice_refund_amount = 0;
  422. var purchase_invoice_amount = 0;
  423. var balance = 0;
  424. var array = [];
  425. var out_array = [];
  426. var order = self.getThisMonthPosOrder();
  427. var sale_invoice = self.getThisMonthAccountInvoice('sale');
  428. var sale_invoice_refund = self.getThisMonthAccountInvoice('sale_refund');
  429. var purchase_invoice = self.getThisMonthAccountInvoice('purchase');
  430. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  431. if(order.length > 0){
  432. amount_order = _.reduce(_.map(order, function (map) {
  433. return map.amount_total;
  434. }), function (memo, num) {
  435. return memo + num;
  436. });
  437. }
  438. /*
  439. SALE INVOICE
  440. */
  441. if(sale_invoice.length > 0){
  442. array = [];
  443. _.each(sale_invoice, function (item) {
  444. var currency = self.getResCurrency(item.currency_id[0]).shift();
  445. array.push({
  446. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  447. })
  448. });
  449. sale_invoice_amount = _.reduce(_.map(array, function (map) {
  450. return map.amount;
  451. }), function (memo, num) {
  452. return memo + num;
  453. });
  454. }
  455. /*
  456. SALE INVOICE REFUND
  457. */
  458. if(sale_invoice_refund.length > 0){
  459. array = [];
  460. _.each(sale_invoice_refund, function (item) {
  461. var currency = self.getResCurrency(item.currency_id[0]).shift();
  462. array.push({
  463. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  464. })
  465. });
  466. sale_invoice_refund_amount = _.reduce(_.map(array, function (map) {
  467. return map.amount;
  468. }), function (memo, num) {
  469. return memo + num;
  470. });
  471. }
  472. /*
  473. PURCHASE INVOICE
  474. */
  475. if(purchase_invoice.length > 0){
  476. _.each(purchase_invoice, function (item) {
  477. var currency = self.getResCurrency(item.currency_id[0]).shift();
  478. out_array.push({
  479. amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
  480. })
  481. });
  482. purchase_invoice_amount = _.reduce(_.map(out_array, function (map) {
  483. return map.amount;
  484. }), function (memo, num) {
  485. return memo + num;
  486. });
  487. }
  488. balance = (sale_invoice_amount + amount_order) - (sale_invoice_refund_amount + purchase_invoice_amount);
  489. self.month = balance;
  490. if(self.objective > 0){
  491. var percentage = (balance*100)/self.objective;
  492. }else{
  493. var percentage = 0;
  494. }
  495. $("#circle-balance").circliful({
  496. animationStep: 10,
  497. foregroundBorderWidth: 15,
  498. backgroundBorderWidth: 1,
  499. percent: percentage,
  500. fontColor: '#0288d1',
  501. foregroundColor: '#0288d1',
  502. percentageTextSize: 35,
  503. });
  504. self.percentage = percentage;
  505. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(balance, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  506. },
  507. /*
  508. MODAL
  509. */
  510. showModal: function (e) {
  511. var self = this;
  512. self.showThisWeek();
  513. self.showToday();
  514. var titleData = [
  515. {
  516. title: "Balance"
  517. }
  518. ];
  519. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  520. var modal = Qweb.render('WidgetModal', {
  521. modalTitle: titleData,
  522. objective: accounting.formatMoney(self.objective, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
  523. today: accounting.formatMoney(self.today, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
  524. week: accounting.formatMoney(self.week, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
  525. month: accounting.formatMoney(self.month, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator),
  526. });
  527. $('.openerp_webclient_container').after(modal);
  528. $('.widget-modal').modal()
  529. $('.widget-modal').on('hidden.bs.modal', function (e) {
  530. self.removeModal(e);
  531. })
  532. $("#myItemModal").circliful({
  533. animationStep: 10,
  534. foregroundBorderWidth: 15,
  535. backgroundBorderWidth: 1,
  536. percent: self.percentage,
  537. fontColor: '#0288d1',
  538. foregroundColor: '#0288d1',
  539. percentageTextSize: 35,
  540. });
  541. },
  542. removeModal: function (e) {
  543. $('.widget-modal').remove();
  544. $('.modal-backdrop').remove();
  545. },
  546. });
  547. }