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) { 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) { return page === 1 || invoice.account.all_pages_header == '1' ? val : ''; } } else if (key === 'footer') { return function(page, pages) { return page === pages || invoice.account.all_pages_footer == '1' ? val : ''; } } } // 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; if (!invoice.features.remove_created_by && ! isEdge) { 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= 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); } 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 0 && field == 'balance_due') { field = 'partial_due'; } else if (invoice.is_quote) { field = field.replace('invoice', 'quote'); } 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 var regExp = new RegExp('"\\$[a-z][\\\w\\\.]*?[Value]?"', 'g'); var matches = javascript.match(regExp); if (matches) { for (var i=0; i= 0) { continue; } // legacy style had 'Value' at the end if (endsWith(match, 'Value"')) { field = match.substring(2, match.indexOf('Value')); } else { field = match.substring(2, match.length - 1); } field = toSnakeCase(field); var value = getDescendantProp(invoice, field) || ' '; value = doubleDollarSign(value); javascript = javascript.replace(match, '"'+value+'"'); } } return javascript; } 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'); } NINJA.invoiceColumns = function(invoice) { var account = invoice.account; var columns = []; if (invoice.has_product_key) { columns.push("15%"); } columns.push("*") if (invoice.features.invoice_settings && account.custom_invoice_item_label1) { columns.push("10%"); } if (invoice.features.invoice_settings && account.custom_invoice_item_label2) { columns.push("10%"); } var count = 3; if (account.hide_quantity == '1') { count -= 2; } if (account.show_item_taxes == '1') { count++; } for (var i=0; i= 0); var custom2InPattern = (pattern && pattern.indexOf('{$custom2}') >= 0); data = [ {text:clientName || ' ', style: ['clientName']}, {text:client.id_number}, {text:client.vat_number}, {text:client.address1}, {text:client.address2}, {text:cityStatePostal}, {text:client.country ? client.country.name : ''}, {text:clientEmail}, {text: client.custom_value1 && !custom1InPattern ? account.custom_client_label1 + ' ' + client.custom_value1 : false}, {text: client.custom_value2 && !custom2InPattern ? account.custom_client_label2 + ' ' + client.custom_value2 : false} ]; 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 = []; for (var i=0; i 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 1 ? parts : val; } NINJA.parseRegExpLine = function(line, regExp, formatter, groupText) { var parts = []; var lastIndex = 0; 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; }