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) { // check if we need to add a second table for tasks var itemsTable = false; if (invoice.hasSecondTable) { var json = JSON.parse(javascript); for (var i=0; i 0 && field == 'balance_due') { field = 'partial_due'; } else if (invoice.is_quote) { if (field == 'due_date') { field = 'valid_until'; } else { field = field.replace('invoice', 'quote'); } } 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'; } } else if (invoice.balance_amount < 0) { 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'; } } 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) + ''; value = value.replace(/\n/g, "\\n").replace(/\r/g, "\\r"); 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.statementColumns = function(invoice) { return ["22%", "22%", "22%", "17%", "17%"]; } NINJA.statementLines = function(invoice) { var grid = [[]]; grid[0].push({text: invoiceLabels.invoice_number, style: ['tableHeader', 'itemTableHeader']}); 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 = []; var rowStyle = (i % 2 == 0) ? 'odd' : 'even'; grid.push([ {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]}, ]); } return NINJA.prepareDataTable(grid, 'invoiceItems'); } NINJA.invoiceColumns = function(invoice, isTasks) { var account = invoice.account; var columns = []; var fields = NINJA.productFields(invoice, isTasks); var hasDescription = fields.indexOf('product.description') >= 0; for (var i=0; i= 0; for (var i=0; i= 0 ? (account.custom_invoice_label1 || invoiceLabels.surcharge) : invoiceLabels.discount; var customValue2 = NINJA.parseFloat(invoice.custom_value2); var customValue2Label = customValue2 >= 0 ? (account.custom_invoice_label2 || invoiceLabels.surcharge) : invoiceLabels.discount; if (customValue1 && invoice.custom_taxes1 == '1') { data.push([{text: customValue1Label, style: ['subtotalsLabel', 'customTax1Label']}, {text: formatMoneyInvoice(invoice.custom_value1, invoice), style: ['subtotals', 'customTax1']}]); } if (customValue2 && invoice.custom_taxes2 == '1') { data.push([{text: customValue2Label, style: ['subtotalsLabel', 'customTax2Label']}, {text: formatMoneyInvoice(invoice.custom_value2, invoice), style: ['subtotals', 'customTax2']}]); } 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']}]); } } if (parseFloat(invoice.tax_rate1 || 0) != 0 || invoice.tax_name1) { 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']}]); } if (parseFloat(invoice.tax_rate2 || 0) != 0 || invoice.tax_name2) { 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']}]); } if (customValue1 && invoice.custom_taxes1 != '1') { data.push([{text: customValue1Label, style: ['subtotalsLabel', 'custom1Label']}, {text: formatMoneyInvoice(invoice.custom_value1, invoice), style: ['subtotals', 'custom1']}]); } if (customValue2 && invoice.custom_taxes2 != '1') { data.push([{text: customValue2Label, style: ['subtotalsLabel', 'custom2Label']}, {text: formatMoneyInvoice(invoice.custom_value2, invoice), style: ['subtotals', 'custom2']}]); } var paid = invoice.amount - invoice.balance; if (!invoice.is_quote && invoice.balance_amount >= 0 && (invoice.account.hide_paid_to_date != '1' || paid)) { 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([ { 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'] } ]); } if (!hideBalance) { if (isPartial) { data.push([ { text: invoiceLabels.partial_due, style: ['subtotalsLabel', 'subtotalsBalanceDueLabel'] }, { text: formatMoneyInvoice(invoice.balance_amount, invoice), style: ['subtotals', 'subtotalsBalanceDue'] } ]); } } return NINJA.prepareDataPairs(data, 'subtotals'); } NINJA.subtotalsBalance = function(invoice) { var isPartial = NINJA.parseFloat(invoice.partial); return [[ {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']} ]]; } NINJA.accountDetails = function(invoice) { var account = invoice.account; if (invoice.features.invoice_settings && account.invoice_fields) { var fields = JSON.parse(account.invoice_fields).account_fields1; } else { var fields = [ 'account.company_name', 'account.id_number', 'account.vat_number', 'account.website', 'account.email', 'account.phone', ]; } var data = []; for (var i=0; i < fields.length; i++) { var field = fields[i]; var value = NINJA.renderField(invoice, field); if (value) { data.push(value); } } return NINJA.prepareDataList(data, 'accountDetails'); } NINJA.accountAddress = function(invoice) { var account = invoice.account; if (invoice.features.invoice_settings && account.invoice_fields) { var fields = JSON.parse(account.invoice_fields).account_fields2; } else { var fields = [ 'account.address1', 'account.address2', 'account.city_state_postal', 'account.country', 'account.custom_value1', 'account.custom_value2', ] } var data = []; for (var i=0; i < fields.length; i++) { var field = fields[i]; var value = NINJA.renderField(invoice, field); if (value) { data.push(value); } } return NINJA.prepareDataList(data, 'accountAddress'); } NINJA.invoiceDetails = function(invoice) { var account = invoice.account; if (invoice.features.invoice_settings && account.invoice_fields) { var fields = JSON.parse(account.invoice_fields).invoice_fields; } 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 = []; for (var i=0; i < fields.length; i++) { var field = fields[i]; var value = NINJA.renderField(invoice, field, true); if (value) { data.push(value); } } return NINJA.prepareDataPairs(data, 'invoiceDetails'); } NINJA.renderField = function(invoice, field, twoColumn) { var client = invoice.client; if (!client) { return false; } var account = invoice.account; var contact = client.contacts[0]; var clientName = client.name || (contact.first_name || contact.last_name ? (contact.first_name + ' ' + contact.last_name) : contact.email); var label = false; var value = false; if (field == 'client.client_name') { value = clientName || ' '; } else if (field == 'client.contact_name') { value = (contact.first_name || contact.last_name) ? contact.first_name + ' ' + contact.last_name : false; } else if (field == 'client.id_number') { value = client.id_number; if (invoiceLabels.id_number_orig) { label = invoiceLabels.id_number; } } else if (field == 'client.vat_number') { value = client.vat_number; if (invoiceLabels.vat_number_orig) { label = invoiceLabels.vat_number; } } else if (field == 'client.address1') { value = client.address1; } else if (field == 'client.address2') { value = client.address2; } else if (field == 'client.city_state_postal') { var cityStatePostal = ''; 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); } value = cityStatePostal; } else if (field == 'client.postal_city_state') { var postalCityState = ''; if (client.city || client.state || client.postal_code) { postalCityState = formatAddress(client.city, client.state, client.postal_code, true); } value = postalCityState; } else if (field == 'client.country') { value = client.country ? client.country.name : ''; } else if (field == 'client.email') { value = contact.email == clientName ? '' : contact.email; } else if (field == 'client.phone') { value = contact.phone; } else if (field == 'client.custom_value1') { if (account.custom_client_label1 && client.custom_value1) { label = account.custom_client_label1; value = client.custom_value1; } } else if (field == 'client.custom_value2') { if (account.custom_client_label2 && client.custom_value2) { label = account.custom_client_label2; value = client.custom_value2; } } else if (field == 'contact.custom_value1') { if (account.custom_contact_label1 && contact.custom_value1) { label = account.custom_contact_label1; value = contact.custom_value1; } } else if (field == 'contact.custom_value2') { if (account.custom_contact_label2 && contact.custom_value2) { label = account.custom_contact_label2; value = contact.custom_value2; } } else if (field == 'account.company_name') { value = account.name; } else if (field == 'account.id_number') { value = account.id_number; if (invoiceLabels.id_number_orig) { label = invoiceLabels.id_number; } } else if (field == 'account.vat_number') { value = account.vat_number; if (invoiceLabels.vat_number_orig) { label = invoiceLabels.vat_number; } } else if (field == 'account.website') { value = account.website; } else if (field == 'account.email') { value = account.work_email; } else if (field == 'account.phone') { value = account.work_phone; } else if (field == 'account.address1') { value = account.address1; } else if (field == 'account.address2') { value = account.address2; } 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); } value = cityStatePostal; } 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); } value = postalCityState; } else if (field == 'account.country') { value = account.country ? account.country.name : false; } else if (field == 'account.custom_value1') { if (invoice.account.custom_label1 && invoice.account.custom_value1) { label = invoice.account.custom_label1; value = invoice.account.custom_value1; } } else if (field == 'account.custom_value2') { if (invoice.account.custom_label2 && invoice.account.custom_value2) { label = invoice.account.custom_label2; 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; if (invoice.is_recurring) { value = false; } else if (invoice.partial_due_date) { value = invoice.partial_due_date; } else { value = invoice.due_date; } } else if (field == 'invoice.custom_text_value1') { if (invoice.custom_text_value1 && account.custom_invoice_text_label1) { label = invoice.account.custom_invoice_text_label1; value = invoice.is_recurring ? processVariables(invoice.custom_text_value1) : invoice.custom_text_value1; } } else if (field == 'invoice.custom_text_value2') { if (invoice.custom_text_value2 && account.custom_invoice_text_label2) { label = invoice.account.custom_invoice_text_label2; 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); } else if (field == 'invoice.partial_due') { 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); } } else if (field == '.blank') { value = ' '; } 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; } } NINJA.clientDetails = function(invoice) { var account = invoice.account; if (invoice.features.invoice_settings && account.invoice_fields) { var fields = JSON.parse(account.invoice_fields).client_fields; } 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', 'contact.custom_value1', 'contact.custom_value2', ]; } var data = []; for (var i=0; i < fields.length; i++) { var field = fields[i]; var value = NINJA.renderField(invoice, field); if (value) { data.push(value); } } 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 = []; if (! oldData.length) { oldData.push({text:' '}); } 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 = -1; 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; }