<?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'];
		
		if($order_number == '')
		{
			$Error	=	"Please Enter Order No";	
		}
		
		if($Error == ''){
			$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);
				}
			}
			
			for($p = 0; $p < count($porduct); $p++)
			{
				$Sql_Product	=	"UPDATE `product` SET `in_transit` =`in_transit` + '$order_quantity[$p]' WHERE `id` = '{$porduct[$p]}'";	
				mysql_query($Sql_Product);
			}

		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 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_no_error').innerHTML = " Order # Already Exist";					
					document.getElementById('order_number').value = "";	
				}
				document.getElementById('order_number_hid').value = data;
				
			}
		});
	
}

function compar_val(val)
{
	alert(val);	
}

</script>
    <div id="mainPanel">
<h2><?php echo $Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', '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($Error != '')
{
	echo '<div style="text-align:left; padding-bottom:5px; color:green;">';
		echo $Error;
	echo '</div>';
}
?>

<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><?php echo $Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Order_Number')?>:</label></td>
    <td width="30%" nowrap><input type="text" name="order_number" style="width:135px !important;" class="textfield1" onblur="check_value(this.value);" id="order_number" value="<?php echo $Order_Numnber; ?>" /><span id="order_no_error" style="font-size:small;"></span></td>
    <td nowrap width="15%" align="right"> <label><?php echo $Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', '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">
  <!--<h3 style="padding-left:10px">Order detail</h3>-->
      <!------------------------------------------------------------------------------------------------------------->
      <?php
      $Header	=	'<table width="100%" id="tabling" cellpadding="0" cellspacing="0">';
      $Header	= 	$Header . '<tr>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Product_Name').'</span></td>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Threshold').'</span></td>';
        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Current_Stock').'</span></td>';
			
			$Header	= 	$Header . '<td class="col2"><span class="blue">encomendado</span></td>  '; 
			
        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', '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' Order By `producttitle` ASC";
				$Exe_Product_Query	=	mysql_query($Product_Query);
				while($Porduct_Row	=	mysql_fetch_array($Exe_Product_Query))
				{
					$Intransit			=	$Porduct_Row["in_transit"];
					if($Intransit < 0 || $Intransit == '')
					{
						$Intransit	=	0;
					}
					
					$Quantity_Amount	=	$Porduct_Row["alertcount"] - $Porduct_Row["currentstocklevel"] - $Intransit;
					
					$Product_ID			=	$Porduct_Row["id"];
					
					if($Quantity_Amount < 0)
					{
						$Quantity_Amount = 0;
					}
					
					
					$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>';
						/*-----------------------------------------------------------------*/
						/*$quantityorder	=	'';
						$Get_Instransit	=	"SELECT a.*, b.* FROM `purchaseorder` as a, purchaseorderdetails as b where a.id = b.orderid and a.orderdate = '2013-01-29' and a.orderstatusid = '1' and b.productid = '$Product_ID'";
						$Exe_Instransit =	mysql_query($Get_Instransit);
						while($Row_Instransit	=	mysql_fetch_array($Exe_Instransit))
						{
							$quantityorder	+=	$Row_Instransit['quantityorder'];	
						}
						
						if($quantityorder < 0 || $quantityorder == '')
						{
							$quantityorder	=	0;
						}*/
						
						$Html	=	$Html .  '<td class="col2">'.$Intransit.'</td>';
						
						/*$Quantity_Amount	=	  $Quantity_Amount - $quantityorder;	
						
						if($Quantity_Amount < 0)
						{
								$Quantity_Amount = 0;
						}*/				
						/*-----------------------------------------------------------------*/
						$Html	=	$Html .  '<td class="col2"><span class="blue"><input type="text" class="intcheck" name="order_quantity[]" value="'.$Quantity_Amount.'" id="order_quantity_'.$Product_ID.'" /></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 />";	
			}
		?>
      <!------------------------------------------------------------------------------------------------------------->
  </td>
</tr>
<tr>
    <td colspan="4" style="text-align:center">
    	<input type="submit" name="submit" id="submit" value="<?php echo $Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Create_Order')?>" class="form_button"/>
        <a href="#" onclick="cancel_check()" class="form_button"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_create_order.xml', 'Cancel')?></a>
    </td>
</tr>
</table>
</form>
</body>
</html>