report_purchase.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. function report_purchase(reporting){
  2. "use strict";
  3. var model = openerp;
  4. reporting.ReportPurchaseWidget = reporting.Base.extend({
  5. template: 'ReportPurchase',
  6. rowsData :[],
  7. content :[],
  8. events:{
  9. 'click #generate':'fetchGenerate',
  10. 'click .print-report':'clickOnAction',
  11. 'change #current-company':'updateSelections',
  12. 'change #current-date':'ShowDateRange',
  13. },
  14. init : function(parent){
  15. this._super(parent);
  16. },
  17. start: function () {
  18. var table = this.$el.find('#table');
  19. table.bootstrapTable({data : self.rowsData});
  20. var date = new reporting.ReportDatePickerWidget(self);
  21. date.fecthFecha();
  22. this.fetchInitial();
  23. },
  24. valorNull:function(dato){
  25. var valor = "";
  26. if (dato){
  27. valor = dato;
  28. }
  29. return valor;
  30. },
  31. ShowDateRange : function(){
  32. var self = this;
  33. var date = self.$el.find('#current-date').val();
  34. if(date == 'range'){
  35. self.$el.find('.datepicker').css('display','block');
  36. }
  37. if(date != 'range'){
  38. self.$el.find('.datepicker').css('display','none');
  39. }
  40. },
  41. fetchInitial: function () {
  42. var self = this;
  43. self.fetchIntialSQL().then(function (IntialSQL) {
  44. return IntialSQL;
  45. }).then(function(IntialSQL) {
  46. /*
  47. =================================
  48. RES COMPANY
  49. =================================
  50. */
  51. self.ResCompany = IntialSQL.companies;
  52. self.CompanyLogo = IntialSQL.logo;
  53. if(self.ResCompany.length > 1){
  54. self.$el.find('#current-company').append('<option value="9999999">Todas las empresas</option>');
  55. _.each(self.ResCompany,function(item){
  56. self.$el.find('#current-company').append('<option value="' + item.id + '">' + item.name + '</option>');
  57. });
  58. }else{
  59. self.$el.find('.company').css('display','none');
  60. }
  61. /*
  62. =================================
  63. RES STORE
  64. =================================
  65. */
  66. self.ResStore = IntialSQL.stores;
  67. if(self.ResStore.length > 1){
  68. self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
  69. _.each(self.ResStore,function(item){
  70. self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
  71. });
  72. }else{
  73. self.$el.find('.store').css('display','none');
  74. }
  75. /*
  76. =================================
  77. ACCOUNT JOURNAL
  78. =================================
  79. */
  80. self.AccountJournal = IntialSQL.journals;
  81. if(self.AccountJournal.length > 1){
  82. self.$el.find('#current-journal').append('<option value="9999999">Todas las facturas</option>');
  83. _.each(self.AccountJournal,function(item){
  84. if(item.type == 'sale'){
  85. self.$el.find('#current-journal').append('<option value="' + item.id + '">' + item.name + '</option>');
  86. }
  87. });
  88. }else{
  89. self.$el.find('.journal').css('display','none');
  90. }
  91. self.ResUsers = IntialSQL.users;
  92. var store_ids = _.flatten(_.map(self.ResStore, function (item) {
  93. return item.id;
  94. }));
  95. var ResUsers = _.flatten(_.filter(self.ResUsers,function (item) {
  96. return _.contains(store_ids, item.store_id);
  97. }));
  98. if(ResUsers.length > 1){
  99. self.$el.find('#current-user').append('<option value="9999999">Todos los responsables</option>');
  100. _.each(ResUsers,function(item){
  101. self.$el.find('#current-user').append('<option value="' + item.id + '">' + item.name + '</option>');
  102. });
  103. }
  104. });
  105. self.$el.find('#generate').css('display','inline');
  106. return;
  107. },
  108. fetchGenerate: function () {
  109. var self = this;
  110. self.$el.find('.search-form').block({
  111. message: null,
  112. overlayCSS: {
  113. backgroundColor: '#FAFAFA'
  114. }
  115. });
  116. self.$el.find('.report-form').block({
  117. message: null,
  118. overlayCSS: {
  119. backgroundColor: '#FAFAFA'
  120. }
  121. });
  122. this.fetchDataSQL().then(function(DataSQL) {
  123. return DataSQL;
  124. }).then(function (DataSQL) {
  125. self.AccountInvoice = DataSQL.invoices;
  126. self.AccountVoucher = DataSQL.vouchers;
  127. return self.BuildTable();
  128. });
  129. },
  130. fetchIntialSQL: function() {
  131. var self = this;
  132. var data = $.get('/report-filter-data');
  133. return data;
  134. },
  135. fetchDataSQL: function() {
  136. var self = this;
  137. var data = $.get('/report-purchase-history');
  138. return data;
  139. },
  140. /*====================================================================
  141. UPDATE SELECTIONS
  142. ====================================================================*/
  143. updateSelections: function () {
  144. var self = this;
  145. var store;
  146. var company = self.$el.find('#current-company').val();
  147. if(company != 9999999){
  148. store = self.$el.find('#current-store').empty();
  149. self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
  150. _.each(self.ResStore,function(item){
  151. if(parseFloat(company) == item.company_id[0]){
  152. self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
  153. }
  154. });
  155. }else{
  156. store = self.$el.find('#current-store').empty();
  157. self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
  158. _.each(self.ResStore,function(item){
  159. self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
  160. });
  161. }
  162. },
  163. getAccountInvoice:function() {
  164. var self = this;
  165. var content = self.AccountInvoice;
  166. var company = self.$el.find('#current-company').val();
  167. var store = self.$el.find('#current-store').val();
  168. var state = self.$el.find('#current-state').val();
  169. var user = self.$el.find('#current-user').val();
  170. var date = self.$el.find('#current-date').val();
  171. var desde = self.$el.find('#from').val();
  172. var hasta = self.$el.find('#to').val();
  173. var store_ids = _.flatten(_.map(self.ResStore, function (item) {
  174. return item.id;
  175. }));
  176. var company_ids = _.flatten(_.map(self.ResCompany, function (item) {
  177. return item.id;
  178. }));
  179. content = _.flatten(_.filter(content,function (item) {
  180. return _.contains(store_ids, item.store_id) && _.contains(company_ids, item.company_id);
  181. }));
  182. if(company && company != 9999999){
  183. content = _.flatten(_.filter(content,function (item) {
  184. return item.company_id == company;
  185. }));
  186. }
  187. if(store && store != 9999999){
  188. content = _.flatten(_.filter(content,function (item) {
  189. return item.store_id == store;
  190. }));
  191. }
  192. if(state && state != 9999999){
  193. content = _.flatten(_.filter(content,function (item) {
  194. return item.state == state;
  195. }));
  196. }
  197. if(user && user != 9999999){
  198. content = _.flatten(_.filter(content,function (item) {
  199. return item.user_id == user;
  200. }));
  201. }
  202. if(date && date != 9999999){
  203. if(date == 'range'){
  204. if(desde){
  205. date = desde.split('/');
  206. date = (date[2]+"-"+date[1]+"-"+date[0]);
  207. content = _.flatten(_.filter(content,function (inv) {
  208. return moment(inv.date).format('YYYY-MM-DD') >= date;
  209. }));
  210. }
  211. if(hasta){
  212. date = hasta.split('/');
  213. date = (date[2]+"-"+date[1]+"-"+date[0]);
  214. content = _.flatten(_.filter(content,function (inv) {
  215. return moment(inv.date).format('YYYY-MM-DD') <= date;
  216. }));
  217. }
  218. }
  219. if(date == 'today'){
  220. var today = moment().format('YYYY-MM-DD');
  221. content = _.flatten(_.filter(content,function (inv) {
  222. return moment(inv.date).format('YYYY-MM-DD') === today;
  223. }));
  224. }
  225. if(date == 'yesterday'){
  226. var yesterday = moment().add(-1,'days').format('YYYY-MM-DD');
  227. content = _.flatten(_.filter(content,function (inv) {
  228. return moment(inv.date).format('YYYY-MM-DD') === yesterday;
  229. }));
  230. }
  231. if(date == 'currentMonth'){
  232. var currentMonth = moment().format('YYYY-MM');
  233. content = _.flatten(_.filter(content,function (inv) {
  234. return moment(inv.date).format('YYYY-MM') === currentMonth;
  235. }));
  236. }
  237. if(date == 'lastMonth'){
  238. var lastMonth = moment().add(-1,'months').format('YYYY-MM');
  239. content = _.flatten(_.filter(content,function (inv) {
  240. return moment(inv.date).format('YYYY-MM') === lastMonth;
  241. }));
  242. }
  243. }
  244. return content;
  245. },
  246. getAccountVoucher: function (number) {
  247. var self = this;
  248. return _.filter(self.AccountVoucher,function (item) {
  249. return item.reference == number;
  250. });
  251. },
  252. BuildTable: function(){
  253. var self = this;
  254. var data = [];
  255. var residual = 0;
  256. var CurrencyBase = self.ResCompany[0].currency_id;
  257. /*
  258. ==========================================
  259. ACCOUNT INVOICE
  260. ==========================================
  261. */
  262. var AccountInvoice = self.getAccountInvoice();
  263. console.log(AccountInvoice);
  264. _.each(AccountInvoice, function(item){
  265. residual = 0;
  266. if(item.state == 'open'){
  267. residual = self.getAccountVoucher(item.number);
  268. residual = _.reduce(_.map(residual,function(item){
  269. return item.amount_currency;
  270. }), function(memo, num){
  271. return memo + num;
  272. },0);
  273. residual = item.amount - residual;
  274. }
  275. data.push({
  276. /*=======================
  277. IDS
  278. =======================*/
  279. id:item.invoice_id,
  280. origin:item.origin,
  281. /*=======================
  282. INFO
  283. =======================*/
  284. supplier_invoice_number:self.valorNull(item.supplier_invoice_number),
  285. number:item.number,
  286. date:moment(item.date).format('DD/MM/YYYY'),
  287. user_name:item.user_name,
  288. user_id:item.user_id,
  289. customer_ruc:self.valorNull(item.supplier_ruc),
  290. partner_name:self.valorNull(item.supplier_name),
  291. residual:accounting.formatMoney(residual,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
  292. untaxed:accounting.formatMoney(item.amount - item.amount_tax,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
  293. tax:accounting.formatMoney(item.amount_tax,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
  294. total:accounting.formatMoney(item.amount,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
  295. /*=======================
  296. VALORES SIN FORMATEAR
  297. =======================*/
  298. date_no_format: moment(item.date).format('YYYY-MM-DD'),
  299. residual_no_format:residual,
  300. untaxed_no_format:item.amount - item.amount_tax,
  301. tax_no_format:item.amount_tax,
  302. total_no_format:item.amount,
  303. /*==============================
  304. TOTAL FOOTER CONFIGURATION
  305. ==============================*/
  306. decimal_places:CurrencyBase.decimal_places,
  307. thousands_separator:CurrencyBase.thousands_separator,
  308. decimal_separator:CurrencyBase.decimal_separator,
  309. });
  310. });
  311. data.sort(function (a, b) {
  312. if (a.date_no_format > b.date_no_format) {
  313. return -1;
  314. }
  315. if (a.date_no_format < b.date_no_format) {
  316. return 1;
  317. }
  318. return 0;
  319. });
  320. self.content = data;
  321. self.loadTable(data);
  322. self.$el.find('.report-form').css('display','block');
  323. self.$el.find('.search-form').unblock();
  324. self.$el.find('.report-form').unblock();
  325. },
  326. loadTable:function(rowsTable){
  327. var self = this;
  328. self.rowsData = rowsTable;
  329. var table = this.$el.find('#table');
  330. table.bootstrapTable('load', rowsTable);
  331. },
  332. clickOnAction: function (e) {
  333. var self = this;
  334. var ResCompany;
  335. var CurrencyBase;
  336. var action = this.$el.find(e.target).val();
  337. var company = $('#current-company').val();
  338. if(company && company != 9999999){
  339. ResCompany = _.flatten(_.filter(self.CompanyLogo,function (inv) {
  340. return inv.id == company;
  341. }));
  342. ResCompany = ResCompany[0];
  343. CurrencyBase = ResCompany[0].currency_id;
  344. }else{
  345. ResCompany = self.CompanyLogo[0];
  346. CurrencyBase = self.ResCompany[0].currency_id;
  347. }
  348. var getColumns=[];
  349. var rows=[];
  350. var table = this.$el.find("#table");
  351. var column = table.bootstrapTable('getVisibleColumns');
  352. var row = table.bootstrapTable('getData');
  353. var residual = ResidualFooter(row);
  354. var untaxed = UntaxedFooter(row);
  355. var tax = TaxFooter(row);
  356. var total = TotalFooter(row);
  357. row.push({
  358. number:'Totales',
  359. residual:residual,
  360. untaxed:untaxed,
  361. tax:tax,
  362. total:total,
  363. });
  364. if (action === 'pdf') {
  365. var data = _.map(column, function (val){
  366. return val.field;
  367. });
  368. _.each(_.map(column,function(val){
  369. return val;
  370. }), function(item){
  371. getColumns.push([{
  372. title: item.title,
  373. dataKey: item.field
  374. }]);
  375. });
  376. /*
  377. ============================================================
  378. CONFIGURACION DEL PDF
  379. ============================================================
  380. */
  381. var pdf_title = 'Facturas de Compra.';
  382. var pdf_type = '';
  383. var pdf_name = 'facturas_de_compra_';
  384. var pdf_columnStyles = {
  385. supplier_invoice_number:{columnWidth: 15,halign:'left'},
  386. number:{columnWidth: 30,halign:'left'},
  387. origin:{columnWidth: 15, halign:'center'},
  388. date_invoice:{halign:'center'},
  389. user_name:{ halign:'left'},
  390. customer_ruc:{columnWidth:12, halign:'left'},
  391. partner_name:{columnWidth: 30, halign:'left'},
  392. residual:{columnWidth: 20, halign:'right'},
  393. untaxed:{columnWidth: 20, halign:'right'},
  394. tax:{columnWidth: 20, halign:'right'},
  395. total:{columnWidth: 20, halign:'right'},
  396. };
  397. /*
  398. ============================================================
  399. LLAMAR FUNCION DE IMPRESION
  400. ============================================================
  401. */
  402. var filter = self.getFilter();
  403. var pdf = new reporting.ReportPdfWidget(self);
  404. pdf.drawPDF(
  405. _.flatten(getColumns),
  406. row,
  407. ResCompany,
  408. pdf_title,
  409. pdf_type,
  410. pdf_name,
  411. pdf_columnStyles,
  412. filter
  413. );
  414. }
  415. },
  416. getFilter: function(){
  417. var self = this;
  418. var company = self.$el.find('#current-company').val();
  419. var store = self.$el.find('#current-store').val();
  420. var state = self.$el.find('#current-state').val();
  421. var date = self.$el.find('#current-date').val();
  422. var desde = self.$el.find('#from').val();
  423. var hasta = self.$el.find('#to').val();
  424. var filter = [];
  425. if(company && company != 9999999){
  426. var ResCompany = _.filter(self.ResCompany, function(item){
  427. return item.id == company;
  428. });
  429. filter.push({
  430. title:'Empresa',
  431. value: ResCompany[0].name,
  432. });
  433. }
  434. if(store && store != 9999999){
  435. var ResStore = _.filter(self.ResStore,function (item) {
  436. return item.id == store;
  437. });
  438. filter.push({
  439. title: 'Sucursal',
  440. value: ResStore[0].name,
  441. });
  442. }
  443. if(state && state != 9999999){
  444. filter.push({
  445. title: 'Estado',
  446. value: $('#current-state option:selected').text(),
  447. });
  448. }
  449. if(date && date != 9999999){
  450. moment.locale('es', {
  451. months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
  452. });
  453. if(date == 'range'){
  454. filter.push({
  455. title: 'Fecha',
  456. value: desde +' al '+hasta,
  457. });
  458. }else {
  459. var fecha;
  460. if(date == 'today'){
  461. fecha = moment().format('DD/MM/YYYY');
  462. }
  463. if(date == 'yesterday'){
  464. fecha = moment().add(-1,'days').format('DD/MM/YYYY');
  465. }
  466. if(date == 'currentMonth'){
  467. fecha = moment().format('MMMM/YYYY');
  468. }
  469. if(date == 'lastMonth'){
  470. fecha = moment().add(-1,'months').format('MMMM/YYYY');
  471. }
  472. filter.push({
  473. title: 'Fecha',
  474. value: fecha,
  475. });
  476. }
  477. }
  478. return filter;
  479. },
  480. });
  481. }