invoice-fs
Version:
Nostalgic command-line invoicing application producing plain text invoices and JSON data structures. Uses the file system as a database
106 lines (93 loc) • 3.79 kB
JavaScript
var out = require('../lib/out');
function InvoicePrinter(data) {
this.data = data;
this.width = 60;
this.output = '';
}
InvoicePrinter.prototype.write = function(text, newLine) {
if (text === undefined) { text = ""; }
if (newLine === undefined) { newLine = true; }
this.output += text;
this.output += newLine ? "\n" : "";
};
InvoicePrinter.prototype.issuer = function() {
this.write(out.centeredColumn(this.data.issuer.name, this.width));
this.write(out.centeredColumn("ABN " + this.data.issuer.abn, this.width));
this.write(out.centeredColumn("Telephone " + this.data.issuer.telephone, this.width));
this.write(out.centeredColumn(this.data.issuer.address, this.width));
this.write(out.centeredColumn("Email " + this.data.issuer.email, this.width));
this.write(out.centeredColumn("Web " + this.data.issuer.web, this.width));
this.write();
var prefix = "";
if (this.data.status === "UNLOCKED") {
prefix = "** DRAFT ** ";
} else if (this.data.status === "PAID") {
prefix = "** PAID ** ";
}
this.write(out.centeredColumn(prefix + "TAX INVOICE #" +
this.data.number, this.width));
};
InvoicePrinter.prototype.customer = function() {
this.write("To:");
this.write("[" + this.data.customer.code + "]");
this.write(this.data.customer.companyName);
this.write(this.data.customer.address1);
this.write(this.data.customer.address2);
};
InvoicePrinter.prototype.invoice = function() {
this.write(out.leftColumn("Date:", 20) + this.data.invoiceDate.toDateString());
this.write(out.leftColumn("Order Number:", 20) + this.data.orderNumber);
this.write(out.leftColumn("A.B.N.:", 20) + this.data.customer.abn);
this.write(out.leftColumn("Payment Terms:", 20) + this.data.daysUntilDue + " Days");
this.write(out.leftColumn("Due Date:", 20) + this.data.dueDate.toDateString());
this.write(out.leftColumn("Payment Method:", 20) + "A/C " + this.data.issuer.account);
this.write(out.leftColumn("", 20) + "BSB " + this.data.issuer.bsb + " (REF " + this.data.number + ")");
};
InvoicePrinter.prototype.items = function() {
for (item in this.data.items) {
this.write(this.data.items[item].description);
this.write(
out.rightColumn(this.data.items[item].units.toString(), 4) +
out.leftColumn(" @", 4) +
out.leftColumn(this.data.currencySymbol + this.data.items[item].unitPrice.toFixed(2), 24) +
out.rightColumn(this.data.currencySymbol + this.data.items[item].amount.toFixed(2), 28) +
(this.data.items[item].taxable ? '*' : '')
);
}
};
InvoicePrinter.prototype.headerSection = function() {
this.write("\n\n", false);
this.write(this.data.issuer.asciiArt);
this.issuer();
this.write(out.divider("-", this.width));
};
InvoicePrinter.prototype.detailsSection = function() {
this.customer();
this.write();
this.invoice();
this.write();
this.write(out.divider("-", this.width));
};
InvoicePrinter.prototype.itemsSection = function() {
this.write();
this.items();
this.write(out.divider("-", this.width));
};
InvoicePrinter.prototype.footerSection = function() {
this.write(
out.leftColumn('TOTAL AMOUNT:', 20) +
out.rightColumn(this.data.currencySymbol + this.data.total.toFixed(2), 40)
);
this.write("\n\n", false);
this.write("Total includes GST of: " + this.data.currencySymbol + this.data.tax.toFixed(2));
this.write(" * indicates taxable supply");
this.write();
};
InvoicePrinter.prototype.print = function() {
this.headerSection();
this.detailsSection();
this.itemsSection();
this.footerSection();
return this.output;
};
module.exports = InvoicePrinter;