widget_balance.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. function widget_balance(widget) {
  2. "use strict";
  3. var model = openerp;
  4. var Qweb = openerp.web.qweb;
  5. widget.WidgetBalance = widget.Base.extend({
  6. template: 'WidgetBalance',
  7. ContentSale: [],
  8. ContentPurchase: [],
  9. ContentExpense: [],
  10. init: function (parent) {
  11. this._super(parent, {
  12. width: 12,
  13. height: 5
  14. });
  15. },
  16. start: function () {
  17. var self = this;
  18. self.fetchInitial();
  19. },
  20. fetchInitial: function(){
  21. var self = this;
  22. self.fetchDataSQL().then(function (DataSQL) {
  23. return DataSQL;
  24. }).then(function(DataSQL) {
  25. self.ResCompany = DataSQL.company;
  26. self.PosOrder = DataSQL.orders;
  27. self.AccountInvoice = DataSQL.invoices;
  28. self.HrPayslip = DataSQL.payslips;
  29. return self.showThisMonth();
  30. });
  31. },
  32. fetchDataSQL: function() {
  33. var self = this;
  34. var data = $.get('/dashboard-Balance');
  35. return data;
  36. },
  37. getContentByDate:function(date, content) {
  38. var self = this;
  39. return _.flatten(_.filter(content,function (inv) {
  40. return moment(inv.date).format('YYYY-MM-DD') === date;
  41. }));
  42. },
  43. showThisMonth: function () {
  44. var self = this;
  45. var ContentSale = [];
  46. var ContentPurchase = [];
  47. var ContentExpense = [];
  48. /*
  49. ==============
  50. VENTAS
  51. ==============
  52. */
  53. _.each(self.PosOrder,function(item){
  54. var utc = moment.utc(item.date,'YYYY-MM-DD h:mm:ss A');
  55. ContentSale.push({
  56. date: moment(utc._d).format('YYYY-MM-DD'),
  57. amount: item.amount,
  58. });
  59. });
  60. _.each(self.AccountInvoice,function(item){
  61. if(item.type == "out_invoice" && item.origin.match(/SO/) != null){
  62. ContentSale.push({
  63. date: item.date,
  64. amount: item.amount,
  65. });
  66. }
  67. if(item.type == "out_refund"){
  68. ContentSale.push({
  69. date: item.date,
  70. amount: item.amount * -1,
  71. });
  72. }
  73. });
  74. /*
  75. ==============
  76. COMPRAS
  77. ==============
  78. */
  79. _.each(self.AccountInvoice,function(item){
  80. var origin = item.origin;
  81. if(item.type == "in_invoice" && item.origin != null){
  82. if(origin.match(/PO/) != null){
  83. ContentPurchase.push({
  84. date: item.date,
  85. amount: item.amount,
  86. });
  87. }
  88. }
  89. });
  90. /*
  91. ==============
  92. GASTOS
  93. ==============
  94. */
  95. _.each(self.AccountInvoice,function(item){
  96. var origin = item.origin;
  97. if(item.type == "in_invoice"){
  98. if(origin == null){
  99. ContentExpense.push({
  100. date: item.date,
  101. amount: item.amount,
  102. });
  103. }else{
  104. if(origin != null){
  105. if(origin.match(/PO/) == null){
  106. ContentExpense.push({
  107. date: item.date,
  108. amount: item.amount,
  109. });
  110. }
  111. }
  112. }
  113. }
  114. });
  115. /*
  116. =====================
  117. RECURSOS HUMANOS
  118. =====================
  119. */
  120. var net = _.reduce(_.map(self.HrPayslip,function(item) {
  121. ContentExpense.push({
  122. date: item.date,
  123. amount: item.net,
  124. });
  125. return item.net;
  126. }),function(memo, num) {
  127. return memo + num;
  128. },0);
  129. var ipsc = _.reduce(_.map(self.HrPayslip,function(item) {
  130. if(item.code == 'IPSC' && item.ipsc != null){
  131. ContentExpense.push({
  132. date: item.date,
  133. amount: item.ipsc,
  134. });
  135. return item.ipsc;
  136. }else{
  137. return 0;
  138. }
  139. }),function(memo, num) {
  140. return memo + num;
  141. },0);
  142. var ipse = _.reduce(_.map(self.HrPayslip,function(item) {
  143. if(item.code == 'IPSE' && item.ipse != null){
  144. ContentExpense.push({
  145. date: item.date,
  146. amount: item.ipse * -1,
  147. });
  148. return item.ipse * -1;
  149. }else{
  150. return 0;
  151. }
  152. }),function(memo, num) {
  153. return memo + num;
  154. },0);
  155. self.ContentSale = ContentSale;
  156. self.ContentPurchase = ContentPurchase;
  157. self.ContentExpense = ContentExpense;
  158. self.BuildChartMonth();
  159. },
  160. /*
  161. =====================================================
  162. BUILD CHART - MONTH
  163. =====================================================
  164. */
  165. BuildChartMonth: function(){
  166. var self = this;
  167. var data = [];
  168. var CurrencyBase = self.ResCompany[0].currency_id;
  169. var label = [];
  170. var bodySale = [];
  171. var bodyPurchase = [];
  172. var bodyExpense = [];
  173. var name = '.month-dashboard-chart';
  174. var date_start = moment().format('YYYY-MM-01');
  175. var date_stop = moment().add(1,'months').format('YYYY-MM-01');
  176. var date = date_start;
  177. var total, i;
  178. /*
  179. ===================
  180. VENTAS
  181. ===================
  182. */
  183. for (i = 0; i < 32; i++) {
  184. total = 0;
  185. if(i > 0){
  186. date = moment(date).add(1,'days').format('YYYY-MM-DD');
  187. }
  188. if(date == date_stop){
  189. break;
  190. }
  191. data = self.getContentByDate(date, self.ContentSale);
  192. if(data.length > 0){
  193. total = _.reduce(_.map(data,function(item) {
  194. return item.amount;
  195. }),function(memo, num) {
  196. return memo + num;
  197. },0);
  198. }
  199. label.push(moment(date).format('DD'));
  200. bodySale.push(total);
  201. }
  202. /*
  203. ===================
  204. COMPRAS
  205. ===================
  206. */
  207. date = date_start;
  208. for (i = 0; i < 32; i++) {
  209. total = 0;
  210. if(i > 0){
  211. date = moment(date).add(1,'days').format('YYYY-MM-DD');
  212. }
  213. if(date == date_stop){
  214. break;
  215. }
  216. data = self.getContentByDate(date, self.ContentPurchase);
  217. if(data.length > 0){
  218. total = _.reduce(_.map(data,function(item) {
  219. return item.amount;
  220. }),function(memo, num) {
  221. return memo + num;
  222. },0);
  223. }
  224. bodyPurchase.push(total);
  225. }
  226. /*
  227. ===================
  228. GASTOS
  229. ===================
  230. */
  231. date = date_start;
  232. for (i = 0; i < 32; i++) {
  233. total = 0;
  234. if(i > 0){
  235. date = moment(date).add(1,'days').format('YYYY-MM-DD');
  236. }
  237. if(date == date_stop){
  238. break;
  239. }
  240. data = self.getContentByDate(date, self.ContentExpense);
  241. if(data.length > 0){
  242. total = _.reduce(_.map(data,function(item) {
  243. return item.amount;
  244. }),function(memo, num) {
  245. return memo + num;
  246. },0);
  247. }
  248. bodyExpense.push(total);
  249. }
  250. var chart = new widget.DashboardChartWidget(self);
  251. chart.BuildBalanceLineChart(name,label,bodySale,bodyPurchase,bodyExpense,CurrencyBase);
  252. },
  253. });
  254. }