|
@@ -1,16 +1,51 @@
|
|
|
openerp.report_invoice_utility = function (instance, local) {
|
|
|
|
|
|
local.ReportWidget = instance.Widget.extend({
|
|
|
- template: 'ReportContainerTemplate1',
|
|
|
- invoices: [],
|
|
|
- invoiceLines: [],
|
|
|
- productProduct:[],
|
|
|
- Currency:[],
|
|
|
+ template: 'ReportContainerTemplate1',
|
|
|
+ invoices: [],
|
|
|
+ invoiceLines: [],
|
|
|
+ productProduct:[],
|
|
|
+ Currency:[],
|
|
|
|
|
|
start: function () {
|
|
|
this.fetchJournal();
|
|
|
+ this.fecthFecha();
|
|
|
this.$el.find('#report_form').submit(_.bind(this.submitForm, this));
|
|
|
},
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ },
|
|
|
// Buscar Diario
|
|
|
fetchJournal: function () {
|
|
|
var self = this;
|
|
@@ -23,34 +58,35 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
self.$el.find('#current-journal').append('<option value="9999999">TODAS LAS SUC.</option>');
|
|
|
});
|
|
|
},
|
|
|
-
|
|
|
+ // Buscar Cambio de Monedas USD,PYG,ARG,BRL
|
|
|
fetchCurency: function () {
|
|
|
var defer = $.Deferred();
|
|
|
var currency_Rate = new instance.web.Model('res.currency.rate');
|
|
|
- fields = ['id', 'name', 'currency_id', 'rate', 'create_date'];
|
|
|
- domain = [['currency_id', '=', [166 , 20, 7, 3]]];
|
|
|
+ var fields = ['id', 'name', 'currency_id', 'rate', 'create_date'];
|
|
|
+ var domain = [['currency_id', '=', [166 , 20, 7, 3]]];
|
|
|
currency_Rate.query(fields).filter(domain).all().then(function (results) {
|
|
|
defer.resolve(results);
|
|
|
});
|
|
|
return defer;
|
|
|
},
|
|
|
-
|
|
|
// Invoice (FACTURAS)
|
|
|
fetchInvoiceV2: function (journalId) {
|
|
|
- var domain = null;
|
|
|
- if( journalId == 9999999){
|
|
|
- domain = [['state', '=',['open','paid']],['type', '=', 'out_invoice']];
|
|
|
- }else{
|
|
|
- domain = [['state', '=',['open','paid']],['type', '=', 'out_invoice'],['journal_id.id', '=', journalId]];
|
|
|
- }
|
|
|
+ var desde =(this.$el.find('#from').val());
|
|
|
+ var hasta =(this.$el.find('#to').val());
|
|
|
|
|
|
+ var domain ="[['state', '=',['open','paid']],['type', '=', 'out_invoice']";
|
|
|
+ if( journalId != 9999999){
|
|
|
+ domain += ",['journal_id.id', '=', "+journalId+"]";
|
|
|
+ }
|
|
|
+ if (desde.length > 0 ){
|
|
|
+ domain += ",['date_invoice','>=', '"+desde+"'], ['date_invoice', '<=', '"+hasta+"']";
|
|
|
+ }
|
|
|
+ domain += "]";
|
|
|
var defer = $.Deferred();
|
|
|
-
|
|
|
var Invoice = new instance.web.Model('account.invoice');
|
|
|
- Invoice.query(['id', 'type', 'number', 'origin', 'state', 'journal_id', 'currency_id', 'invoice_line']).filter(domain).all().then(function (results) {
|
|
|
+ Invoice.query(['id', 'type', 'number', 'origin', 'state', 'journal_id', 'currency_id', 'invoice_line','date_invoice']).filter(domain).all().then(function (results) {
|
|
|
defer.resolve(results);
|
|
|
});
|
|
|
-
|
|
|
return defer;
|
|
|
},
|
|
|
// Invoice line (Linea de Factura)
|
|
@@ -59,12 +95,10 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
var linesIds = _.flatten(_.map(invoices, function (item) {
|
|
|
return item.invoice_line;
|
|
|
}));
|
|
|
-
|
|
|
var InvoiceLine = new instance.web.Model('account.invoice.line');
|
|
|
InvoiceLine.query(['id', 'quantity', 'price_unit', 'discount', 'name', 'product_id', 'origin','invoice_id']).filter([['id', 'in', linesIds]]).all().then(function (results) {
|
|
|
defer.resolve(results)
|
|
|
});
|
|
|
-
|
|
|
return defer;
|
|
|
},
|
|
|
// Product Product
|
|
@@ -73,21 +107,20 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
var porductIDS = _.flatten(_.map(invoiceLines, function (item) {
|
|
|
return item.product_id[0];
|
|
|
}));
|
|
|
-
|
|
|
var ProductProdcut = new instance.web.Model('product.product');
|
|
|
- var fields = ['id', 'default_code', 'name_template', 'factory_code', 'factory_reference', 'factory_barcode', 'standard_price'];
|
|
|
+ var fields = ['id', 'default_code', 'name_template', 'factory_code', 'factory_reference', 'factory_barcode', 'standard_price','type'];
|
|
|
ProductProdcut.query(fields).filter([['id', 'in', porductIDS]]).all().then(function (results) {
|
|
|
defer.resolve(results)
|
|
|
});
|
|
|
-
|
|
|
return defer;
|
|
|
},
|
|
|
-
|
|
|
// Cansultar
|
|
|
submitForm: function (e) {
|
|
|
+ var desde =this.$el.find('#from').val();
|
|
|
+ var hasta =this.$el.find('#to').val();
|
|
|
e.preventDefault();
|
|
|
var formData = this.$(e.currentTarget).serializeJSON();
|
|
|
- if (formData.journal == 0) {
|
|
|
+ if ((formData.journal == 0) || (((desde.length == 0) && (hasta.length > 0)) || ((desde.length > 0) && (hasta.length == 0)))){
|
|
|
$("#dialog" ).dialog({
|
|
|
autoOpen: true,
|
|
|
resizable: false,
|
|
@@ -111,11 +144,11 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
}
|
|
|
});
|
|
|
return;
|
|
|
- }
|
|
|
+ }
|
|
|
+ var self = this;
|
|
|
this.fetchCurency().then(function(currency) {
|
|
|
self.Currency = currency;
|
|
|
});
|
|
|
- var self = this;
|
|
|
this.fetchInvoiceV2(formData.journal).then(function (invoices) {
|
|
|
self.invoices = invoices;
|
|
|
return invoices;
|
|
@@ -128,47 +161,58 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
self.productProduct = productProduct;
|
|
|
return self.invoice_Currency();
|
|
|
});
|
|
|
-
|
|
|
return false;
|
|
|
},
|
|
|
-
|
|
|
- invoice_Currency: function(){
|
|
|
- var currency_new = null;
|
|
|
+ // Obtener Monedas de la Factura
|
|
|
+ getCutrrency: function (id){
|
|
|
+ return _.find(this.Currency,function (curr) {
|
|
|
+ return _.contains(curr.currency_id,id);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // Actualizar cambio de las moneda de Factura
|
|
|
+ invoice_Currency: function(){
|
|
|
for (var i = 0; i < this.invoices.length; i++) {
|
|
|
- item = this.invoices[i];
|
|
|
- currency_new = _.find(this.Currency, function(curr){
|
|
|
- return _.contains(curr.currency_id, item.currency_id[0]);
|
|
|
- });
|
|
|
- if (!currency_new){
|
|
|
+ var currency_new;
|
|
|
+ var item = this.invoices[i];
|
|
|
+ var id = item.currency_id[0];
|
|
|
+ currency_new = this.getCutrrency(id)
|
|
|
+ if (!currency_new){
|
|
|
currency_new={};
|
|
|
currency_new.rate=1;
|
|
|
- }
|
|
|
- this.invoices[i].rate=(currency_new.rate);
|
|
|
+ }
|
|
|
+ this.invoices[i].rate=(currency_new.rate);
|
|
|
}
|
|
|
- return this.fectUtility();
|
|
|
+ return this.fectUtility();
|
|
|
},
|
|
|
-
|
|
|
+ // Obtener la Detalles de la Factura
|
|
|
+ getInvoice: function (id_line){
|
|
|
+ return _.find(this.invoices, function (inv) {
|
|
|
+ return _.contains(inv.invoice_line, id_line);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // Obtener las lineas de las Facturas
|
|
|
+ getProduct: function(pro_id){
|
|
|
+ return _.find(this.productProduct, function(prod){
|
|
|
+ return _.contains(pro_id, prod.id);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // unir los objetos
|
|
|
fectUtility: function(){
|
|
|
- var data = [];
|
|
|
- var item = null;
|
|
|
- var invoice = null;
|
|
|
- var producto =null;
|
|
|
+ var data=[];
|
|
|
+ var item;
|
|
|
+ var invoice;
|
|
|
+ var producto;
|
|
|
|
|
|
for (var i = 0; i < this.invoiceLines.length; i++) {
|
|
|
item = this.invoiceLines[i];
|
|
|
-
|
|
|
- invoice = _.find(this.invoices, function (inv) {
|
|
|
- return _.contains(inv.invoice_line, item.id);
|
|
|
- });
|
|
|
- producto =_.find(this.productProduct, function(prod){
|
|
|
- return _.contains(item.product_id, prod.id);
|
|
|
- });
|
|
|
+ invoice = this.getInvoice(item.id)
|
|
|
+ producto =this.getProduct(item.product_id)
|
|
|
if (!producto){
|
|
|
producto={};
|
|
|
producto.standard_price=0;
|
|
|
}
|
|
|
-
|
|
|
- data.push({number : (invoice.number),
|
|
|
+ if (producto.type =="product"){
|
|
|
+ data.push({number : (invoice.number),
|
|
|
name : (item.name),
|
|
|
quantity : (item.quantity),
|
|
|
price_unity : (item.price_unit.toFixed(2) / invoice.rate.toFixed(2)),
|
|
@@ -176,16 +220,18 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
price_tot : (item.quantity * (item.price_unit.toFixed(2) / invoice.rate.toFixed(2))),
|
|
|
standar_tot : (item.quantity * producto.standard_price.toFixed(2)),
|
|
|
utility : ((item.quantity * (item.price_unit.toFixed(2) / invoice.rate.toFixed(2))) - (item.quantity * producto.standard_price)).toFixed(2)});
|
|
|
-
|
|
|
+ }
|
|
|
}
|
|
|
this.drawPDF(data);
|
|
|
},
|
|
|
-
|
|
|
+ // Generar el pdfDoc
|
|
|
drawPDF: function (rows) {
|
|
|
- rows2=[];
|
|
|
+ var rows2=[];
|
|
|
var sucusal = this.sucDescrip = this.$el.find('#current-journal option:selected').text();
|
|
|
+ var desde =(this.$el.find('#from').val());
|
|
|
+ var hasta =(this.$el.find('#to').val());
|
|
|
var totalPagesExp = "{total_pages_count_string}";
|
|
|
- getColumns = [
|
|
|
+ var getColumns = [
|
|
|
{title: "Factura", dataKey: "number"},
|
|
|
{title: "Producto", dataKey: "name"},
|
|
|
{title: "Cantidad", dataKey: "quantity"},
|
|
@@ -197,7 +243,7 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
];
|
|
|
|
|
|
var pdfDoc = new window.jsPDF();
|
|
|
- var quantity=precio_cost=precio_unit=tol_cost=tot_unit=utility=0;
|
|
|
+ var quantity=0,precio_cost=0,precio_unit=0,tol_cost=0,tot_unit=0,utility=0;
|
|
|
_.each(rows, function (datos) {
|
|
|
quantity += datos.quantity;
|
|
|
precio_unit += datos.price_unity;
|
|
@@ -233,54 +279,49 @@ openerp.report_invoice_utility = function (instance, local) {
|
|
|
standar_price : accounting.formatNumber(rows1.standar_price, 2, ".", ","),
|
|
|
price_tot : accounting.formatNumber(rows1.price_tot, 2, ".", ","),
|
|
|
standar_tot : accounting.formatNumber(rows1.standar_tot, 2, ".", ","),
|
|
|
- utility :accounting.formatNumber(rows1.utility, 2, ".", ",")});
|
|
|
+ utility : accounting.formatNumber(rows1.utility, 2, ".", ",")});
|
|
|
});
|
|
|
|
|
|
pdfDoc.autoTable(getColumns, rows2, {
|
|
|
-
|
|
|
- theme: 'striped', // 'striped', 'grid' or 'plain',
|
|
|
- // startY: pdfDoc.autoTable.previous.finalY + 15,
|
|
|
- // margin: {},
|
|
|
- // tableLineWidth: 0.75,
|
|
|
- styles: { overflow: 'hidden',
|
|
|
- fontSize: 7,
|
|
|
- columnWidth: 'wrap'},
|
|
|
- columnStyles: {
|
|
|
- // text: {columnWidth: 'auto'},
|
|
|
- number: {fontStyle: 'bold'},
|
|
|
+ styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
|
|
|
+ columnStyles: {number: {fontStyle: 'bold'},
|
|
|
name :{columnWidth: '10px'},
|
|
|
quantity :{halign:'right' },
|
|
|
price_unity : {halign:'right' },
|
|
|
standar_price : {halign:'right' },
|
|
|
price_tot : {halign:'right' },
|
|
|
standar_tot : {halign:'right' },
|
|
|
- utility : {halign:'right'},
|
|
|
-
|
|
|
- },
|
|
|
-
|
|
|
- margin: { top: 15, horizontal: 7},
|
|
|
+ utility : {halign:'right'},},
|
|
|
+ margin: { top: 16, horizontal: 7},
|
|
|
|
|
|
addPageContent: function (data) {
|
|
|
- pdfDoc.text('Análisis de Utilidad '+ sucusal, 10, 10);
|
|
|
+ pdfDoc.setFontSize(12);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text('Análisis de Utilidad '+ sucusal, data.settings.margin.left, 10);
|
|
|
+ if(desde.length > 0 ){
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40)
|
|
|
+ pdfDoc.text('Desde: '+desde+' Hasta: '+hasta, 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(7);
|
|
|
+ pdfDoc.setFontSize(9);
|
|
|
pdfDoc.setFontStyle('bold');
|
|
|
pdfDoc.setTextColor(40);
|
|
|
- pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 10);
|
|
|
+ pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
|
|
|
}
|
|
|
-
|
|
|
});
|
|
|
if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
pdfDoc.putTotalPages(totalPagesExp);
|
|
|
}
|
|
|
pdfDoc.save('Analisis de utilidad.pdf')
|
|
|
- // pdfDoc.output('dataurlnewwindow');
|
|
|
- },
|
|
|
+/ },
|
|
|
|
|
|
});
|
|
|
instance.web.client_actions.add('report_invoice_utility.action_report', 'instance.report_invoice_utility.ReportWidget');
|