|
@@ -0,0 +1,395 @@
|
|
|
+function report_balance (reporting){
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var instance = openerp;
|
|
|
+
|
|
|
+ reporting.ReportBalanceWidget = reporting.Base.extend({
|
|
|
+ template: 'ReportBalance',
|
|
|
+ invoice: [],
|
|
|
+ Currency:[],
|
|
|
+ resCurrency :[],
|
|
|
+ resCompany:[],
|
|
|
+ accountJournal:[],
|
|
|
+ supplier:[],
|
|
|
+ content:[],
|
|
|
+ rowsData :[],
|
|
|
+ // event
|
|
|
+ events:{
|
|
|
+ 'click #toolbar > button' : 'clickOnAction',
|
|
|
+ 'change #from' : 'factSearch',
|
|
|
+ 'change #to' : 'factSearch',
|
|
|
+ 'click-row.bs.table #table ' : 'ckickAnalysisDetail',
|
|
|
+ },
|
|
|
+ // Initil
|
|
|
+ init : function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+ // start
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.rowsData});
|
|
|
+ this.fecthFecha();
|
|
|
+ this.submitForm();
|
|
|
+ },
|
|
|
+ // Analisis Detallado
|
|
|
+ ckickAnalysisDetail: function(e, row, $element,field){
|
|
|
+ if (field == 'number'){
|
|
|
+ this.do_action({
|
|
|
+ name:"Registro del Cliente",
|
|
|
+ type: 'ir.actions.act_window',
|
|
|
+ res_model: "res.partner",
|
|
|
+ views: [[false,'form']],
|
|
|
+ target: 'new',
|
|
|
+ domain: [['id','=', row.id]],
|
|
|
+ context: {},
|
|
|
+ flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
|
|
|
+ res_id: row.id,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ e.stopImmediatePropagation();
|
|
|
+ },
|
|
|
+ // Consultar
|
|
|
+ submitForm: function () {
|
|
|
+ var self = this;
|
|
|
+ self.fetchAccountInvoice().then(function(AccountInvoice) {
|
|
|
+ return AccountInvoice;
|
|
|
+ }).then(function(AccountInvoice){
|
|
|
+ self.AccountInvoice = AccountInvoice;
|
|
|
+ return self.fetchResPartner();
|
|
|
+ }).then(function(ResPartner){
|
|
|
+ self.ResPartner = ResPartner;
|
|
|
+ self.search();
|
|
|
+ return self.buildTable();
|
|
|
+
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // Fecha
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fetchAccountInvoice: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var AccountInvoice = new instance.web.Model('account.invoice');
|
|
|
+ AccountInvoice.query(['id','number','partner_id','amount_total','residual','state','date_invoice','type']).filter([['state','in',['open','paid']]]).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],['customer','=',true]]).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ valorNull:function(dato){
|
|
|
+ var valor ="";
|
|
|
+ if (dato){
|
|
|
+ valor=dato;
|
|
|
+ }
|
|
|
+ return valor;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Buscador
|
|
|
+ search: function () {
|
|
|
+ var self = this;
|
|
|
+ var results = self.ResPartner;
|
|
|
+ results = _.map(results, function (item) {
|
|
|
+ return {
|
|
|
+ label: item.name,
|
|
|
+ value: item.id + '-'+ item.name
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$('#customer').autocomplete({
|
|
|
+ source: results,
|
|
|
+ minLength:0,
|
|
|
+ search: function(event, ui) {
|
|
|
+ if (!(self.$('#customer').val())){
|
|
|
+ self.factSearch();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ close: function( event, ui ) {
|
|
|
+ self.factSearch();
|
|
|
+ },
|
|
|
+ select: function(event, ui) {
|
|
|
+ self.factSearch();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ buildTable: function(){
|
|
|
+ var self = this;
|
|
|
+ var invoice = self.AccountInvoice;
|
|
|
+ var entry;
|
|
|
+ var egress;
|
|
|
+ var to_receive;
|
|
|
+ var to_pay;
|
|
|
+ var data = [];
|
|
|
+ _.each(invoice, function(item){
|
|
|
+ if (item.type == 'out_invoice') {
|
|
|
+ entry = item.amount_total - item.residual;
|
|
|
+ egress = 0;
|
|
|
+ to_receive = item.residual;
|
|
|
+ to_pay = 0;
|
|
|
+ } else {
|
|
|
+ entry = 0;
|
|
|
+ egress = item.amount_total - item.residual;
|
|
|
+ to_receive = 0;
|
|
|
+ to_pay = item.residual;
|
|
|
+ }
|
|
|
+ data.push({
|
|
|
+ id : item.id,
|
|
|
+ number : item.number,
|
|
|
+ partner : item.partner_id[1],
|
|
|
+ partner_id : item.partner_id[0],
|
|
|
+ date_invoice : moment(item.date_invoice).format("DD/MM/YYYY"),
|
|
|
+ date : item.date_invoice,
|
|
|
+ amount_total : accounting.formatNumber(item.amount_total,0,".",","),
|
|
|
+ entry : accounting.formatNumber(entry,0,".",","),
|
|
|
+ egress : accounting.formatNumber(egress,0,".",","),
|
|
|
+ to_receive : accounting.formatNumber(to_receive,0,".",","),
|
|
|
+ to_pay : accounting.formatNumber(to_pay,0,".",","),
|
|
|
+
|
|
|
+ intamount_total : item.amount_total,
|
|
|
+ intentry : entry,
|
|
|
+ integress : egress,
|
|
|
+ intreceive : to_receive,
|
|
|
+ intpay : to_pay,
|
|
|
+ });
|
|
|
+ });
|
|
|
+ self.content = data;
|
|
|
+ this.loadTable(data);
|
|
|
+ },
|
|
|
+ // Buscar
|
|
|
+ factSearch: function(){
|
|
|
+ var self = this;
|
|
|
+ var desde =this.$el.find('#from').val();
|
|
|
+ var hasta =this.$el.find('#to').val();
|
|
|
+ var customer= this.$el.find('#customer').val().split('-');
|
|
|
+ var content = self.content;
|
|
|
+ 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]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (customer != ""){
|
|
|
+ content = _.filter(content, function(inv){
|
|
|
+ return inv.partner_id == customer[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(){
|
|
|
+ var self = this;
|
|
|
+ var rows=[];
|
|
|
+ var data = self.content;
|
|
|
+ var desde =this.$el.find('#from').val();
|
|
|
+ var hasta =this.$el.find('#to').val();
|
|
|
+ var customer= this.$el.find('#customer').val().split('-');
|
|
|
+ if (desde.length > 0){
|
|
|
+ var date= desde.split('/');
|
|
|
+ data = _.filter(data, function (inv){
|
|
|
+ return inv.date >= (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (hasta.length > 0){
|
|
|
+ var date= desde.split('/');
|
|
|
+ data = _.filter(data, function (inv){
|
|
|
+ return inv.date <= (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (customer != ""){
|
|
|
+ data = _.filter(data, function(inv){
|
|
|
+ return inv.partner_id == customer[0];
|
|
|
+ });
|
|
|
+ }
|
|
|
+ rows = data;
|
|
|
+
|
|
|
+ // Valor de la Factura
|
|
|
+ var intamount_total = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.intamount_total);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ // Ingreso
|
|
|
+ var intentry = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.intentry);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ // Egreso
|
|
|
+ var integress = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.integress);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ // Monto a Cobrar
|
|
|
+ var intreceive = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.intreceive);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ var intpay = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.intpay);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ rows.push({
|
|
|
+ id : '',
|
|
|
+ number : 'Totales',
|
|
|
+ partner : '',
|
|
|
+ partner_id : '',
|
|
|
+ date_invoice : '',
|
|
|
+ date : '',
|
|
|
+ amount_total : accounting.formatNumber(intamount_total,0,".",","),
|
|
|
+ entry : accounting.formatNumber(intentry,0,".",","),
|
|
|
+ egress : accounting.formatNumber(integress,0,".",","),
|
|
|
+ to_receive : accounting.formatNumber(intreceive,0,".",","),
|
|
|
+ to_pay : accounting.formatNumber(intpay,0,".",","),
|
|
|
+
|
|
|
+ intamount_total : 0,
|
|
|
+ intentry : 0,
|
|
|
+ integress : 0,
|
|
|
+ intreceive : 0,
|
|
|
+ intpay : 0,
|
|
|
+ });
|
|
|
+ return rows;
|
|
|
+ },
|
|
|
+ // imprimir PDF
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // Generar pdfDoc
|
|
|
+ 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('l');
|
|
|
+
|
|
|
+ pdfDoc.autoTable(getColumns, rows, {
|
|
|
+ styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
|
|
|
+ columnStyles: {
|
|
|
+ number : {columnWidth: '8px'},
|
|
|
+ partner : {columnWidth: '8px'},
|
|
|
+ partner_id : {columnWidth: '8px'},
|
|
|
+ date_invoice : {columnWidth: '8px'},
|
|
|
+ date : {columnWidth: '8px'},
|
|
|
+ amount_total : {halign:'right',columnWidth: '8px'},
|
|
|
+ entry : {halign:'right',columnWidth: '8px'},
|
|
|
+ egress : {halign:'right',columnWidth: '8px'},
|
|
|
+ to_receive : {halign:'right',columnWidth: '8px'},
|
|
|
+ to_pay : {halign:'right',columnWidth: '8px'},
|
|
|
+ },
|
|
|
+ margin: { top: 16, horizontal: 7},
|
|
|
+
|
|
|
+ addPageContent: function (data) {
|
|
|
+ pdfDoc.setFontSize(12);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text('Balance de Sumas y Saldos', 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('Balance de Sumas y Saldos.pdf')
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|