<?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'];

	

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

	$Delet_Rect	=	'';

	

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

	{

		$Action_Mes	=	$_GET['action'];

	}

	

	if($Action_Mes == 'delete_message')

	{

		$message_idd	=	$_GET['message_id'];

		$Sql_Mess	=	"DELETE FROM `message` WHERE `id` = $message_idd";	

		if(mysql_query($Sql_Mess))

		{

			$Delet_Rect	=	"Message Deleted Successfully";	

		}

	}



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



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 `stockout` (`id`, `solddate`, `productid`, `quantitysold`) VALUES ( '', '$order_date', '$User_ID', '$order_quantity')";

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

		

		}*/

}



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

$message = '';

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

{
		

$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 `stockout` (`id`, `solddate`, `productid`, `quantitysold`) VALUES ( '', '$order_date', '$User_ID', '$order_quantity')";

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

		

		}
		

		$sold_date 		= $_POST['sold_date'];

		$product_ml 	= $_POST['product_ml'];

		$ml35 			= $_POST['35ml'];

		$ml55 			= $_POST['55ml'];

		$ml110 			= $_POST['110ml'];
		
		$rollON         = $_POST['rollon'];

		

		$order_quantity 	= $_POST['order_quantity'];

		$product 			= $_POST['porduct'];

		$User_ID			= $_SESSION['id'];



		$message_check		= $_POST['message_check'];

		

		for($mk = 0; $mk < count($message_check); $mk++)

		{

			$Sql_Messge	=	"UPDATE `message` SET `stokc_pin` = '1' WHERE `id` = {$message_check[$mk]}";	

			mysql_query($Sql_Messge);

	//		$message = 1;

		}



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

		{

			if($order_quantity[$i]!=0)

			{

				$sql = "Insert into stockout(`solddate`,`userid`, `quantitysold`, `productid`) values('$sold_date','$User_ID' ,'$order_quantity[$i] ', '$product[$i]')";

				mysql_query($sql);

				

				$update_sql = "Update product set currentstocklevel = currentstocklevel - $order_quantity[$i] where id = $product[$i]";

				if(mysql_query($update_sql))

				{

					$message = 1;

				}

			

			}

		}

		

		$Get_Date	=	"SELECT * FROM `product_stockout` where `sold_date` = '$sold_date'";

		$Exe_Get_Date	=	mysql_query($Get_Date);

		$Data_prod_Stock	=	mysql_fetch_array($Exe_Get_Date);

		if($Data_prod_Stock['sold_date'] != '')

		{

			$Rec_id	=	$Data_prod_Stock['id'];

			$sql_update_ml	=	"UPDATE `product_stockout` SET `product` = '$product_ml', `35ml` = '$ml35', `55ml` = '$ml55', `110ml` = '$ml110', `RollOn` = '$rollON' WHERE `sold_date` = '$sold_date'";
//echo $sql_update_ml; exit;
			mysql_query($sql_update_ml);

			$message = 1;

		}else{

			$sql_product_ml =	"INSERT INTO `product_stockout` (`sold_date`, `product`, `35ml`, `55ml`, `110ml`, `userid`, `RollOn`) VALUES ('$sold_date', '$product_ml', '$ml35', '$ml55', '$ml110', '$User_ID', '$rollON')";
			mysql_query($sql_product_ml);

			$message = 1;

		}

		

		if($message == 1)

		{

			$_SESSION['msg'] = 'Obrigado, o formulário foi transmitido com sucesso';

			redirect(index);		

		}		

		

		

}

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



?>

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

    <link rel="stylesheet" href="calender/development-bundle/themes/base/jquery.ui.all.css" />

	<!--<script src="../../jquery-1.8.3.js"></script>-->

	<script src="calender/development-bundle/ui/jquery.ui.core.js"></script>

    <script type="text/javascript" src="calender/development-bundle/ui/jquery.ui.widget.js"></script>


	<link rel="stylesheet" href="calender/development-bundle/demos/demos.css">    

    

    <script type="text/javascript" src="source/jquery.fancybox.js?v=2.1.1"></script>

	<link rel="stylesheet" type="text/css" href="source/jquery.fancybox.css?v=2.1.1" media="screen" />

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



<script type="text/javascript" src="javascripts/jquery.maskedinput-1.3.js"></script>

<script type="text/javascript">

 $(document).ready(function(){

$(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
		$(".intcheck").keypress(function (e){

		  	var charCode = (e.which) ? e.which : e.keyCode;

		  	if (charCode > 31 && (charCode < 48 || charCode > 57)) {

				return false;

		  	}

		});

		

		

		/*$("#fancybox-manual-b").click(function() {

			$.fancybox.open({

					type : 'iframe',

					padding : 5

			});

		});*/

		

		$('.fancybox').fancybox();

		

 });

function cancel_check()

{

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

}





$(function() {

		$('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' });

});





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 delete_message_row(OrderNO)

{

	if(confirm('Are you sure want to delete this message?'))

	{

		$.ajax({

			url: "Delete_Message.php",

			type: "post",

			data: 'Q1='+OrderNO,

			success: function(data) {

				if(data == '1')

				{

					$("table#tabling51 tr#message_row_"+OrderNO).remove();

					/*document.getElementById('order_no_error').innerHTML = " Order # Already Exist";*/					

				}

			}

		});

	}

	

}





function get_prev_rec(val)

{

	$.ajax({

		url: "get_prev_data.php",

		type: "post",

		data: 'Q1='+val,

		success: function(data) {


				var data_arr	=	data.split("++");

				var id	=	data_arr[0];

				var sold_date	=	data_arr[1];

				var product	=	data_arr[2];

				var ml35	=	data_arr[3];

				var ml55	=	data_arr[4];

				var ml110	=	data_arr[5];
				
				var rollon	=	data_arr[6];



				if(product != ''){

					document.getElementById('product_ml').value = product;

				}else{

					document.getElementById('product_ml').value = '0';

				}

				

				if(ml35 != ''){

					document.getElementById('35ml').value = ml35;

				}else{

					document.getElementById('35ml').value = '0';

				}

				

				if(ml55 != ''){

					document.getElementById('55ml').value = ml55;

				}else{

					document.getElementById('55ml').value = '0';

				}

				

				if(ml110 != ''){

					document.getElementById('110ml').value = ml110;

				}else{

					document.getElementById('110ml').value = '0';

				}
				
				if(rollon != ''){

					document.getElementById('rollon').value = rollon;

				}else{

					document.getElementById('rollon').value = '0';

				}



		}

	});	

}



function stockcheck(Currentstock)

{

	var quantity='order_quantity_'+Currentstock;

	var quantitysold = 'current_stock_'+Currentstock;

	var error = 'stock_error_'+Currentstock;

	var qtyele = document.getElementById(quantity);

	var qty = document.getElementById(quantity).value;



	var qtysold = document.getElementById(quantitysold).value;

	if(parseInt(qty) > parseInt(qtysold))

	{

		qtyele.value='0';

		qtyele.focus();

		document.getElementById(error).style.display='inline';

	}else{

		document.getElementById(error).style.display='none';

	}

}





function check_val(val)

{

	if(val < 10)

	{

		//document.getElementById('factura_erro').innerHTML = '*Enter Value Greater Than 10';

		//document.getElementById('product_ml').value	=	0;

//		alert('Enter Value Greater Than Ten');	

	}



}



</script>

    <div id="mainPanel">

    

<h2><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Stock_Out')?></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>';

}

?>

<?php

if($message == 1)

{

	echo '<span style="color:green">'.'Stockout has been done successfully!'.'</span>';

}

?>

<?php

if($Delet_Rect != '')

{

	echo "<span style='color:green'>$Delet_Rect</span>";

}

?>






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

<input type="hidden" name="order_number" id="order_number_hid" value="" />



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

<?php

	

	$Sql_Notice	=	"SELECT * FROM `product_stockout` where `sold_date` = '$Current_Date'";



	$Exe_Query_notice	=	mysql_query($Sql_Notice);

	$Number_Of_Notice	=	mysql_num_rows($Exe_Query_notice);

	if($Number_Of_Notice > 0)

	{



	

	 echo  "<div style='text-align:left; color:green;'>".$Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'StockMess').'</div>';

	}

?>

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







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



	<tr>

		<td nowrap width="13%" align="right" ><label><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Sold_Date')?>:</label></td>

    	<td style="border-right:0px;">&nbsp;</td>

        <td nowrap><input type="text" name="sold_date" id="datepicker" onchange="get_prev_rec(this.value);" value="<?php echo date('Y-m-d'); ?>" /></td>

        <td><label><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Facturacao')?>:</label></td>

        <td >

        	<!--<input type="text" name="product_ml" id="product_ml" value="0" onblur="check_val(this.value);" />-->
            
            <input type="text" name="product_ml" id="product_ml" value="0" />

            <span id="factura_erro" style="color:red"></span>

        </td>

       <td  width="13%">Roll On</td>

        <td><input type="text" name="rollon" id="rollon" value="0" onblur="check_val(this.value);" /></td>

	</tr>

    

	<tr>

		<td nowrap width="13%" align="right" > <label><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Vendas_de_frascos')?>:</label></td>

    	<td><label>35ml:</label></td>

        <td width="26%" nowrap align="left"><input type="text" name="35ml" id="35ml" value="0" /></td>

        <td><label>55ml:</label></td>

        <td width="26%" nowrap align="left"><input type="text" name="55ml" id="55ml" value="0" /></td>

        <td><label>110ml:</label></td>

        <td width="26%" nowrap align="right"><input type="text" name="110ml" id="110ml" value="0" /></td>

	</tr>

        

</table>



<br /><br />



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

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



<!--<tr >

	<td nowrap width="13%" align="right" > <label><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Sold_Date')?>:</label></td>

    <td width="87%" nowrap> <?php echo date('d-m-Y');?> </td>-->

<!--   <td nowrap width="15%" align="right"> <label>Order Number:</label></td>-->

   

<!--</tr>-->

<!-- <input type="hidden" 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>
-->


<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_stock_out.xml', 'Product_Name').'</span></td>';

        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Current_Stock').'</span></td>';

        	$Header	= 	$Header . '<td class="col2"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Sold_Quantity').'</span></td>  ';          

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

	  

	  $Footer	=	'</table>';

      

		

        	$Cat_Query	=	"SELECT * FROM `category` where `delete` = '0'";

			$Exe_Cat_Query	=	mysql_query($Cat_Query);

			$outer = 0;

			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	=	'';

				$outer++;

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

				{

					$Quantity_Amount	=	$Porduct_Row["alertcount"] - $Porduct_Row["currentstocklevel"];

					$current_stock = $Porduct_Row["currentstocklevel"];

					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["currentstocklevel"].'</td>';

						

						/*$Html	=	$Html .  '<td class="col2"><span class="blue"><input type="text" class="intcheck" name="order_quantity[]" value="'.$Quantity_Amount.'" id="order_quantity_'.$k.'" onclick="testone($k);"  /></span></td>';*/

						

						$Html	=	$Html .  "<td width='400px'><input type='text'  id='order_quantity_".$outer.$k."' onblur='stockcheck(".$outer.$k.");'  value='0' name='order_quantity[]'/>&nbsp;<span style='color:Red;display:none;' id='stock_error_".$outer.$k."'>Enter less then current stock</span></td>";

						

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

						$Html	=	$Html .  '<input type="hidden" name="current_stock[]" id="current_stock_'.$outer.$k.'" value="'.$current_stock.'" />';            

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

					$k++;

													

				}

				

				if($k > 1)

				{

					echo $Category_title;

					echo $Header;

					echo $Html;

					echo $Footer;	

					echo "<br />";

				}

					

					

			}

			

			

/*------------------------------------------------------------ Client To Admin Section -------------------------*/

	if($_SESSION['usertype'] != 'Admin')

	{ ?>

    <?php /*?><span style="float:left"><a id="form_button" href="day_manage.php">Day Management</a></span><?php */?>

<br/>



<br clear="all" />



<span style="float:left"><a id="form_button" class="fancybox fancybox.iframe" href="Send_Message.php">Create Message</a></span>

<br/>



<br clear="all" />



<?php

	$Me		=	1;

	if($_SESSION['usertype'] == 'Admin')

	{

		$Sql_M	=	"select a.*, b.* from users AS a, message as b where a.id = b.user";

	}else{

		$Sql_M	=	"SELECT * FROM `message` where `user` = '$User_ID'";		

	}

	$Exe_Qyer	=	mysql_query($Sql_M);

	$Number_of_message	=	mysql_num_rows($Exe_Qyer);

	while($row_m	=	mysql_fetch_array($Exe_Qyer))

	{

		$Message_ID	=	$row_m['id'];

		$Html_M	=	$Html_M . '<tr id="message_row_'.$Message_ID.'">';

		$Html_M	=	$Html_M .  '<td>'.$Me.'</td>';

									

		

		if($_SESSION['usertype'] == 'Admin'){ 	

		$Html_M	=	$Html_M .  '<td>'.$row_m['firstname'].' '.$row_m['lastname'].'</td>'; 

		}

		$Html_M	=	$Html_M .  '<td id="message_id_'.$Message_ID.'">'.$row_m['message'].'</td>';

		$Html_M	=	$Html_M .  '<td>'.$row_m['current_date'].'</td>';

			

		$Html_M	=	$Html_M .  '<td>';

		if($_SESSION['usertype'] != 'Admin'){

		$Html_M	=	$Html_M .  '<a id="fancybox-manual-b" class="fancybox fancybox.iframe" href="Send_Message.php?act=edit&uid='.$Message_ID.'"><img src="images/edit.png" alt="Edit" border="0"></a>'; 

		}

		

		$Html_M	=	$Html_M .  '<img onclick="delete_message_row('.$Message_ID.');" src="images/delete.png" alt="Delete" />';

		

		$Html_M	=	$Html_M .  '<input type="hidden" name="message_check[]" id="message_check" value="'.$Message_ID.'" />';

		

		$Html_M	=	$Html_M .  '</td>';

			

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

		

		$Me++;

		

	}

	$Me--;

	

	echo "<input type='hidden' name='count_mesage_row' id='count_mesage_row' value='{$Me}' />";

	

	$Header_M	=	'<table width="100%" id="tabling51"  cellpadding="0" cellspacing="0">';

    	$Header_M	= 	$Header_M . '<tr>';

        	$Header_M	= 	$Header_M . '<td width="3%" class="col2"><span class="blue">#</span></td>';

        	

			if($_SESSION['usertype'] == 'Admin'){ 	

				$Header_M	=	$Header_M .  '<td width="15%" class="col2"><span class="blue">Name</span></td>';

				$Header_M	= 	$Header_M . '<td width="65%" class="col2"><span class="blue">Message</span></td>'; 

			}else{

				$Header_M	= 	$Header_M . '<td width="70%" class="col2"><span class="blue">Message</span></td>';				

			}

			

        	$Header_M	= 	$Header_M . '<td width="15%" class="col2"><span class="blue">Date</span></td>  ';

			$Header_M	= 	$Header_M . '<td width="10%" class="col2"><span class="blue">Action</span></td>  ';          

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

	  

	  $Footer_M	=	'</table>';	

	  		  

		  echo $Header_M;  

		  if($Number_of_message > 0){ echo $Html_M; }else{echo "<tr id='no_message_found'><td colspan='4'>No Message Found</td></tr>";}

		  echo $Footer_M;

	}

	

	?>

    

<br /> 



<!------------------------------------ Client To Admin Section End ------------------------------------------------>



  </td>

</tr>


<tr>

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

    <input type="submit"  name="stock" id="stock" value="<?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Stock_Out')?>" class="form_button" />

    <a href="#" onclick="cancel_check()" class="form_button"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_stock_out.xml', 'Cancel')?></a>

    </td>

</tr>

</table>

</form>

</body>

</html>