function InvoiceEditor() {
var private = {};
var public = {};
private.element = null;
private.json = [];
private.vatrate = .2;
private.validationtimeout = null;
private.guids = [];
private.deletions = [];
public.init = function(element, json){
private.element = element;
if(typeof(json) == 'object' && json !== null){
private.json = json;
private.addGUIDs();
}
public.jsonToTable();
$('#addinvoiceitem').on('click' , private.addItem);
$(private.element).on('input' , private.setInputTimeout);
$(private.element).on('click' , '.removeitem-button' , private.removeItem);
//$(private.element).on('blur' , private.checkInput);
}
public.setVatRate = function(vatrate){
private.vatrate = vatrate;
}
public.jsonToTable = function(){
var content = [
'
',
' ' ,
' ' ,
' Qty | ' ,
' Description | ' ,
' Unit Price | ' ,
' Line Total | ' ,
' VAT | ' ,
' Total | ' ,
' | ' ,
'
' ,
' ' ,
' '
].join("\n");
var total = 0
var total_tax = 0
private.json.forEach(function(item, index){
total += parseFloat(item.line_total);
total_tax += parseFloat(item.line_total_tax);
content += [
' ' ,
' ' + item.qty + ' | ' ,
' ' + item.description + ' | ' ,
' ' + private.getCurrency(item.unit_price) + ' | ' ,
' ' + private.getCurrency(item.line_total) + ' | ' ,
' ' + private.getCurrency(item.line_total_tax) + ' | ' ,
' ' + private.getCurrency(parseFloat(item.line_total) + parseFloat(item.line_total_tax)) + ' | ' ,
' | ' ,
'
'
].join("\n");
});
content += [
' ' ,
' | ' ,
' TOTAL | ' ,
' | ' ,
' ' + private.getCurrency(total) + ' | ' ,
' ' + private.getCurrency(total_tax) + ' | ' ,
' ' + private.getCurrency(parseFloat(total) + parseFloat(total_tax)) + ' | ' ,
' | ' ,
'
'
].join("\n");
content += '
';
$(private.element).html(content);
}
public.getData = function(){
private.checkInput();
return private.json
}
public.getDeletions = function(){
return private.deletions
}
private.getCurrency = function(number, decPlaces, decSep, thouSep){
decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
decSep = typeof decSep === "undefined" ? "." : decSep;
thouSep = typeof thouSep === "undefined" ? "," : thouSep;
var sign = number < 0 ? "-" : "";
var i = String(parseInt(number = Math.abs(Number(number) || 0).toFixed(decPlaces)));
var j = (j = i.length) > 3 ? j % 3 : 0;
return '£' + sign +
(j ? i.substr(0, j) + thouSep : "") +
i.substr(j).replace(/(\decSep{3})(?=\decSep)/g, "$1" + thouSep) +
(decPlaces ? decSep + Math.abs(number - i).toFixed(decPlaces).slice(2) : "");
}
private.addItem = function(){
private.json.push({
'invoiceitem_id': '' ,
'rowid': private.getNewId() ,
'description': '' ,
'taxcode_id': 2 ,
'qty': 1 ,
'unit_price': 0 ,
'line_total': 0 ,
'line_total_tax': 0
});
public.jsonToTable();
}
private.removeItem = function(e){
private.checkInput();
InvModal.confirm('Remove Item?' , function(){
var itemid = $(e.target).closest('tr').data('rowid');
var invoiceitem_id = $(e.target).closest('tr').data('itemid');
if(invoiceitem_id){
private.deletions.push(invoiceitem_id);
}
var newjson = private.json;
private.json = []
newjson.forEach(function(item){
if(item.rowid != itemid){
private.json.push(item);
}
});
public.jsonToTable();
InvModal.hideModal();
});
}
private.setInputTimeout = function(e){
window.clearTimeout(private.validationtimeout);
var waittime = 1000;
if($(e.target).hasClass('description')){
waittime = 30000;
}
private.validationtimeout = window.setTimeout(function(){
private.checkInput();
} , waittime);
}
private.checkInput = function(e){
private.json = [];
$('.itemrow').each(function(index , item){
var qty = $(item).find('.qty').text().replace(/[^0-9\.\-]/g, "");
var unit_price = $(item).find('.unit_price').text().replace(/[^0-9\.\-]/g, "");
var line_total = unit_price * qty;
var line_total_tax = 0;
if($(item).data('taxcodeid') == 2){
var line_total_tax = line_total * private.vatrate;
}
private.json.push({
'invoiceitem_id': $(item).data('itemid') ,
'rowid': $(item).data('rowid') ,
'taxcode_id': $(item).data('taxcodeid') ,
'description': $(item).find('.description').text() ,
'qty': qty ,
'unit_price': unit_price ,
'line_total': line_total ,
'line_total_tax': line_total_tax
});
})
public.jsonToTable();
}
private.addGUIDs = function(){
private.json.forEach(function(item){
item.rowid = private.getNewId();
});
}
private.getNewId = function(){
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
if(private.guids.indexOf(guid) == -1){
private.guids.push(guid);
return guid;
} else {
return private.getNewId();
}
}
return public;
}