2016-07-21 14:35:23 +02:00
|
|
|
var NINJA = NINJA || {};
|
|
|
|
|
|
|
|
NINJA.TEMPLATES = {
|
|
|
|
CLEAN: "1",
|
|
|
|
BOLD:"2",
|
|
|
|
MODERN: "3",
|
|
|
|
NORMAL:"4",
|
|
|
|
BUSINESS:"5",
|
|
|
|
CREATIVE:"6",
|
|
|
|
ELEGANT:"7",
|
|
|
|
HIPSTER:"8",
|
|
|
|
PLAYFUL:"9",
|
|
|
|
PHOTO:"10"
|
|
|
|
};
|
|
|
|
|
|
|
|
function GetPdfMake(invoice, javascript, callback) {
|
2017-06-19 21:19:52 +02:00
|
|
|
var itemsTable = false;
|
2017-10-29 20:14:05 +01:00
|
|
|
if (invoice.hasTasks) {
|
|
|
|
// check if we need to add a second table for tasks
|
|
|
|
if (invoice.hasSecondTable) {
|
|
|
|
var json = JSON.parse(javascript);
|
|
|
|
for (var i=0; i<json.content.length; i++) {
|
|
|
|
var item = json.content[i];
|
|
|
|
if (item.table && item.table.body == '$invoiceLineItems') {
|
|
|
|
itemsTable = JSON.stringify(item);
|
|
|
|
itemsTable = itemsTable.replace('$invoiceLineItems', '$taskLineItems');
|
|
|
|
itemsTable = itemsTable.replace('$invoiceLineItemColumns', '$taskLineItemColumns');
|
|
|
|
break;
|
|
|
|
}
|
2017-06-19 21:19:52 +02:00
|
|
|
}
|
2017-10-29 20:14:05 +01:00
|
|
|
itemsTable = JSON.parse(itemsTable);
|
|
|
|
json.content.splice(i+1, 0, itemsTable);
|
|
|
|
javascript = JSON.stringify(json);
|
|
|
|
// use the single product table for tasks
|
|
|
|
} else {
|
|
|
|
javascript = javascript.replace('$invoiceLineItems', '$taskLineItems');
|
|
|
|
javascript = javascript.replace('$invoiceLineItemColumns', '$taskLineItemColumns');
|
2017-06-19 21:19:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
javascript = NINJA.decodeJavascript(invoice, javascript);
|
|
|
|
|
|
|
|
function jsonCallBack(key, val) {
|
|
|
|
|
|
|
|
// handle custom functions
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (val.indexOf('$firstAndLast') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return function (i, node) {
|
|
|
|
return (i === 0 || i === node.table.body.length) ? parseFloat(parts[1]) : 0;
|
|
|
|
};
|
|
|
|
} else if (val.indexOf('$none') === 0) {
|
|
|
|
return function (i, node) {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
} else if (val.indexOf('$notFirstAndLastColumn') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return function (i, node) {
|
|
|
|
return (i === 0 || i === node.table.widths.length) ? 0 : parseFloat(parts[1]);
|
|
|
|
};
|
|
|
|
} else if (val.indexOf('$notFirst') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return function (i, node) {
|
|
|
|
return i === 0 ? 0 : parseFloat(parts[1]);
|
|
|
|
};
|
|
|
|
} else if (val.indexOf('$amount') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return function (i, node) {
|
|
|
|
return parseFloat(parts[1]);
|
|
|
|
};
|
|
|
|
} else if (val.indexOf('$primaryColor') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return NINJA.primaryColor || parts[1];
|
|
|
|
} else if (val.indexOf('$secondaryColor') === 0) {
|
|
|
|
var parts = val.split(':');
|
|
|
|
return NINJA.secondaryColor || parts[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine whether or not to show the header/footer
|
|
|
|
if (invoice.features.customize_invoice_design) {
|
|
|
|
if (key === 'header') {
|
|
|
|
return function(page, pages) {
|
2017-03-27 13:14:01 +02:00
|
|
|
if (page === 1 || invoice.account.all_pages_header == '1') {
|
|
|
|
if (invoice.features.remove_created_by) {
|
|
|
|
return NINJA.updatePageCount(JSON.parse(JSON.stringify(val)), page, pages);
|
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
} else if (key === 'footer') {
|
|
|
|
return function(page, pages) {
|
2017-03-27 13:14:01 +02:00
|
|
|
if (page === pages || invoice.account.all_pages_footer == '1') {
|
|
|
|
if (invoice.features.remove_created_by) {
|
|
|
|
return NINJA.updatePageCount(JSON.parse(JSON.stringify(val)), page, pages);
|
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for markdown
|
|
|
|
if (key === 'text') {
|
|
|
|
val = NINJA.parseMarkdownText(val, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (key === 'stack') {
|
|
|
|
val = NINJA.parseMarkdownStack(val);
|
|
|
|
val = NINJA.parseMarkdownText(val, false);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ninja logo to the footer
|
|
|
|
var dd = JSON.parse(javascript, jsonCallBack);
|
|
|
|
var designId = invoice.invoice_design_id;
|
2017-09-03 09:22:54 +02:00
|
|
|
if (!invoice.features.remove_created_by) {
|
2016-07-21 14:35:23 +02:00
|
|
|
var footer = (typeof dd.footer === 'function') ? dd.footer() : dd.footer;
|
|
|
|
if (footer) {
|
|
|
|
if (footer.hasOwnProperty('columns')) {
|
|
|
|
footer.columns.push({image: logoImages.imageLogo1, alignment: 'right', width: 130, margin: [0, 0, 0, 0]})
|
|
|
|
} else {
|
|
|
|
var foundColumns;
|
|
|
|
for (var i=0; i<footer.length; i++) {
|
|
|
|
var item = footer[i];
|
|
|
|
if (item.hasOwnProperty('columns')) {
|
|
|
|
foundColumns = true;
|
|
|
|
var columns = item.columns;
|
|
|
|
if (columns[0].hasOwnProperty('stack')) {
|
|
|
|
columns[0].stack.push({image: logoImages.imageLogo3, alignment: 'left', width: 130, margin: [40, 6, 0, 0]});
|
|
|
|
} else {
|
2016-07-26 08:55:39 +02:00
|
|
|
columns.push({image: logoImages.imageLogo1, alignment: 'right', width: 130, margin: [0, -40, 20, 0]})
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundColumns) {
|
|
|
|
footer.push({image: logoImages.imageLogo1, alignment: 'right', width: 130, margin: [0, 0, 10, 10]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:53:45 +02:00
|
|
|
// support setting noWrap as a style
|
|
|
|
dd.styles.noWrap = {'noWrap': true};
|
2017-12-21 18:56:07 +01:00
|
|
|
dd.styles.discount = {'alignment': 'right'};
|
2017-08-23 20:53:45 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
// set page size
|
|
|
|
dd.pageSize = invoice.account.page_size;
|
|
|
|
|
2018-02-15 09:29:45 +01:00
|
|
|
if (invoice.watermark) {
|
|
|
|
dd.watermark = {
|
|
|
|
text: invoice.watermark,
|
|
|
|
color: 'black',
|
|
|
|
opacity: 0.04,
|
|
|
|
};
|
|
|
|
}
|
2016-10-26 11:44:04 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
pdfMake.fonts = {}
|
|
|
|
fonts = window.invoiceFonts || invoice.invoice_fonts;
|
|
|
|
|
|
|
|
// Add only the loaded fonts
|
|
|
|
$.each(fonts, function(i,font){
|
|
|
|
addFont(font);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function addFont(font){
|
|
|
|
if(window.ninjaFontVfs[font.folder]){
|
|
|
|
folder = 'fonts/'+font.folder;
|
|
|
|
pdfMake.fonts[font.name] = {
|
|
|
|
normal: folder+'/'+font.normal,
|
|
|
|
italics: folder+'/'+font.italics,
|
|
|
|
bold: folder+'/'+font.bold,
|
|
|
|
bolditalics: folder+'/'+font.bolditalics
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!dd.defaultStyle)dd.defaultStyle = {font:NINJA.bodyFont};
|
|
|
|
else if(!dd.defaultStyle.font)dd.defaultStyle.font = NINJA.bodyFont;
|
|
|
|
|
|
|
|
doc = pdfMake.createPdf(dd);
|
|
|
|
doc.save = function(fileName) {
|
|
|
|
this.download(fileName);
|
|
|
|
};
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2017-03-25 22:35:08 +01:00
|
|
|
NINJA.updatePageCount = function(obj, pageNumber, pageCount)
|
|
|
|
{
|
|
|
|
var pageNumberRegExp = new RegExp('\\$pageNumber', 'g');
|
|
|
|
var pageCountRegExp = new RegExp('\\$pageCount', 'g');
|
|
|
|
|
|
|
|
for (key in obj) {
|
|
|
|
if (!obj.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var val = obj[key];
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
val = val.replace(pageNumberRegExp, pageNumber);
|
|
|
|
val = val.replace(pageCountRegExp, pageCount);
|
|
|
|
obj[key] = val;
|
|
|
|
} else if (typeof val === 'object') {
|
|
|
|
obj[key] = NINJA.updatePageCount(val, pageNumber, pageCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
NINJA.decodeJavascript = function(invoice, javascript)
|
|
|
|
{
|
|
|
|
var account = invoice.account;
|
|
|
|
var blankImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=';
|
|
|
|
|
|
|
|
// search/replace variables
|
|
|
|
var json = {
|
|
|
|
'accountName': account.name || ' ',
|
2017-09-03 09:22:54 +02:00
|
|
|
'accountLogo': window.accountLogo ? window.accountLogo : blankImage,
|
2016-07-21 14:35:23 +02:00
|
|
|
'accountDetails': NINJA.accountDetails(invoice),
|
|
|
|
'accountAddress': NINJA.accountAddress(invoice),
|
|
|
|
'invoiceDetails': NINJA.invoiceDetails(invoice),
|
|
|
|
'invoiceDetailsHeight': (NINJA.invoiceDetails(invoice).length * 16) + 16,
|
2017-10-29 20:14:05 +01:00
|
|
|
'invoiceLineItems': invoice.is_statement ? NINJA.statementLines(invoice) : NINJA.invoiceLines(invoice),
|
|
|
|
'invoiceLineItemColumns': invoice.is_statement ? NINJA.statementColumns(invoice) : NINJA.invoiceColumns(invoice, javascript),
|
2017-06-19 21:19:52 +02:00
|
|
|
'taskLineItems': NINJA.invoiceLines(invoice, true),
|
2017-10-29 15:06:40 +01:00
|
|
|
'taskLineItemColumns': NINJA.invoiceColumns(invoice, javascript, true),
|
2017-09-03 09:22:54 +02:00
|
|
|
'invoiceDocuments' : NINJA.invoiceDocuments(invoice),
|
2017-10-29 15:06:40 +01:00
|
|
|
'quantityWidth': NINJA.quantityWidth(invoice),
|
|
|
|
'taxWidth': NINJA.taxWidth(invoice),
|
2016-07-21 14:35:23 +02:00
|
|
|
'clientDetails': NINJA.clientDetails(invoice),
|
|
|
|
'notesAndTerms': NINJA.notesAndTerms(invoice),
|
2017-01-23 16:00:44 +01:00
|
|
|
'subtotals': invoice.is_statement ? NINJA.statementSubtotals(invoice) : NINJA.subtotals(invoice),
|
2016-07-21 14:35:23 +02:00
|
|
|
'subtotalsHeight': (NINJA.subtotals(invoice).length * 16) + 16,
|
2017-02-24 13:15:02 +01:00
|
|
|
'subtotalsWithoutBalance': invoice.is_statement ? [[]] : NINJA.subtotals(invoice, true),
|
2016-07-21 14:35:23 +02:00
|
|
|
'subtotalsBalance': NINJA.subtotalsBalance(invoice),
|
|
|
|
'balanceDue': formatMoneyInvoice(invoice.balance_amount, invoice),
|
|
|
|
'invoiceFooter': NINJA.invoiceFooter(invoice),
|
2017-11-30 20:26:46 +01:00
|
|
|
'invoiceNumber': invoice.is_statement ? '' : (invoice.invoice_number || ' '),
|
2017-12-03 14:21:03 +01:00
|
|
|
'entityType': NINJA.entityType(invoice),
|
|
|
|
'entityTypeUC': NINJA.entityType(invoice).toUpperCase(),
|
2017-01-23 16:00:44 +01:00
|
|
|
'entityTaxType': invoice.is_statement ? invoiceLabels.statement : invoice.is_quote ? invoiceLabels.tax_quote : invoiceLabels.tax_invoice,
|
2016-07-21 14:35:23 +02:00
|
|
|
'fontSize': NINJA.fontSize,
|
|
|
|
'fontSizeLarger': NINJA.fontSize + 1,
|
|
|
|
'fontSizeLargest': NINJA.fontSize + 2,
|
|
|
|
'fontSizeSmaller': NINJA.fontSize - 1,
|
|
|
|
'bodyFont': NINJA.bodyFont,
|
|
|
|
'headerFont': NINJA.headerFont,
|
2018-03-12 21:02:48 +01:00
|
|
|
'signature': NINJA.signature(invoice),
|
2017-12-27 11:54:50 +01:00
|
|
|
'signatureBase64': NINJA.signatureImage(invoice),
|
|
|
|
'signatureDate': NINJA.signatureDate(invoice),
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var key in json) {
|
2017-10-29 15:06:40 +01:00
|
|
|
// remove trailing commas for these fields
|
|
|
|
if (['quantityWidth', 'taxWidth'].indexOf(key) >= 0) {
|
|
|
|
var regExp = new RegExp('"\\$'+key+'",', 'g');
|
|
|
|
val = json[key];
|
|
|
|
} else {
|
|
|
|
var regExp = new RegExp('"\\$'+key+'"', 'g');
|
|
|
|
var val = JSON.stringify(json[key]);
|
|
|
|
val = doubleDollarSign(val);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
javascript = javascript.replace(regExp, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// search/replace labels
|
|
|
|
var regExp = new RegExp('"\\$\\\w*?Label(UC)?(:)?(\\\?)?"', 'g');
|
|
|
|
var matches = javascript.match(regExp);
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
for (var i=0; i<matches.length; i++) {
|
|
|
|
var match = matches[i];
|
|
|
|
field = match.substring(2, match.indexOf('Label'));
|
|
|
|
field = toSnakeCase(field);
|
|
|
|
var value = getDescendantProp(invoice, field);
|
|
|
|
if (match.indexOf('?') < 0 || value) {
|
|
|
|
if (invoice.partial > 0 && field == 'balance_due') {
|
|
|
|
field = 'partial_due';
|
|
|
|
} else if (invoice.is_quote) {
|
2016-09-19 13:08:21 +02:00
|
|
|
if (field == 'due_date') {
|
|
|
|
field = 'valid_until';
|
|
|
|
} else {
|
|
|
|
field = field.replace('invoice', 'quote');
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-02-24 13:15:02 +01:00
|
|
|
if (invoice.is_statement) {
|
|
|
|
if (field == 'your_invoice') {
|
|
|
|
field = 'your_statement';
|
|
|
|
} else if (field == 'invoice_issued_to') {
|
|
|
|
field = 'statement_issued_to';
|
|
|
|
} else if (field == 'invoice_to') {
|
|
|
|
field = 'statement_to';
|
|
|
|
}
|
2018-03-16 10:31:15 +01:00
|
|
|
} else if (invoice.is_delivery_note) {
|
|
|
|
field = 'delivery_note';
|
2017-03-26 20:15:51 +02:00
|
|
|
} else if (invoice.balance_amount < 0) {
|
2017-03-26 17:25:59 +02:00
|
|
|
if (field == 'your_invoice') {
|
|
|
|
field = 'your_credit';
|
|
|
|
} else if (field == 'invoice_issued_to') {
|
|
|
|
field = 'credit_issued_to';
|
|
|
|
} else if (field == 'invoice_to') {
|
|
|
|
field = 'credit_to';
|
|
|
|
}
|
2017-02-24 13:15:02 +01:00
|
|
|
}
|
2017-03-26 17:25:59 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
var label = invoiceLabels[field];
|
|
|
|
if (match.indexOf('UC') >= 0) {
|
|
|
|
label = label.toUpperCase();
|
|
|
|
}
|
|
|
|
if (match.indexOf(':') >= 0) {
|
|
|
|
label = label + ':';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label = ' ';
|
|
|
|
}
|
|
|
|
javascript = javascript.replace(match, '"'+label+'"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// search/replace values
|
2018-03-13 11:03:14 +01:00
|
|
|
var regExp = new RegExp('\\$[a-zA-Z\\.]*[Value]?', 'g');
|
2016-07-21 14:35:23 +02:00
|
|
|
var matches = javascript.match(regExp);
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
for (var i=0; i<matches.length; i++) {
|
|
|
|
var match = matches[i];
|
|
|
|
|
|
|
|
// reserved words
|
2017-03-27 13:14:01 +02:00
|
|
|
if ([
|
2018-03-13 11:03:14 +01:00
|
|
|
'$none',
|
|
|
|
'$firstAndLast',
|
|
|
|
'$notFirstAndLastColumn',
|
|
|
|
'$notFirst',
|
|
|
|
'$amount',
|
|
|
|
'$primaryColor',
|
|
|
|
'$secondaryColor',
|
2017-03-27 13:14:01 +02:00
|
|
|
].indexOf(match) >= 0) {
|
2016-07-21 14:35:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-20 20:30:55 +01:00
|
|
|
field = match.replace('$invoice.', '$');
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
// legacy style had 'Value' at the end
|
2018-03-13 11:03:14 +01:00
|
|
|
if (endsWith(field, 'Value')) {
|
|
|
|
field = field.substring(1, field.indexOf('Value'));
|
2016-07-21 14:35:23 +02:00
|
|
|
} else {
|
2018-03-13 11:03:14 +01:00
|
|
|
field = field.substring(1, field.length);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2018-03-13 11:03:14 +01:00
|
|
|
|
|
|
|
if (! field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
field = toSnakeCase(field);
|
|
|
|
|
2018-02-20 20:30:55 +01:00
|
|
|
if (field == 'footer') {
|
|
|
|
field = 'invoice_footer';
|
|
|
|
} else if (match == '$account.phone') {
|
|
|
|
field = 'account.work_phone';
|
|
|
|
} else if (match == '$client.phone') {
|
|
|
|
field = 'client.phone';
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
var value = getDescendantProp(invoice, field) || ' ';
|
2017-10-24 11:03:05 +02:00
|
|
|
value = doubleDollarSign(value) + '';
|
2017-06-14 10:01:18 +02:00
|
|
|
value = value.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
2018-02-13 21:13:49 +01:00
|
|
|
|
|
|
|
if (['amount', 'partial', 'client.balance', 'client.paid_to_date'].indexOf(field) >= 0) {
|
|
|
|
value = formatMoneyInvoice(value, invoice);
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:03:14 +01:00
|
|
|
if ($.trim(value)) {
|
|
|
|
javascript = javascript.replace(match, value);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return javascript;
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:02:48 +01:00
|
|
|
NINJA.signature = function(invoice) {
|
|
|
|
var invitation = NINJA.getSignatureInvitation(invoice);
|
|
|
|
if (invitation) {
|
|
|
|
return {
|
|
|
|
"stack": [
|
|
|
|
{
|
|
|
|
"image": "$signatureBase64",
|
|
|
|
"margin": [200, 10, 0, 0]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"canvas": [{
|
|
|
|
"type": "line",
|
|
|
|
"x1": 200,
|
2018-03-23 15:18:11 +01:00
|
|
|
"y1": -25,
|
2018-03-12 21:02:48 +01:00
|
|
|
"x2": 504,
|
2018-03-23 15:18:11 +01:00
|
|
|
"y2": -25,
|
2018-03-12 21:02:48 +01:00
|
|
|
"lineWidth": 1,
|
|
|
|
"lineColor": "#888888"
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": [invoiceLabels.date, ": ", "$signatureDate"],
|
2018-03-23 15:18:11 +01:00
|
|
|
"margin": [200, -20, 0, 0]
|
2018-03-12 21:02:48 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return '';
|
2017-12-28 09:02:59 +01:00
|
|
|
}
|
2018-03-12 21:02:48 +01:00
|
|
|
}
|
2017-12-28 09:02:59 +01:00
|
|
|
|
2018-03-12 21:02:48 +01:00
|
|
|
NINJA.signatureImage = function(invoice) {
|
|
|
|
var blankImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=';
|
|
|
|
var invitation = NINJA.getSignatureInvitation(invoice);
|
|
|
|
return invitation ? invitation.signature_base64 : blankImage;
|
2017-12-27 11:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.signatureDate = function(invoice) {
|
2018-03-12 21:02:48 +01:00
|
|
|
var invitation = NINJA.getSignatureInvitation(invoice);
|
|
|
|
return invitation ? NINJA.formatDateTime(invitation.signature_date, invoice.account) : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.getSignatureInvitation = function(invoice) {
|
2017-12-27 11:54:50 +01:00
|
|
|
if (! invoice.invitations || ! invoice.invitations.length) {
|
2018-03-12 21:02:48 +01:00
|
|
|
return false;
|
2017-12-27 11:54:50 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 19:34:36 +01:00
|
|
|
if (! parseInt(invoice.account.signature_on_pdf)) {
|
2018-03-12 21:02:48 +01:00
|
|
|
return false;
|
2017-12-27 19:34:36 +01:00
|
|
|
}
|
2017-12-27 11:54:50 +01:00
|
|
|
|
2017-12-28 09:02:59 +01:00
|
|
|
for (var i=0; i<invoice.invitations.length; i++) {
|
|
|
|
var invitation = invoice.invitations[i];
|
|
|
|
if (invitation.signature_base64) {
|
2018-03-12 21:02:48 +01:00
|
|
|
return invitation;
|
2017-12-28 09:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:02:48 +01:00
|
|
|
return false;
|
2017-12-27 11:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.formatDateTime = function(date, account) {
|
|
|
|
var format = account.datetime_format ? account.datetime_format.format_moment : 'LLL';
|
2018-03-09 11:51:24 +01:00
|
|
|
var timezone = account.timezone ? account.timezone.name : 'US/Eastern';
|
2017-12-27 11:54:50 +01:00
|
|
|
|
|
|
|
return date ? moment.utc(date).tz(timezone).format(format) : '';
|
|
|
|
}
|
|
|
|
|
2017-12-03 14:21:03 +01:00
|
|
|
NINJA.entityType = function(invoice)
|
|
|
|
{
|
|
|
|
if (invoice.is_delivery_note) {
|
|
|
|
return invoiceLabels.delivery_note;
|
|
|
|
} else if (invoice.is_statement) {
|
|
|
|
return invoiceLabels.statement;
|
|
|
|
} else if (invoice.is_quote) {
|
|
|
|
return invoiceLabels.quote;
|
|
|
|
} else if (invoice.balance_amount < 0) {
|
|
|
|
return invoiceLabels.credit_note;
|
|
|
|
} else {
|
|
|
|
return invoiceLabels.invoice;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
|
|
|
NINJA.notesAndTerms = function(invoice)
|
|
|
|
{
|
|
|
|
var data = [];
|
|
|
|
|
|
|
|
if (invoice.public_notes) {
|
|
|
|
data.push({stack:[{text: invoice.is_recurring ? processVariables(invoice.public_notes) : invoice.public_notes, style: ['notes']}]});
|
|
|
|
data.push({text:' '});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invoice.terms) {
|
|
|
|
data.push({text:invoiceLabels.terms, style: ['termsLabel']});
|
|
|
|
data.push({stack:[{text: invoice.is_recurring ? processVariables(invoice.terms) : invoice.terms, style: ['terms']}]});
|
|
|
|
}
|
|
|
|
|
|
|
|
return NINJA.prepareDataList(data, 'notesAndTerms');
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:00:44 +01:00
|
|
|
NINJA.statementColumns = function(invoice)
|
|
|
|
{
|
|
|
|
return ["22%", "22%", "22%", "17%", "17%"];
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.statementLines = function(invoice)
|
|
|
|
{
|
|
|
|
var grid = [[]];
|
2017-02-24 13:15:02 +01:00
|
|
|
grid[0].push({text: invoiceLabels.invoice_number, style: ['tableHeader', 'itemTableHeader']});
|
2017-01-23 16:00:44 +01:00
|
|
|
grid[0].push({text: invoiceLabels.invoice_date, style: ['tableHeader', 'invoiceDateTableHeader']});
|
|
|
|
grid[0].push({text: invoiceLabels.due_date, style: ['tableHeader', 'dueDateTableHeader']});
|
|
|
|
grid[0].push({text: invoiceLabels.total, style: ['tableHeader', 'totalTableHeader']});
|
|
|
|
grid[0].push({text: invoiceLabels.balance, style: ['tableHeader', 'balanceTableHeader']});
|
|
|
|
|
|
|
|
for (var i = 0; i < invoice.invoice_items.length; i++) {
|
|
|
|
var item = invoice.invoice_items[i];
|
|
|
|
var row = [];
|
2017-02-24 13:15:02 +01:00
|
|
|
var rowStyle = (i % 2 == 0) ? 'odd' : 'even';
|
2017-01-23 16:00:44 +01:00
|
|
|
grid.push([
|
2017-02-24 13:15:02 +01:00
|
|
|
{text: item.invoice_number, style:['invoiceNumber', 'productKey', rowStyle]},
|
|
|
|
{text: item.invoice_date && item.invoice_date != '0000-00-00' ? moment(item.invoice_date).format(invoice.date_format) : ' ', style:['invoiceDate', rowStyle]},
|
|
|
|
{text: item.due_date && item.due_date != '0000-00-00' ? moment(item.due_date).format(invoice.date_format) : ' ', style:['dueDate', rowStyle]},
|
|
|
|
{text: formatMoneyInvoice(item.amount, invoice), style:['subtotals', rowStyle]},
|
|
|
|
{text: formatMoneyInvoice(item.balance, invoice), style:['lineTotal', rowStyle]},
|
2017-01-23 16:00:44 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NINJA.prepareDataTable(grid, 'invoiceItems');
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:06:40 +01:00
|
|
|
NINJA.invoiceColumns = function(invoice, design, isTasks)
|
2016-07-21 14:35:23 +02:00
|
|
|
{
|
|
|
|
var account = invoice.account;
|
|
|
|
var columns = [];
|
2017-10-29 08:48:23 +01:00
|
|
|
var fields = NINJA.productFields(invoice, isTasks);
|
|
|
|
var hasDescription = fields.indexOf('product.description') >= 0;
|
2017-10-29 20:29:46 +01:00
|
|
|
var hasPadding = design.indexOf('"pageMargins":[0') == -1 && design.indexOf('"pageMargins": [0') == -1;
|
2017-10-29 08:48:23 +01:00
|
|
|
|
|
|
|
for (var i=0; i<fields.length; i++) {
|
|
|
|
var field = fields[i];
|
2017-10-29 15:06:40 +01:00
|
|
|
var width = 0;
|
|
|
|
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note) {
|
|
|
|
var skipFields = [
|
|
|
|
'product.unit_cost',
|
|
|
|
'product.rate',
|
|
|
|
'product.tax',
|
|
|
|
'product.line_total',
|
|
|
|
];
|
|
|
|
if (skipFields.indexOf(field) >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
if (field == 'product.custom_value1') {
|
|
|
|
if (invoice.has_custom_item_value1) {
|
2017-10-29 15:06:40 +01:00
|
|
|
width = 10;
|
|
|
|
} else {
|
|
|
|
continue;
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
|
|
|
} else if (field == 'product.custom_value2') {
|
|
|
|
if (invoice.has_custom_item_value2) {
|
2017-10-29 15:06:40 +01:00
|
|
|
width = 10;
|
|
|
|
} else {
|
|
|
|
continue;
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
|
|
|
} else if (field == 'product.tax') {
|
2017-10-29 11:39:15 +01:00
|
|
|
if (invoice.has_item_taxes) {
|
2017-10-29 15:06:40 +01:00
|
|
|
width = 15;
|
|
|
|
} else {
|
|
|
|
continue;
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
2018-01-02 10:51:50 +01:00
|
|
|
} else if (field == 'product.discount') {
|
|
|
|
if (invoice.has_item_discounts) {
|
|
|
|
width = 15;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
} else if (field == 'product.description') {
|
2017-10-29 15:06:40 +01:00
|
|
|
width = 0;
|
|
|
|
} else {
|
|
|
|
width = 14;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width) {
|
2017-10-29 20:29:46 +01:00
|
|
|
// make the first and last columns of the Bold design a bit wider
|
|
|
|
if (! hasPadding) {
|
|
|
|
if (i == 0 || i == fields.length - 1) {
|
|
|
|
width += 8;
|
|
|
|
}
|
|
|
|
}
|
2017-10-29 15:06:40 +01:00
|
|
|
if (! hasDescription) {
|
|
|
|
width = '*';
|
|
|
|
} else {
|
|
|
|
width += '%';
|
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
} else {
|
2017-10-29 15:06:40 +01:00
|
|
|
width = '*';
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
2017-10-29 15:06:40 +01:00
|
|
|
|
|
|
|
columns.push(width)
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 10:56:10 +01:00
|
|
|
//console.log(columns);
|
2016-07-21 14:35:23 +02:00
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.invoiceFooter = function(invoice)
|
|
|
|
{
|
|
|
|
var footer = invoice.invoice_footer;
|
|
|
|
|
|
|
|
if (invoice.is_recurring) {
|
|
|
|
footer = processVariables(footer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!invoice.features.invoice_settings && invoice.invoice_design_id == 3) {
|
|
|
|
return footer ? footer.substring(0, 200) : ' ';
|
|
|
|
} else {
|
|
|
|
return footer || ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:06:40 +01:00
|
|
|
NINJA.quantityWidth = function(invoice)
|
|
|
|
{
|
2017-10-29 20:14:05 +01:00
|
|
|
var fields = NINJA.productFields(invoice);
|
|
|
|
return fields.indexOf('product.quantity') >= 0 ? '"14%", ' : '';
|
2017-10-29 15:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.taxWidth = function(invoice)
|
|
|
|
{
|
2017-10-29 20:14:05 +01:00
|
|
|
var fields = NINJA.productFields(invoice);
|
2017-10-29 20:29:46 +01:00
|
|
|
return invoice.has_item_taxes && fields.indexOf('product.tax') >= 0 ? '"14%", ' : '';
|
2017-10-29 15:06:40 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
NINJA.productFields = function(invoice, isTasks) {
|
|
|
|
var account = invoice.account;
|
2017-10-29 15:09:42 +01:00
|
|
|
var allFields = JSON.parse(account.invoice_fields);
|
2017-10-29 08:48:23 +01:00
|
|
|
|
2017-10-29 15:09:42 +01:00
|
|
|
if (allFields) {
|
2017-10-29 16:36:10 +01:00
|
|
|
if (isTasks && allFields.task_fields && allFields.task_fields.length) {
|
2017-10-29 15:09:42 +01:00
|
|
|
return allFields.task_fields;
|
2017-10-29 16:36:10 +01:00
|
|
|
} else if (! isTasks && allFields.product_fields && allFields.product_fields.length) {
|
2017-10-29 15:09:42 +01:00
|
|
|
return allFields.product_fields;
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:09:42 +01:00
|
|
|
var fields = [
|
|
|
|
isTasks ? 'product.service' : 'product.item',
|
|
|
|
'product.description',
|
|
|
|
'product.custom_value1',
|
|
|
|
'product.custom_value2',
|
|
|
|
isTasks ? 'product.rate' : 'product.unit_cost',
|
|
|
|
isTasks ? 'product.hours' : 'product.quantity',
|
|
|
|
'product.tax',
|
|
|
|
'product.line_total',
|
|
|
|
];
|
2017-10-29 08:48:23 +01:00
|
|
|
|
2017-10-29 16:36:10 +01:00
|
|
|
// add backwards compatibility for 'hide qty' setting
|
|
|
|
if (invoice.account.hide_quantity == '1' && ! isTasks) {
|
|
|
|
fields.splice(5, 1);
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2017-06-19 21:19:52 +02:00
|
|
|
NINJA.invoiceLines = function(invoice, isSecondTable) {
|
2016-07-21 14:35:23 +02:00
|
|
|
var account = invoice.account;
|
|
|
|
var total = 0;
|
|
|
|
var shownItem = false;
|
2017-06-19 21:19:52 +02:00
|
|
|
var isTasks = isSecondTable || (invoice.hasTasks && !invoice.hasStandard);
|
2016-07-21 14:35:23 +02:00
|
|
|
var grid = [[]];
|
2017-06-20 12:10:56 +02:00
|
|
|
var styles = ['tableHeader'];
|
2017-11-20 14:40:12 +01:00
|
|
|
var skipFields = [
|
|
|
|
'product.unit_cost',
|
|
|
|
'product.rate',
|
|
|
|
'product.tax',
|
|
|
|
'product.line_total',
|
2017-12-21 18:56:07 +01:00
|
|
|
'product.discount',
|
2017-11-20 14:40:12 +01:00
|
|
|
];
|
2017-10-29 08:48:23 +01:00
|
|
|
|
2018-04-05 12:12:03 +02:00
|
|
|
if (isSecondTable && invoice.hasStandard) {
|
2017-06-20 12:10:56 +02:00
|
|
|
styles.push('secondTableHeader');
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
var fields = NINJA.productFields(invoice, isTasks);
|
|
|
|
var hasDescription = fields.indexOf('product.description') >= 0;
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
for (var i=0; i<fields.length; i++) {
|
|
|
|
var field = fields[i].split('.')[1]; // split to remove 'product.'
|
2017-11-20 14:40:12 +01:00
|
|
|
|
|
|
|
if (invoice.is_delivery_note && skipFields.indexOf(fields[i]) >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:39:15 +01:00
|
|
|
var headerStyles = styles.concat([snakeToCamel(field), snakeToCamel(field) + 'TableHeader']);
|
2017-10-29 16:36:10 +01:00
|
|
|
var value = invoiceLabels[field];
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-29 16:36:10 +01:00
|
|
|
if (field == 'custom_value1') {
|
|
|
|
if (invoice.has_custom_item_value1) {
|
2018-04-04 15:24:59 +02:00
|
|
|
value = NINJA.getCustomLabel(account.custom_fields.product1);
|
2017-10-29 16:36:10 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (field == 'custom_value2') {
|
|
|
|
if (invoice.has_custom_item_value2) {
|
2018-04-04 15:24:59 +02:00
|
|
|
value = NINJA.getCustomLabel(account.custom_fields.product2);
|
2017-10-29 16:36:10 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
} else if (field == 'tax' && ! invoice.has_item_taxes) {
|
|
|
|
continue;
|
2018-01-02 10:51:50 +01:00
|
|
|
} else if (field == 'discount' && ! invoice.has_item_discounts) {
|
|
|
|
continue;
|
2017-10-29 11:39:15 +01:00
|
|
|
} else if (field == 'unit_cost' || field == 'rate' || field == 'hours') {
|
|
|
|
headerStyles.push('cost');
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-29 20:14:05 +01:00
|
|
|
if (i == 0) {
|
|
|
|
headerStyles.push('firstColumn');
|
|
|
|
} else if (i == fields.length - 1) {
|
|
|
|
headerStyles.push('lastColumn');
|
|
|
|
}
|
|
|
|
|
2017-10-29 16:36:10 +01:00
|
|
|
grid[0].push({text: value, style: headerStyles});
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
for (var i=0; i<invoice.invoice_items.length; i++) {
|
2016-07-21 14:35:23 +02:00
|
|
|
var row = [];
|
|
|
|
var item = invoice.invoice_items[i];
|
2017-12-13 15:41:19 +01:00
|
|
|
var cost = NINJA.parseFloat(item.cost) ? formatMoneyInvoice(NINJA.parseFloat(item.cost), invoice, null, getPrecision(NINJA.parseFloat(item.cost))) : ' ';
|
2017-12-31 14:35:03 +01:00
|
|
|
var qty = NINJA.parseFloat(item.qty) ? formatMoneyInvoice(NINJA.parseFloat(item.qty), invoice, 'none', getPrecision(NINJA.parseFloat(item.qty))) + '' : ' ';
|
2017-12-30 20:22:03 +01:00
|
|
|
var discount = roundToTwo(NINJA.parseFloat(item.discount));
|
2016-07-21 14:35:23 +02:00
|
|
|
var notes = item.notes;
|
|
|
|
var productKey = item.product_key;
|
|
|
|
var tax1 = '';
|
|
|
|
var tax2 = '';
|
2018-01-16 12:03:04 +01:00
|
|
|
var customValue1 = item.custom_value1;
|
|
|
|
var customValue2 = item.custom_value2;
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-06-19 21:19:52 +02:00
|
|
|
if (isTasks) {
|
|
|
|
if (item.invoice_item_type_id != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (item.invoice_item_type_id == 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
if (parseFloat(item.tax_rate1) != 0) {
|
|
|
|
tax1 = parseFloat(item.tax_rate1);
|
|
|
|
}
|
|
|
|
if (parseFloat(item.tax_rate2) != 0) {
|
|
|
|
tax2 = parseFloat(item.tax_rate2);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// show at most one blank line
|
2017-09-14 17:18:34 +02:00
|
|
|
if (shownItem && !notes && !productKey && !item.cost) {
|
2016-07-21 14:35:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
shownItem = true;
|
|
|
|
|
|
|
|
// process date variables
|
|
|
|
if (invoice.is_recurring) {
|
|
|
|
notes = processVariables(notes);
|
|
|
|
productKey = processVariables(productKey);
|
2018-01-16 12:03:04 +01:00
|
|
|
customValue1 = processVariables(item.custom_value1);
|
|
|
|
customValue2 = processVariables(item.custom_value2);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-11-20 14:40:12 +01:00
|
|
|
|
2017-11-06 09:08:23 +01:00
|
|
|
var lineTotal = roundSignificant(NINJA.parseFloat(item.cost) * NINJA.parseFloat(item.qty));
|
2017-12-21 18:56:07 +01:00
|
|
|
|
|
|
|
if (discount != 0) {
|
|
|
|
if (parseInt(invoice.is_amount_discount)) {
|
|
|
|
lineTotal -= discount;
|
|
|
|
} else {
|
|
|
|
lineTotal -= (lineTotal * discount / 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:08:06 +02:00
|
|
|
if (account.include_item_taxes_inline == '1') {
|
2017-06-15 09:16:35 +02:00
|
|
|
var taxAmount1 = 0;
|
|
|
|
var taxAmount2 = 0;
|
2016-08-15 17:08:06 +02:00
|
|
|
if (tax1) {
|
2017-06-15 09:16:35 +02:00
|
|
|
taxAmount1 = roundToTwo(lineTotal * tax1 / 100);
|
2016-08-15 17:08:06 +02:00
|
|
|
}
|
|
|
|
if (tax2) {
|
2017-06-15 09:16:35 +02:00
|
|
|
taxAmount2 = roundToTwo(lineTotal * tax2 / 100);
|
2016-08-15 17:08:06 +02:00
|
|
|
}
|
2017-06-15 09:16:35 +02:00
|
|
|
lineTotal += taxAmount1 + taxAmount2;
|
2016-08-15 17:08:06 +02:00
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-17 13:38:02 +02:00
|
|
|
if (lineTotal != 0) {
|
|
|
|
lineTotal = formatMoneyInvoice(lineTotal, invoice);
|
|
|
|
}
|
2017-06-20 11:53:27 +02:00
|
|
|
rowStyle = (grid.length % 2 == 0) ? 'even' : 'odd';
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
for (var j=0; j<fields.length; j++) {
|
|
|
|
var field = fields[j].split('.')[1]; // split to remove 'product.'
|
2017-11-20 14:40:12 +01:00
|
|
|
|
|
|
|
if (invoice.is_delivery_note && skipFields.indexOf(fields[j]) >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
var value = item[field];
|
|
|
|
var styles = [snakeToCamel(field), rowStyle];
|
|
|
|
|
|
|
|
if (field == 'custom_value1' && ! invoice.has_custom_item_value1) {
|
|
|
|
continue;
|
2017-10-29 18:19:14 +01:00
|
|
|
} else if (field == 'custom_value2' && ! invoice.has_custom_item_value2) {
|
2017-10-29 08:48:23 +01:00
|
|
|
continue;
|
|
|
|
} else if (field == 'tax' && ! invoice.has_item_taxes) {
|
|
|
|
continue;
|
2018-01-02 10:51:50 +01:00
|
|
|
} else if (field == 'discount' && ! invoice.has_item_discounts) {
|
|
|
|
continue;
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
|
|
|
|
if (field == 'item' || field == 'service') {
|
2018-01-16 12:03:04 +01:00
|
|
|
value = productKey;
|
2017-10-29 08:48:23 +01:00
|
|
|
styles.push('productKey');
|
|
|
|
} else if (field == 'description') {
|
2018-01-16 12:03:04 +01:00
|
|
|
value = notes;
|
2017-10-29 11:39:15 +01:00
|
|
|
} else if (field == 'unit_cost' || field == 'rate') {
|
2017-10-30 10:56:10 +01:00
|
|
|
value = cost;
|
2017-10-29 08:48:23 +01:00
|
|
|
styles.push('cost');
|
2017-10-29 11:39:15 +01:00
|
|
|
} else if (field == 'quantity' || field == 'hours') {
|
2017-10-30 10:56:10 +01:00
|
|
|
value = qty;
|
2017-10-29 11:39:15 +01:00
|
|
|
if (field == 'hours') {
|
|
|
|
styles.push('cost');
|
|
|
|
}
|
2018-01-16 12:03:04 +01:00
|
|
|
} else if (field == 'custom_value1') {
|
|
|
|
value = customValue1;
|
|
|
|
} else if (field == 'custom_value2') {
|
|
|
|
value = customValue2;
|
2017-12-21 18:56:07 +01:00
|
|
|
} else if (field == 'discount') {
|
2018-03-27 10:55:07 +02:00
|
|
|
if (NINJA.parseFloat(discount)) {
|
|
|
|
if (parseInt(invoice.is_amount_discount)) {
|
|
|
|
value = formatMoneyInvoice(discount, invoice);
|
|
|
|
} else {
|
|
|
|
if (discount) {
|
|
|
|
value = discount + '%';
|
|
|
|
}
|
2018-01-02 10:51:50 +01:00
|
|
|
}
|
2018-03-27 10:55:07 +02:00
|
|
|
} else {
|
|
|
|
value = '';
|
2017-12-21 18:56:07 +01:00
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
} else if (field == 'tax') {
|
|
|
|
value = ' ';
|
2016-07-21 19:21:27 +02:00
|
|
|
if (item.tax_name1) {
|
2018-01-20 20:46:48 +01:00
|
|
|
value += (tax1 || '0') + '%';
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
if (item.tax_name2) {
|
|
|
|
if (item.tax_name1) {
|
|
|
|
value += ' ';
|
|
|
|
}
|
2018-01-20 20:46:48 +01:00
|
|
|
value += (tax2 || '0') + '%';
|
2017-10-29 08:48:23 +01:00
|
|
|
}
|
|
|
|
} else if (field == 'line_total') {
|
|
|
|
value = lineTotal;
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-10-29 08:48:23 +01:00
|
|
|
|
2017-10-29 15:06:40 +01:00
|
|
|
if (j == 0) {
|
|
|
|
styles.push('firstColumn');
|
|
|
|
} else if (j == fields.length - 1) {
|
|
|
|
styles.push('lastColumn');
|
|
|
|
}
|
|
|
|
|
2017-10-29 08:48:23 +01:00
|
|
|
row.push({text:value || ' ', style:styles});
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
grid.push(row);
|
|
|
|
}
|
|
|
|
|
2017-10-30 10:56:10 +01:00
|
|
|
//console.log(JSON.stringify(grid));
|
2016-07-21 14:35:23 +02:00
|
|
|
return NINJA.prepareDataTable(grid, 'invoiceItems');
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.invoiceDocuments = function(invoice) {
|
2017-02-04 19:53:17 +01:00
|
|
|
if (invoice.account.invoice_embed_documents != '1') {
|
2017-02-01 11:40:32 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-02-04 19:53:17 +01:00
|
|
|
var j = 0;
|
2016-07-21 14:35:23 +02:00
|
|
|
var stack = [];
|
|
|
|
var stackItem = null;
|
|
|
|
|
2017-02-01 11:40:32 +01:00
|
|
|
if (invoice.documents) {
|
|
|
|
for (var i = 0; i < invoice.documents.length; i++) {
|
|
|
|
addDoc(invoice.documents[i]);
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2017-02-01 11:40:32 +01:00
|
|
|
if (invoice.expenses) {
|
2016-07-21 14:35:23 +02:00
|
|
|
for (var i = 0; i < invoice.expenses.length; i++) {
|
|
|
|
var expense = invoice.expenses[i];
|
2017-02-01 11:40:32 +01:00
|
|
|
for (var j = 0; j < expense.documents.length; j++) {
|
|
|
|
addDoc(expense.documents[j]);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addDoc(document){
|
|
|
|
var path = document.base64;
|
|
|
|
|
|
|
|
if(!path)path = 'docs/'+document.public_id+'/'+document.name;
|
|
|
|
if(path && (window.pdfMake.vfs[path] || document.base64)){
|
|
|
|
// Only embed if we actually have an image for it
|
|
|
|
if(j%3==0){
|
|
|
|
stackItem = {columns:[]};
|
|
|
|
stack.push(stackItem);
|
|
|
|
}
|
|
|
|
stackItem.columns.push({stack:[{image:path,style:'invoiceDocument',fit:[150,150]}], width:175})
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack.length?{stack:stack}:[];
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:00:44 +01:00
|
|
|
NINJA.statementSubtotals = function(invoice)
|
|
|
|
{
|
|
|
|
var data = [[
|
2017-05-14 16:18:30 +02:00
|
|
|
{ text: invoiceLabels.balance_due, style: ['subtotalsLabel', 'subtotalsBalanceDueLabel'] },
|
|
|
|
{ text: formatMoneyInvoice(invoice.balance_amount, invoice), style: ['subtotals', 'subtotalsBalanceDue'] }
|
2017-01-23 16:00:44 +01:00
|
|
|
]];
|
|
|
|
|
|
|
|
return NINJA.prepareDataPairs(data, 'subtotals');
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
NINJA.subtotals = function(invoice, hideBalance)
|
|
|
|
{
|
2017-11-20 14:40:12 +01:00
|
|
|
if (! invoice || invoice.is_delivery_note) {
|
|
|
|
return [[]];
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var account = invoice.account;
|
|
|
|
var data = [];
|
|
|
|
data.push([{text: invoiceLabels.subtotal, style: ['subtotalsLabel', 'subtotalLabel']}, {text: formatMoneyInvoice(invoice.subtotal_amount, invoice), style: ['subtotals', 'subtotal']}]);
|
|
|
|
|
|
|
|
if (invoice.discount_amount != 0) {
|
|
|
|
data.push([{text: invoiceLabels.discount , style: ['subtotalsLabel', 'discountLabel']}, {text: formatMoneyInvoice(invoice.discount_amount, invoice), style: ['subtotals', 'discount']}]);
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:32:46 +01:00
|
|
|
var customValue1 = NINJA.parseFloat(invoice.custom_value1);
|
2018-04-04 15:24:59 +02:00
|
|
|
var customValue1Label = account.custom_fields.invoice1 || invoiceLabels.surcharge;
|
2017-03-16 16:32:46 +01:00
|
|
|
|
|
|
|
var customValue2 = NINJA.parseFloat(invoice.custom_value2);
|
2018-04-04 15:24:59 +02:00
|
|
|
var customValue2Label = account.custom_fields.invoice2 || invoiceLabels.surcharge;
|
2017-03-16 16:32:46 +01:00
|
|
|
|
|
|
|
if (customValue1 && invoice.custom_taxes1 == '1') {
|
|
|
|
data.push([{text: customValue1Label, style: ['subtotalsLabel', 'customTax1Label']}, {text: formatMoneyInvoice(invoice.custom_value1, invoice), style: ['subtotals', 'customTax1']}]);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-03-16 16:32:46 +01:00
|
|
|
if (customValue2 && invoice.custom_taxes2 == '1') {
|
|
|
|
data.push([{text: customValue2Label, style: ['subtotalsLabel', 'customTax2Label']}, {text: formatMoneyInvoice(invoice.custom_value2, invoice), style: ['subtotals', 'customTax2']}]);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var key in invoice.item_taxes) {
|
|
|
|
if (invoice.item_taxes.hasOwnProperty(key)) {
|
|
|
|
var taxRate = invoice.item_taxes[key];
|
|
|
|
var taxStr = taxRate.name + ' ' + (taxRate.rate*1).toString() + '%';
|
|
|
|
data.push([{text: taxStr, style: ['subtotalsLabel', 'taxLabel']}, {text: formatMoneyInvoice(taxRate.amount, invoice), style: ['subtotals', 'tax']}]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 11:19:10 +02:00
|
|
|
if (parseFloat(invoice.tax_rate1 || 0) != 0 || invoice.tax_name1) {
|
2016-07-21 14:35:23 +02:00
|
|
|
var taxStr = invoice.tax_name1 + ' ' + (invoice.tax_rate1*1).toString() + '%';
|
|
|
|
data.push([{text: taxStr, style: ['subtotalsLabel', 'tax1Label']}, {text: formatMoneyInvoice(invoice.tax_amount1, invoice), style: ['subtotals', 'tax1']}]);
|
|
|
|
}
|
2017-10-18 11:19:10 +02:00
|
|
|
if (parseFloat(invoice.tax_rate2 || 0) != 0 || invoice.tax_name2) {
|
2016-07-21 14:35:23 +02:00
|
|
|
var taxStr = invoice.tax_name2 + ' ' + (invoice.tax_rate2*1).toString() + '%';
|
|
|
|
data.push([{text: taxStr, style: ['subtotalsLabel', 'tax2Label']}, {text: formatMoneyInvoice(invoice.tax_amount2, invoice), style: ['subtotals', 'tax2']}]);
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:32:46 +01:00
|
|
|
if (customValue1 && invoice.custom_taxes1 != '1') {
|
|
|
|
data.push([{text: customValue1Label, style: ['subtotalsLabel', 'custom1Label']}, {text: formatMoneyInvoice(invoice.custom_value1, invoice), style: ['subtotals', 'custom1']}]);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-03-16 16:32:46 +01:00
|
|
|
if (customValue2 && invoice.custom_taxes2 != '1') {
|
|
|
|
data.push([{text: customValue2Label, style: ['subtotalsLabel', 'custom2Label']}, {text: formatMoneyInvoice(invoice.custom_value2, invoice), style: ['subtotals', 'custom2']}]);
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var paid = invoice.amount - invoice.balance;
|
2017-03-26 20:15:51 +02:00
|
|
|
if (!invoice.is_quote && invoice.balance_amount >= 0 && (invoice.account.hide_paid_to_date != '1' || paid)) {
|
2016-07-21 14:35:23 +02:00
|
|
|
data.push([{text:invoiceLabels.paid_to_date, style: ['subtotalsLabel', 'paidToDateLabel']}, {text:formatMoneyInvoice(paid, invoice), style: ['subtotals', 'paidToDate']}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var isPartial = NINJA.parseFloat(invoice.partial);
|
|
|
|
|
|
|
|
if (!hideBalance || isPartial) {
|
|
|
|
data.push([
|
2017-05-14 16:18:30 +02:00
|
|
|
{ text: invoice.is_quote || invoice.balance_amount < 0 ? invoiceLabels.total : invoiceLabels.balance_due, style: ['subtotalsLabel', isPartial ? '' : 'subtotalsBalanceDueLabel'] },
|
|
|
|
{ text: formatMoneyInvoice(invoice.total_amount, invoice), style: ['subtotals', isPartial ? '' : 'subtotalsBalanceDue'] }
|
2016-07-21 14:35:23 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hideBalance) {
|
|
|
|
if (isPartial) {
|
|
|
|
data.push([
|
2017-05-14 16:18:30 +02:00
|
|
|
{ text: invoiceLabels.partial_due, style: ['subtotalsLabel', 'subtotalsBalanceDueLabel'] },
|
|
|
|
{ text: formatMoneyInvoice(invoice.balance_amount, invoice), style: ['subtotals', 'subtotalsBalanceDue'] }
|
2016-07-21 14:35:23 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NINJA.prepareDataPairs(data, 'subtotals');
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.subtotalsBalance = function(invoice) {
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note) {
|
|
|
|
return [[]];
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
var isPartial = NINJA.parseFloat(invoice.partial);
|
|
|
|
return [[
|
2017-05-14 16:18:30 +02:00
|
|
|
{text: isPartial ? invoiceLabels.partial_due : (invoice.is_quote || invoice.balance_amount < 0 ? invoiceLabels.total : invoiceLabels.balance_due), style:['subtotalsLabel', 'subtotalsBalanceDueLabel']},
|
|
|
|
{text: formatMoneyInvoice(invoice.balance_amount, invoice), style:['subtotals', 'subtotalsBalanceDue']}
|
2016-07-21 14:35:23 +02:00
|
|
|
]];
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.accountDetails = function(invoice) {
|
|
|
|
var account = invoice.account;
|
2016-09-05 14:28:59 +02:00
|
|
|
if (invoice.features.invoice_settings && account.invoice_fields) {
|
2016-09-05 16:44:01 +02:00
|
|
|
var fields = JSON.parse(account.invoice_fields).account_fields1;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else {
|
|
|
|
var fields = [
|
|
|
|
'account.company_name',
|
|
|
|
'account.id_number',
|
|
|
|
'account.vat_number',
|
|
|
|
'account.website',
|
|
|
|
'account.email',
|
|
|
|
'account.phone',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = [];
|
|
|
|
|
2016-09-05 16:44:01 +02:00
|
|
|
for (var i=0; i < fields.length; i++) {
|
|
|
|
var field = fields[i];
|
2017-05-08 23:27:24 +02:00
|
|
|
var value = NINJA.renderField(invoice, field);
|
2016-09-05 14:28:59 +02:00
|
|
|
if (value) {
|
|
|
|
data.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
return NINJA.prepareDataList(data, 'accountDetails');
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.accountAddress = function(invoice) {
|
|
|
|
var account = invoice.account;
|
2016-09-05 14:28:59 +02:00
|
|
|
if (invoice.features.invoice_settings && account.invoice_fields) {
|
2016-09-05 16:44:01 +02:00
|
|
|
var fields = JSON.parse(account.invoice_fields).account_fields2;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else {
|
|
|
|
var fields = [
|
|
|
|
'account.address1',
|
|
|
|
'account.address2',
|
|
|
|
'account.city_state_postal',
|
|
|
|
'account.country',
|
|
|
|
'account.custom_value1',
|
|
|
|
'account.custom_value2',
|
|
|
|
]
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 14:28:59 +02:00
|
|
|
var data = [];
|
|
|
|
|
2016-09-05 16:44:01 +02:00
|
|
|
for (var i=0; i < fields.length; i++) {
|
|
|
|
var field = fields[i];
|
2017-05-08 23:27:24 +02:00
|
|
|
var value = NINJA.renderField(invoice, field);
|
2016-09-05 14:28:59 +02:00
|
|
|
if (value) {
|
|
|
|
data.push(value);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NINJA.prepareDataList(data, 'accountAddress');
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:28:59 +02:00
|
|
|
NINJA.invoiceDetails = function(invoice) {
|
2016-07-21 14:35:23 +02:00
|
|
|
|
2016-09-05 14:28:59 +02:00
|
|
|
var account = invoice.account;
|
|
|
|
if (invoice.features.invoice_settings && account.invoice_fields) {
|
2016-09-05 16:44:01 +02:00
|
|
|
var fields = JSON.parse(account.invoice_fields).invoice_fields;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else {
|
|
|
|
var fields = [
|
|
|
|
'invoice.invoice_number',
|
|
|
|
'invoice.po_number',
|
|
|
|
'invoice.invoice_date',
|
|
|
|
'invoice.due_date',
|
|
|
|
'invoice.balance_due',
|
|
|
|
'invoice.partial_due',
|
|
|
|
'invoice.custom_text_value1',
|
|
|
|
'invoice.custom_text_value2',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
var data = [];
|
|
|
|
|
2016-09-05 16:44:01 +02:00
|
|
|
for (var i=0; i < fields.length; i++) {
|
|
|
|
var field = fields[i];
|
2017-05-08 23:27:24 +02:00
|
|
|
var value = NINJA.renderField(invoice, field, true);
|
2016-09-05 14:28:59 +02:00
|
|
|
if (value) {
|
|
|
|
data.push(value);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NINJA.prepareDataPairs(data, 'invoiceDetails');
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:28:59 +02:00
|
|
|
|
2017-05-08 23:27:24 +02:00
|
|
|
NINJA.renderField = function(invoice, field, twoColumn) {
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note) {
|
|
|
|
var skipFields = [
|
|
|
|
'invoice.due_date',
|
|
|
|
'invoice.balance_due',
|
|
|
|
'invoice.partial_due',
|
|
|
|
];
|
|
|
|
if (skipFields.indexOf(field) >= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
var client = invoice.client;
|
|
|
|
if (!client) {
|
2016-09-05 14:28:59 +02:00
|
|
|
return false;
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
|
|
|
var account = invoice.account;
|
2018-03-29 21:32:17 +02:00
|
|
|
var contact = invoice.contact || client.contacts[0];
|
2017-12-31 16:01:16 +01:00
|
|
|
var clientName = client.name || (contact.first_name || contact.last_name ? ((contact.first_name || '') + ' ' + (contact.last_name || '')) : contact.email);
|
2016-09-05 14:28:59 +02:00
|
|
|
|
2017-05-08 23:27:24 +02:00
|
|
|
var label = false;
|
|
|
|
var value = false;
|
|
|
|
|
2016-09-05 14:28:59 +02:00
|
|
|
if (field == 'client.client_name') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = clientName || ' ';
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.contact_name') {
|
2017-12-31 16:01:16 +01:00
|
|
|
value = (contact.first_name || contact.last_name) ? (contact.first_name || '') + ' ' + (contact.last_name || '') : false;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.id_number') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = client.id_number;
|
2017-07-09 19:06:34 +02:00
|
|
|
if (invoiceLabels.id_number_orig) {
|
|
|
|
label = invoiceLabels.id_number;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.vat_number') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = client.vat_number;
|
2017-07-09 19:06:34 +02:00
|
|
|
if (invoiceLabels.vat_number_orig) {
|
|
|
|
label = invoiceLabels.vat_number;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.address1') {
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note && client.shipping_address1) {
|
|
|
|
value = client.shipping_address1;
|
|
|
|
} else {
|
|
|
|
value = client.address1;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.address2') {
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note && client.shipping_address1) {
|
|
|
|
value = client.shipping_address2;
|
|
|
|
} else {
|
|
|
|
value = client.address2;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.city_state_postal') {
|
|
|
|
var cityStatePostal = '';
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note && client.shipping_address1) {
|
|
|
|
if (client.shipping_city || client.shipping_state || client.shipping_postal_code) {
|
|
|
|
var swap = client.shipping_country && client.shipping_country.swap_postal_code;
|
|
|
|
cityStatePostal = formatAddress(client.shipping_city, client.shipping_state, client.shipping_postal_code, swap);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (client.city || client.state || client.postal_code) {
|
|
|
|
var swap = client.country && client.country.swap_postal_code;
|
|
|
|
cityStatePostal = formatAddress(client.city, client.state, client.postal_code, swap);
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
}
|
2017-05-08 23:27:24 +02:00
|
|
|
value = cityStatePostal;
|
2017-01-02 12:58:51 +01:00
|
|
|
} else if (field == 'client.postal_city_state') {
|
|
|
|
var postalCityState = '';
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note && client.shipping_address1) {
|
|
|
|
if (client.shipping_city || client.shipping_state || client.shipping_postal_code) {
|
|
|
|
postalCityState = formatAddress(client.shipping_city, client.shipping_state, client.shipping_postal_code, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (client.city || client.state || client.postal_code) {
|
|
|
|
postalCityState = formatAddress(client.city, client.state, client.postal_code, true);
|
|
|
|
}
|
2017-01-02 12:58:51 +01:00
|
|
|
}
|
2017-05-08 23:27:24 +02:00
|
|
|
value = postalCityState;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.country') {
|
2017-11-20 14:40:12 +01:00
|
|
|
if (invoice.is_delivery_note && client.shipping_address1) {
|
|
|
|
value = client.shipping_country ? client.shipping_country.name : '';
|
|
|
|
} else {
|
|
|
|
value = client.country ? client.country.name : '';
|
|
|
|
}
|
2018-03-04 20:23:50 +01:00
|
|
|
} else if (field == 'client.website') {
|
|
|
|
value = client.website;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.email') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = contact.email == clientName ? '' : contact.email;
|
2016-09-23 11:11:46 +02:00
|
|
|
} else if (field == 'client.phone') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = contact.phone;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.custom_value1') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (account.custom_fields.client1 && client.custom_value1) {
|
|
|
|
label = NINJA.getCustomLabel(account.custom_fields.client1);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = client.custom_value1;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'client.custom_value2') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (account.custom_fields.client2 && client.custom_value2) {
|
|
|
|
label = NINJA.getCustomLabel(account.custom_fields.client2);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = client.custom_value2;
|
|
|
|
}
|
2017-04-16 13:31:14 +02:00
|
|
|
} else if (field == 'contact.custom_value1') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (account.custom_fields.contact1 && contact.custom_value1) {
|
|
|
|
label = NINJA.getCustomLabel(account.custom_fields.contact1);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = contact.custom_value1;
|
|
|
|
}
|
2017-04-16 13:31:14 +02:00
|
|
|
} else if (field == 'contact.custom_value2') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (account.custom_fields.contact2 && contact.custom_value2) {
|
|
|
|
label = NINJA.getCustomLabel(account.custom_fields.contact2);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = contact.custom_value2;
|
|
|
|
}
|
|
|
|
} else if (field == 'account.company_name') {
|
|
|
|
value = account.name;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.id_number') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.id_number;
|
2017-07-09 19:06:34 +02:00
|
|
|
if (invoiceLabels.id_number_orig) {
|
|
|
|
label = invoiceLabels.id_number;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.vat_number') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.vat_number;
|
2017-07-09 19:06:34 +02:00
|
|
|
if (invoiceLabels.vat_number_orig) {
|
|
|
|
label = invoiceLabels.vat_number;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.website') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.website;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.email') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.work_email;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.phone') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.work_phone;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.address1') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.address1;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.address2') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.address2;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.city_state_postal') {
|
|
|
|
var cityStatePostal = '';
|
|
|
|
if (account.city || account.state || account.postal_code) {
|
|
|
|
var swap = account.country && account.country.swap_postal_code;
|
|
|
|
cityStatePostal = formatAddress(account.city, account.state, account.postal_code, swap);
|
|
|
|
}
|
2017-05-08 23:27:24 +02:00
|
|
|
value = cityStatePostal;
|
2017-01-02 12:58:51 +01:00
|
|
|
} else if (field == 'account.postal_city_state') {
|
|
|
|
var postalCityState = '';
|
|
|
|
if (account.city || account.state || account.postal_code) {
|
|
|
|
postalCityState = formatAddress(account.city, account.state, account.postal_code, true);
|
|
|
|
}
|
2017-05-08 23:27:24 +02:00
|
|
|
value = postalCityState;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.country') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = account.country ? account.country.name : false;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else if (field == 'account.custom_value1') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (invoice.account.custom_fields.account1 && invoice.account.custom_value1) {
|
|
|
|
label = invoice.account.custom_fields.account1;
|
2017-05-08 23:27:24 +02:00
|
|
|
value = invoice.account.custom_value1;
|
2016-09-05 14:28:59 +02:00
|
|
|
}
|
|
|
|
} else if (field == 'account.custom_value2') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (invoice.account.custom_fields.account2 && invoice.account.custom_value2) {
|
|
|
|
label = invoice.account.custom_fields.account2;
|
2017-05-08 23:27:24 +02:00
|
|
|
value = invoice.account.custom_value2;
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.invoice_number') {
|
|
|
|
if (! invoice.is_statement) {
|
|
|
|
label = invoice.is_quote ? invoiceLabels.quote_number : invoice.balance_amount < 0 ? invoiceLabels.credit_number : invoiceLabels.invoice_number;
|
|
|
|
value = invoice.invoice_number;
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.po_number') {
|
|
|
|
value = invoice.po_number;
|
|
|
|
} else if (field == 'invoice.invoice_date') {
|
|
|
|
label = invoice.is_statement ? invoiceLabels.statement_date : invoice.is_quote ? invoiceLabels.quote_date : invoice.balance_amount < 0 ? invoiceLabels.credit_date : invoiceLabels.invoice_date;
|
|
|
|
value = invoice.invoice_date;
|
|
|
|
} else if (field == 'invoice.due_date') {
|
|
|
|
label = invoice.is_quote ? invoiceLabels.valid_until : invoiceLabels.due_date;
|
2018-03-22 11:40:01 +01:00
|
|
|
if (invoice.partial_due_date) {
|
2017-10-26 09:56:59 +02:00
|
|
|
value = invoice.partial_due_date;
|
|
|
|
} else {
|
|
|
|
value = invoice.due_date;
|
|
|
|
}
|
2017-05-08 23:27:24 +02:00
|
|
|
} else if (field == 'invoice.custom_text_value1') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (invoice.custom_text_value1 && account.custom_fields.invoice_text1) {
|
|
|
|
label = NINJA.getCustomLabel(invoice.account.custom_fields.invoice_text1);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = invoice.is_recurring ? processVariables(invoice.custom_text_value1) : invoice.custom_text_value1;
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.custom_text_value2') {
|
2018-04-04 15:24:59 +02:00
|
|
|
if (invoice.custom_text_value2 && account.custom_fields.invoice_text2) {
|
|
|
|
label = NINJA.getCustomLabel(invoice.account.custom_fields.invoice_text2);
|
2017-05-08 23:27:24 +02:00
|
|
|
value = invoice.is_recurring ? processVariables(invoice.custom_text_value2) : invoice.custom_text_value2;
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.balance_due') {
|
|
|
|
label = invoice.is_quote || invoice.balance_amount < 0 ? invoiceLabels.total : invoiceLabels.balance_due;
|
|
|
|
value = formatMoneyInvoice(invoice.total_amount, invoice);
|
2017-10-26 09:56:59 +02:00
|
|
|
} else if (field == 'invoice.partial_due') {
|
2017-05-08 23:27:24 +02:00
|
|
|
if (NINJA.parseFloat(invoice.partial)) {
|
|
|
|
label = invoiceLabels.partial_due;
|
|
|
|
value = formatMoneyInvoice(invoice.balance_amount, invoice);
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.invoice_total') {
|
|
|
|
if (invoice.is_statement || invoice.is_quote || invoice.balance_amount < 0) {
|
|
|
|
// hide field
|
|
|
|
} else {
|
|
|
|
value = formatMoneyInvoice(invoice.amount, invoice);
|
|
|
|
}
|
|
|
|
} else if (field == 'invoice.outstanding') {
|
|
|
|
if (invoice.is_statement || invoice.is_quote) {
|
|
|
|
// hide field
|
|
|
|
} else {
|
|
|
|
value = formatMoneyInvoice(client.balance, invoice);
|
2016-09-05 14:28:59 +02:00
|
|
|
}
|
2016-09-25 10:07:15 +02:00
|
|
|
} else if (field == '.blank') {
|
2017-05-08 23:27:24 +02:00
|
|
|
value = ' ';
|
2016-09-05 14:28:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 23:27:24 +02:00
|
|
|
if (value) {
|
|
|
|
var shortField = false;
|
|
|
|
var parts = field.split('.');
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
var shortField = parts[1];
|
|
|
|
}
|
|
|
|
var style = snakeToCamel(shortField == 'company_name' ? 'account_name' : shortField); // backwards compatibility
|
|
|
|
if (twoColumn) {
|
|
|
|
// try to automatically determine the label
|
|
|
|
if (! label && label != 'Blank') {
|
|
|
|
if (invoiceLabels[shortField]) {
|
|
|
|
label = invoiceLabels[shortField];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [{text: label, style: [style + 'Label']}, {text: value, style: [style]}];
|
|
|
|
} else {
|
|
|
|
// if the label is set prepend it to the value
|
|
|
|
if (label) {
|
|
|
|
value = label + ': ' + value;
|
|
|
|
}
|
|
|
|
return {text:value, style: [style]};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-05 14:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.clientDetails = function(invoice) {
|
|
|
|
var account = invoice.account;
|
|
|
|
if (invoice.features.invoice_settings && account.invoice_fields) {
|
2016-09-05 16:44:01 +02:00
|
|
|
var fields = JSON.parse(account.invoice_fields).client_fields;
|
2016-09-05 14:28:59 +02:00
|
|
|
} else {
|
|
|
|
var fields = [
|
|
|
|
'client.client_name',
|
|
|
|
'client.id_number',
|
|
|
|
'client.vat_number',
|
|
|
|
'client.address1',
|
|
|
|
'client.address2',
|
|
|
|
'client.city_state_postal',
|
|
|
|
'client.country',
|
|
|
|
'client.email',
|
|
|
|
'client.custom_value1',
|
|
|
|
'client.custom_value2',
|
2017-04-16 13:31:14 +02:00
|
|
|
'contact.custom_value1',
|
|
|
|
'contact.custom_value2',
|
2016-09-05 14:28:59 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
var data = [];
|
|
|
|
|
2016-09-05 16:44:01 +02:00
|
|
|
for (var i=0; i < fields.length; i++) {
|
|
|
|
var field = fields[i];
|
2017-05-08 23:27:24 +02:00
|
|
|
var value = NINJA.renderField(invoice, field);
|
2016-09-05 14:28:59 +02:00
|
|
|
if (value) {
|
|
|
|
data.push(value);
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
|
|
|
|
return NINJA.prepareDataList(data, 'clientDetails');
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.getPrimaryColor = function(defaultColor) {
|
|
|
|
return NINJA.primaryColor ? NINJA.primaryColor : defaultColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.getSecondaryColor = function(defaultColor) {
|
|
|
|
return NINJA.primaryColor ? NINJA.secondaryColor : defaultColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove blanks and add section style to all elements
|
|
|
|
NINJA.prepareDataList = function(oldData, section) {
|
|
|
|
var newData = [];
|
2017-05-08 23:27:24 +02:00
|
|
|
if (! oldData.length) {
|
|
|
|
oldData.push({text:' '});
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
for (var i=0; i<oldData.length; i++) {
|
|
|
|
var item = NINJA.processItem(oldData[i], section);
|
|
|
|
if (item.text || item.stack) {
|
|
|
|
newData.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.prepareDataTable = function(oldData, section) {
|
|
|
|
var newData = [];
|
|
|
|
for (var i=0; i<oldData.length; i++) {
|
|
|
|
var row = oldData[i];
|
|
|
|
var newRow = [];
|
|
|
|
for (var j=0; j<row.length; j++) {
|
|
|
|
var item = NINJA.processItem(row[j], section);
|
|
|
|
if (item.text || item.stack) {
|
|
|
|
newRow.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newRow.length) {
|
|
|
|
newData.push(newRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.prepareDataPairs = function(oldData, section) {
|
|
|
|
var newData = [];
|
2017-05-08 23:27:24 +02:00
|
|
|
if (! oldData.length) {
|
|
|
|
oldData.push([{text:' '}, {text:' '}]);
|
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
for (var i=0; i<oldData.length; i++) {
|
|
|
|
var row = oldData[i];
|
|
|
|
var isBlank = false;
|
|
|
|
for (var j=0; j<row.length; j++) {
|
|
|
|
var item = NINJA.processItem(row[j], section);
|
|
|
|
if (!item.text) {
|
|
|
|
isBlank = true;
|
|
|
|
}
|
|
|
|
if (j == 1) {
|
|
|
|
NINJA.processItem(row[j], section + "Value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isBlank) {
|
|
|
|
newData.push(oldData[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.processItem = function(item, section) {
|
2017-08-23 20:53:45 +02:00
|
|
|
if (! item.style) {
|
|
|
|
item.style = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
item.style.push(section);
|
|
|
|
|
|
|
|
// make sure numbers aren't wrapped
|
2017-12-29 09:46:39 +01:00
|
|
|
if (item.text && item.length && item.length < 20 && item.text.match && item.text.match(/\d[.,]\d\d($| [A-Z]{3}$)/)) {
|
2017-08-23 20:53:45 +02:00
|
|
|
item.style.push('noWrap');
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|
2017-08-23 20:53:45 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NINJA.parseMarkdownText = function(val, groupText)
|
|
|
|
{
|
|
|
|
var rules = [
|
|
|
|
['\\\*\\\*(\\\w.+?)\\\*\\\*', {'bold': true}], // **value**
|
|
|
|
['\\\*(\\\w.+?)\\\*', {'italics': true}], // *value*
|
|
|
|
['^###(.*)', {'style': 'help'}], // ### Small/gray help
|
|
|
|
['^##(.*)', {'style': 'subheader'}], // ## Header
|
2017-10-17 17:35:18 +02:00
|
|
|
['^#(.*)', {'style': 'fullheader'}] // # Subheader
|
2016-07-21 14:35:23 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
var parts = typeof val === 'string' ? [val] : val;
|
|
|
|
for (var i=0; i<rules.length; i++) {
|
|
|
|
var rule = rules[i];
|
|
|
|
var formatter = function(data) {
|
|
|
|
return $.extend(data, rule[1]);
|
|
|
|
}
|
|
|
|
parts = NINJA.parseRegExp(parts, rule[0], formatter, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts.length > 1 ? parts : val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
NINJA.parseMarkdownStack = function(val)
|
|
|
|
{
|
|
|
|
if (val.length == 1) {
|
|
|
|
var item = val[0];
|
|
|
|
var line = item.hasOwnProperty('text') ? item.text : item;
|
|
|
|
|
|
|
|
if (typeof line === 'string') {
|
|
|
|
line = [line];
|
|
|
|
}
|
|
|
|
|
|
|
|
var regExp = '^\\\* (.*[\r\n|\n|\r]?)';
|
|
|
|
var formatter = function(data) {
|
|
|
|
return {"ul": [data.text]};
|
|
|
|
}
|
|
|
|
|
|
|
|
val = NINJA.parseRegExp(line, regExp, formatter, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
NINJA.parseRegExp = function(val, regExpStr, formatter, groupText)
|
|
|
|
{
|
|
|
|
var regExp = new RegExp(regExpStr, 'gm');
|
|
|
|
var parts = [];
|
|
|
|
|
|
|
|
for (var i=0; i<val.length; i++) {
|
|
|
|
var line = val[i];
|
|
|
|
parts = parts.concat(NINJA.parseRegExpLine(line, regExp, formatter, groupText));
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts.length > 1 ? parts : val;
|
|
|
|
}
|
|
|
|
|
|
|
|
NINJA.parseRegExpLine = function(line, regExp, formatter, groupText)
|
|
|
|
{
|
|
|
|
var parts = [];
|
2017-06-29 14:15:54 +02:00
|
|
|
var lastIndex = -1;
|
2016-07-21 14:35:23 +02:00
|
|
|
|
|
|
|
while (match = regExp.exec(line)) {
|
|
|
|
if (match.index > lastIndex) {
|
|
|
|
parts.push(line.substring(lastIndex, match.index));
|
|
|
|
}
|
|
|
|
var data = {};
|
|
|
|
data.text = match[1];
|
|
|
|
data = formatter(data);
|
|
|
|
parts.push(data);
|
|
|
|
lastIndex = match.index + match[0].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parts.length) {
|
|
|
|
if (lastIndex < line.length) {
|
|
|
|
parts.push(line.substring(lastIndex));
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
2018-01-17 11:19:46 +01:00
|
|
|
|
|
|
|
NINJA.getCustomLabel = function(value) {
|
|
|
|
if (value && value.indexOf('|') > 0) {
|
|
|
|
return value.split('|')[0];
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|