<?php 

	include("includes/config.php");

	include("includes/func.php");

	include("classes/safeSQL.class.php");

	

	if (($_SESSION['id'] == ""))

	{

		redirect("login");

	}

	

	include("templates/header.php");

	

	if(isset($_GET['id']))

	{

		$Edit_ID	=	$_GET['id'];

	}

	

	$safeSQL 			= new safeSQL;

	$User_ID			= $_SESSION['id'];

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/



if(isset($_POST['submit']))

{

		$order_quantity 			= $_POST['order_quantity'];
		//echo '<pre>'; print_r($order_quantity);
		$productdetailid 			= $_POST['productdetailid'];

		

		$porduct 					= $_POST['porduct'];

		$porduct_old_quantiyt 		= $_POST['porduct_old_quantiyt'];

			//echo '<pre>'; print_r($porduct_old_quantiyt); exit;		

		for($i = 0; $i < count($order_quantity); $i++)

		{



				$Sql_Ins_Pordct	=	"UPDATE `purchaseorderdetails` SET `quantityorder` = '{$order_quantity[$i]}' WHERE `id` = $productdetailid[$i]";

				mysql_query($Sql_Ins_Pordct);



		}

		

		for($p = 0; $p < count($porduct); $p++)

		{

			$suutract	=	$order_quantity[$p] - $porduct_old_quantiyt[$p];

			$Sql_Product	=	"UPDATE `product` SET `in_transit` =`in_transit` + '$suutract' WHERE `id` = '{$porduct[$p]}'";	

			mysql_query($Sql_Product);

		}



		redirect("index");

}



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/



if(isset($_POST['Confirm']))

{

		$order_quantity 			= $_POST['order_quantity'];

		$productdetailid 			= $_POST['productdetailid'];

		$porduct 					= $_POST['porduct'];

		

		

		$Sql_Confirm	=	"UPDATE `purchaseorder` SET `orderstatusid` = '3' WHERE `id` = $Edit_ID";

		mysql_query($Sql_Confirm);			

		for($i = 0; $i < count($order_quantity); $i++)

		{

			$Sql_up_product	=	"UPDATE `product` SET `currentstocklevel` = currentstocklevel + $order_quantity[$i] WHERE `id` = $porduct[$i]";

				

			mysql_query($Sql_up_product);

			

			$Sql_Product	=	"UPDATE `product` SET `in_transit` =`in_transit` - '{$order_quantity[$i]}' WHERE `id` = '{$porduct[$i]}'";	

			

			mysql_query($Sql_Product);

		}



		redirect("index");

}



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

if(isset($_POST['Cancel']))

{

		$order_quantity 			= $_POST['order_quantity'];
		
		$porduct_old_quantiyt 		= $_POST['porduct_old_quantiyt'];

		$productdetailid 			= $_POST['productdetailid'];

		$porduct 					= $_POST['porduct'];
		//echo '<pre>'; print_r($porduct); exit;

		

		

		$Sql_Confirm	=	"UPDATE `purchaseorder` SET `orderstatusid` = '2' WHERE `id` = $Edit_ID";

		mysql_query($Sql_Confirm);		
		
		for($p = 0; $p < count($porduct); $p++)

		{

			$suutract	=	$order_quantity[$p] ;

			$Sql_Product	=	"UPDATE `product` SET `in_transit` =`in_transit` - '$suutract' WHERE `id` = '{$porduct[$p]}'";	

			mysql_query($Sql_Product);

		}	



		redirect("index");

}



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

?>

<script type="text/javascript">

function cancel_check()

{

	window.location.href = 'index.php';

}



/*function validation()

{

	var quantinty	=	document.getElementById('order_quantity').value;

	alert(quantinty);

	return false;	

}*/

</script>

    <div id="mainPanel">

<h2>Order Detail</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;	

}



?>

<form action="" method="POST" name="order_form" id="order_form">

<table width="100%" cellpadding="0" cellspacing="0" border="0" id="tabling1">



<?php

	$Sql_Date	=	mysql_query("SELECT * FROM `purchaseorder` where id = '$Edit_ID'");

	$Date_Row	=	mysql_fetch_array($Sql_Date);

?>



<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;" class="textfield1" id="order_number" readonly="readonly" value="<?php echo $Date_Row['OrderNo']; ?>" /></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_Row['orderdate'];?>"/></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">Product Name</span></td>';

        	$Header	= 	$Header . '<td class="col2"><span class="blue">Threshold Value</span></td>';

        	$Header	= 	$Header . '<td class="col2"><span class="blue">Current Stock</span></td>';

			//$Header	= 	$Header . '<td class="col2"><span class="blue">encomendado</span></td>  '; 

        	$Header	= 	$Header . '<td class="col2"><span class="blue">Order Quantity</span></td>  ';          

      $Header	= 	$Header . '</tr>';

	  

	  $Footer	=	'</table>';

      

        	

			$Cat_Query	=	"SELECT distinct category.id,category.desc FROM `category`

inner join product on product.catid=category.id

inner join purchaseorderdetails on product.id = purchaseorderdetails.productid

where purchaseorderdetails.orderid = '$Edit_ID'";

			$Exe_Cat_Query	=	mysql_query($Cat_Query);

			while($Cat_Row	=	mysql_fetch_array($Exe_Cat_Query))

			{

				$Cat_No	=	$Cat_Row['id'];

				

					echo '<h3 style="padding-left:10px">'.$Cat_Row['desc'].'</h3>';

				

					echo $Header;

				/*$Product_Query	=	"SELECT * FROM `product` 

inner join purchaseorderdetails on product.id = purchaseorderdetails.productid

where product.`catid` = '$Cat_No' and purchaseorderdetails.orderid='$Edit_ID'";*/

				 

				 $Product_Query	=	"SELECT product.id as productid, product.in_transit, product.producttitle, product.catid, product.currentstocklevel, product.alertcount, product. 	product_status_id, purchaseorderdetails.id as purchasedetailid, purchaseorderdetails.orderid, purchaseorderdetails.productid, purchaseorderdetails.quantityorder, purchaseorderdetails.old_quantity FROM `product` inner join purchaseorderdetails on product.id = purchaseorderdetails.productid where product.`catid` = '$Cat_No' and purchaseorderdetails.orderid='$Edit_ID'";

	

				$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;

					}

					

					echo '<tr>';

						echo '<td class="col2">'.$Porduct_Row['producttitle'].'</td>';

						echo '<td class="col2">'.$Porduct_Row["alertcount"].'</td>';

						echo '<td class="col2">'.$Porduct_Row["currentstocklevel"].'</td>';

						/*--------------------------------------------------------------------*/

						//echo '<td class="col2">'.$Intransit.'</td>';

						/*--------------------------------------------------------------------*/

						

						if(!$_GET['status']) {

							echo '<td class="col2"><span class="blue"><input type="text" name="order_quantity[]" value="'.$Porduct_Row["quantityorder"].'" id="order_quantity" /></span></td>'; 

						} else { 

							echo '<td class="col2"><span>'.$Porduct_Row["quantityorder"].'</span></td>'; 

						}
echo '<input type="hidden" name="porduct_old_quantiyt[]" id="porduct_old_quantiyt" value="'.$Porduct_Row["quantityorder"].'" />';
						

						echo '<input type="hidden" name="porduct[]" id="product_id" value="'.$Porduct_Row["productid"].'" />';

						echo '<input type="hidden" name="productdetailid[]" id="productdetailid" value="'.$Porduct_Row["purchasedetailid"].'" />';            

					echo '</tr>';

													

				}

					echo $Footer;

					echo "<br />";	

			}

		?>

      <!------------------------------------------------------------------------------------------------------------->

  </td>

</tr>

<tr>

    <td colspan="4" style="text-align:center">

    <?php if(!$_GET['status']) { ?>

    	<input type="submit" name="submit" id="submit" value="Adjust" class="form_button"/>

        <input type="submit" name="Confirm" id="Confirm" value="Confirm" class="form_button"/>

        <input type="submit" name="Cancel" id="Cancel" value="Cancel" class="form_button"/>

        <?php }?>

		 <a href="#" onclick="cancel_check()" class="form_button">Back</a>

    </td>

</tr>

</table>

</form>

</body>

</html>