
if(localStorage.getItem('poitems')){loadItems();}
function loadItems() {
    if (localStorage.getItem('poitems')) {
        product_discount = 0;
        order_discount = 0;
        an = 0;
        count = 0;
        total = 0;
		var row_count=0;
		

        poitems = JSON.parse(localStorage.getItem('poitems'));
        $("#poTable tbody").empty();
        $.each(poitems, function() {
			
			
			
			row_count=row_count+1;
			//alert(JSON.stringify(poitems));
            var item = this;
            var data_item_id = item.id;
		   // var data_item_id = Math.floor((Math.random() * 10) + 1);;
            var product_id = item.row.product_id;
            var item_name = item.row.product_name;
            var product_code = item.row.product_code;
            var product_price = item.row.product_price;
            var product_cost  = item.row.product_cost;
			
			var product_sn  = item.row.product_sn;
			
			
			//console.log(poitems);
			
			var test='';
			//alert(product_cost);
			if(product_sn==undefined) {
				product_sn='';
			}else {
				//test='have val';
			}
			
			
			
			if(product_sn=='') {
				//product_sn='def sn:'+product_id;
			}
			
			//alert(product_sn);
			
			var product_sn_exissts  = item.row.product_sn_exissts;
			if(product_sn_exissts=='undefined') product_sn_exissts='';
			if(!product_sn_exissts) product_sn_exissts='';
			
			
			var product_w  = item.row.product_w;
			if(product_w=='undefined') product_w='';
			if(!product_w) product_w='';
			
			
			var item_st_product  = item.row.item_st_product;
			//alert(item_st_product);
			var good_sel='';
			var dmg_sel='';
			var def_sel='';
			var rvt_sel='';
			var aged_sel='';
			if(item_st_product!='undefined'){
				if(item_st_product=='Good'){
					good_sel='selected';
				}
				if(item_st_product=='DMG'){
					dmg_sel='selected';
				}
				if(item_st_product=='Defect'){
				   def_sel='selected';	
				}
				if(item_st_product=='RVT'){
					rvt_sel='selected';
				}
				if(item_st_product=='AGED'){
					aged_sel='selected';
				}
			}
			
			//alert(good_sel);
			//alert(dmg_sel);
			
            var item_qty = item.qty;
			item_ds='';
            var item_ds = item.row.item_discount;
			//alert(item_ds);
			if(item_ds=='undefined') item_ds='';
			if(!item_ds) item_ds='';
			
			
                item_discount = 0;


            var row_no =Math.floor(Math.random() * 999999) + 1 ;//Math.random();
			var row_no =new Date().valueOf();
			
            var ds = item_ds ? item_ds : '0';
            if (ds.indexOf("%") !== -1) {
                var pds = ds.split("%");
                if (!isNaN(pds[0])) {
                    item_discount = formatDecimal(parseFloat(((product_cost) * parseFloat(pds[0])) / 100));
                } else {
                    item_discount = formatDecimal(ds);
                }
            } else {
                item_discount = parseFloat(ds);
            }
			
			if(item_discount=='undefined') item_discount='';

            product_discount += parseFloat(item_discount * item_qty);
            unit_cost         = formatDecimal(product_cost - item_discount);
            product_cost     = formatDecimal(product_cost);
			
			//if(item_ds=='undefined') item_ds='';
		//	alert();
		var row_first='';
		var last_row=$('#last_row').val();
		
		if(data_item_id==last_row) {
			row_first='row_first';
			//alert(last_row);
		}else {
			//alert(1);
		}
		var test='last_row:'+last_row+' / data_item_id:'+data_item_id;
		test='';
		//row_first='row_first';
		
		
		//alert(product_sn);
		
		//alert(count(poitems));
		var row_no=data_item_id;

            var newTr = $('<tr class="'+row_first+'" id="row_'+row_no+'" data-item-id="'+data_item_id+'"></tr>');
            tr_html = '<td><span id="name_'+row_no+'" class="sname">'+test+item_name+' ('+product_code+')</span><input type="hidden" value="'+product_code+'" class="rcode" name="product[]"><input type="hidden" value="'+product_id+'" name="product_id[]"><input type="hidden" value="'+item_name+'" class="rname" name="product_name[]"><i id="'+row_no+'" data-item="'+data_item_id+'" class="pull-right fa fa-edit tip pointer edit" title="Edit" style="cursor:pointer;"></i></td>';
			
			tr_html += '<td class="text-right"><input style="text-align:right; width:70px;" text-align="right" data-item="' + data_item_id + '" id="w_'+row_no+'" type="text" value="'+(product_w)+'" name="product_w[]" class="ruw"><span id="sw_'+row_no+'" class="text-right sw">'+'</span></td>';
			 tr_html += '<td class="text-right"><input style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="sn_'+row_no+'" type="text" value="'+(product_sn)+'" name="product_sn[]" class="rusn"><span id="ssn_'+row_no+'" class="text-right ssn">'+'</span><input readonly style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="product_sn_exissts_'+row_no+'" type="hidden" value="'+(product_sn_exissts)+'" name="product_sn_exissts[]" class="rusnw"><input readonly style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="'+row_no+'" type="hidden" value="'+row_no+'" name="row_no[]" class="row_no"></td>';
			 
            tr_html += '<td class="text-right"><input style="text-align:right;" text-align="right" data-item="' + data_item_id + '" id="cost_'+row_no+'" type="text" value="'+parseFloat(product_cost)+'" name="unit_cost[]" class="rucost"><span id="scost_'+row_no+'" class="text-right scost">'+'</span></td>';
            tr_html += '<td><input style="width: 50px; margin-right: 5px; float: left;" class="form-control text-center rquantity" name="quantity[]" type="text" value="' + item_qty + '" data-id="' + row_no + '" data-item="' + data_item_id + '" id="quantity_' + row_no + '" onClick="this.select();"> ';
			
			//alert(product_sn_exissts);	
			if(product_sn_exissts!=0){
				//alert(1);	
				tr_html += '<i style="cursor: pointer; margin-top: 3px;" id="addIcon" class="fa fa-2x fa-plus-circle addIcon" onClick="pop_up_add_serial_no('+row_no+','+product_id+')" title="Add Serial No."></i>';
			}else {
			//alert(2);	
			}
			
			tr_html += '</td>';
			
            tr_html += '<td class="text-right"><input type="hidden" name="discount_cal[]" value="'+item_discount * item_qty+'" /><input class="form-control input-sm rdiscount" name="product_discount[]" type="text" id="discount_' + row_no + '" value="' + item_ds + '" data-item="' + data_item_id + '" id="discount_' + row_no + '" onClick="this.select();"></td>';
            tr_html += '<td class="text-right"><input name="subtotal[]" type="hidden" value="'+parseFloat(unit_cost*item_qty)+'"/><span id="subtotal_'+row_no+'" class="text-right ssubtotal">'+accounting.formatMoney((parseFloat(unit_cost)) *(parseFloat(item_qty)),"")+'</span></td>';
            tr_html += '<td class="text-center"><i class="fa fa-times tip pointer sldel" id="row_delete_'+row_no+'" title="Remove" style="cursor:pointer;"></i></td>';
            newTr.html(tr_html);
            newTr.prependTo("#poTable");
            total += parseFloat(unit_cost) * parseFloat(item_qty);
        });
        var tfoot = '<tr id="tfoot" class="tfoot active"><th colspan="4">Total</th><th class="text-center"></th>';
            tfoot += '<th class="text-right"><input type="hidden" name="order_cal_des" value=""/>' + '</th>';
            tfoot += '<th class="text-right"><input type="hidden" value="'+total+'" name="gross_total"/>'+ accounting.formatMoney(total,"") + '</th><th class="text-center"><i class="fa fa-trash-o" style="opacity:0.5; filter:alpha(opacity=50);"></i></th></tr>';
        $('#poTable tfoot').html(tfoot);
       // calculate_total_cost();

      // alert( (total,""));

        if (podiscount = localStorage.getItem('podiscount')) {
            var ds = podiscount;
            if (ds.indexOf("%") !== -1) {
                var pds = ds.split("%");
                if (!isNaN(pds[0])) {
                    order_discount = ((total) * parseFloat(pds[0])) / 100;
                } else {
                    order_discount = parseFloat(ds);
                }
            } else {
                order_discount = parseFloat(ds);
            }
        }


		

       //total_discount = parseFloat(order_discount);
       var gtotal = (total - order_discount);
        $('#total').text(accounting.formatMoney(total,""));
        $('input#total').val(total);
       $('#tds').text(accounting.formatMoney(order_discount,""));
       $('#gtotal').text(accounting.formatMoney(gtotal,""));
       $('input#hgtotal').val(gtotal);
	   
	   //
	   
	 //  set_serial_numbers();

    };

}

<!--start test-->
function set_serial_numbers(){
if (localStorage.getItem('poitems')) {

		 poitems = JSON.parse(localStorage.getItem('poitems'));

		 $.each(poitems, function() {
			 var item = this;
			var item_id = item.id;
			var fld="#sn_"+item_id;
			$(fld).val(item.row.product_sn_localstorage);	
			//alert(item.row.product_sn_localstorage);
			
		 })

}
}
<!--end test-->

var old_row_qty;
$(document).on("focus", '.rquantity', function() {
    old_row_qty = $(this).val();
}).on("change", '.rquantity', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_row_qty);
        bootbox.alert("unexpected value !");
        return;
    }

     var new_qty = parseFloat($(this).val());
     var item_id = $(this).attr('data-item');
   // alert(item_id);
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].qty = new_qty
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();

});

var old_row_w;
$(document).on("focus", '.ruw', function() {
    old_row_w = $(this).val();
}).on("change", '.ruw', function() {

    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_row_w);
        bootbox.alert("unexpected value !");
        return;
    }

     var new_product_w = parseFloat($(this).val());
	// alert(new_product_w);
     var item_id = $(this).attr('data-item');
   // alert(item_id);
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].row.product_w = new_product_w
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();

});


var old_rucost;
$(document).on("focus", '.rucost', function() {
    old_rucost = $(this).val();
}).on("change", '.rucost', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
        $(this).val(old_rucost);
        bootbox.alert("unexpected value !");
        return;
    }

     var new_rucost = parseFloat($(this).val());
     var item_id = $(this).attr('data-item');
	 
    
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].row.product_cost = new_rucost
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();

});

var old_rusn;
$(document).on("focus", '.rusn', function() {
    old_rusn = $(this).val();
}).on("change", '.rusn', function() {
	
	//alert();
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
       // $(this).val(old_rusn);
        //bootbox.alert("unexpected value !");
      //  return;
    }
     var new_rusn = ($(this).val());
	 //alert(new_rusn);
     var item_id = $(this).attr('data-item');
    var poitems = JSON.parse(localStorage.getItem('poitems'));
	
    poitems[item_id].row.product_sn = new_rusn;
	//alert(item_id);
    localStorage.setItem('poitems', JSON.stringify(poitems));
	
	//console.log(poitems);
	//alert();
    loadItems();
	console.log(poitems);
});

var old_rusnw;
$(document).on("focus", '.rusnw', function() {
    old_rusnw = $(this).val();
}).on("change", '.rusnw', function() {
    var row = $(this).closest('tr');
    if (!is_numeric($(this).val())) {
       // $(this).val(old_rusn);
        //bootbox.alert("unexpected value !");
      //  return;
    }
     var new_rusnw = ($(this).val());
	 //alert(new_rusn);
     var item_id = $(this).attr('data-item');
    var poitems = JSON.parse(localStorage.getItem('poitems'));
    poitems[item_id].row.product_sn_exissts = new_rusnw
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
});





    var old_podiscount;
    $('#podiscount').focus(function() {
        old_podiscount = $(this).val();
    }).change(function() {
        if (is_valid_discount($(this).val())) {
            localStorage.removeItem('podiscount');
            localStorage.setItem('podiscount', $(this).val());
            loadItems();
            return;
            
        }else{

            $(this).val(old_podiscount);
            bootbox.alert("unexpected value!");
        }
    });

    var old_rdiscount;
    $(document).on("focus", '.rdiscount', function() {
        old_rdiscount = $(this).val();
    }).on("change", '.rdiscount', function() {

        if (!is_valid_discount($(this).val())) {
            $(this).val(old_rdiscount);
            bootbox.alert("unexpected value!");
        }

         var new_item_discount = $(this).val();
         var item_id = $(this).attr('data-item');
        
        // alert(new_item_discount);

           var poitems = JSON.parse(localStorage.getItem('poitems'));
           poitems[item_id].row.item_discount = new_item_discount;
           //console.log(poitems[item_id].row.item_discount);
           //alert(new_item_discount);
           localStorage.setItem('poitems', JSON.stringify(poitems));
           loadItems();

    });
   
   
     var old_st_product;
    $(document).on("focus", '.st_product', function() {
        old_st_product = $(this).val();
    }).on("change", '.st_product', function() {

      

         var new_st_product = $(this).val();
         var item_id = $(this).attr('data-item');
        
        // alert(new_item_discount);

           var poitems = JSON.parse(localStorage.getItem('poitems'));
           poitems[item_id].row.item_st_product = new_st_product;
           //console.log(poitems[item_id].row.item_discount);
           //alert(new_item_discount);
           localStorage.setItem('poitems', JSON.stringify(poitems));
           loadItems();

    });
   
   
   
   




    $(document).on('click', '.sldel', function() {
        var row = $(this).closest('tr');
        var item_id = row.attr('data-item-id');

            poitems = JSON.parse(localStorage.getItem('poitems'));
            delete poitems[item_id];
            localStorage.setItem('poitems', JSON.stringify(poitems));
            row.remove();
            loadItems();
    });


//common function
function is_numeric(mixed_var){var whitespace=" \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";return(typeof mixed_var==='number'||(typeof mixed_var==='string'&&whitespace.indexOf(mixed_var.slice(-1))===-
1))&&mixed_var!==''&&!isNaN(mixed_var);}

function formatDecimal(x){
    return parseFloat(parseFloat(x).toFixed(2));
}

function is_valid_discount(mixed_var){return(is_numeric(mixed_var)||(/([0-9]%)/i.test(mixed_var)))?true:false;}

//end common function

var add_purchase_item = function (item) {
    var oldItems = JSON.parse(localStorage.getItem('poitems')) || [];
   
     if (count == 1) {
        if ($('#supplier').val()) {
            $('#supplier').select2("readonly", true);
        } else {
            bootbox.alert("Please select above first.");
            item = null;
            return;
        }
    }

    if (item == null) {
        return;
    }
	
	

    var item_id = item.id;
    if (poitems[item_id]) {
	 poitems[item_id].row.qty = parseFloat(poitems[item_id].row.qty) + 1;
    } else {
        poitems[item_id] = item;
    }
    localStorage.setItem('poitems', JSON.stringify(poitems));
    loadItems();
	
	//alert(item.id);
	var fld='row_'+item.id;
	//$(fld).css('background-color', 'Red');
    return true;
};

if (typeof(Storage) === "undefined") {
    $(window).bind('beforeunload', function(e) {
        if (count > 1) {
            var message = "You will loss data!";
            return message;
        }
    });
}
//*****************************

$('#poref').change(function(e) {
        localStorage.setItem('poref', $(this).val());
});

if (poref = localStorage.getItem('poref')) {
        $('#poref').val(poref);
}

//*****************
$('#powarehouse').change(function(e) {
    localStorage.setItem('powarehouse', $(this).val());
});

if (powarehouse = localStorage.getItem('powarehouse')) {
    $('#powarehouse').val(powarehouse);
}

//*****************
$('#postatus').change(function(e) {
    localStorage.setItem('postatus', $(this).val());
});

if (postatus = localStorage.getItem('postatus')) {
    $('#postatus').val(postatus);
}

//*****************
$('#ponote').change(function(e) {
    localStorage.setItem('ponote', $(this).val());
});

if (ponote = localStorage.getItem('ponote')) {
    $('#ponote').val(ponote);
}

//*************************

if (localStorage.getItem('poextras')) {
    $('#extras').iCheck('check');
    $('#extras-con').show();
}
$('#extras').on('ifChecked', function() {
    localStorage.setItem('poextras', 1);
    $('#extras-con').slideDown();
});
$('#extras').on('ifUnchecked', function() {
    localStorage.removeItem("poextras");
    $('#extras-con').slideUp();
});

//*****************
$('#tax_select').change(function(e) {
    localStorage.setItem('tax_select', $(this).val());
});

if (tax_select = localStorage.getItem('tax_select')) {
    $('#tax_select').val(tax_select);
}

//*****************
$('#podiscount').change(function(e) {
    localStorage.setItem('podiscount', $(this).val());
});

if (podiscount = localStorage.getItem('podiscount')) {
    $('#podiscount').val(podiscount);
}

//*****************
$('#poshipping').change(function(e) {
    localStorage.setItem('poshipping', $(this).val());
});

if (poshipping = localStorage.getItem('poshipping')) {
    $('#poshipping').val(poshipping);
}

//**********************
$('#supplier').change(function(e) {
    localStorage.setItem('supplier', $(this).val());
});

if (supplier = localStorage.getItem('supplier')) {
    $('#supplier').val(supplier);
}


//************** reset function *******

    $('#reset').click(function(e) {
        bootbox.confirm("Are you sure?", function(result) {
            if (result) {
                if (localStorage.getItem('poitems')) {
                    localStorage.removeItem('poitems');
                }
                if (localStorage.getItem('podiscount')) {
                    localStorage.removeItem('podiscount');
                }
                if (localStorage.getItem('tax_select')) {
                    localStorage.removeItem('tax_select');
                }
                if (localStorage.getItem('poref')) {
                    localStorage.removeItem('poref');
                }
                if (localStorage.getItem('powarehouse')) {
                    localStorage.removeItem('powarehouse');
                }
                if (localStorage.getItem('ponote')) {
                    localStorage.removeItem('ponote');
                }
                if (localStorage.getItem('posupplier')) {
                    localStorage.removeItem('posupplier');
                }
                if (localStorage.getItem('supplier')) {
                    localStorage.removeItem('supplier');
                }
                if (localStorage.getItem('poextras')) {
                    localStorage.removeItem('poextras');
                }
                if (localStorage.getItem('podate')) {
                    localStorage.removeItem('podate');
                }
                if (localStorage.getItem('postatus')) {
                    localStorage.removeItem('postatus');
                }
                if (localStorage.getItem('poshipping')) {
                    localStorage.removeItem('poshipping');
                }

               $('body').modalmanager('loading');
                location.reload();
            }
        });
    });


console.log(poitems);

//console.log(poitems);
//************** end reset function ***


//begin calculation function //

//end calculation function //