report_sales.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. function report_sales(reporting){
  2. "use strict";
  3. var instance = openerp;
  4. reporting.ReportSaleWidget = reporting.Base.extend({
  5. template: 'ReportSale',
  6. content: [],
  7. rowsData :[],
  8. events:{
  9. 'click #toolbar > button' : 'clickOnAction',
  10. 'click #X' : 'factSearch',
  11. 'click #A' : 'factSearch',
  12. 'click #B' : 'factSearch',
  13. 'click #C' : 'factSearch',
  14. 'click #D' : 'factSearch',
  15. 'click #Z' : 'factSearch',
  16. 'change #from' : 'factSearch',
  17. 'change #to' : 'factSearch',
  18. 'click-row.bs.table #table ' : 'ckickAnalysisDetail',
  19. },
  20. init : function(parent){
  21. this._super(parent);
  22. },
  23. start: function () {
  24. var self = this;
  25. var table = this.$el.find('#table');
  26. table.bootstrapTable({data : self.rowsData});
  27. this.fecthFecha();
  28. this.submitForm();
  29. },
  30. ckickAnalysisDetail: function(e, row, $element, field){
  31. if (field == 'number'){
  32. this.do_action({
  33. name:"Factura de proveedor",
  34. type: 'ir.actions.act_window',
  35. res_model: "account.invoice",
  36. views: [[false,'form']],
  37. target: 'new',
  38. domain: [['type', '=', 'in_invoice'],['id','=', row.id]],
  39. context: {},
  40. flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
  41. res_id: row.id,
  42. });
  43. }
  44. e.stopImmediatePropagation();
  45. },
  46. submitForm: function () {
  47. var self = this;
  48. self.fetchAccountInvoice().then(function(AccountInvoice) {
  49. return AccountInvoice;
  50. }).then(function (AccountInvoice){
  51. self.AccountInvoice = AccountInvoice;
  52. return self.fetchResPartner();
  53. }).then(function(ResPartner){
  54. self.ResPartner=ResPartner;
  55. self.search();
  56. return self.BuildTable();
  57. });
  58. },
  59. fecthFecha: function() {
  60. var to;
  61. var dateFormat1 = "mm/dd/yy",
  62. from = $( "#from" )
  63. .datepicker({
  64. dateFormat: "dd/mm/yy",
  65. changeMonth: true,
  66. numberOfMonths: 1,
  67. })
  68. .on( "change", function() {
  69. to.datepicker( "option", "minDate", getDate(this), "dd/mm/yyyy");
  70. });
  71. to = $( "#to" ).datepicker({
  72. dateFormat: "dd/mm/yy",
  73. defaultDate: "+7d",
  74. changeMonth: true,
  75. numberOfMonths: 1,
  76. })
  77. .on( "change", function() {
  78. from.datepicker( "option", "maxDate", getDate(this));
  79. });
  80. function getDate( element ) {
  81. var fechaSel =element.value.split('/');
  82. var date;
  83. try {
  84. date = $.datepicker.parseDate( dateFormat1, (fechaSel[1]+"/"+fechaSel[0]+"/"+fechaSel[2]));
  85. } catch( error ) {
  86. date = null;
  87. }
  88. return date;
  89. }
  90. },
  91. fetchAccountInvoice: function () {
  92. var filter =[['state', 'in',['open','paid']],['type', '=', 'out_invoice']];
  93. var field =['id', 'type', 'number', 'origin', 'state', 'journal_id', 'currency_id', 'supplier_invoice_number','date_invoice','partner_id','amount_total','user_id','residual'];
  94. var defer = $.Deferred();
  95. var AccountInvoice = new instance.web.Model('account.invoice');
  96. AccountInvoice.query(field).filter(filter).all().then(function (results) {
  97. defer.resolve(results);
  98. });
  99. return defer;
  100. },
  101. fetchResPartner: function() {
  102. var self = this;
  103. var defer = $.Deferred();
  104. var ResPartner = new instance.web.Model('res.partner');
  105. ResPartner.query(['id', 'name', 'ruc']).filter([['active', '=', true], ['customer', '=', true]]).all().then(function (results) {
  106. defer.resolve(results);
  107. });
  108. return defer;
  109. },
  110. valorNull:function(dato){
  111. var valor ="";
  112. if (dato){
  113. valor=dato;
  114. }
  115. return valor;
  116. },
  117. search: function () {
  118. var self = this;
  119. var results = self.ResPartner;
  120. results = _.map(results, function (item) {
  121. return {
  122. label: item.id + '-'+ item.name + ' ' + self.valorNull(item.ruc),
  123. value: item.id + '-'+ item.name + ' ' + self.valorNull(item.ruc)
  124. }
  125. });
  126. self.$('#partner').autocomplete({
  127. source: results,
  128. minLength:0,
  129. search: function(event, ui) {
  130. if (!(self.$('#partner').val())){
  131. self.factSearch();
  132. }
  133. },
  134. close: function( event, ui ) {
  135. self.factSearch();
  136. },
  137. select: function(event, ui) {
  138. self.factSearch();
  139. }
  140. });
  141. },
  142. BuildTable: function(invoices){
  143. var self = this;
  144. var AccountInvoice = self.AccountInvoice;
  145. var data = [];
  146. _.each(AccountInvoice, function(item){
  147. data.push({
  148. id : item.id,
  149. number: item.number,
  150. origin: self.valorNull(item.origin),
  151. partner: item.partner_id[1],
  152. date_invoice: moment(item.date_invoice).format("DD/MM/YYYY"),
  153. amount_total: accounting.formatNumber(item.amount_total,2,".",","),
  154. residual: accounting.formatNumber(item.residual,2,".",","),
  155. date: item.date_invoice,
  156. partner_id : item.partner_id[0],
  157. amount : item.amount_total,
  158. total_residual : item.residual
  159. });
  160. });
  161. self.content = data;
  162. this.loadTable(data);
  163. },
  164. factSearch: function(){
  165. var self = this;
  166. var hoy = moment().format('YYYY-MM-DD');
  167. var desde =this.$el.find('#from').val();
  168. var hasta =this.$el.find('#to').val();
  169. var partner= this.$el.find('#partner').val().split('-');
  170. var content = self.content;
  171. if ($('#A').is(":checked")){
  172. content = _.filter(content, function (inv){
  173. return inv.date == hoy;
  174. });
  175. }
  176. if ($('#B').is(":checked")){
  177. var date = hoy.split('-');
  178. var ayer = date[2] - 1;
  179. date.splice(2,0);
  180. date[2] = '0'+ayer;
  181. content = _.filter(content, function (inv){
  182. return inv.date == date[0]+'-'+date[1]+'-'+date[2];
  183. });
  184. }
  185. if ($('#C').is(":checked")){
  186. var date = hoy.split('-');
  187. content = _.filter(content, function (inv){
  188. var mes = inv.date.split('-');
  189. return mes[0]+'-'+mes[1] == date[0]+'-'+date[1];
  190. });
  191. }
  192. if ($('#D').is(":checked")){
  193. var date = hoy.split('-');
  194. var mes = date[1] - 1;
  195. date.splice(1,0);
  196. if(date[1] < 10){
  197. date[1] = '0'+mes;
  198. }else{
  199. date[1] = mes;
  200. }
  201. content = _.filter(content, function (inv){
  202. var mes = inv.date.split('-');
  203. return mes[0]+'-'+mes[1] == date[0]+'-'+date[1];
  204. });
  205. }
  206. if ($('#Z').is(":checked")){
  207. $('#datepicker').css('display','block');
  208. if (desde.length > 0){
  209. var date= desde.split('/');
  210. content = _.filter(content, function (inv){
  211. return inv.date >= (date[2]+"-"+date[1]+"-"+date[0]);
  212. });
  213. }
  214. if (hasta.length > 0){
  215. var date= hasta.split('/');
  216. content = _.filter(content, function (inv){
  217. return inv.date <= (date[2]+"-"+date[1]+"-"+date[0]);
  218. });
  219. }
  220. }else{
  221. $('#datepicker').css('display','none');
  222. }
  223. if (partner != ""){
  224. content = _.filter(content, function(inv){
  225. return inv.partner_id == partner[0];
  226. });
  227. }
  228. self.loadTable(content)
  229. },
  230. loadTable:function(rowsTable){
  231. var self = this;
  232. self.rowsData = rowsTable;
  233. var table = this.$el.find('#table');
  234. table.bootstrapTable('load',rowsTable);
  235. },
  236. getObjetPdf: function(rowsTable){
  237. var self = this;
  238. var rows=self.rowsData;
  239. var amount = _.reduce(_.map(rows,function(map){
  240. return(map.amount);
  241. }),function(memo, num){
  242. return memo + num;
  243. },0);
  244. var total_residual = _.reduce(_.map(rows,function(map){
  245. return(map.total_residual);
  246. }),function(memo, num){
  247. return memo + num;
  248. },0);
  249. if (rows.length > 0){
  250. rows.push({
  251. number: "Total",
  252. amount_total: accounting.formatNumber((amount),2,".",","),
  253. residual: accounting.formatNumber((total_residual),2,".",","),
  254. });
  255. }
  256. // console.log(rows);
  257. return rows;
  258. },
  259. clickOnAction: function (e) {
  260. var self = this;
  261. var rowsNew;
  262. var action = self.$el.find(e.target).val();
  263. var table = self.$el.find("#table");
  264. var data2 = table.bootstrapTable('getVisibleColumns');
  265. var getColumns=[];
  266. var rows=[];
  267. rowsNew = self.getObjetPdf();
  268. if (action === 'pdf') {
  269. var dataNEW = _.map(data2, function (val){
  270. return val.field;
  271. });
  272. _.each(rowsNew,function (item){
  273. rows.push(_.pick(item, dataNEW));
  274. });
  275. // Obtener los nombre de la Cabezera
  276. _.each(_.map(data2,function(val){
  277. return val;
  278. }), function(item){
  279. getColumns.push([{
  280. title: item.title,
  281. dataKey: item.field
  282. }]);
  283. });
  284. this.drawPDF(_.flatten(getColumns),rows);
  285. }
  286. },
  287. drawPDF: function (getColumns,rows) {
  288. var self = this;
  289. var desde =(this.$el.find('#from').val());
  290. var hasta =(this.$el.find('#to').val());
  291. var totalPagesExp = "{total_pages_count_string}";
  292. var pdfDoc = new jsPDF();
  293. pdfDoc.autoTable(getColumns, rows, {
  294. styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
  295. columnStyles: {
  296. number : {columnWidth: '8px'},
  297. origin : {columnWidth: '8px'},
  298. partner : {columnWidth: '8px'},
  299. date_invoice : {columnWidth: '8px'},
  300. amount_total : {halign:'right',columnWidth: '8px'},
  301. residual : {halign:'right',columnWidth: '8px'},
  302. },
  303. margin: { top: 16, horizontal: 7},
  304. addPageContent: function (data) {
  305. pdfDoc.setFontSize(12);
  306. pdfDoc.setFontStyle('bold');
  307. pdfDoc.setTextColor(40);
  308. pdfDoc.text('Histórico de Ventas ', data.settings.margin.left, 10);
  309. if(desde.length > 0 || hasta.length > 0){
  310. var fecha='';
  311. if(desde){
  312. fecha=fecha.concat(' Desde '+desde);
  313. }
  314. if (hasta){
  315. fecha=fecha.concat(' Hasta '+hasta);
  316. }
  317. pdfDoc.setFontSize(10);
  318. pdfDoc.setFontStyle('bold');
  319. pdfDoc.setTextColor(40)
  320. pdfDoc.text(fecha, data.settings.margin.left,14);
  321. }
  322. // FOOTER
  323. var str = "Pagina " + data.pageCount;
  324. // Total page number plugin only available in jspdf v1.0+
  325. if (typeof pdfDoc.putTotalPages === 'function') {
  326. str = str + " de " + totalPagesExp;
  327. }
  328. pdfDoc.setFontSize(9);
  329. pdfDoc.setFontStyle('bold');
  330. pdfDoc.setTextColor(40);
  331. pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
  332. }
  333. });
  334. if (typeof pdfDoc.putTotalPages === 'function') {
  335. pdfDoc.putTotalPages(totalPagesExp);
  336. }
  337. pdfDoc.save('Histórico de ventas.pdf')
  338. },
  339. });
  340. }