|
@@ -0,0 +1,342 @@
|
|
|
|
+openerp.invoice_print_nota = function (instance, local) {
|
|
|
|
+ local.widgetInstance = null;
|
|
|
|
+ local.parentInstance = null;
|
|
|
|
+
|
|
|
|
+ local.PrintNotaWidget = instance.Widget.extend({
|
|
|
|
+ template : "invoice_print_nota.PrintNota",
|
|
|
|
+ resInvoice:[],
|
|
|
|
+ resPartner:[],
|
|
|
|
+ partner:[],
|
|
|
|
+ pagare:[],
|
|
|
|
+ resInvoiceLine:[],
|
|
|
|
+ jsonDoc:[],
|
|
|
|
+ // Init
|
|
|
|
+
|
|
|
|
+ init:function(parent){
|
|
|
|
+ this._super(parent);
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ updateId : function(id){
|
|
|
|
+ var self = this;
|
|
|
|
+ self.id=id;
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ start: function () {
|
|
|
|
+ var self = this;
|
|
|
|
+ this.$el.click(function () {
|
|
|
|
+ self.fecthInitial();
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ fecthInitial: function(){
|
|
|
|
+ var id= openerp.webclient._current_state.id;
|
|
|
|
+ var self = this;
|
|
|
|
+ self.fecthInvoice(id).then(function(invoice){
|
|
|
|
+ return invoice;
|
|
|
|
+ }).then(function(invoice){
|
|
|
|
+ self.resInvoice = invoice;
|
|
|
|
+ return self.fetchInvoiceLine(invoice);
|
|
|
|
+ }).then(function(invoiceLine){
|
|
|
|
+ self.resInvoiceLine = invoiceLine;
|
|
|
|
+ return self.fetchPartner();
|
|
|
|
+ }).then(function(partner){
|
|
|
|
+ self.resPartner = partner;
|
|
|
|
+ return self.fetchCompany();
|
|
|
|
+ }).then(function(company){
|
|
|
|
+ self.resCompany = company;
|
|
|
|
+ return self.drawPDF();
|
|
|
|
+ });
|
|
|
|
+ return false;
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Consultar Pagos
|
|
|
|
+ fecthInvoice: function(id){
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var fields =['id', 'partner_id', 'type', 'number', 'origin', 'state', 'reference', 'currency_id', 'invoice_line', 'supplier_invoice_number','date_invoice','amount_total','user_id','company_id','amount_untaxed','amount_tax'];
|
|
|
|
+ var domain=[['id', '=', id]];
|
|
|
|
+ var Invoice = new instance.web.Model('account.invoice');
|
|
|
|
+ Invoice.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // Invoice line (Linea de Factura)
|
|
|
|
+ fetchInvoiceLine: function (invoice) {
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var linesIds = _.flatten(_.map(invoice, 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','price_subtotal','invoice_line_tax_id']).filter([['id', 'in', linesIds]]).all().then(function (results) {
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Consultar Clientes
|
|
|
|
+ fetchPartner: function () {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var partner_id = _.flatten(_.map(self.resInvoice,function(map){
|
|
|
|
+ return map.partner_id[0];
|
|
|
|
+ }));
|
|
|
|
+ var fields=['id','name','street','street2','state_id', 'country_id', 'phone'];
|
|
|
|
+ var domain=[['id','in', partner_id]];
|
|
|
|
+ var Partner = new instance.web.Model('res.partner');
|
|
|
|
+ Partner.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // Consultar Clientes
|
|
|
|
+ fetchCompany: function () {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var company_id = _.flatten(_.map(self.resInvoice,function(map){
|
|
|
|
+ return map.company_id[0];
|
|
|
|
+ }));
|
|
|
|
+ var fields=['id','name','street','street2','state_id', 'country_id'];
|
|
|
|
+ var domain=[['id','in', company_id]];
|
|
|
|
+ var Company = new instance.web.Model('res.company');
|
|
|
|
+ Company.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ getPartner: function(partner_id){
|
|
|
|
+ var self = this;
|
|
|
|
+ return _.filter(self.resPartner,function(item){
|
|
|
|
+ return item.id === partner_id;
|
|
|
|
+ }).shift();
|
|
|
|
+ },
|
|
|
|
+ getCompany: function(company_id){
|
|
|
|
+ var self = this;
|
|
|
|
+ return _.filter(self.resCompany,function(item){
|
|
|
|
+ return item.id === company_id;
|
|
|
|
+ }).shift();
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Obtener Detalles de la Factura
|
|
|
|
+ getInvoice: function (id_line){
|
|
|
|
+ return _.find(this.invoice, function (inv) {
|
|
|
|
+ return _.contains(inv.invoice_line, id_line);
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Verificar si los Valores no son nulos
|
|
|
|
+ valorNull:function(dato){
|
|
|
|
+ var valor ="";
|
|
|
|
+ if (dato){
|
|
|
|
+ valor=dato;
|
|
|
|
+ }
|
|
|
|
+ return valor;
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Generar el PDF
|
|
|
|
+ drawPDF:function(){
|
|
|
|
+ var self = this;
|
|
|
|
+ var doc=[];
|
|
|
|
+ var docItem=[];
|
|
|
|
+ var getColumns=[];
|
|
|
|
+ var resInvoice = self.resInvoice;
|
|
|
|
+ var resInvoiceLine = self.resInvoiceLine;
|
|
|
|
+ var pdfDoc = new jsPDF();
|
|
|
|
+ for (var k = 0; k < resInvoice.length; k++) {
|
|
|
|
+ var partner = self.getPartner(resInvoice[k].partner_id[0]);
|
|
|
|
+ var company = self.getCompany(resInvoice[k].company_id[0]);
|
|
|
|
+ doc.push({
|
|
|
|
+ id : resInvoice[k].id,
|
|
|
|
+ number : self.valorNull(resInvoice[k].number),
|
|
|
|
+ date : moment(resInvoice[k].date_invoice).format("DD [de] MMMM [de] YYYY"),
|
|
|
|
+ reference : resInvoice[k].reference,
|
|
|
|
+ amount : accounting.formatNumber(resInvoice[k].amount_total,0,".",","),
|
|
|
|
+ partner_id : self.valorNull(partner.name),
|
|
|
|
+ partner_ruc : self.valorNull(partner.ruc),
|
|
|
|
+ partner_street : self.valorNull(partner.street),
|
|
|
|
+ partner_tel : self.valorNull(partner.phone),
|
|
|
|
+ company_id : resInvoice[k].company_id[1],
|
|
|
|
+ customer_address : partner.state_id[1]+", "+partner.country_id[1],
|
|
|
|
+ company_address : company.state_id[1]+", "+company.country_id[1],
|
|
|
|
+ amount_untaxed : accounting.formatNumber(resInvoice[k].amount_untaxed,2,".",","),
|
|
|
|
+ amount_tax : accounting.formatNumber(resInvoice[k].amount_tax,2,".",","),
|
|
|
|
+ amount_total : accounting.formatNumber(resInvoice[k].amount_total,2,".",",")
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ var journal=resInvoice[k].id;
|
|
|
|
+
|
|
|
|
+ for (var i = 0; i < resInvoiceLine.length; i++) {
|
|
|
|
+ if(journal === resInvoiceLine[i].invoice_id[0]){
|
|
|
|
+ docItem.push({
|
|
|
|
+ line_invoice_id : resInvoiceLine[i].invoice_id[0],
|
|
|
|
+ line_quantity : accounting.formatNumber(resInvoiceLine[i].quantity,0,".",","),
|
|
|
|
+ line_name : resInvoiceLine[i].name,
|
|
|
|
+ line_price_unit : accounting.formatNumber(resInvoiceLine[i].price_unit,0,".",","),
|
|
|
|
+ line_price_subtotal : accounting.formatNumber(resInvoiceLine[i].price_subtotal,2,".",",")
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Nuevo pdf
|
|
|
|
+ getColumns.push({
|
|
|
|
+ title : 'Cantidad',
|
|
|
|
+ dataKey: 'line_quantity'
|
|
|
|
+ });
|
|
|
|
+ getColumns.push({
|
|
|
|
+ title : 'Descripcion',
|
|
|
|
+ dataKey: 'line_name'
|
|
|
|
+ });
|
|
|
|
+ getColumns.push({
|
|
|
|
+ title : 'Precio Unit.',
|
|
|
|
+ dataKey: 'line_price_unit'
|
|
|
|
+ });
|
|
|
|
+ getColumns.push({
|
|
|
|
+ title : 'SubTotal',
|
|
|
|
+ dataKey: 'line_price_subtotal'
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ var rows = [];
|
|
|
|
+ for (var j = 0; j < doc.length; j++) {
|
|
|
|
+ var factura = doc[j].id
|
|
|
|
+ var line = [];
|
|
|
|
+ for (var l = 0; l < docItem.length; l++) {
|
|
|
|
+ var linea_factura = docItem[l].line_invoice_id;
|
|
|
|
+ if (factura === linea_factura) {
|
|
|
|
+ line.push(docItem[l]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ rows = line;
|
|
|
|
+
|
|
|
|
+ pdfDoc.autoTable(getColumns, rows, {
|
|
|
|
+ theme: 'plain',
|
|
|
|
+ styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
|
|
|
|
+ columnStyles: {
|
|
|
|
+ line_quantity : {halign:'right',columnWidth: '4px'},
|
|
|
|
+ line_name : {columnWidth: '10px'},
|
|
|
|
+ line_price_unit : {columnWidth: '4px'},
|
|
|
|
+ line_price_subtotal : {columnWidth: '4px'},
|
|
|
|
+ },
|
|
|
|
+ margin: { top: 45, horizontal: 7},
|
|
|
|
+
|
|
|
|
+ addPageContent: function (data) {
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text('Fecha: ', data.settings.margin.left, 15);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].date, 21, 15);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text('Numero de Factura: ', data.settings.margin.left, 25);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].number, 42, 25);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text('Cliente: ', data.settings.margin.left, 30);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].partner_id, 25, 30);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(135, 30,'R.U.C. N°: ');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].partner_ruc, 160, 30);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text('Direccion: ', data.settings.margin.left, 35);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(28,35, doc[0].partner_street);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(135,35,'Tel.: ');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(147,35, doc[0].partner_tel);
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(135,120,'Sub Total: ');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].amount_untaxed,180,120,'right');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(135,124,'Iva: ');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].amount_tax,180,124,'right');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(135,128,'Total: ');
|
|
|
|
+
|
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
|
+ pdfDoc.setFontStyle('normal');
|
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
|
+ pdfDoc.text(doc[0].amount_total,180,128,'right');
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ pdfDoc.save('Nota Comun.pdf')
|
|
|
|
+ },
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ if(instance.web && instance.web.FormView){
|
|
|
|
+ instance.web.FormView.include({
|
|
|
|
+ load_form: function (record) {
|
|
|
|
+ this._super.apply(this, arguments);
|
|
|
|
+ if (this.model !== 'account.invoice') return
|
|
|
|
+ local.parentInstance = this;
|
|
|
|
+ if (local.widgetInstance) {
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+ local.parentInstance = this;
|
|
|
|
+ if (local.widgetInstance) {
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+ local.widgetInstance = new local.PrintNotaWidget(this);
|
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
|
+ elemento = elemento.find('.oe_right.oe_button_box.invoice_button_box');
|
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|