1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/public/js/pdf.pdfmake.js

297 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-05-22 09:22:24 +02:00
var NINJA = NINJA || {};
2015-04-13 08:02:33 +02:00
function GetPdfMake(invoice, javascript, callback) {
2015-05-05 11:48:23 +02:00
var account = invoice.account;
2015-05-19 21:14:00 +02:00
var baseDD = {
2015-05-22 09:22:24 +02:00
pageMargins: [40, 40, 40, 40],
styles: {
bold: {
bold: true
},
cost: {
alignment: 'right'
},
quantity: {
alignment: 'right'
},
tax: {
alignment: 'right'
},
lineTotal: {
alignment: 'right'
},
right: {
alignment: 'right'
},
subtotals: {
alignment: 'right'
},
termsLabel: {
bold: true,
margin: [0, 10, 0, 4]
}
},
footer: function(){
2015-06-03 19:55:48 +02:00
f = [{ text:invoice.invoice_footer?processVariables(invoice.invoice_footer):"", margin: [40, 0]}]
2015-05-22 09:22:24 +02:00
if (!invoice.is_pro && logoImages.imageLogo1) {
f.push({
image: logoImages.imageLogo1,
width: 150,
margin: [40,0]
});
}
return f;
},
2015-05-19 21:14:00 +02:00
};
2015-05-22 09:22:24 +02:00
eval(javascript);
dd = $.extend(true, baseDD, dd);
2015-05-19 21:14:00 +02:00
/*
2015-06-03 19:55:48 +02:00
pdfMake.fonts = {
wqy: {
normal: 'wqy.ttf',
bold: 'wqy.ttf',
italics: 'wqy.ttf',
bolditalics: 'wqy.ttf'
}
};
*/
/*
pdfMake.fonts = {
NotoSansCJKsc: {
normal: 'NotoSansCJKsc-Regular.ttf',
bold: 'NotoSansCJKsc-Medium.ttf',
italics: 'NotoSansCJKsc-Italic.ttf',
bolditalics: 'NotoSansCJKsc-Italic.ttf'
},
2015-05-19 21:14:00 +02:00
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-Italic.ttf'
},
};
*/
2015-06-03 19:55:48 +02:00
2015-05-19 21:14:00 +02:00
doc = pdfMake.createPdf(dd);
doc.save = function(fileName) {
this.download(fileName);
};
return doc;
2015-04-13 08:02:33 +02:00
}
2015-05-22 09:22:24 +02:00
NINJA.notesAndTerms = function(invoice)
2015-04-13 22:45:09 +02:00
{
2015-05-05 11:48:23 +02:00
var text = [];
if (invoice.public_notes) {
2015-06-03 19:55:48 +02:00
text.push({text:processVariables(invoice.public_notes), style:'notes'});
2015-05-05 11:48:23 +02:00
}
2015-04-13 22:45:09 +02:00
2015-05-05 11:48:23 +02:00
if (invoice.terms) {
text.push({text:invoiceLabels.terms, style:'termsLabel'});
2015-06-03 19:55:48 +02:00
text.push({text:processVariables(invoice.terms), style:'terms'});
2015-05-05 11:48:23 +02:00
}
2015-04-13 22:45:09 +02:00
2015-05-05 11:48:23 +02:00
return text;
2015-04-13 22:45:09 +02:00
}
2015-04-13 14:37:42 +02:00
2015-05-22 09:22:24 +02:00
NINJA.invoiceLines = function(invoice) {
2015-05-19 21:14:00 +02:00
var grid = [
[
{text: invoiceLabels.item, style: 'tableHeader'},
{text: invoiceLabels.description, style: 'tableHeader'},
{text: invoiceLabels.unit_cost, style: 'tableHeader'},
{text: invoiceLabels.quantity, style: 'tableHeader'},
{text: invoice.has_taxes?invoiceLabels.tax:'', style: 'tableHeader'},
{text: invoiceLabels.line_total, style: 'tableHeader'}
]
];
2015-05-05 11:48:23 +02:00
var total = 0;
var shownItem = false;
var currencyId = invoice && invoice.client ? invoice.client.currency_id : 1;
var hideQuantity = invoice.account.hide_quantity == '1';
for (var i = 0; i < invoice.invoice_items.length; i++) {
var row = [];
var item = invoice.invoice_items[i];
var cost = formatMoney(item.cost, currencyId, true);
var qty = NINJA.parseFloat(item.qty) ? roundToTwo(NINJA.parseFloat(item.qty)) + '' : '';
var notes = item.notes;
var productKey = item.product_key;
var tax = "";
if (item.tax && parseFloat(item.tax.rate)) {
tax = parseFloat(item.tax.rate);
} else if (item.tax_rate && parseFloat(item.tax_rate)) {
tax = parseFloat(item.tax_rate);
}
2015-05-22 09:22:24 +02:00
// show at most one blank line
if (shownItem && (!cost || cost == '0.00') && !notes && !productKey) {
continue;
}
shownItem = true;
2015-05-05 11:48:23 +02:00
2015-05-22 09:22:24 +02:00
// process date variables
if (invoice.is_recurring) {
notes = processVariables(notes);
productKey = processVariables(productKey);
}
2015-05-05 11:48:23 +02:00
2015-05-22 09:22:24 +02:00
var lineTotal = roundToTwo(NINJA.parseFloat(item.cost)) * roundToTwo(NINJA.parseFloat(item.qty));
if (tax) {
lineTotal += lineTotal * tax / 100;
}
if (lineTotal) {
total += lineTotal;
}
lineTotal = formatMoney(lineTotal, currencyId);
2015-05-05 11:48:23 +02:00
2015-05-22 09:22:24 +02:00
rowStyle = i%2===0?'odd':'even';
2015-05-05 11:48:23 +02:00
2015-05-22 09:22:24 +02:00
row[0] = {style:["productKey", rowStyle], text:productKey};
row[1] = {style:["notes", rowStyle], text:notes};
row[2] = {style:["cost", rowStyle], text:cost};
row[3] = {style:["quantity", rowStyle], text:qty};
row[4] = {style:["tax", rowStyle], text:""+tax};
row[5] = {style:["lineTotal", rowStyle], text:lineTotal};
2015-05-05 11:48:23 +02:00
2015-05-22 09:22:24 +02:00
grid.push(row);
}
return grid;
2015-05-05 11:48:23 +02:00
}
2015-05-22 09:22:24 +02:00
NINJA.subtotals = function(invoice)
2015-05-05 11:48:23 +02:00
{
if (!invoice) {
return;
2015-04-13 14:37:42 +02:00
}
2015-05-05 11:48:23 +02:00
var data = [
2015-05-19 21:14:00 +02:00
[invoiceLabels.subtotal, formatMoney(invoice.subtotal_amount, invoice.client.currency_id)],
2015-05-05 11:48:23 +02:00
];
2015-05-19 21:14:00 +02:00
2015-05-05 11:48:23 +02:00
if(invoice.discount_amount != 0) {
data.push([invoiceLabels.discount, formatMoney(invoice.discount_amount, invoice.client.currency_id)]);
2015-04-13 14:37:42 +02:00
}
2015-05-05 11:48:23 +02:00
if (NINJA.parseFloat(invoice.custom_value1) && invoice.custom_taxes1 == '1') {
data.push([invoiceLabels.custom_invoice_label1, formatMoney(invoice.custom_value1, invoice.client.currency_id)]);
}
if (NINJA.parseFloat(invoice.custom_value2) && invoice.custom_taxes2 == '1') {
data.push([invoiceLabels.custom_invoice_label2, formatMoney(invoice.custom_value2, invoice.client.currency_id)]);
}
if(invoice.tax && invoice.tax.name || invoice.tax_name) {
data.push([invoiceLabels.tax, formatMoney(invoice.tax_amount, invoice.client.currency_id)]);
2015-04-13 14:37:42 +02:00
}
2015-05-05 11:48:23 +02:00
if (NINJA.parseFloat(invoice.custom_value1) && invoice.custom_taxes1 != '1') {
data.push([invoiceLabels.custom_invoice_label1, formatMoney(invoice.custom_value1, invoice.client.currency_id)]);
2015-04-13 14:37:42 +02:00
}
2015-05-05 11:48:23 +02:00
if (NINJA.parseFloat(invoice.custom_value2) && invoice.custom_taxes2 != '1') {
data.push([invoiceLabels.custom_invoice_label2, formatMoney(invoice.custom_value2, invoice.client.currency_id)]);
2015-04-13 14:37:42 +02:00
}
2015-05-05 11:48:23 +02:00
var paid = invoice.amount - invoice.balance;
if (invoice.account.hide_paid_to_date != '1' || paid) {
data.push([invoiceLabels.paid_to_date, formatMoney(paid, invoice.client.currency_id)]);
}
2015-04-14 12:23:35 +02:00
2015-05-05 11:48:23 +02:00
data.push([{text:invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due, style:'balanceDueLabel'},
{text:formatMoney(invoice.balance_amount, invoice.client.currency_id), style:'balanceDueValue'}]);
return data;
}
2015-05-22 09:22:24 +02:00
NINJA.accountDetails = function(account) {
2015-05-05 11:48:23 +02:00
var data = [];
if(account.name) data.push({text:account.name, style:'accountName'});
if(account.id_number) data.push({text:account.id_number, style:'accountDetails'});
if(account.vat_number) data.push({text:account.vat_number, style:'accountDetails'});
if(account.work_email) data.push({text:account.work_email, style:'accountDetails'});
if(account.work_phone) data.push({text:account.work_phone, style:'accountDetails'});
return data;
2015-04-14 12:23:35 +02:00
}
2015-05-22 09:22:24 +02:00
NINJA.accountAddress = function(account) {
2015-05-05 11:48:23 +02:00
var address = '';
if (account.city || account.state || account.postal_code) {
address = ((account.city ? account.city + ', ' : '') + account.state + ' ' + account.postal_code).trim();
}
var data = [];
if(account.address1) data.push({text:account.address1, style:'accountDetails'});
if(account.address2) data.push({text:account.address2, style:'accountDetails'});
if(address) data.push({text:address, style:'accountDetails'});
if(account.country) data.push({text:account.country.name, style: 'accountDetails'});
2015-06-03 19:55:48 +02:00
if(account.custom_label1 && account.custom_value1) data.push({text:account.custom_label1 +' '+ account.custom_value1, style: 'accountDetails'});
if(account.custom_label2 && account.custom_value2) data.push({text:account.custom_label2 +' '+ account.custom_value2, style: 'accountDetails'});
2015-05-05 11:48:23 +02:00
return data;
}
2015-05-22 09:22:24 +02:00
NINJA.invoiceDetails = function(invoice) {
2015-05-05 11:48:23 +02:00
var data = [
[
invoice.is_quote ? invoiceLabels.quote_number : invoiceLabels.invoice_number,
{style: 'bold', text: invoice.invoice_number},
],
[
invoice.is_quote ? invoiceLabels.quote_date : invoiceLabels.invoice_date,
invoice.invoice_date,
],
[
invoice.is_quote ? invoiceLabels.total : invoiceLabels.balance_due,
formatMoney(invoice.balance_amount, invoice.client.currency_id),
],
];
return data;
2015-04-14 12:23:35 +02:00
}
2015-05-22 09:22:24 +02:00
NINJA.clientDetails = function(invoice) {
2015-05-05 11:48:23 +02:00
var client = invoice.client;
if (!client) {
return;
}
2015-05-19 21:14:00 +02:00
2015-05-05 11:48:23 +02:00
var fields = [
getClientDisplayName(client),
client.id_number,
client.vat_number,
concatStrings(client.address1, client.address2),
concatStrings(client.city, client.state, client.postal_code),
client.country ? client.country.name : false,
invoice.contact && getClientDisplayName(client) != invoice.contact.email ? invoice.contact.email : false,
invoice.client.custom_value1 ? invoice.account['custom_client_label1'] + ' ' + invoice.client.custom_value1 : false,
invoice.client.custom_value2 ? invoice.account['custom_client_label2'] + ' ' + invoice.client.custom_value2 : false,
];
var data = [];
for (var i=0; i<fields.length; i++) {
var field = fields[i];
if (!field) {
continue;
}
2015-05-22 09:22:24 +02:00
data.push([field]);
}
if (!data.length) {
data.push(['']);
}
2015-05-05 11:48:23 +02:00
return data;
}
2015-05-22 09:22:24 +02:00
NINJA.getPrimaryColor = function(defaultColor) {
return NINJA.primaryColor ? NINJA.primaryColor : defaultColor;
}
NINJA.getSecondaryColor = function(defaultColor) {
return NINJA.primaryColor ? NINJA.secondaryColor : defaultColor;
2015-04-14 12:23:35 +02:00
}
2015-05-22 09:22:24 +02:00
NINJA.getEntityLabel = function(invoice) {
return invoice.is_quote ? invoiceLabels.quote : invoiceLabels.invoice;
2015-04-14 12:23:35 +02:00
}