<?php 
	include("includes/config.php");
	include("includes/func.php");
	include("classes/safeSQL.class.php");
	
	if (($_SESSION['id'] == ""))
	{
		redirect("login");
	}
	
	include("templates/header.php");
	
	$safeSQL 			= new safeSQL;
	$User_ID			= $_SESSION['id'];
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

if(isset($_POST['submit']))
{
		$order_number 				= $safeSQL->$_POST['order_number'];
		$order_date 				= date('Y-m-d');
		$order_quantity 			= $_POST['order_quantity'];
		$porduct					= $_POST['porduct'];
		

			$Sql_Insert_Orde	=	"INSERT INTO `purchaseorder` (`OrderNo`, `orderdate`, `ordercreatedby`, `orderstatusid`) VALUES ( '$order_number', '$order_date', '$User_ID', '1')";
			$Exe_Sql_Inst_Query	=	mysql_query($Sql_Insert_Orde);
			
			
			$Last_Insert_Queyr	=	mysql_insert_id();
			
			for($i = 0; $i < count($order_quantity); $i++)
			{
				if($order_quantity[$i] != '0' && $order_quantity[$i] != '' )
				{
					$Sql_Ins_Pordct	=	"INSERT INTO `purchaseorderdetails` (`orderid`, `productid`, `quantityorder`, `old_quantity`, `delete`) VALUES ('$Last_Insert_Queyr', '{$porduct[$i]}', '{$order_quantity[$i]}', '0', '')";
					mysql_query($Sql_Ins_Pordct);
				}
			}

		redirect("index");
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
?>
<script type="text/javascript" src="javascripts/jquery.maskedinput-1.3.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
		$(".intcheck").keypress(function (e){
		  	var charCode = (e.which) ? e.which : e.keyCode;
		  	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
				return false;
		  	}
		});
		
 });

function cancel_check()
{
	window.location.href = 'index.php';
}

function show_div(val)
{
//	$("#order_form").submit();
	document.getElementById("change_div").submit();
}

function check_value(OrderNO)
{
		$.ajax({
			url: "check_orderNO.php",
			type: "post",
			data: 'Q1='+OrderNO,
			// callback handler that will be called on success
			success: function(data) {
				if(data == '1')
				{
					alert(OrderNO+' This Order No Already Exist');
					document.getElementById('order_number').value = "";	
				}
				document.getElementById('order_number_hid').value = data;
				
			}
		});
	
		// prevent default posting of form
	//	event.preventDefault();
}

</script>
    <div id="mainPanel">
<h2>Create Order</h2>
<?php
$edit = "";
$update = "";
if(isset($_GET['edit']))
{
	$edit = $_GET['edit'];
}
if(isset($_GET['delete']))
{
	$id= $_GET['delete'];
	

	//header('location:order_parchase_show.php');
	//redirect('order_parchase.php');
}
if(isset($_POST['submit']))
{
	$order_date = $_POST['order_date'];
	$d_m_y = explode("/", $order_date);
	$m = $d_m_y[0];
	$d = $d_m_y[1];
	$y = $d_m_y[2];
	$order_date = $y."-".$m."-".$d;
	$cat_name = $_POST['cat_name'];
	$product_name = $_POST['product_name'];
}

$Order_Numnber	=	date('dmY');
$Current_Date	=	date('Y-m-d');

$Quey_Order_No		=	"SELECT * FROM `purchaseorder` where `orderdate` = '$Current_Date'";
$Quey_Order_No_Exe	=	mysql_query($Quey_Order_No);
$Num_Rows			=	mysql_num_rows($Quey_Order_No_Exe);

if($Num_Rows == 0)
{
	$Order_Numnber	=	$Order_Numnber."-1";	
}else{
	$Num_Rows	=	$Num_Rows + 1;
	$Order_Numnber	=	$Order_Numnber."-".$Num_Rows;	
}


if(isset($_GET['show_product_grid']))
{
	$show_product_grig	=	$_GET['show_product_grid'];  
}
?>

<form action="" method="get" name="change_div" id="change_div" >
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="tabling1">
	<tr>
    	<td nowrap width="15%" align="right">Thresholod Products:</td>
    	<td width="30%" nowrap><input type="radio" <?php if($show_product_grig == '1'){echo "checked";} ?> name="show_product_grid" value="1"  onclick="show_div();" /></td>
        
    	<td nowrap width="15%" align="right">All Products:</td>
    	<td width="30%" nowrap><input type="radio" name="show_product_grid" <?php if($show_product_grig == '2'){echo "checked";} ?> value="2" onclick="show_div();" /></td> 
	</tr>
</table>
</form>

<br />
<br />


<form action="" method="POST" name="order_form" id="order_form">
<input type="hidden" name="order_number" id="order_number_hid" value="" />
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="tabling1">

<tr>
    <td nowrap width="15%" align="right"> <label>Order Number:</label></td>
    <td width="30%" nowrap><input type="text" name="order_number" style="width:135px !important;" onblur="check_value(this.value);" class="textfield1" id="order_number" value="<?php echo $Order_Numnber; ?>" /></td>
    <td nowrap width="15%" align="right"> <label>Order Date:</label></td>
    <td width="30%" nowrap><input type="text" name="order_date" style="width:135px !important;" class="textfield1" id="datepick" readonly="readonly" value="<?php echo date('d-m-Y');?>"/></td>
</tr>

<!------------------------------------------------------------------------------------------------------------->
<tr>
  <td colspan="4">
      <div id="show_threshold_prodcut" style="display:block">
      <?php
      $Header	=	'<table width="100%" id="tabling" cellpadding="0" cellspacing="0">';
      $Header	= 	$Header . '<tr>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">Product Name</span></td>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">Threshold</span></td>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">Current Stock</span></td>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">Order Quantity</span></td>  ';          
      $Header	= 	$Header . '</tr>';
	  
	  $Footer	=	'</table>';
      
		
        	$Cat_Query	=	"SELECT * FROM `category` where `delete` = '0'";
			$Exe_Cat_Query	=	mysql_query($Cat_Query);
			while($Cat_Row	=	mysql_fetch_array($Exe_Cat_Query))
			{
				$Cat_No	=	$Cat_Row['id'];
				
				$Category_title	= '<h3 style="padding-left:10px">'.$Cat_Row['desc'].'</h3>';
				
				$Html	=	'';
				$k		=	1;
								
				$Product_Query	=	"SELECT * FROM `product` where `catid` = '{$Cat_No}' and `delete` = '0'";
				$Exe_Product_Query	=	mysql_query($Product_Query);
				while($Porduct_Row	=	mysql_fetch_array($Exe_Product_Query))
				{
					
					if($show_product_grig == '2')
					{
							$Quantity_Amount	=	$Porduct_Row["alertcount"] - $Porduct_Row["currentstocklevel"];
							
							$Html	=	$Html . '<tr>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row['producttitle'].'</td>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row["alertcount"].'</td>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row["currentstocklevel"].'</td>';
								$Html	=	$Html .  '<td class="col2"><span class="blue"><input type="text" class="intcheck" name="order_quantity[]" value="'.$Quantity_Amount.'" id="order_quantity" /></span></td>';
								$Html	=	$Html .  '<input type="hidden" name="porduct[]" id="product_id" value="'.$Porduct_Row["id"].'" />';            
							$Html	=	$Html .  '</tr>';
							$k++;
					}else{
						if($Porduct_Row["alertcount"] > $Porduct_Row["currentstocklevel"])
						{
							$Quantity_Amount	=	$Porduct_Row["alertcount"] - $Porduct_Row["currentstocklevel"];
							
							$Html	=	$Html . '<tr>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row['producttitle'].'</td>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row["alertcount"].'</td>';
								$Html	=	$Html .  '<td class="col2">'.$Porduct_Row["currentstocklevel"].'</td>';
								$Html	=	$Html .  '<td class="col2"><span class="blue"><input type="text" class="intcheck" name="order_quantity[]" value="'.$Quantity_Amount.'" id="order_quantity" /></span></td>';
								$Html	=	$Html .  '<input type="hidden" name="porduct[]" id="product_id" value="'.$Porduct_Row["id"].'" />';            
							$Html	=	$Html .  '</tr>';
							$k++;
						}
					}
				
				}
				
					
				if($k > 1)
				{
					echo $Category_title;
					echo $Header;
					echo $Html;
					echo $Footer;	
				}	
					echo "<br />";	
			}
		?>
      </div>
  </td>
</tr>
<tr>
    <td colspan="4" style="text-align:center">
    	<input type="button" name="submit" id="submit" value="Create Order" class="form_button"/>
        <a href="#" onclick="cancel_check()" class="form_button">Cancel</a>
    </td>
</tr>
</table>
</form>
</body>
</html>