1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/public/js/script.js

1754 lines
51 KiB
JavaScript
Raw Normal View History

2014-03-02 05:57:00 +01:00
// http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
2013-12-31 00:19:17 +01:00
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
2014-01-27 23:40:40 +01:00
var isChromium = isChrome && navigator.userAgent.indexOf('Chromium') >= 0;
2013-12-31 00:19:17 +01:00
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
2014-01-16 22:12:46 +01:00
2014-03-09 22:19:56 +01:00
function GetReportTemplate4(doc, invoice, layout, checkMath) {
2014-03-05 21:19:12 +01:00
var client = invoice.client;
var account = invoice.account;
2014-03-09 22:19:56 +01:00
var currencyId = client.currency_id;
2014-03-05 21:19:12 +01:00
if (invoice.image)
{
2014-03-09 22:19:56 +01:00
var left = layout.headerRight - invoice.imageWidth;
doc.addImage(invoice.image, 'JPEG', left, 30);
2014-03-05 21:19:12 +01:00
}
/* table header */
doc.setDrawColor(200,200,200);
doc.setFillColor(230,230,230);
2014-05-20 23:40:09 +02:00
var detailsHeight = getInvoiceDetailsHeight(invoice, layout);
2014-03-09 22:19:56 +01:00
var left = layout.headerLeft - layout.tablePadding;
2014-04-21 21:09:16 +02:00
var top = layout.headerTop + detailsHeight - layout.rowHeight - layout.tablePadding;
2014-03-09 22:19:56 +01:00
var width = layout.headerRight - layout.headerLeft + (2 * layout.tablePadding);
var height = layout.rowHeight + 1;
doc.rect(left, top, width, height, 'FD');
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(10);
doc.setFontType("normal");
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
displayAccount(doc, invoice, layout.marginLeft, layout.accountTop, layout);
displayClient(doc, invoice, layout.marginLeft, layout.headerTop, layout);
2014-04-21 21:09:16 +02:00
2014-03-09 22:19:56 +01:00
displayInvoice(doc, invoice, layout.headerLeft, layout.headerTop, layout, layout.headerRight);
2014-04-21 21:09:16 +02:00
layout.tableTop = Math.max(layout.tableTop, layout.headerTop + detailsHeight + (2 * layout.tablePadding));
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
var headerY = layout.headerTop;
var total = 0;
2014-03-05 21:19:12 +01:00
doc.setDrawColor(200,200,200);
doc.setFillColor(230,230,230);
2014-03-09 22:19:56 +01:00
var left = layout.marginLeft - layout.tablePadding;
var top = layout.tableTop - layout.tablePadding;
var width = layout.headerRight - layout.marginLeft + (2 * layout.tablePadding);
var height = layout.rowHeight + 2;
doc.rect(left, top, width, height, 'FD');
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
displayInvoiceHeader(doc, invoice, layout);
var y = displayInvoiceItems(doc, invoice, layout);
2014-03-05 21:19:12 +01:00
doc.setFontSize(10);
/* table footer */
2014-03-09 22:19:56 +01:00
/*
doc.setDrawColor(200,200,200);
2014-03-05 21:19:12 +01:00
doc.setLineWidth(1);
2014-03-09 22:19:56 +01:00
doc.line(layout.marginLeft - layout.tablePadding, x, layout.lineTotalRight+layout.tablePadding, x);
*/
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
displayNotesAndTerms(doc, layout, invoice, y+20);
2014-03-05 21:19:12 +01:00
2014-03-09 22:19:56 +01:00
y += displaySubtotals(doc, layout, invoice, y+20, 480) + 20;
2014-03-05 21:19:12 +01:00
2014-03-23 15:23:53 +01:00
/*
2014-03-05 21:19:12 +01:00
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;
}
2014-03-23 15:23:53 +01:00
*/
2014-03-05 21:19:12 +01:00
doc.setDrawColor(200,200,200);
doc.setFillColor(230,230,230);
2014-03-09 22:19:56 +01:00
var left = layout.footerLeft - layout.tablePadding;
var top = y - layout.tablePadding;
var width = layout.headerRight - layout.footerLeft + (2 * layout.tablePadding);
var height = layout.rowHeight + 2;
doc.rect(left, top, width, height, 'FD');
2014-03-05 21:19:12 +01:00
doc.setFontType("bold");
2014-05-20 23:40:09 +02:00
doc.text(layout.footerLeft, y, invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due);
2014-04-03 19:54:06 +02:00
total = formatMoney(invoice.balance_amount, currencyId);
2014-03-09 22:19:56 +01:00
var totalX = layout.headerRight - (doc.getStringUnitWidth(total) * doc.internal.getFontSize());
doc.text(totalX, y, total);
2014-03-05 21:19:12 +01:00
2014-04-13 15:34:58 +02:00
if (!invoice.is_pro) {
doc.setFontType("normal");
doc.text(layout.marginLeft, 790, "Created by InvoiceNinja.com");
}
2014-03-05 21:19:12 +01:00
return doc;
}
2013-12-31 20:49:54 +01:00
2014-04-14 13:33:43 +02:00
var invoiceOld;
2014-04-21 23:25:40 +02:00
function generatePDF(invoice, force) {
2014-04-14 14:16:32 +02:00
invoice = calculateAmounts(invoice);
2014-04-14 13:33:43 +02:00
var a = copyInvoice(invoice);
var b = copyInvoice(invoiceOld);
2014-04-21 23:25:40 +02:00
if (!force && _.isEqual(a, b)) {
2014-04-14 13:33:43 +02:00
return;
}
invoiceOld = invoice;
report_id = invoice.invoice_design_id;
2014-04-21 23:25:40 +02:00
doc = GetPdf(invoice, false, report_id);
2014-04-14 13:33:43 +02:00
return doc;
2013-11-26 13:45:07 +01:00
}
2014-04-14 13:33:43 +02:00
function copyInvoice(orig) {
if (!orig) return false;
var copy = JSON.stringify(orig);
var copy = JSON.parse(copy);
return copy;
}
2014-02-25 13:54:50 +01:00
2013-12-10 18:18:35 +01:00
/* Handle converting variables in the invoices (ie, MONTH+1) */
function processVariables(str) {
2014-03-02 07:30:30 +01:00
if (!str) return '';
var variables = ['MONTH','QUARTER','YEAR'];
for (var i=0; i<variables.length; i++) {
var variable = variables[i];
2013-12-11 12:11:59 +01:00
var regexp = new RegExp(':' + variable + '[+-]?[\\d]*', 'g');
2013-12-10 18:18:35 +01:00
var matches = str.match(regexp);
if (!matches) {
continue;
}
for (var j=0; j<matches.length; j++) {
var match = matches[j];
var offset = 0;
if (match.split('+').length > 1) {
offset = match.split('+')[1];
} else if (match.split('-').length > 1) {
offset = parseInt(match.split('-')[1]) * -1;
}
str = str.replace(match, getDatePart(variable, offset));
}
2014-03-02 07:30:30 +01:00
}
return str;
2013-12-10 18:18:35 +01:00
}
function getDatePart(part, offset) {
offset = parseInt(offset);
if (!offset) {
offset = 0;
}
2014-03-02 07:30:30 +01:00
if (part == 'MONTH') {
return getMonth(offset);
} else if (part == 'QUARTER') {
return getQuarter(offset);
} else if (part == 'YEAR') {
return getYear(offset);
}
2013-12-10 18:18:35 +01:00
}
function getMonth(offset) {
2014-03-02 07:30:30 +01:00
var today = new Date();
var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var month = today.getMonth();
2013-12-10 18:18:35 +01:00
month = parseInt(month) + offset;
month = month % 12;
2014-01-09 11:05:35 +01:00
if (month < 0) {
2014-03-02 07:30:30 +01:00
month += 12;
2014-01-09 11:05:35 +01:00
}
2013-12-10 18:18:35 +01:00
return months[month];
}
function getYear(offset) {
2014-03-02 07:30:30 +01:00
var today = new Date();
var year = today.getFullYear();
return parseInt(year) + offset;
2013-12-10 18:18:35 +01:00
}
function getQuarter(offset) {
2014-03-02 07:30:30 +01:00
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
quarter += offset;
2013-12-10 18:18:35 +01:00
quarter = quarter % 4;
if (quarter == 0) {
quarter = 4;
}
return 'Q' + quarter;
}
2013-11-26 13:45:07 +01:00
/* Default class modification */
$.extend( $.fn.dataTableExt.oStdClasses, {
2014-03-02 07:30:30 +01:00
"sWrapper": "dataTables_wrapper form-inline"
2013-11-26 13:45:07 +01:00
} );
/* API method to get paging information */
$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
{
2014-03-02 07:30:30 +01:00
return {
"iStart": oSettings._iDisplayStart,
"iEnd": oSettings.fnDisplayEnd(),
"iLength": oSettings._iDisplayLength,
"iTotal": oSettings.fnRecordsTotal(),
"iFilteredTotal": oSettings.fnRecordsDisplay(),
"iPage": oSettings._iDisplayLength === -1 ?
0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
"iTotalPages": oSettings._iDisplayLength === -1 ?
0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
};
2013-11-26 13:45:07 +01:00
};
/* Bootstrap style pagination control */
$.extend( $.fn.dataTableExt.oPagination, {
2014-03-02 07:30:30 +01:00
"bootstrap": {
"fnInit": function( oSettings, nPaging, fnDraw ) {
var oLang = oSettings.oLanguage.oPaginate;
var fnClickHandler = function ( e ) {
e.preventDefault();
if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
fnDraw( oSettings );
}
};
$(nPaging).addClass('pagination').append(
'<ul class="pagination">'+
'<li class="prev disabled"><a href="#">&laquo;</a></li>'+
'<li class="next disabled"><a href="#">&raquo;</a></li>'+
'</ul>'
);
var els = $('a', nPaging);
$(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
$(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
},
"fnUpdate": function ( oSettings, fnDraw ) {
var iListLength = 5;
var oPaging = oSettings.oInstance.fnPagingInfo();
var an = oSettings.aanFeatures.p;
var i, ien, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
if ( oPaging.iTotalPages < iListLength) {
iStart = 1;
iEnd = oPaging.iTotalPages;
}
else if ( oPaging.iPage <= iHalf ) {
iStart = 1;
iEnd = iListLength;
} else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
iStart = oPaging.iTotalPages - iListLength + 1;
iEnd = oPaging.iTotalPages;
} else {
iStart = oPaging.iPage - iHalf + 1;
iEnd = iStart + iListLength - 1;
}
for ( i=0, ien=an.length ; i<ien ; i++ ) {
// Remove the middle elements
$('li:gt(0)', an[i]).filter(':not(:last)').remove();
// Add the new list items and their event handlers
for ( j=iStart ; j<=iEnd ; j++ ) {
sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
$('<li '+sClass+'><a href="#">'+j+'</a></li>')
.insertBefore( $('li:last', an[i])[0] )
.bind('click', function (e) {
e.preventDefault();
oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
fnDraw( oSettings );
} );
}
// Add / remove disabled classes from the static elements
if ( oPaging.iPage === 0 ) {
$('li:first', an[i]).addClass('disabled');
} else {
$('li:first', an[i]).removeClass('disabled');
}
if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
$('li:last', an[i]).addClass('disabled');
} else {
$('li:last', an[i]).removeClass('disabled');
}
}
}
}
2013-11-26 13:45:07 +01:00
} );
/*
* TableTools Bootstrap compatibility
* Required TableTools 2.1+
*/
if ( $.fn.DataTable.TableTools ) {
2014-03-02 07:30:30 +01:00
// Set the classes that TableTools uses to something suitable for Bootstrap
$.extend( true, $.fn.DataTable.TableTools.classes, {
"container": "DTTT btn-group",
"buttons": {
"normal": "btn",
"disabled": "disabled"
},
"collection": {
"container": "DTTT_dropdown dropdown-menu",
"buttons": {
"normal": "",
"disabled": "disabled"
}
},
"print": {
"info": "DTTT_print_info modal"
},
"select": {
"row": "active"
}
} );
// Have the collection use a bootstrap compatible dropdown
$.extend( true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
"collection": {
"container": "ul",
"button": "li",
"liner": "a"
}
} );
2013-11-26 13:45:07 +01:00
}
/*
$(document).ready(function() {
2014-03-02 07:30:30 +01:00
$('#example').dataTable( {
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
} );
2013-11-26 13:45:07 +01:00
} );
*/
function isStorageSupported() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
2013-12-02 20:54:24 +01:00
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
2013-12-03 18:32:33 +01:00
};
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
});
2013-12-05 16:23:24 +01:00
function enableHoverClick($combobox, $entityId, url) {
2014-03-02 07:30:30 +01:00
/*
$combobox.mouseleave(function() {
$combobox.css('text-decoration','none');
}).on('mouseenter', function(e) {
setAsLink($combobox, $combobox.closest('.combobox-container').hasClass('combobox-selected'));
}).on('focusout mouseleave', function(e) {
setAsLink($combobox, false);
}).on('click', function() {
var clientId = $entityId.val();
if ($(combobox).closest('.combobox-container').hasClass('combobox-selected')) {
if (parseInt(clientId) > 0) {
window.open(url + '/' + clientId, '_blank');
} else {
$('#myModal').modal('show');
}
};
});
2013-12-05 16:23:24 +01:00
*/
}
function setAsLink($input, enable) {
2014-03-02 07:30:30 +01:00
if (enable) {
$input.css('text-decoration','underline');
$input.css('cursor','pointer');
} else {
$input.css('text-decoration','none');
$input.css('cursor','text');
}
2013-12-05 16:23:24 +01:00
}
function setComboboxValue($combobox, id, name) {
2014-03-02 07:30:30 +01:00
$combobox.find('input').val(id);
$combobox.find('input.form-control').val(name);
if (id && name) {
$combobox.find('select').combobox('setSelected');
$combobox.find('.combobox-container').addClass('combobox-selected');
} else {
$combobox.find('.combobox-container').removeClass('combobox-selected');
}
2013-12-08 20:08:17 +01:00
}
var BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
ko.bindingHandlers.dropdown = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().dropdownOptions|| {};
var value = ko.utils.unwrapObservable(valueAccessor());
2013-12-24 22:27:36 +01:00
var id = (value && value.public_id) ? value.public_id() : (value && value.id) ? value.id() : value ? value : false;
if (id) $(element).val(id);
2014-01-01 18:34:37 +01:00
//console.log("combo-init: %s", id);
$(element).combobox(options);
/*
ko.utils.registerEventHandler(element, "change", function () {
2014-03-17 19:05:01 +01:00
console.log("change: %s", $(element).val());
//var
2014-03-02 07:30:30 +01:00
valueAccessor($(element).val());
//$(element).combobox('refresh');
});
2014-03-17 19:05:01 +01:00
*/
},
2014-03-02 07:30:30 +01:00
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var id = (value && value.public_id) ? value.public_id() : (value && value.id) ? value.id() : value ? value : false;
//console.log("combo-update: %s", id);
if (id) {
$(element).val(id);
$(element).combobox('refresh');
} else {
$(element).combobox('clearTarget');
$(element).combobox('clearElement');
}
}
};
2013-12-25 22:34:42 +01:00
ko.bindingHandlers.datePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) $(element).datepicker('update', value);
2014-01-02 19:13:21 +01:00
$(element).change(function() {
2014-03-02 07:30:30 +01:00
var value = valueAccessor();
2014-04-22 13:33:53 +02:00
value($(element).val());
2014-01-02 19:13:21 +01:00
})
2013-12-25 22:34:42 +01:00
},
2014-03-02 07:30:30 +01:00
update: function (element, valueAccessor) {
2013-12-25 22:34:42 +01:00
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) $(element).datepicker('update', value);
}
};
2013-12-15 13:55:50 +01:00
function wordWrapText(value, width)
{
2014-03-02 07:30:30 +01:00
var doc = new jsPDF('p', 'pt');
doc.setFont('Helvetica','');
doc.setFontSize(10);
2013-12-15 13:55:50 +01:00
2014-03-02 07:30:30 +01:00
var lines = value.split("\n");
2014-04-02 20:20:01 +02:00
for (var i = 0; i < lines.length; i++) {
var numLines = doc.splitTextToSize(lines[i], width).length;
if (numLines <= 1) continue;
var j = 0; space = lines[i].length;
while (j++ < lines[i].length) {
if (lines[i].charAt(j) === ' ') space = j;
2013-12-15 13:55:50 +01:00
}
2014-04-02 20:20:01 +02:00
if (space == lines[i].length) space = width/6;
lines[i + 1] = lines[i].substring(space + 1) + ' ' + (lines[i + 1] || '');
lines[i] = lines[i].substring(0, space);
}
var newValue = (lines.join("\n")).trim();
2013-12-30 21:17:45 +01:00
2014-04-02 20:20:01 +02:00
if (value == newValue) {
return newValue;
} else {
return wordWrapText(newValue, width);
}
2013-12-15 13:55:50 +01:00
}
2013-12-30 21:17:45 +01:00
function getClientDisplayName(client)
{
2014-03-02 07:30:30 +01:00
var contact = client.contacts[0];
if (client.name) {
return client.name;
} else if (contact.first_name || contact.last_name) {
return contact.first_name + ' ' + contact.last_name;
} else {
return contact.email;
}
2013-12-30 21:17:45 +01:00
}
2014-01-02 14:21:15 +01:00
function populateInvoiceComboboxes(clientId, invoiceId) {
2014-03-02 07:30:30 +01:00
var clientMap = {};
var invoiceMap = {};
var invoicesForClientMap = {};
var $clientSelect = $('select#client');
for (var i=0; i<invoices.length; i++) {
var invoice = invoices[i];
var client = invoice.client;
if (!invoicesForClientMap.hasOwnProperty(client.public_id)) {
invoicesForClientMap[client.public_id] = [];
}
invoicesForClientMap[client.public_id].push(invoice);
invoiceMap[invoice.public_id] = invoice;
}
for (var i=0; i<clients.length; i++) {
var client = clients[i];
clientMap[client.public_id] = client;
}
$clientSelect.append(new Option('', ''));
for (var i=0; i<clients.length; i++) {
var client = clients[i];
$clientSelect.append(new Option(getClientDisplayName(client), client.public_id));
}
if (clientId) {
$clientSelect.val(clientId);
}
$clientSelect.combobox();
$clientSelect.on('change', function(e) {
var clientId = $('input[name=client]').val();
var invoiceId = $('input[name=invoice]').val();
var invoice = invoiceMap[invoiceId];
if (invoice && invoice.client.public_id == clientId) {
e.preventDefault();
return;
}
setComboboxValue($('.invoice-select'), '', '');
$invoiceCombobox = $('select#invoice');
$invoiceCombobox.find('option').remove().end().combobox('refresh');
$invoiceCombobox.append(new Option('', ''));
var list = clientId ? (invoicesForClientMap.hasOwnProperty(clientId) ? invoicesForClientMap[clientId] : []) : invoices;
for (var i=0; i<list.length; i++) {
var invoice = list[i];
var client = clientMap[invoice.client.public_id];
2014-02-25 12:52:40 +01:00
if (!client) continue; // client is deleted/archived
2014-03-02 07:30:30 +01:00
$invoiceCombobox.append(new Option(invoice.invoice_number + ' - ' + invoice.invoice_status.name + ' - ' +
2014-02-21 06:26:40 +01:00
getClientDisplayName(client) + ' - ' + formatMoney(invoice.amount, invoice.currency_id) + ' | ' +
formatMoney(invoice.balance, invoice.currency_id), invoice.public_id));
2014-03-02 07:30:30 +01:00
}
$('select#invoice').combobox('refresh');
});
var $invoiceSelect = $('select#invoice').on('change', function(e) {
$clientCombobox = $('select#client');
var invoiceId = $('input[name=invoice]').val();
if (invoiceId) {
var invoice = invoiceMap[invoiceId];
var client = clientMap[invoice.client.public_id];
setComboboxValue($('.client-select'), client.public_id, getClientDisplayName(client));
if (!parseFloat($('#amount').val())) {
$('#amount').val(formatMoney(invoice.balance, invoice.currency_id, true));
}
}
});
$invoiceSelect.combobox();
if (invoiceId) {
var invoice = invoiceMap[invoiceId];
var client = clientMap[invoice.client.public_id];
setComboboxValue($('.invoice-select'), invoice.public_id, (invoice.invoice_number + ' - ' +
2014-02-21 06:26:40 +01:00
invoice.invoice_status.name + ' - ' + getClientDisplayName(client) + ' - ' +
formatMoney(invoice.amount, invoice.currency_id) + ' | ' + formatMoney(invoice.balance, invoice.currency_id)));
2014-03-02 07:30:30 +01:00
$invoiceSelect.trigger('change');
} else if (clientId) {
var client = clientMap[clientId];
setComboboxValue($('.client-select'), client.public_id, getClientDisplayName(client));
$clientSelect.trigger('change');
} else {
$clientSelect.trigger('change');
}
2014-01-02 09:27:48 +01:00
}
var CONSTS = {};
CONSTS.INVOICE_STATUS_DRAFT = 1;
CONSTS.INVOICE_STATUS_SENT = 2;
CONSTS.INVOICE_STATUS_VIEWED = 3;
CONSTS.INVOICE_STATUS_PARTIAL = 4;
CONSTS.INVOICE_STATUS_PAID = 5;
2014-01-02 14:21:15 +01:00
$.fn.datepicker.defaults.autoclose = true;
2014-02-21 06:26:40 +01:00
$.fn.datepicker.defaults.todayHighlight = true;
2014-02-25 13:54:50 +01:00
2014-02-21 06:26:40 +01:00
//====================================================================================================================
function GetPdf(invoice,checkMath,report_id){
2014-03-09 22:19:56 +01:00
var layout = {
accountTop: 40,
2014-03-09 22:19:56 +01:00
marginLeft: 50,
marginRight: 550,
headerTop: 150,
2014-03-09 22:19:56 +01:00
headerLeft: 360,
headerRight: 550,
rowHeight: 15,
tableRowHeight: 10,
footerLeft: 420,
tablePadding: 12,
2014-04-21 21:09:16 +02:00
tableTop: 250,
2014-03-09 22:19:56 +01:00
descriptionLeft: 162,
unitCostRight: 410,
qtyRight: 480,
taxRight: 480,
lineTotalRight: 550
};
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.has_taxes)
{
layout.descriptionLeft -= 20;
layout.unitCostRight -= 40;
layout.qtyRight -= 40;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
/*
@param orientation One of "portrait" or "landscape" (or shortcuts "p" (Default), "l")
@param unit Measurement unit to be used when coordinates are specified. One of "pt" (points), "mm" (Default), "cm", "in"
@param format One of 'a3', 'a4' (Default),'a5' ,'letter' ,'legal'
@returns {jsPDF}
*/
var doc = new jsPDF('portrait', 'pt', 'a4');
//Set PDF properities
doc.setProperties({
title: 'Invoice ' + invoice.invoice_number,
subject: '',
author: 'InvoiceNinja.com',
keywords: 'pdf, invoice',
creator: 'InvoiceNinja.com'
});
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//set default style for report
doc.setFont('Helvetica','');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (report_id==1) {
return GetReportTemplate1(doc, invoice, layout, checkMath);
} else if (report_id==2) {
return GetReportTemplate2(doc, invoice, layout, checkMath);
} else if (report_id==3) {
return GetReportTemplate3(doc, invoice, layout, checkMath);
} else {
return GetReportTemplate4(doc, invoice, layout, checkMath);
}
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function GetReportTemplate1(doc, invoice, layout, checkMath)
2014-02-21 06:26:40 +01:00
{
var GlobalY=0;//Y position of line at current page
var client = invoice.client;
var account = invoice.account;
2014-03-09 22:19:56 +01:00
var currencyId = client.currency_id;
layout.headerRight = 550;
layout.rowHeight = 15;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(9);
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.image)
{
var left = layout.headerRight - invoice.imageWidth;
doc.addImage(invoice.image, 'JPEG', layout.marginLeft, 30);
2014-03-09 22:19:56 +01:00
}
2014-03-05 13:31:15 +01:00
2014-04-14 13:33:43 +02:00
if (!invoice.is_pro && logoImages.imageLogo1)
2014-03-09 22:19:56 +01:00
{
pageHeight=820;
2014-04-14 13:33:43 +02:00
y=pageHeight-logoImages.imageLogoHeight1;
doc.addImage(logoImages.imageLogo1, 'JPEG', layout.marginLeft, y, logoImages.imageLogoWidth1, logoImages.imageLogoHeight1);
2014-03-09 22:19:56 +01:00
}
2014-03-05 13:31:15 +01:00
2014-03-05 14:21:53 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(9);
2014-04-29 22:46:40 +02:00
SetPdfColor('LightBlue', doc, 'primary');
2014-03-09 22:19:56 +01:00
displayAccount(doc, invoice, 220, layout.accountTop, layout);
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
SetPdfColor('LightBlue', doc, 'primary');
2014-03-09 22:19:56 +01:00
doc.setFontSize('11');
2014-05-20 23:40:09 +02:00
doc.text(50, layout.headerTop, (invoice.is_quote ? invoiceLabels.quote : invoiceLabels.invoice).toUpperCase());
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
//doc.setDrawColor(220,220,220);
//doc.line(30, y, 560, y); // horizontal line
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
SetPdfColor('Black',doc); //set black color
doc.setFontSize(9);
2014-03-05 13:31:15 +01:00
2014-04-26 21:00:31 +02:00
var invoiceHeight = displayInvoice(doc, invoice, 50, 170, layout);
var clientHeight = displayClient(doc, invoice, 220, 170, layout);
var detailsHeight = Math.max(invoiceHeight, clientHeight);
2014-04-21 21:09:16 +02:00
layout.tableTop = Math.max(layout.tableTop, layout.headerTop + detailsHeight + (3 * layout.rowHeight));
2014-04-26 21:00:31 +02:00
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
doc.setLineWidth(0.3);
doc.setDrawColor(200,200,200);
doc.line(layout.marginLeft - layout.tablePadding, layout.headerTop + 6, layout.marginRight + layout.tablePadding, layout.headerTop + 6);
2014-04-21 21:09:16 +02:00
doc.line(layout.marginLeft - layout.tablePadding, layout.headerTop + detailsHeight + 14, layout.marginRight + layout.tablePadding, layout.headerTop + detailsHeight + 14);
2014-03-09 22:19:56 +01:00
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//doc.setDrawColor(220,220,220);
//doc.line(30, y-8, 560, y-8); // horizontal line
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(10);
doc.setFontType("bold");
displayInvoiceHeader(doc, invoice, layout);
var y = displayInvoiceItems(doc, invoice, layout);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//doc.setFontType("normal");
doc.setFontSize(9);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontType("bold");
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
GlobalY=GlobalY+25;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setLineWidth(0.3);
doc.setDrawColor(241,241,241);
doc.setFillColor(241,241,241);
var x1 = layout.marginLeft - 12;
var y1 = GlobalY-layout.tablePadding;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var w2 = 510 + 24;
var h2 = doc.internal.getFontSize()*3+layout.tablePadding*2;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.discount) {
h2 += doc.internal.getFontSize()*2;
2014-02-21 06:26:40 +01:00
}
2014-03-09 22:19:56 +01:00
if (invoice.tax_amount) {
h2 += doc.internal.getFontSize()*2;
2014-02-21 06:26:40 +01:00
}
2014-03-09 22:19:56 +01:00
//doc.rect(x1, y1, w2, h2, 'FD');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(9);
displayNotesAndTerms(doc, layout, invoice, y);
2014-03-09 22:24:05 +01:00
y += displaySubtotals(doc, layout, invoice, y, layout.unitCostRight);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(10);
2014-05-20 23:40:09 +02:00
Msg = invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due;
2014-03-09 22:24:05 +01:00
var TmpMsgX = layout.unitCostRight-(doc.getStringUnitWidth(Msg) * doc.internal.getFontSize());
2014-03-09 22:19:56 +01:00
doc.text(TmpMsgX, y, Msg);
2014-02-21 06:26:40 +01:00
2014-04-29 22:46:40 +02:00
SetPdfColor('LightBlue', doc, 'primary');
2014-03-09 22:19:56 +01:00
AmountText = formatMoney(invoice.balance_amount, currencyId);
headerLeft=layout.headerRight+400;
var AmountX = layout.lineTotalRight - (doc.getStringUnitWidth(AmountText) * doc.internal.getFontSize());
doc.text(AmountX, y, AmountText);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return doc;
}
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
function GetReportTemplate2(doc, invoice, layout, checkMath)
{
var GlobalY=0;//Y position of line at current page
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var client = invoice.client;
var account = invoice.account;
var currencyId = client.currency_id;
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
layout.headerRight = 150;
layout.rowHeight = 15;
layout.headerTop = 125;
layout.tableTop = 300;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setLineWidth(0.5);
2014-04-29 22:46:40 +02:00
if (NINJA.primaryColor) {
setDocHexFill(doc, NINJA.primaryColor);
setDocHexDraw(doc, NINJA.primaryColor);
} else {
doc.setFillColor(46,43,43);
}
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
var x1 =0;
var y1 = 0;
var w2 = 595;
var h2 = 100;
doc.rect(x1, y1, w2, h2, 'FD');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.image)
{
var left = layout.headerRight - invoice.imageWidth;
2014-03-18 21:43:12 +01:00
doc.addImage(invoice.image, 'JPEG', layout.marginLeft, 30);
2014-03-09 22:19:56 +01:00
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
Report2AddFooter (invoice,doc);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(7);
doc.setFontType("bold");
SetPdfColor('White',doc);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
displayAccount(doc, invoice, 300, layout.accountTop, layout);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
/*
var spacer = ' ';
var line1 = account.name + spacer + account.work_email + spacer + account.work_phone;
var lineWidth = doc.getStringUnitWidth(line1) * doc.internal.getFontSize();
var nameWidth = doc.getStringUnitWidth(account.name + spacer) * doc.internal.getFontSize();
var nameX = lineTotalRight - lineWidth;
var detailsX = lineTotalRight - (lineWidth - nameWidth);
doc.text(nameX, accountTop, account.name);
doc.setFontType("normal");
doc.text(detailsX, accountTop, account.work_email + spacer + account.work_phone);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var line2 = concatStrings(account.address1, account.address2) + spacer + concatStrings(account.city, account.state, account.postal_code);
var lineWidth = doc.getStringUnitWidth(line2) * doc.internal.getFontSize();
var line2X = lineTotalRight - lineWidth;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.text(line2X, accountTop + 16, line2);
*/
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//-----------------------------Publish Client Details block--------------------------------------------
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var y = layout.accountTop;
var left = layout.marginLeft;
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
var headerY = layout.headerTop;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
SetPdfColor('GrayLogo',doc); //set black color
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(7);
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
//show left column
SetPdfColor('Black',doc); //set black color
doc.setFontType("normal");
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
//publish filled box
doc.setDrawColor(200,200,200);
2014-04-29 22:46:40 +02:00
if (NINJA.secondaryColor) {
setDocHexFill(doc, NINJA.secondaryColor);
} else {
doc.setFillColor(54,164,152);
}
2014-03-09 22:19:56 +01:00
GlobalY=190;
doc.setLineWidth(0.5);
var BlockLenght=220;
var x1 =595-BlockLenght;
var y1 = GlobalY-12;
var w2 = BlockLenght;
2014-04-21 21:09:16 +02:00
var h2 = getInvoiceDetailsHeight(invoice, layout) + layout.tablePadding + 2;
2014-02-21 06:26:40 +01:00
2014-04-21 21:09:16 +02:00
doc.rect(x1, y1, w2, h2, 'FD');
2014-03-09 02:23:05 +01:00
2014-04-29 22:46:40 +02:00
SetPdfColor('SomeGreen', doc, 'secondary');
2014-03-09 22:19:56 +01:00
doc.setFontSize('14');
doc.setFontType("bold");
2014-05-20 23:40:09 +02:00
doc.text(50, GlobalY, (invoice.is_quote ? invoiceLabels.your_quote : invoiceLabels.your_invoice).toUpperCase());
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
var z=GlobalY;
z=z+30;
2014-03-09 02:23:05 +01:00
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize('8');
SetPdfColor('Black',doc);
displayClient(doc, invoice, layout.marginLeft, z, layout);
2014-02-21 06:26:40 +01:00
2014-03-05 10:36:13 +01:00
2014-03-09 22:19:56 +01:00
marginLeft2=395;
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
//publish left side information
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
SetPdfColor('White',doc);
doc.setFontSize('8');
2014-04-21 21:09:16 +02:00
var detailsHeight = displayInvoice(doc, invoice, marginLeft2, z-25, layout) + 75;
layout.tableTop = Math.max(layout.tableTop, layout.headerTop + detailsHeight + (2 * layout.tablePadding));
2014-03-09 02:23:05 +01:00
2014-03-09 22:19:56 +01:00
y=z+60;
2014-02-21 06:26:40 +01:00
2014-03-05 10:36:13 +01:00
2014-03-09 22:19:56 +01:00
x = GlobalY + 100;
doc.setFontType("bold");
2014-03-09 22:24:05 +01:00
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(12);
doc.setFontType("bold");
SetPdfColor('Black',doc);
displayInvoiceHeader(doc, invoice, layout);
var y = displayInvoiceItems(doc, invoice, layout);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//GlobalY=600;
2014-03-05 10:36:13 +01:00
2014-03-09 22:19:56 +01:00
doc.setLineWidth(0.3);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
/*
doc.setDrawColor(251,251,251);
doc.setFillColor(251,251,251);
var x1 = layout.marginLeft-layout.tablePadding*2 +14;
var y1 = GlobalY-layout.tablePadding;
var w2 = 510+layout.tablePadding*2;//lineTotalRight-tablePadding*5;
var h2 = doc.internal.getFontSize()*3+layout.tablePadding*2;
doc.rect(x1, y1, w2, h2, 'FD');
*/
displayNotesAndTerms(doc, layout, invoice, y);
2014-03-09 22:24:05 +01:00
y += displaySubtotals(doc, layout, invoice, y, layout.unitCostRight);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontType("bold");
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(12);
x += doc.internal.getFontSize()*4;
2014-05-20 23:40:09 +02:00
Msg = invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due;
2014-03-09 22:24:05 +01:00
var TmpMsgX = layout.unitCostRight-(doc.getStringUnitWidth(Msg) * doc.internal.getFontSize());
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.text(TmpMsgX, y, Msg);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//SetPdfColor('LightBlue',doc);
AmountText = formatMoney(invoice.balance_amount , currencyId);
headerLeft=layout.headerRight+400;
var AmountX = headerLeft - (doc.getStringUnitWidth(AmountText) * doc.internal.getFontSize());
2014-04-29 22:46:40 +02:00
SetPdfColor('SomeGreen', doc, 'secondary');
2014-03-09 22:19:56 +01:00
doc.text(AmountX, y, AmountText);
2014-03-05 14:20:21 +01:00
2014-03-09 22:19:56 +01:00
return doc;
}
2014-02-21 06:26:40 +01:00
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
function SetPdfColor(color, doc, role)
2014-03-09 22:19:56 +01:00
{
2014-04-29 22:46:40 +02:00
if (role === 'primary' && NINJA.primaryColor) {
return setDocHexColor(doc, NINJA.primaryColor);
} else if (role === 'secondary' && NINJA.secondaryColor) {
return setDocHexColor(doc, NINJA.secondaryColor);
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='LightBlue') {
return doc.setTextColor(41,156, 194);
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='Black') {
return doc.setTextColor(46,43,43);//select color black
}
if (color=='GrayLogo') {
return doc.setTextColor(207,241, 241);//select color Custom Report GRAY
}
2014-03-09 22:19:56 +01:00
2014-04-29 22:46:40 +02:00
if (color=='GrayBackground') {
return doc.setTextColor(251,251, 251);//select color Custom Report GRAY
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='GrayText') {
return doc.setTextColor(161,160,160);//select color Custom Report GRAY Colour
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='White') {
return doc.setTextColor(255,255,255);//select color Custom Report GRAY Colour
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='SomeGreen') {
return doc.setTextColor(54,164,152);//select color Custom Report GRAY Colour
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='LightGrayReport2-gray') {
return doc.setTextColor(240,240,240);//select color Custom Report GRAY Colour
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (color=='LightGrayReport2-white') {
return doc.setTextColor(251,251,251);//select color Custom Report GRAY Colour
}
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
}
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
function Report2AddFooter (invoice,doc)
{
2014-04-29 22:46:40 +02:00
doc.setLineWidth(0.5);
if (NINJA.primaryColor) {
setDocHexFill(doc, NINJA.primaryColor);
setDocHexDraw(doc, NINJA.primaryColor);
} else {
doc.setFillColor(46,43,43);
doc.setDrawColor(46,43,43);
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
// return doc.setTextColor(240,240,240);//select color Custom Report GRAY Colour
var x1 = 0;//tableLeft-tablePadding ;
var y1 = 750;
var w2 = 596;
var h2 = 94;//doc.internal.getFontSize()*length+length*1.1;//+h;//+tablePadding;
2014-03-05 13:31:15 +01:00
2014-03-09 02:43:50 +01:00
2014-04-29 22:46:40 +02:00
doc.rect(x1, y1, w2, h2, 'FD');
2014-03-09 02:43:50 +01:00
2014-04-29 22:46:40 +02:00
if (!invoice.is_pro && logoImages.imageLogo2)
{
pageHeight=820;
var left = 250;//headerRight ;
y=pageHeight-logoImages.imageLogoHeight2;
var headerRight=370;
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
var left = headerRight - logoImages.imageLogoWidth2;
doc.addImage(logoImages.imageLogo2, 'JPEG', left, y, logoImages.imageLogoWidth2, logoImages.imageLogoHeight2);
}
2014-03-09 22:19:56 +01:00
}
2014-03-05 13:31:15 +01:00
2014-03-09 22:19:56 +01:00
function Report3AddFooter (invoice, account, doc, layout)
{
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
doc.setLineWidth(0.5);
if (NINJA.primaryColor) {
setDocHexFill(doc, NINJA.primaryColor);
setDocHexDraw(doc, NINJA.primaryColor);
} else {
2014-03-09 22:19:56 +01:00
doc.setDrawColor(242,101,34);
doc.setFillColor(242,101,34);
2014-04-29 22:46:40 +02:00
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
var x1 = 0;//tableLeft-tablePadding ;
var y1 = 750;
var w2 = 596;
var h2 = 94;//doc.internal.getFontSize()*length+length*1.1;//+h;//+tablePadding;
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
doc.rect(x1, y1, w2, h2, 'FD');
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
if (!invoice.is_pro && logoImages.imageLogo3)
{
pageHeight=820;
// var left = 25;//250;//headerRight ;
y=pageHeight-logoImages.imageLogoHeight3;
//var headerRight=370;
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
//var left = headerRight - invoice.imageLogoWidth3;
doc.addImage(logoImages.imageLogo3, 'JPEG', 40, y, logoImages.imageLogoWidth3, logoImages.imageLogoHeight3);
}
2014-03-05 13:31:15 +01:00
2014-04-29 22:46:40 +02:00
doc.setFontSize(10);
var marginLeft = 340;
displayAccount(doc, invoice, marginLeft, 780, layout);
2014-03-09 22:19:56 +01:00
}
2014-03-05 13:31:15 +01:00
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function GetReportTemplate3(doc, invoice, layout, checkMath)
{
var client = invoice.client;
var account = invoice.account;
var currencyId = client.currency_id;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
layout.headerRight = 400;
layout.rowHeight = 15;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(7);
2014-02-21 06:26:40 +01:00
2014-04-21 21:09:16 +02:00
Report3AddHeader(invoice, layout, doc);
2014-03-05 16:42:38 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.image)
2014-02-21 06:26:40 +01:00
{
2014-03-09 22:19:56 +01:00
y=130;
var left = layout.headerRight - invoice.imageWidth;
2014-03-18 21:43:12 +01:00
doc.addImage(invoice.image, 'JPEG', layout.marginLeft, y);
2014-02-21 06:26:40 +01:00
}
2014-03-09 22:19:56 +01:00
Report3AddFooter (invoice, account, doc, layout);
2014-02-21 06:26:40 +01:00
2014-02-25 13:54:50 +01:00
2014-04-21 21:09:16 +02:00
SetPdfColor('White',doc);
doc.setFontSize('8');
var detailsHeight = displayInvoice(doc, invoice, layout.headerRight, layout.accountTop-10, layout);
layout.headerTop = Math.max(layout.headerTop, detailsHeight + 50);
layout.tableTop = Math.max(layout.tableTop, detailsHeight + 150);
2014-03-09 22:19:56 +01:00
SetPdfColor('Black',doc); //set black color
doc.setFontSize(7);
doc.setFontType("normal");
displayClient(doc, invoice, layout.headerRight, layout.headerTop, layout);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
2014-04-21 21:09:16 +02:00
SetPdfColor('White',doc);
doc.setFontType('bold');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setLineWidth(0.3);
2014-04-29 22:46:40 +02:00
if (NINJA.secondaryColor) {
setDocHexFill(doc, NINJA.secondaryColor);
setDocHexDraw(doc, NINJA.secondaryColor);
} else {
doc.setDrawColor(63,60,60);
doc.setFillColor(63,60,60);
}
2014-03-09 22:19:56 +01:00
var left = layout.marginLeft - layout.tablePadding;
var top = layout.tableTop - layout.tablePadding;
var width = layout.marginRight - (2 * layout.tablePadding);
var height = 20;
doc.rect(left, top, width, height, 'FD');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
displayInvoiceHeader(doc, invoice, layout);
SetPdfColor('Black',doc);
var y = displayInvoiceItems(doc, invoice, layout);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var height1 = displayNotesAndTerms(doc, layout, invoice, y);
var height2 = displaySubtotals(doc, layout, invoice, y, layout.unitCostRight);
y += Math.max(height1, height2);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var left = layout.marginLeft - layout.tablePadding;
var top = y - layout.tablePadding;
var width = layout.marginRight - (2 * layout.tablePadding);
var height = 20;
2014-04-29 22:46:40 +02:00
if (NINJA.secondaryColor) {
setDocHexFill(doc, NINJA.secondaryColor);
setDocHexDraw(doc, NINJA.secondaryColor);
} else {
doc.setDrawColor(63,60,60);
doc.setFillColor(63,60,60);
}
2014-03-09 22:19:56 +01:00
doc.rect(left, top, width, height, 'FD');
doc.setFontType('bold');
SetPdfColor('White', doc);
doc.setFontSize(12);
2014-05-20 23:40:09 +02:00
var label = invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due;
2014-03-09 22:19:56 +01:00
var labelX = layout.unitCostRight-(doc.getStringUnitWidth(label) * doc.internal.getFontSize());
doc.text(labelX, y+2, label);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontType('normal');
var amount = formatMoney(invoice.balance_amount , currencyId);
headerLeft=layout.headerRight+400;
var amountX = layout.lineTotalRight - (doc.getStringUnitWidth(amount) * doc.internal.getFontSize());
doc.text(amountX, y+2, amount);
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return doc;
}
2014-02-21 06:26:40 +01:00
2014-04-21 21:09:16 +02:00
function Report3AddHeader (invoice, layout, doc)
2014-03-09 22:19:56 +01:00
{
doc.setLineWidth(0.5);
2014-04-29 22:46:40 +02:00
if (NINJA.primaryColor) {
setDocHexFill(doc, NINJA.primaryColor);
setDocHexDraw(doc, NINJA.primaryColor);
} else {
doc.setDrawColor(242,101,34);
doc.setFillColor(242,101,34);
}
2014-04-21 21:09:16 +02:00
2014-03-09 22:19:56 +01:00
var x1 =0;
var y1 = 0;
var w2 = 595;
2014-04-21 21:09:16 +02:00
var h2 = Math.max(110, getInvoiceDetailsHeight(invoice, layout) + 30);
2014-03-09 22:19:56 +01:00
doc.rect(x1, y1, w2, h2, 'FD');
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
SetPdfColor('White',doc);
2014-04-21 21:09:16 +02:00
2014-03-09 22:19:56 +01:00
//second column
2014-04-21 21:09:16 +02:00
doc.setFontType('bold');
2014-03-09 22:19:56 +01:00
var MaxWidth=594;
2014-04-21 21:09:16 +02:00
var LineOne= invoice.account.name;
2014-03-09 22:19:56 +01:00
var AlignLine = MaxWidth-30- (doc.getStringUnitWidth(LineOne) * doc.internal.getFontSize());
2014-04-21 21:09:16 +02:00
if (LineOne) {
2014-06-04 22:41:34 +02:00
doc.setFontSize('30');
2014-04-21 21:09:16 +02:00
doc.setFontType('bold');
doc.text(40, 50, LineOne);
2014-03-09 22:19:56 +01:00
}
}
2014-02-21 08:20:46 +01:00
2014-03-09 22:19:56 +01:00
function Report1AddNewPage(invoice,account,doc)
{
doc.addPage();
2014-04-14 13:33:43 +02:00
if (logoImages.imageLogo1)
2014-03-09 22:19:56 +01:00
{
pageHeight=820;
2014-04-14 13:33:43 +02:00
y=pageHeight-logoImages.imageLogoHeight1;
2014-03-09 22:19:56 +01:00
var left = 20;//headerRight - invoice.imageLogoWidth1;
2014-04-14 13:33:43 +02:00
doc.addImage(logoImages.imageLogo1, 'JPEG', left, y, logoImages.imageLogoWidth1, logoImages.imageLogoHeight1);
2014-02-21 08:20:46 +01:00
2014-03-09 22:19:56 +01:00
}
2014-02-21 08:20:46 +01:00
2014-03-09 22:19:56 +01:00
GlobalY = 40;
return GlobalY;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function displayAccount(doc, invoice, x, y, layout) {
var account = invoice.account;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (!account) {
return;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var data = [
account.name,
account.work_email,
account.work_phone
];
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
displayGrid(doc, invoice, data, x, y, layout, true);
2014-02-21 06:29:38 +01:00
2014-03-09 22:19:56 +01:00
data = [
concatStrings(account.address1, account.address2),
concatStrings(account.city, account.state, account.postal_code),
account.country ? account.country.name : false
];
2014-02-21 06:26:40 +01:00
2014-04-27 15:22:03 +02:00
var nameWidth = doc.getStringUnitWidth(account.name) * doc.internal.getFontSize() * 1.1;
var emailWidth = doc.getStringUnitWidth(account.work_email) * doc.internal.getFontSize() * 1.1;
width = Math.max(emailWidth, nameWidth, 120);
2014-03-09 22:19:56 +01:00
x += width;
2014-03-05 15:43:41 +01:00
2014-03-09 22:19:56 +01:00
displayGrid(doc, invoice, data, x, y, layout);
}
2014-03-05 15:43:41 +01:00
2014-03-09 22:19:56 +01:00
function displayClient(doc, invoice, x, y, layout) {
var client = invoice.client;
if (!client) {
return;
2014-03-17 19:11:39 +01:00
}
2014-03-09 22:19:56 +01:00
var data = [
getClientDisplayName(client),
concatStrings(client.address1, client.address2),
concatStrings(client.city, client.state, client.postal_code),
2014-03-17 19:05:01 +01:00
client.country ? client.country.name : false,
2014-03-09 22:19:56 +01:00
client.contacts[0].email
];
2014-03-05 15:43:41 +01:00
2014-04-26 21:00:31 +02:00
return displayGrid(doc, invoice, data, x, y, layout, true);
2014-03-09 22:19:56 +01:00
}
2014-03-05 15:43:41 +01:00
2014-03-09 22:19:56 +01:00
function displayInvoice(doc, invoice, x, y, layout, rightAlignX) {
if (!invoice) {
return;
}
2014-02-21 06:26:40 +01:00
2014-04-21 21:09:16 +02:00
var data = getInvoiceDetails(invoice);
return displayGrid(doc, invoice, data, x, y, layout, true, rightAlignX);
}
function getInvoiceDetails(invoice) {
return [
2014-03-23 13:19:54 +01:00
{'invoice_number': invoice.invoice_number},
{'po_number': invoice.po_number},
{'invoice_date': invoice.invoice_date},
{'due_date': invoice.due_date},
2014-04-21 21:09:16 +02:00
{'custom_label1': invoice.account.custom_value1},
{'custom_label2': invoice.account.custom_value2},
{'custom_client_label1': invoice.client.custom_value1},
{'custom_client_label2': invoice.client.custom_value2},
{'balance_due': formatMoney(invoice.balance_amount, invoice.client.currency_id)},
];
}
2014-02-21 06:26:40 +01:00
2014-04-21 21:09:16 +02:00
function getInvoiceDetailsHeight(invoice, layout) {
var data = getInvoiceDetails(invoice);
var count = 0;
for (var key in data) {
if (!data.hasOwnProperty(key)) {
continue;
}
var obj = data[key];
for (var subKey in obj) {
if (!obj.hasOwnProperty(subKey)) {
continue;
}
if (obj[subKey]) {
count++;
}
}
}
return count * layout.rowHeight;
2014-03-09 22:19:56 +01:00
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function displaySubtotals(doc, layout, invoice, y, rightAlignTitleX)
{
if (!invoice) {
return;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
//var taxTitle = 'Tax ' + getInvoiceTaxRate(invoice) + '%';
var data = [
2014-03-23 13:19:54 +01:00
{'subtotal': formatMoney(invoice.subtotal_amount, invoice.client.currency_id)},
{'discount': invoice.discount_amount > 0 ? formatMoney(invoice.discount_amount, invoice.client.currency_id) : false},
{'tax': invoice.tax_amount > 0 ? formatMoney(invoice.tax_amount, invoice.client.currency_id) : false},
{'paid_to_date': formatMoney(invoice.amount - invoice.balance, invoice.client.currency_id)}
2014-03-09 22:19:56 +01:00
];
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return displayGrid(doc, invoice, data, 300, y, layout, true, 550, rightAlignTitleX) + 10;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function concatStrings() {
var concatStr = '';
2014-03-17 19:05:01 +01:00
var data = [];
2014-03-09 22:19:56 +01:00
for (var i=0; i<arguments.length; i++) {
var string = arguments[i];
if (string) {
2014-03-17 19:05:01 +01:00
data.push(string);
2014-03-09 22:19:56 +01:00
}
2014-03-17 19:05:01 +01:00
}
for (var i=0; i<data.length; i++) {
concatStr += data[i];
if (i == 0 && data.length > 1) {
2014-03-09 22:19:56 +01:00
concatStr += ', ';
2014-03-17 19:05:01 +01:00
} else if (i < data.length -1) {
2014-03-09 22:19:56 +01:00
concatStr += ' ';
}
}
2014-03-17 19:05:01 +01:00
return data.length ? concatStr : false;
2014-03-09 22:19:56 +01:00
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function displayGrid(doc, invoice, data, x, y, layout, hasheader, rightAlignX, rightAlignTitleX) {
var numLines = 0;
var origY = y;
for (var i=0; i<data.length; i++) {
doc.setFontType('normal');
2014-03-23 19:19:03 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.invoice_design_id == 1 && i > 0 && origY === layout.accountTop) {
SetPdfColor('GrayText',doc);
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var row = data[i];
if (!row) {
continue;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (hasheader && i === 0 && !rightAlignTitleX) {
doc.setFontType('bold');
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (typeof row === 'object') {
for (var key in row) {
if (row.hasOwnProperty(key)) {
var value = row[key] ? row[key] + '' : false;
2014-02-21 06:26:40 +01:00
}
2014-03-09 22:19:56 +01:00
}
if (!value) {
continue;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
var marginLeft;
if (rightAlignX) {
marginLeft = rightAlignX - (doc.getStringUnitWidth(value) * doc.internal.getFontSize());
} else {
marginLeft = x + 80;
}
2014-04-21 21:09:16 +02:00
doc.text(marginLeft, y, value);
2014-03-23 13:53:16 +01:00
doc.setFontType('normal');
2014-05-20 23:40:09 +02:00
if (invoice.is_quote) {
if (key == 'invoice_number') {
key = 'quote_number';
} else if (key == 'invoice_date') {
key = 'quote_date';
} else if (key == 'balance_due') {
key = 'total';
}
}
2014-04-21 21:09:16 +02:00
if (key.substring(0, 6) === 'custom') {
key = invoice.account[key];
} else {
key = invoiceLabels[key];
}
2014-03-23 19:19:03 +01:00
2014-03-09 22:19:56 +01:00
if (rightAlignTitleX) {
marginLeft = rightAlignTitleX - (doc.getStringUnitWidth(key) * doc.internal.getFontSize());
} else {
marginLeft = x;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
doc.text(marginLeft, y, key);
} else {
doc.text(x, y, row);
2014-02-25 13:54:50 +01:00
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
numLines++;
y += layout.rowHeight;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return numLines * layout.rowHeight;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function displayNotesAndTerms(doc, layout, invoice, y)
{
doc.setFontType("normal");
var origY = y;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.public_notes) {
doc.text(layout.marginLeft, y, invoice.public_notes);
y += 16 + (doc.splitTextToSize(invoice.public_notes, 300).length * doc.internal.getFontSize());
}
if (invoice.terms) {
doc.setFontType("bold");
2014-03-23 13:19:54 +01:00
doc.text(layout.marginLeft, y, invoiceLabels.terms);
2014-03-09 22:19:56 +01:00
y += 16;
doc.setFontType("normal");
doc.text(layout.marginLeft, y, invoice.terms);
y += 16 + (doc.splitTextToSize(invoice.terms, 300).length * doc.internal.getFontSize());
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return y - origY;
}
2014-03-03 16:49:10 +01:00
2014-03-09 22:19:56 +01:00
function calculateAmounts(invoice) {
var total = 0;
var hasTaxes = false;
2014-03-03 16:49:10 +01:00
2014-03-09 22:19:56 +01:00
for (var i=0; i<invoice.invoice_items.length; i++) {
var item = invoice.invoice_items[i];
var tax = 0;
if (item.tax && parseFloat(item.tax.rate)) {
tax = parseFloat(item.tax.rate);
} else if (item.tax_rate && parseFloat(item.tax_rate)) {
tax = parseFloat(item.tax_rate);
}
2014-03-03 16:49:10 +01:00
2014-03-09 22:19:56 +01:00
var lineTotal = NINJA.parseFloat(item.cost) * NINJA.parseFloat(item.qty);
if (tax) {
2014-06-01 15:35:19 +02:00
lineTotal += roundToTwo(lineTotal * tax / 100);
2014-03-09 22:19:56 +01:00
}
if (lineTotal) {
total += lineTotal;
2014-03-03 16:49:10 +01:00
}
2014-03-09 22:19:56 +01:00
if ((item.tax && item.tax.rate > 0) || (item.tax_rate && parseFloat(item.tax_rate) > 0)) {
hasTaxes = true;
}
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
invoice.subtotal_amount = total;
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.discount > 0) {
2014-02-21 06:26:40 +01:00
2014-06-01 15:35:19 +02:00
var discount = roundToTwo(total * (invoice.discount/100));
2014-03-09 22:19:56 +01:00
total -= discount;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
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);
2014-06-01 15:35:19 +02:00
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (tax) {
2014-06-01 15:35:19 +02:00
var tax = roundToTwo(total * (tax/100));
2014-03-09 22:19:56 +01:00
total = parseFloat(total) + parseFloat(tax);
}
2014-02-21 06:26:40 +01:00
2014-06-01 16:38:23 +02:00
invoice.balance_amount = roundToTwo(total) - (roundToTwo(invoice.amount) - roundToTwo(invoice.balance));
2014-03-09 22:19:56 +01:00
invoice.tax_amount = tax;
invoice.discount_amount = discount;
invoice.has_taxes = hasTaxes;
2014-02-21 07:02:06 +01:00
2014-03-09 22:19:56 +01:00
return invoice;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function getInvoiceTaxRate(invoice) {
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);
}
return tax;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
function displayInvoiceHeader(doc, invoice, layout) {
2014-03-05 17:42:48 +01:00
2014-03-23 13:19:54 +01:00
var costX = layout.unitCostRight - (doc.getStringUnitWidth(invoiceLabels.unit_cost) * doc.internal.getFontSize());
var qtyX = layout.qtyRight - (doc.getStringUnitWidth(invoiceLabels.quantity) * doc.internal.getFontSize());
var taxX = layout.taxRight - (doc.getStringUnitWidth(invoiceLabels.tax) * doc.internal.getFontSize());
var totalX = layout.lineTotalRight - (doc.getStringUnitWidth(invoiceLabels.line_total) * doc.internal.getFontSize());
2014-03-05 17:42:48 +01:00
2014-03-23 13:19:54 +01:00
doc.text(layout.marginLeft, layout.tableTop, invoiceLabels.item);
doc.text(layout.descriptionLeft, layout.tableTop, invoiceLabels.description);
doc.text(costX, layout.tableTop, invoiceLabels.unit_cost);
doc.text(qtyX, layout.tableTop, invoiceLabels.quantity);
doc.text(totalX, layout.tableTop, invoiceLabels.line_total);
2014-03-05 17:42:48 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.has_taxes)
{
2014-03-23 13:19:54 +01:00
doc.text(taxX, layout.tableTop, invoiceLabels.tax);
2014-03-09 22:19:56 +01:00
}
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
}
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
function displayInvoiceItems(doc, invoice, layout) {
doc.setFontType("normal");
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
var line = 1;
var total = 0;
var shownItem = false;
var currencyId = invoice && invoice.client ? invoice.client.currency_id : 1;
var tableTop = layout.tableTop;
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
doc.setFontSize(8);
for (var i=0; i<invoice.invoice_items.length; i++) {
var item = invoice.invoice_items[i];
var numLines = doc.splitTextToSize(item.notes, 200).length + 2;
2014-03-10 12:04:15 +01:00
//console.log('num lines %s', numLines);
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
var y = tableTop + (line * layout.tableRowHeight) + (2 * layout.tablePadding);
var top = y - layout.tablePadding;
var newTop = top + (numLines * layout.tableRowHeight);
2014-03-09 03:06:00 +01:00
2014-03-09 22:19:56 +01:00
if (newTop > 770) {
line = 0;
tableTop = layout.accountTop + layout.tablePadding;
y = tableTop;
top = y - layout.tablePadding;
newTop = top + (numLines * layout.tableRowHeight);
doc.addPage();
2014-03-09 03:06:00 +01:00
}
2014-03-09 22:19:56 +01:00
var left = layout.marginLeft - layout.tablePadding;
var width = layout.marginRight + layout.tablePadding;
2014-03-05 17:40:10 +01:00
2014-03-09 22:19:56 +01:00
var cost = formatMoney(item.cost, currencyId, true);
var qty = NINJA.parseFloat(item.qty) ? NINJA.parseFloat(item.qty) + '' : '';
var notes = item.notes;
var productKey = item.product_key;
var tax = 0;
if (item.tax && parseFloat(item.tax.rate)) {
tax = parseFloat(item.tax.rate);
} else if (item.tax_rate && parseFloat(item.tax_rate)) {
tax = parseFloat(item.tax_rate);
}
2014-03-05 17:40:10 +01:00
2014-03-09 22:19:56 +01:00
// show at most one blank line
if (shownItem && (!cost || cost == '0.00') && !qty && !notes && !productKey) {
continue;
}
shownItem = true;
2014-03-05 17:40:10 +01:00
2014-03-09 22:19:56 +01:00
// process date variables
2014-05-01 21:54:02 +02:00
if (invoice.is_recurring) {
notes = processVariables(notes);
productKey = processVariables(productKey);
}
2014-03-09 22:19:56 +01:00
var lineTotal = NINJA.parseFloat(item.cost) * NINJA.parseFloat(item.qty);
if (tax) {
lineTotal += lineTotal * tax / 100;
2014-03-05 17:42:48 +01:00
}
2014-03-09 22:19:56 +01:00
if (lineTotal) {
total += lineTotal;
2014-03-05 17:40:10 +01:00
}
2014-03-09 22:19:56 +01:00
lineTotal = formatMoney(lineTotal, currencyId, true);
var costX = layout.unitCostRight - (doc.getStringUnitWidth(cost) * doc.internal.getFontSize());
var qtyX = layout.qtyRight - (doc.getStringUnitWidth(qty) * doc.internal.getFontSize());
var taxX = layout.taxRight - (doc.getStringUnitWidth(tax+'%') * doc.internal.getFontSize());
var totalX = layout.lineTotalRight - (doc.getStringUnitWidth(lineTotal) * doc.internal.getFontSize());
2014-03-10 07:10:08 +01:00
//if (i==0) y -= 4;
2014-03-05 17:40:10 +01:00
2014-03-09 22:19:56 +01:00
line += numLines;
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.invoice_design_id == 1) {
if (i%2 == 0) {
doc.setDrawColor(255,255,255);
2014-03-09 23:31:46 +01:00
doc.setFillColor(246,246,246);
2014-03-09 22:19:56 +01:00
doc.rect(left, top, width-left, newTop-top, 'FD');
doc.setLineWidth(0.3);
doc.setDrawColor(200,200,200);
doc.line(left, top, width, top);
doc.line(left, newTop, width, newTop);
}
} else if (invoice.invoice_design_id == 2) {
if (i%2 == 0) {
left = 0;
width = 1000;
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
doc.setDrawColor(255,255,255);
doc.setFillColor(235,235,235);
doc.rect(left, top, width-left, newTop-top, 'FD');
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
}
} else {
doc.setLineWidth(0.3);
doc.setDrawColor(150,150,150);
doc.line(left, newTop, width, newTop);
2014-02-21 06:26:40 +01:00
}
2014-03-09 22:19:56 +01:00
y += 4;
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
if (invoice.invoice_design_id == 1) {
2014-05-04 19:11:27 +02:00
SetPdfColor('LightBlue', doc, 'primary');
2014-03-09 22:19:56 +01:00
} else if (invoice.invoice_design_id == 2) {
2014-05-04 19:11:27 +02:00
SetPdfColor('SomeGreen', doc, 'primary');
2014-03-09 22:19:56 +01:00
} else if (invoice.invoice_design_id == 3) {
doc.setFontType('bold');
} else {
SetPdfColor('Black', doc);
2014-02-21 06:26:40 +01:00
}
2014-03-10 07:10:08 +01:00
doc.text(layout.marginLeft, y+2, productKey);
2014-03-09 22:19:56 +01:00
SetPdfColor('Black', doc);
doc.setFontType('normal');
2014-02-25 13:54:50 +01:00
2014-03-10 07:10:08 +01:00
doc.text(layout.descriptionLeft, y+2, notes);
doc.text(costX, y+2, cost);
doc.text(qtyX, y+2, qty);
doc.text(totalX, y+2, lineTotal);
2014-02-21 06:26:40 +01:00
2014-02-25 13:54:50 +01:00
if (tax) {
2014-03-10 07:10:08 +01:00
doc.text(taxX, y+2, tax+'%');
2014-02-25 13:54:50 +01:00
}
2014-03-09 22:19:56 +01:00
}
2014-02-25 13:54:50 +01:00
2014-03-09 22:19:56 +01:00
y = tableTop + (line * layout.tableRowHeight) + (3 * layout.tablePadding);
var cutoff = 700;
if (invoice.terms) {
cutoff -= 50;
}
if (invoice.public_notes) {
cutoff -= 50;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
if (y > cutoff) {
doc.addPage();
return layout.marginLeft;
}
2014-02-21 06:26:40 +01:00
2014-03-09 22:19:56 +01:00
return y;
2014-04-14 13:33:43 +02:00
}
// http://stackoverflow.com/questions/1068834/object-comparison-in-javascript
function objectEquals(x, y) {
// if both are function
if (x instanceof Function) {
if (y instanceof Function) {
return x.toString() === y.toString();
}
return false;
}
if (x === null || x === undefined || y === null || y === undefined) { return x === y; }
if (x === y || x.valueOf() === y.valueOf()) { return true; }
// if one of them is date, they must had equal valueOf
if (x instanceof Date) { return false; }
if (y instanceof Date) { return false; }
// if they are not function or strictly equal, they both need to be Objects
if (!(x instanceof Object)) { return false; }
if (!(y instanceof Object)) { return false; }
var p = Object.keys(x);
return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) ?
p.every(function (i) { return objectEquals(x[i], y[i]); }) : false;
2014-04-29 22:46:40 +02:00
}
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
function setDocHexColor(doc, hex) {
var r = hexToR(hex);
var g = hexToG(hex);
var b = hexToB(hex);
return doc.setTextColor(r, g, b);
}
function setDocHexFill(doc, hex) {
var r = hexToR(hex);
var g = hexToG(hex);
var b = hexToB(hex);
return doc.setFillColor(r, g, b);
}
function setDocHexDraw(doc, hex) {
var r = hexToR(hex);
var g = hexToG(hex);
var b = hexToB(hex);
return doc.setDrawColor(r, g, b);
}
2014-05-09 15:28:20 +02:00
2014-05-11 20:24:30 +02:00
function openUrl(url, track) {
trackUrl(track ? track : url);
2014-05-09 15:28:20 +02:00
window.open(url, '_blank');
2014-05-13 23:04:42 +02:00
}
function toggleDatePicker(field) {
$('#'+field).datepicker('show');
}
2014-06-01 16:38:23 +02:00
function roundToTwo(num, toString) {
var val = +(Math.round(num + "e+2") + "e-2");
return toString ? val.toFixed(2) : val;
2014-06-01 15:35:19 +02:00
}