diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index baee61b8e469..e4fac53c3094 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -207,10 +207,10 @@ -
+ {{ Former::select('invoice_design_id')->label('Design')->style('display:inline;width:120px')->raw() ->fromQuery($invoiceDesigns, 'name', 'id')->data_bind("value: invoice_design_id") }} -
+ {{ Button::normal('Download PDF', array('onclick' => 'onDownloadClick()')) }} @@ -558,6 +558,15 @@ invoice.imageWidth = {{ $account->getLogoWidth() }}; invoice.imageHeight = {{ $account->getLogoHeight() }}; @endif + + + //define logo images + + + invoice.imageLogo1 = "{{ HTML::image_data('images/report_logo1.jpg') }}"; + invoice.imageLogoWidth1 =120; + invoice.imageLogoHeight1 = 40 + return invoice; } diff --git a/public/images/report_logo1.jpg b/public/images/report_logo1.jpg new file mode 100644 index 000000000000..64fe9c87a8a0 Binary files /dev/null and b/public/images/report_logo1.jpg differ diff --git a/public/js/script.js b/public/js/script.js old mode 100755 new mode 100644 index 9b5c18274603..7c16fd03c51c --- a/public/js/script.js +++ b/public/js/script.js @@ -6,381 +6,22 @@ var isChrome = !!window.chrome && !isOpera; // Chrome 1+ var isChromium = isChrome && navigator.userAgent.indexOf('Chromium') >= 0; var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6 + + + + function generatePDF(invoice, checkMath) { - var client = invoice.client; - var account = invoice.account; - var currencyId = client.currency_id; - var invoiceNumber = invoice.invoice_number; - var invoiceDate = invoice.invoice_date ? invoice.invoice_date : ''; - var dueDate = invoice.due_date ? invoice.due_date : ''; - - var marginLeft = 50; - var accountTop = 30; - var headerTop = 140; - var headerLeft = 360; - var headerRight = 550; - var rowHeight = 15; - var tableRowHeight = 20; - var footerLeft = 420; - var tablePadding = 6; - - var tableTop = 240; - var tableLeft = 50; - var descriptionLeft = 162; - var unitCostRight = 410; - var qtyRight = 480; - var taxRight = 480; - var lineTotalRight = 550; - - - var hasTaxes = false; - for (var i=0; i 0) || (item.tax_rate && parseFloat(item.tax_rate) > 0)) { - hasTaxes = true; - break; - } - } - if (hasTaxes) - { - descriptionLeft -= 20; - unitCostRight -= 40; - qtyRight -= 40; - } - var doc = new jsPDF('p', 'pt'); - doc.setFont('Helvetica',''); - doc.setFontSize(10); - - if (invoice.image) - { - var left = headerRight - invoice.imageWidth; - doc.addImage(invoice.image, 'JPEG', left, 30, invoice.imageWidth, invoice.imageHeight); - } - - /* table header */ - doc.setDrawColor(200,200,200); - doc.setFillColor(230,230,230); - var x1 = headerLeft - tablePadding; - var y1 = headerTop + rowHeight + 4; - var x2 = headerRight - headerLeft + 11; - var y2 = rowHeight + 1; - if (invoice.po_number) { - y1 += rowHeight; - } - if (dueDate) { - y1 += rowHeight; - } - doc.rect(x1, y1, x2, y2, 'FD'); + console.log ('DESIGN:'+invoice.invoice_design_id); - var invoiceNumberX = headerRight - (doc.getStringUnitWidth(invoiceNumber, false) * doc.internal.getFontSize()); - var invoiceDateX = headerRight - (doc.getStringUnitWidth(invoiceDate) * doc.internal.getFontSize()); - var dueDateX = headerRight - (doc.getStringUnitWidth(dueDate) * doc.internal.getFontSize()); - var poNumberX = headerRight - (doc.getStringUnitWidth(invoice.po_number) * doc.internal.getFontSize()); + report_id=invoice.invoice_design_id; - doc.setFontType("normal"); + doc= GetPdf(invoice,checkMath,report_id); - var y = accountTop; - var left = marginLeft; - - if (account.name) { - y += rowHeight; - doc.text(left, y, account.name); - } - if (account.address1) { - y += rowHeight; - doc.text(left, y, account.address1); - } - if (account.address2) { - y += rowHeight; - doc.text(left, y, account.address2); - } - if (account.city || account.state || account.postal_code) { - y += rowHeight; - doc.text(left, y, account.city + ', ' + account.state + ' ' + account.postal_code); - } - if (account.country) { - y += rowHeight; - doc.text(left, y, account.country.name); - } - - if (client) { - var y = headerTop; - doc.text(marginLeft, y, getClientDisplayName(client)); - if (client.address1) { - y += rowHeight; - doc.text(marginLeft, y, client.address1); - } - if (client.address2) { - y += rowHeight; - doc.text(marginLeft, y, client.address2); - } - if (client.city || client.state || client.postal_code) { - y += rowHeight; - doc.text(marginLeft, y, client.city + ', ' + client.state + ' ' + client.postal_code); - } - if (client.country) { - y += rowHeight; - doc.text(marginLeft, y, client.country.name); - } - } - - var headerY = headerTop; - doc.text(headerLeft, headerY, 'Invoice Number'); - doc.text(invoiceNumberX, headerY, invoiceNumber); - - if (invoice.po_number) { - headerY += rowHeight; - doc.text(headerLeft, headerY, 'PO Number'); - doc.text(poNumberX, headerY, invoice.po_number); - } - - headerY += rowHeight; - doc.text(headerLeft, headerY, 'Invoice Date'); - doc.text(invoiceDateX, headerY, invoiceDate); - - if (dueDate) { - headerY += rowHeight; - doc.text(headerLeft, headerY, 'Due Date'); - doc.text(dueDateX, headerY, dueDate); - } - - headerY += rowHeight; - doc.setFontType("bold"); - doc.text(headerLeft, headerY, 'Balance Due'); - - var total = 0; - - for (var i=0; i 0) { - - var discount = total * (invoice.discount/100); - total -= discount; - } - - var tax = 0; - if (invoice.tax && parseFloat(invoice.tax.rate)) { - tax = parseFloat(invoice.tax.rate); - } else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) { - tax = parseFloat(invoice.tax_rate); - } - - if (tax) { - var tax = total * (tax/100); - total = parseFloat(total) + parseFloat(tax); - } - - total = formatMoney(total - (invoice.amount - invoice.balance), currencyId); - - var balance = formatMoney(total, currencyId); - balanceX = headerRight - (doc.getStringUnitWidth(balance) * doc.internal.getFontSize()); - doc.text(balanceX, headerY, balance); - - - doc.setDrawColor(200,200,200); - doc.setFillColor(230,230,230); - doc.rect(tableLeft - tablePadding, tableTop - 12, headerRight - tableLeft + 12, rowHeight + 2, 'FD'); - - var costX = unitCostRight - (doc.getStringUnitWidth('Unit Cost') * doc.internal.getFontSize()); - var qtyX = qtyRight - (doc.getStringUnitWidth('Quantity') * doc.internal.getFontSize()); - var taxX = taxRight - (doc.getStringUnitWidth('Tax') * doc.internal.getFontSize()); - var totalX = lineTotalRight - (doc.getStringUnitWidth('Line Total') * doc.internal.getFontSize()); - - doc.text(tableLeft, tableTop, 'Item'); - doc.text(descriptionLeft, tableTop, 'Description'); - doc.text(costX, tableTop, 'Unit Cost'); - doc.text(qtyX, tableTop, 'Quantity'); - doc.text(totalX, tableTop, 'Line Total'); - - if (hasTaxes) - { - doc.text(taxX, tableTop, 'Tax'); - } - - /* line items */ - doc.setFontType("normal"); - var line = 1; - var total = 0; - var shownItem = false; - - for (var i=0; i 20) { - line = 0; - tableTop = 60; - doc.addPage(); - } - } - - /* table footer */ - doc.setDrawColor(200,200,200); - var x = tableTop + (line * tableRowHeight) - 6; - - doc.setLineWidth(1); - doc.line(tableLeft - tablePadding, x, lineTotalRight+tablePadding, x); - //console.log('%s %s %s', lineTotalRight, tableLeft, (lineTotalRight-tableLeft)); - - doc.text(tableLeft, x+16, invoice.public_notes); - if (invoice.terms) { - var termsX = x+16 + (doc.splitTextToSize(invoice.public_notes, 340).length * rowHeight) + (rowHeight/2); - doc.setFontType("bold"); - doc.text(tableLeft, termsX, "Terms"); - doc.setFontType("normal"); - doc.text(tableLeft, termsX + rowHeight, invoice.terms); - } - - x += 16; - doc.text(footerLeft, x, 'Subtotal'); - var prettyTotal = formatMoney(total, currencyId, true); - var totalX = headerRight - (doc.getStringUnitWidth(prettyTotal) * doc.internal.getFontSize()); - doc.text(totalX, x, prettyTotal); - - if (invoice.discount > 0) { - - x += 16; - doc.text(footerLeft, x, 'Discount'); - var discount = total * (invoice.discount/100); - total -= discount; - discount = formatMoney(discount, currencyId, true); - var discountX = headerRight - (doc.getStringUnitWidth(discount) * doc.internal.getFontSize()); - doc.text(discountX, x, discount); - } - - var tax = 0; - if (invoice.tax && parseFloat(invoice.tax.rate)) { - tax = parseFloat(invoice.tax.rate); - } else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) { - tax = parseFloat(invoice.tax_rate); - } - - if (tax) { - x += 16; - doc.text(footerLeft, x, 'Tax ' + tax + '%'); - var tax = total * (tax/100); - total = parseFloat(total) + parseFloat(tax); - tax = formatMoney(tax, currencyId, true); - var taxX = headerRight - (doc.getStringUnitWidth(tax) * doc.internal.getFontSize()); - doc.text(taxX, x, tax); - } - - x += 16; - doc.text(footerLeft, x, 'Paid to Date'); - var paid = formatMoney(invoice.amount - invoice.balance, currencyId, true); - var paidX = headerRight - (doc.getStringUnitWidth(paid) * doc.internal.getFontSize()); - doc.text(paidX, x, paid); - - x += 16; - - if (checkMath && NINJA.parseFloat(total).toFixed(4) != NINJA.parseFloat(invoice.amount).toFixed(4)) - { - var doc = new jsPDF('p', 'pt'); - doc.setFont('Helvetica',''); - doc.setFontSize(10); - doc.text(100, 100, "An error occurred, please try again later."); - onerror('Failed to generate PDF ' + total + ', ' + invoice.amount ); - return doc; - } - - - doc.setDrawColor(200,200,200); - doc.setFillColor(230,230,230); - var x1 = footerLeft - tablePadding; - var y1 = x - 11; - var x2 = headerRight - footerLeft + 11; - var y2 = 16; - doc.rect(x1, y1, x2, y2, 'FD'); - - doc.setFontType("bold"); - doc.text(footerLeft, x, 'Balance Due'); - - total = formatMoney(total - (invoice.amount - invoice.balance), currencyId); - var totalX = headerRight - (doc.getStringUnitWidth(total) * doc.internal.getFontSize()); - doc.text(totalX, x, total); - - - doc.setFontType("normal"); - doc.text(marginLeft, 790, "Created by InvoiceNinja.com"); - - return doc; + return doc; } - - /* Handle converting variables in the invoices (ie, MONTH+1) */ function processVariables(str) { if (!str) return ''; @@ -463,7 +104,7 @@ $.extend( true, $.fn.dataTable.defaults, { //"sLengthMenu": "_MENU_ records per page" "sLengthMenu": "_MENU_", "sSearch": "" - }, + } //"sScrollY": "500px", } ); @@ -840,7 +481,9 @@ function populateInvoiceComboboxes(clientId, invoiceId) { for (var i=0; i 0) { + + var discount = total * (invoice.discount/100); + total -= discount; + } + + var tax = 0; + if (invoice.tax && parseFloat(invoice.tax.rate)) { + tax = parseFloat(invoice.tax.rate); + } else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) { + tax = parseFloat(invoice.tax_rate); + } + + if (tax) { + var tax = total * (tax/100); + total = parseFloat(total) + parseFloat(tax); + } + + total = formatMoney(total - (invoice.amount - invoice.balance), currencyId); + + var balance = formatMoney(total, currencyId); + + + + + + + var doc = new jsPDF('p', 'pt'); + + //set default style for report + doc.setFont('Helvetica',''); + doc.setFontSize(7); + +//---------------------------------------------------------------------------------------------------- + //Print header on document + //for now we will put static header + //but later this could be changed to more flexible solution + + if (invoice.image) + { + var left = headerRight - invoice.imageWidth; + doc.addImage(invoice.image, 'JPEG', left, 30, invoice.imageWidth, invoice.imageHeight); + } + + + + if (invoice.imageLogo1) + { + pageHeight=820; + var left = headerRight ; + y=pageHeight-invoice.imageLogoHeight1; + + + var left = headerRight - invoice.imageLogoWidth1; + doc.addImage(invoice.imageLogo1, 'JPEG', left, y, invoice.imageLogoWidth1, invoice.imageLogoHeight1); + + + } + + + var invoiceNumberX = headerRight - (doc.getStringUnitWidth(invoiceNumber, false) * doc.internal.getFontSize()); + var invoiceDateX = headerRight - (doc.getStringUnitWidth(invoiceDate) * doc.internal.getFontSize()); + var dueDateX = headerRight - (doc.getStringUnitWidth(dueDate) * doc.internal.getFontSize()); + var poNumberX = headerRight - (doc.getStringUnitWidth(invoice.po_number) * doc.internal.getFontSize()); + + // doc.setFontType("normal"); + + var y = accountTop; + var left = marginLeft; + + + doc.setFontSize(7); + + SetPdfColor('LightBlue',doc); + if (account.name) { + y += rowHeight; + doc.text(left, y, account.name); + } + + SetPdfColor('GrayText',doc); + doc.setFontSize(6); + +//TODO:NOT AVAILEABLE FROM DATAMOEL + //account.email='email N/A'; + if (account.email) { + y += rowHeight; + doc.text(left, y, account.email); + } + else + { + //console.log('account.email NOT DEFINED !'); + } + +//TODO:NOT AVAILEABLE FROM DATAMOEL + //account.phone='phone N/A'; + if (account.phone) { + y += rowHeight; + doc.text(left, y, account.phone); + } + else + { + //console.log('account.phone NOT DEFINED !'); + } + + + + var HeaderMarginThirdColumn=70;//should be dynamic and dependent on 1st image and 2nd column width + + var y = accountTop; + var left = marginLeft+HeaderMarginThirdColumn; + + if (account.address1) { + y += rowHeight; + doc.text(left, y, account.address1); + } + if (account.address2) { + y += rowHeight; + doc.text(left, y, account.address2); + } + + if (account.city || account.state || account.postal_code) { + y += rowHeight; + doc.text(left, y, account.city + ', ' + account.state + ' ' + account.postal_code); + } + if (account.country) { + y += rowHeight; + doc.text(left, y, account.country.name); + } + + + + + +//-----------------------------Publish Client Details block-------------------------------------------- + + + var y = accountTop; + var left = marginLeft; + + var headerY = headerTop; + + + + + SetPdfColor('LightBlue',doc); + doc.setFontSize(8); + + //doc.setFontType("bold"); + + doc.text(50, headerTop, 'Invoice'); + + + + + + SetPdfColor('GrayLogo',doc); //set black color + y=130; + doc.line(30, y, 560, y); // horizontal line + + + + var line1=headerTop+16; + var line2=headerTop+16*2; + var line21=headerTop+16*1.6; + var line22=headerTop+16*2.2; + + var line3=headerTop+16*3; + var line31=headerTop+16*3.6; + + var marginLeft1=50; + var marginLeft2=120; + var marginLeft3=180; + + SetPdfColor('Black',doc); //set black color + + doc.setFontSize(6); + doc.text(marginLeft1, line1, 'Invoice Number'); + doc.text(marginLeft1, line2, 'Invoice date'); + doc.text(marginLeft1, line3, 'Amount Due'); + + + + + + // invoiceNumber='12345' + // invoiceDate='12345' + //invoiceAmount='12345' + + doc.setFontType("bold"); + doc.text(marginLeft2, line1, invoiceNumber); + doc.setFontType("normal"); + doc.text(marginLeft2, line2, invoiceDate); + + + SetPdfColor('LightBlue',doc); //set black color + doc.text(marginLeft2, line3, balance); + + ClientCompanyName=client.name; + ClientCompanyEmail='';//client.email;//'22222222'; + ClientCompanyPhone=client.work_phone; + + ClientCompanyAddress1=client.address1; + ClientCompanyAddress2=client.address2+' '+client.postal_code; + + + SetPdfColor('Black',doc); //set black color + + doc.setFontType("bold"); + + doc.text(marginLeft3, line1, ClientCompanyName); + doc.setFontType("normal"); + + + + if(client) + { + ClientCompanyName=getClientDisplayName(client); + ClientCompanyPhone=client.work_phone; + ClientCompanyEmail=client.contacts[0].email; + + } + + + doc.text(marginLeft3, line21, ClientCompanyAddress1); + doc.text(marginLeft3, line22, ClientCompanyAddress2); + + + doc.text(marginLeft3, line3, ClientCompanyEmail); + doc.text(marginLeft3, line31, ClientCompanyPhone); + + + SetPdfColor('GrayLogo',doc); //set black color + y=195; + doc.line(30, y, 560, y); // horizontal line + + +//--------------------------------Publishing Table-------------------------------------------------- + GlobalY=y+30; + + + SetPdfColor('Black',doc); + doc.setFontSize(7); + + var hasTaxes = false; + for (var i=0; i 0) || (item.tax_rate && parseFloat(item.tax_rate) > 0)) { + hasTaxes = true; + break; + } + } + if (hasTaxes) + { + descriptionLeft -= 20; + unitCostRight -= 40; + qtyRight -= 40; + } + + + + var costX = unitCostRight - (doc.getStringUnitWidth('Unit Cost') * doc.internal.getFontSize()); + var qtyX = qtyRight - (doc.getStringUnitWidth('Quantity') * doc.internal.getFontSize()); + var taxX = taxRight - (doc.getStringUnitWidth('Tax') * doc.internal.getFontSize()); + var totalX = lineTotalRight - (doc.getStringUnitWidth('Line Total') * doc.internal.getFontSize()); + + + + tableTop=GlobalY;//redefine this to dynamic value + + doc.setFontSize(9); + + doc.text(tableLeft, tableTop, 'Item'); + doc.text(descriptionLeft, tableTop, 'Description'); + doc.text(costX, tableTop, 'Unit Cost'); + doc.text(qtyX, tableTop, 'Quantity'); + doc.text(totalX, tableTop, 'Line Total'); + + if (hasTaxes) + { + doc.text(taxX, tableTop, 'Tax'); + } + + + doc.setFontSize(7); + + /* line items */ + //doc.setFontType("normal"); + var line = 1; + var total = 0; + var shownItem = false; + + + + +GlobalY=GlobalY+14; //padding from top + + var FontSize=7; + doc.setFontSize(FontSize); + + + var MaxLinesPerPage=40; + + + + for (var i=0; i MaxGlobalY) { + + + tableTop = 40; + GlobalY=tableTop; + + + + + //var MaxLinesPerPage=70; + + doc.addPage(); + if (invoice.imageLogo1) + { + pageHeight=820; + var left = headerRight ; + y=pageHeight-invoice.imageLogoHeight1; + + + var left = headerRight - invoice.imageLogoWidth1; + doc.addImage(invoice.imageLogo1, 'JPEG', left, y, invoice.imageLogoWidth1, invoice.imageLogoHeight1); + + + } + + } + + if ((i%2)===0){ + doc.setLineWidth(0.5); + doc.setDrawColor(200,200,200); + doc.setFillColor(230,230,230); + + var x1 = tableLeft-tablePadding ; + + var y1 = GlobalY-FontSize; + + var w2 = 510+tablePadding*2;//lineTotalRight-tablePadding*5; + var h2 = doc.internal.getFontSize()*length+length*1.1;//+h;//+tablePadding; + + + + doc.rect(x1, y1, w2, h2, 'FD'); + } + x=GlobalY; + + GlobalY=GlobalY+h+tablePadding*2; + + + + + + + SetPdfColor('LightBlue',doc); + doc.text(tableLeft, x, productKey); + + SetPdfColor('Black',doc); + doc.text(descriptionLeft, x, notes); + + doc.text(costX, x, cost); + doc.text(qtyX, x, qty); + doc.text(totalX, x, lineTotal); + + if (tax) { + doc.text(taxX, x, tax+'%'); + } + + + line=line+length; + + + + + + + +/* + + if (line > MaxLinesPerPage) { + line = 0; + + tableTop = 40; + GlobalY=tableTop; + + + + + var MaxLinesPerPage=70; + + doc.addPage(); + if (invoice.imageLogo1) + { + pageHeight=820; + var left = headerRight ; + y=pageHeight-invoice.imageLogoHeight1; + + + var left = headerRight - invoice.imageLogoWidth1; + doc.addImage(invoice.imageLogo1, 'JPEG', left, y, invoice.imageLogoWidth1, invoice.imageLogoHeight1); + + + } + } + */ + + + } + + + + + +//-------------------------------Publishing Document balance------------------------------------------ + + + + + // var dueDateX = headerRight - (doc.getStringUnitWidth(dueDate) * doc.internal.getFontSize()); + x += 16+50; + + doc.setFontType("bold"); + MsgRightAlign=400; + + Msg='Total'; + var TmpMsgX = MsgRightAlign-(doc.getStringUnitWidth(Msg) * doc.internal.getFontSize()); + doc.text(TmpMsgX, x, Msg); + + + + doc.setFontType("normal"); + AmountText = formatMoney(total , currencyId); + headerLeft=headerRight+400; + var AmountX = headerLeft - (doc.getStringUnitWidth(AmountText) * doc.internal.getFontSize()); + doc.text(AmountX, x, AmountText); + + + + + x += doc.internal.getFontSize()*2; + //doc.text(footerLeft, x, ''); + + Msg='Amount Payed'; + var TmpMsgX = MsgRightAlign-(doc.getStringUnitWidth(Msg) * doc.internal.getFontSize()); + doc.text(TmpMsgX, x, Msg); + + + + AmountText = formatMoney(paid_to_date , currencyId); + headerLeft=headerRight+400; + var AmountX = headerLeft - (doc.getStringUnitWidth(AmountText) * doc.internal.getFontSize()); + doc.text(AmountX, x, AmountText); + + + + + + + doc.setFontSize(10); + x += doc.internal.getFontSize()*4; + //doc.text(footerLeft, x, ''); + Msg='Amount Due'; + var TmpMsgX = MsgRightAlign-(doc.getStringUnitWidth(Msg) * doc.internal.getFontSize()); + + + + doc.text(TmpMsgX, x, Msg); + + + SetPdfColor('LightBlue',doc); + AmountText = formatMoney(balance , currencyId); + headerLeft=headerRight+400; + var AmountX = headerLeft - (doc.getStringUnitWidth(AmountText) * doc.internal.getFontSize()); + doc.text(AmountX, x, AmountText); + + + + + + + return doc; +} + + +function SetPdfColor(color,doc) +{ + + if (color=='LightBlue') { + return doc.setTextColor(41,156, 194); + } + + if (color=='Black') { + return doc.setTextColor(0,0,0);//select color black + } + if (color=='GrayLogo') { + return doc.setTextColor(207,209, 210);//select color Custom Report GRAY + } + + if (color=='GrayText') { + return doc.setTextColor(161,160,160);//select color Custom Report GRAY Colour + } + + + alert('color is not defined'); + return false; + +} + + + + + + + + + + + +