<?php
$edit_id = $link->hrefquery();


$get_inventory = $db->get_row('inventory',array('id'=>$edit_id['1']));


if (isset ( $_POST ['inventory_submit'] ) && $_POST ['check'] == 'inventory') {
	$item_name = $_POST['item_name'];
	$item_code = $_POST['item_code'];
	$date_purchase = $_POST['purchase_date'];
	$location = $_POST['location'];
	$item_amount = $_POST['item_amount'];
	$installment = $_POST['installment'];
	$payment_date = $_POST['payment_date'];
	$payment_amount=$_POST['payment_amount'];
	if($installment=='2'){
		$payment_amount=$item_amount;
		$payment_date=$date_purchase;
	}
	else{
		$payment_amount= serialize(array($_POST['payment_amount']));
		$payment_date = serialize(array($_POST['payment_date']));
	}
	
	if(empty($item_name) && $item_name==""){
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Item Name Empty.<br></div>';
	}
	elseif (empty($item_code) && $item_code==""){
		
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Item Code Empty.<br></div>';
	}
	elseif (empty($date_purchase) && $date_purchase==""){
	
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Item Date Purchase Empty.<br></div>';
	}
	
	elseif($location=='0'){
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
							  <i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
										Please Select Location.<br></div>';
	}
	elseif (empty($item_amount) && $item_amount==""){
	
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Item Amount Empty.<br></div>';
	}
	elseif($installment=='0'){
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		Please Select Installment.
		<br>
		</div>';
	}
	elseif (empty($payment_date) && $payment_date==""){
	
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Payment Date Empty.<br></div>';
	}
	elseif (empty($payment_amount) && $payment_amount==""){
	
		$display_inventory = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">
		<i class="icon-remove"></i></button><strong><i class="icon-remove"></i>Oh snap!</strong>
		You Left Payment Amount Empty.<br></div>';
	}
	else{
			
		$inventory_array = array(
							'item_name'=>$item_name,
							'item_code'=>$item_code,
							'purchase_date'=>$date_purchase,
							'location'=>$location,
							'item_amount'=>$item_amount,
							'installment'=>$installment,
							'payment_date'=>$payment_date,
							'payment_amount'=>$payment_amount,
							'created_date'=>time()
							);
			if($edit_id['0']=='edit_id'){
			$update_inventory = $db->update('inventory',$inventory_array,array('id'=>$edit_id['1']));
			if($update_inventory){
				$session->redirect('view_inventory',Admin);
			}	
			}else{		
			$insert_inventory =$db->insert('inventory',$inventory_array);
			if($insert_inventory){
				$display_inventory = '<div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert">
									<i class="icon-remove"></i></button><p><strong><i class="icon-ok"></i>Well done!</strong>
											You Have Been Saved Inventory Successfully.</p></div>';
			}
			}
	}
}

?>
