123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- function report_vouchers (reporting){
- "use strict";
- var instance = openerp;
- reporting.ReportVoucherWidget = reporting.Base.extend({
- template: 'ReportVoucher',
- AccountVoucher: [],
- content:[],
- rowsData :[],
- events:{
- 'click #toolbar > button' : 'clickOnAction',
- 'click #X' : 'factSearch',
- 'click #A' : 'factSearch',
- 'click #B' : 'factSearch',
- 'click #C' : 'factSearch',
- 'click #D' : 'factSearch',
- 'click #Z' : 'factSearch',
- 'click #Y' : 'factSearch',
- 'click #customer' : 'factSearch',
- 'click #supplier' : 'factSearch',
- 'change #current-journal' : 'factSearch',
- 'change #from' : 'factSearch',
- 'change #to' : 'factSearch',
- 'click-row.bs.table #table ' : 'clickAnalysisDetail',
- },
- init : function(parent){
- this._super(parent);
- },
- start: function () {
- var self = this;
- var table = this.$el.find('#table');
- table.bootstrapTable({data : self.rowsData});
- this.fecthFecha();
- this.submitForm();
- },
- valorNull:function(dato){
- var valor ="";
- if (dato){
- valor=dato;
- }
- return valor;
- },
- clickAnalysisDetail: function(e, row, $element, field){
- if (field == 'reference'){
- this.do_action({
- name:"Factura",
- type: 'ir.actions.act_window',
- res_model: "account.invoice",
- views: [[false,'form']],
- target: 'new',
- domain: [['id','=', row.invoice_id]],
- context: {},
- flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
- res_id: row.invoice_id,
- });
- }
- if (field === 'partner'){
- this.do_action({
- name:"Registro",
- type: 'ir.actions.act_window',
- res_model: "res.partner",
- views: [[false,'form']],
- target: 'new',
- domain: [['id','=', row.partner_id]],
- context: {},
- flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
- res_id: row.partner_id,
- });
- }
- e.stopImmediatePropagation();
- },
- submitForm: function () {
- var self = this;
- self.fetchAccountVoucher().then(function(AccountVoucher) {
- return AccountVoucher;
- }).then(function (AccountVoucher) {
- self.AccountVoucher = AccountVoucher;
- return self.fetchResPartner();
- }).then(function(ResPartner){
- self.ResPartner = ResPartner;
- return self.fetchAccountInvoice();
- }).then(function(AccountInvoice){
- self.AccountInvoice = AccountInvoice;
- return self.fetchAccountJournal();
- }).then(function (AccountJournal) {
- self.AccountJournal =AccountJournal;
- self.$el.find('#current-journal').append('<option value="9999999">Todos los pagos</option>');
- _.each(AccountJournal, function (item) {
- self.$el.find('#current-journal').append('<option value="' + item.id + '">' + item.name + '</option>');
- });
- self.search();
- return self.BuildTable();
- });
- },
- fecthFecha: function() {
- var to;
- var dateFormat1 = "mm/dd/yy",
- from = $( "#from" )
- .datepicker({
- dateFormat: "dd/mm/yy",
- changeMonth: true,
- numberOfMonths: 1,
- })
- .on( "change", function() {
- to.datepicker( "option", "minDate", getDate(this), "dd/mm/yyyy");
- });
- to = $( "#to" ).datepicker({
- dateFormat: "dd/mm/yy",
- defaultDate: "+7d",
- changeMonth: true,
- numberOfMonths: 1,
- })
- .on( "change", function() {
- from.datepicker( "option", "maxDate", getDate(this));
- });
- function getDate( element ) {
- var fechaSel =element.value.split('/');
- var date;
- try {
- date = $.datepicker.parseDate( dateFormat1, (fechaSel[1]+"/"+fechaSel[0]+"/"+fechaSel[2]));
- } catch( error ) {
- date = null;
- }
- return date;
- }
- },
- fetchAccountVoucher: function () {
- var self = this;
- var defer = $.Deferred();
- var AccountVoucher = new instance.web.Model('account.voucher');
- AccountVoucher.query(['id', 'number','reference','partner_id','journal_id','amount','date','number','type']).filter([['state', '=', 'posted']]).all().then(function(results){
- defer.resolve(results);
- });
- return defer;
- },
- fetchResPartner: function () {
- var self = this;
- var defer = $.Deferred();
- var ResPartner = new instance.web.Model('res.partner');
- ResPartner.query(['id','name','ruc']).filter([['active', '=', true]]).all().then(function(results){
- defer.resolve(results);
- });
- return defer;
- },
- fetchAccountInvoice: function () {
- var self = this;
- var defer = $.Deferred();
- var AccountInvoice = new instance.web.Model('account.invoice');
- AccountInvoice.query(['id','number']).filter([['state', 'in', ['open','paid']]]).all().then(function(results){
- defer.resolve(results);
- });
- return defer;
- },
- fetchAccountJournal: function () {
- var self = this;
- var defer = $.Deferred();
- var AccountJournal = new instance.web.Model('account.journal');
- AccountJournal.query(['id','name']).filter([['active','=',true],['type', 'in', ['bank','cash']]]).all().then(function(results){
- defer.resolve(results);
- });
- return defer;
- },
- getAccountInvoice: function (number) {
- var self = this;
- return _.filter(self.AccountInvoice,function (item) {
- return item.number == number;
- });
- },
- search: function () {
- var self = this;
- var results = self.ResPartner;
- results = _.map(results, function (item) {
- return {
- label: item.id + '-'+ item.name + ' ' + self.valorNull(item.ruc),
- value: item.id + '-'+ item.name + ' ' + self.valorNull(item.ruc)
- }
- });
- self.$('#partner').autocomplete({
- source: results,
- minLength:0,
- search: function(event, ui) {
- if (!(self.$('#partner').val())){
- self.factSearch();
- }
- },
- close: function( event, ui ) {
- self.factSearch();
- },
- select: function(event, ui) {
- self.factSearch();
- }
- });
- },
- BuildTable: function(){
- var self = this;
- var AccountVoucher = self.AccountVoucher;
- var invoice;
- var amount_in;
- var amount_out;
- var data = [];
- _.each(AccountVoucher, function(item){
- invoice = self.getAccountInvoice(item.reference).shift();
- if (item.type == 'payment') {
- amount_in = 0;
- amount_out = item.amount;
- }else{
- amount_in = item.amount;
- amount_out = 0;
- }
- data.push({
- id : item.id,
- reference: item.reference,
- number: item.number,
- journal: item.journal_id[1],
- date_voucher: moment(item.date).format("DD/MM/YYYY"),
- partner: item.partner_id[1],
- amount_in: accounting.formatNumber(amount_in,2,".",","),
- amount_out: accounting.formatNumber(amount_out,2,".",","),
- type: item.type,
- date: item.date,
- journal_id: item.journal_id[0],
- partner_id: item.partner_id[0],
- invoice_id: invoice.id,
- in_amount:amount_in,
- out_amount:amount_out
- });
- });
- self.content = data;
- this.loadTable(data);
- },
- factSearch: function(){
- var self = this;
- var hoy = moment().format('YYYY-MM-DD');
- var journal = this.$el.find('#current-journal').val();
- var desde = this.$el.find('#from').val();
- var hasta = this.$el.find('#to').val();
- var partner= this.$el.find('#partner').val().split('-');
- var content = self.content;
- if ($('#A').is(":checked")){
- content = _.filter(content, function (inv){
- return inv.date == hoy;
- });
- }
- if ($('#B').is(":checked")){
- var date = hoy.split('-');
- var ayer = date[2] - 1;
- date.splice(2,0);
- if(date[2] < 10){
- date[2] = '0'+ayer;
- }else{
- date[2] = ayer;
- }
- content = _.filter(content, function (inv){
- return inv.date == date[0]+'-'+date[1]+'-'+date[2];
- });
- }
- if ($('#C').is(":checked")){
- var date = hoy.split('-');
- content = _.filter(content, function (inv){
- var mes = inv.date.split('-');
- return mes[0]+'-'+mes[1] == date[0]+'-'+date[1];
- });
- }
- if ($('#D').is(":checked")){
- var date = hoy.split('-');
- var mes = date[1] - 1;
- var year;
- date.splice(1,0);
- if(date[1] == 1){
- date[1] = '12';
- year = date[0] - 1;
- date[0] = year;
- }else{
- if(date[1] < 10){
- date[1] = '0'+mes;
- }else{
- date[1] = mes;
- }
- }
- content = _.filter(content, function (inv){
- var mes = inv.date.split('-');
- return mes[0]+'-'+mes[1] == date[0]+'-'+date[1];
- });
- }
- if ($('#Z').is(":checked")){
- $('#datepicker').css('display','block');
- if (desde.length > 0){
- var date= desde.split('/');
- content = _.filter(content, function (inv){
- return inv.date >= (date[2]+"-"+date[1]+"-"+date[0]);
- });
- }
- if (hasta.length > 0){
- var date= hasta.split('/');
- content = _.filter(content, function (inv){
- return inv.date <= (date[2]+"-"+date[1]+"-"+date[0]);
- });
- }
- }else{
- $('#datepicker').css('display','none');
- }
- if ($('#customer').is(":checked")){
- content = _.filter(content, function (inv){
- return inv.type == 'receipt';
- });
- }
- if ($('#supplier').is(":checked")){
- content = _.filter(content, function (inv){
- return inv.type == 'payment';
- });
- }
- if(journal != 9999999){
- content = _.filter(content,function(inv){
- return inv.journal_id == journal;
- });
- }
- if (partner != ""){
- content = _.filter(content, function(inv){
- return inv.partner_id == partner[0];
- });
- }
- var amount_in_amount = _.reduce(_.map(content,function(map){
- return(map.in_amount);
- }),function(memo, num){
- return memo + num;
- },0);
- var amount_out_amount = _.reduce(_.map(content,function(map){
- return(map.out_amount);
- }),function(memo, num){
- return memo + num;
- },0);
- content.push({
- number: "Total",
- amount_in: accounting.formatNumber((amount_in_amount),0,".",","),
- amount_out: accounting.formatNumber((amount_out_amount),0,".",","),
- });
- self.loadTable(content)
- },
- loadTable:function(rowsTable){
- var self = this;
- self.rowsData = rowsTable;
- var table = this.$el.find('#table');
- table.bootstrapTable('load',rowsTable);
- },
- getObjetPdf: function(rowsTable){
- var self = this;
- var rowsPdf=[];
- var rows=[];
- var rows = self.rowsData;
- var total_in = _.reduce(_.map(rows,function(map){
- return(map.in_amount);
- }),function(memo, num){
- return memo + num;
- },0);
- var total_out = _.reduce(_.map(rows,function(map){
- return(map.out_amount);
- }),function(memo, num){
- return memo + num;
- },0);
- if (rows.length > 0){
- rows.push({
- reference: "Totales ",
- amount_in: accounting.formatNumber(total_in,2,".",","),
- amount_out: accounting.formatNumber(total_out,2,".",","),
- });
- }
- return rows;
- },
- clickOnAction: function (e) {
- var self = this;
- var rowsNew;
- var action = self.$el.find(e.target).val();
- var table = self.$el.find("#table");
- var data2 = table.bootstrapTable('getVisibleColumns');
- var getColumns=[];
- var rows=[];
- rowsNew = self.getObjetPdf();
- if (action === 'pdf') {
- var dataNEW = _.map(data2, function (val){
- return val.field;
- });
- _.each(rowsNew,function (item){
- rows.push(_.pick(item, dataNEW));
- });
- // Obtener los nombre de la Cabezera
- _.each(_.map(data2,function(val){
- return val;
- }), function(item){
- getColumns.push([{
- title: item.title,
- dataKey: item.field
- }]);
- });
- this.drawPDF(_.flatten(getColumns),rows);
- }
- },
- drawPDF: function (getColumns,rows) {
- var self = this;
- var desde =(this.$el.find('#from').val());
- var hasta =(this.$el.find('#to').val());
- var totalPagesExp = "{total_pages_count_string}";
- var pdfDoc = new jsPDF();
- pdfDoc.autoTable(getColumns, rows, {
- styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
- columnStyles: {
- reference : {columnWidth: '10px'},
- number : {columnWidth: '7px'},
- journal_id : {columnWidth: '6px'},
- date_voucher : {columnWidth: '6px'},
- partner : {columnWidth: '8px'},
- amount_in : {halign:'right',columnWidth: '8px'},
- amount_out: {halign:'right',columnWidth: '8px'}
- },
- margin: { top: 16, horizontal: 7},
- addPageContent: function (data) {
- pdfDoc.setFontSize(12);
- pdfDoc.setFontStyle('bold');
- pdfDoc.setTextColor(40);
- pdfDoc.text('Histórico de pagos ', data.settings.margin.left, 10);
- if(desde.length > 0 || hasta.length > 0){
- var fecha='';
- if(desde){
- fecha=fecha.concat(' Desde '+desde);
- }
- if (hasta){
- fecha=fecha.concat(' Hasta '+hasta);
- }
- pdfDoc.setFontSize(10);
- pdfDoc.setFontStyle('bold');
- pdfDoc.setTextColor(40)
- pdfDoc.text(fecha, data.settings.margin.left,14);
- }
- // FOOTER
- var str = "Pagina " + data.pageCount;
- // Total page number plugin only available in jspdf v1.0+
- if (typeof pdfDoc.putTotalPages === 'function') {
- str = str + " de " + totalPagesExp;
- }
- pdfDoc.setFontSize(9);
- pdfDoc.setFontStyle('bold');
- pdfDoc.setTextColor(40);
- pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
- }
- });
- if (typeof pdfDoc.putTotalPages === 'function') {
- pdfDoc.putTotalPages(totalPagesExp);
- }
- pdfDoc.save('Histórico de pagos.pdf')
- },
- });
- }
|