<?php include "../../includes/start_ajax.php";?>
<?php

$this_module_title="Purchase Order";

$this_form_name="form_purchase_order";
$this_url="mod/purchase_order/index.php";

$this_doc_prefix="PO";
$this_doc_number_module_code="PO";
$this_module="PO";
$this_inventory_posting_type="";



echo "<div  align=left >";
echo "<form name='".$this_form_name."'  >";

	echo "<input type='hidden' name='ui_action' value=''>";
	echo "<input type='hidden' name='add_item_id' value=''>";
	echo "<input type='hidden' name='remove_item_ts' value=''>";




	if(($_REQUEST["ui_action"]=="REMOVE_ITEM")and($_REQUEST["remove_item_ts"]!=""))
	{
		unset($_SESSION["PURCHASE_ORDER_ITEM"][$_REQUEST["remove_item_ts"]]);
		
	}


	if($_REQUEST["ui_action"]=="ADD_ITEM1")
	{
		include "search.php";
	}
	
	
	if($_REQUEST["ui_action"]=="ADD_ITEM2")
	{
		
	     include "form.php";
	}
	
	
	if($_REQUEST["ui_action"]=="ADD_ITEM_OK")
	{
		
		//$_REQUEST["add_item_unit_price"];
		//$_REQUEST["add_item_quantity"];
		//$_REQUEST["add_item_gst_code"];
		//$_REQUEST["add_item_gst_mode"];
		//
		
		$query_product = " select * from t_".$key_table_prefix."_product 
				where pdt_id ='".$_REQUEST["add_item_id"]."'  ";
		$result_product = mysqli_query($dbconn,$query_product);
		if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
		{
			
				$error_msg="";
				
				if($_REQUEST["add_item_unit_price"]=="")
				{
					$error_msg=$error_msg."<li>Unit Price</li>";
				}
				else {
					if(!is_numeric($_REQUEST["add_item_unit_price"]))
					{
			
						$error_msg=$error_msg."<li>Unit price must be numeric value only.</li>";	  
					}
				}
				if($_REQUEST["add_item_quantity"]=="")
				{
					$error_msg=$error_msg."<li>Quantity</li>";
				}
				else {
					if(!is_numeric($_REQUEST["add_item_quantity"]))
					{
			
						$error_msg=$error_msg."<li>Quantity must be numeric value only.</li>";	  
					}
				}
				
				if($_REQUEST["add_item_gst_code"]=="")
				{
					//$error_msg=$error_msg."<li>Tax Code</li>";
				}
				else {
					
					if($_REQUEST["add_item_gst_mode"]=="")
					{
						$error_msg=$error_msg."<li>GST Mode</li>";
					}
				}
				
				if($line_product["pdt_select_pattern"]=="YES")
				{
					if($_REQUEST["add_item_pattern"]=="")
					{
						$error_msg=$error_msg."<li>Pattern</li>";
					}
				}
				
				if($line_product["pdt_select_color"]=="YES")
				{
					if($_REQUEST["add_item_color"]=="")
					{
						$error_msg=$error_msg."<li>Color</li>";
					}
				}
				
				if($line_product["pdt_select_size"]=="YES")
				{
					if($_REQUEST["add_item_size"]=="")
					{
						$error_msg=$error_msg."<li>Size</li>";
					}
				}
				
				
				
				
				
				
				
				
			
				if($error_msg=="")
				{
					
					$ts=date("YmdHis");
					
					
					
					
						
						$this_item_gst_percentage=0;
						$query_gst="select * from t_".$key_table_prefix."_gst 
								where gst_code = '".$_REQUEST["add_item_gst_code"]."' ";
						$result_gst = mysqli_query($dbconn,$query_gst);
						if($line_gst = mysqli_fetch_array($result_gst,MYSQLI_ASSOC))
						{
							$this_item_gst_percentage=$line_gst["gst_percentage"];
						}
						
						
						
						if($_REQUEST["add_item_gst_mode"]=="EXCLUSIVE")
						{
							
							$this_item_before_gst_amount = $_REQUEST["add_item_unit_price"]*$_REQUEST["add_item_quantity"];
							$this_item_gst_amount = number_format($this_item_before_gst_amount*($this_item_gst_percentage/100),2,".","");
							$this_item_total_amount = $this_item_before_gst_amount + $this_item_gst_amount;
							
						}
						
						if($_REQUEST["add_item_gst_mode"]=="INCLUSIVE")
						{
							
							$this_item_total_amount = $_REQUEST["add_item_unit_price"]*$_REQUEST["add_item_quantity"];
							
							$this_item_gst_amount = number_format($this_item_total_amount/(100+$this_item_gst_percentage)*$this_item_gst_percentage,2,".","");
							
							$this_item_before_gst_amount = $this_item_total_amount-$this_item_gst_amount;
							
							
						}
						
						
						
						
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["ID"]=$_REQUEST["add_item_id"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["CODE"]=$line_product["pdt_code"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["NAME"]=$line_product["pdt_name"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["PRICE"]=$_REQUEST["add_item_unit_price"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["QTY"]=$_REQUEST["add_item_quantity"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["UOM"]=$line_product["pdt_uom"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["GST_CODE"]=$_REQUEST["add_item_gst_code"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["GST_MODE"]=$_REQUEST["add_item_gst_mode"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["GST_PERCENTAGE"]=$this_item_gst_percentage;
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["BEFORE_GST"]=$this_item_before_gst_amount;
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["GST_AMOUNT"]=$this_item_gst_amount;
						
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["TOTAL_AMOUNT"]=$this_item_total_amount;
						
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["COLOR"]=$_REQUEST["add_item_color"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["SIZE"]=$_REQUEST["add_item_size"];
						$_SESSION["PURCHASE_ORDER_ITEM"]["TS".$ts]["PATTERN"]=$_REQUEST["add_item_pattern"];
						
						
						
						
						
						
						
						
					
					
					
					
					
					
					//displaymessage("test","CREATED");
					
				}
				else {
					displaymessage($error_msg,"FAIL");
					include "form.php";
					$error_msg="";
				}
			
				
	     
		}
		
		
	}



	if ($_SERVER[REQUEST_METHOD] == 'POST') {
		
		
		$_SESSION["PURCHASE_ORDER_HEADER"]["DATE"]=$_REQUEST["date"];
		//$_SESSION["PURCHASE_ORDER_HEADER"]["WAREHOUSE"]=$_REQUEST["warehouse_code"];
		
		//$_SESSION["PURCHASE_ORDER_HEADER"]["CUSTOMER"]=$_REQUEST["customer_code"];
		$_SESSION["PURCHASE_ORDER_HEADER"]["VENDOR"]=$_REQUEST["vendor_code"];
		
		$_SESSION["PURCHASE_ORDER_HEADER"]["REFNO"]=$_REQUEST["refno"];
		$_SESSION["PURCHASE_ORDER_HEADER"]["REMARK"]=$_REQUEST["remark"];
		
		
		
		if($_REQUEST["ui_action"]=="POST")
		{
			
			$item_found=false;
			
			$error_msg="";
			
			
			
			foreach($_SESSION["PURCHASE_ORDER_ITEM"] as $ts=>$ts_data)
			{
				$item_found=true;
			}
			
			
			
			if(!$item_found)
			{
				$error_msg=$error_msg."<li>No Item Input</li>";
			}
			
			
			
			//if($_REQUEST["warehouse_code"]=="")
			//{
				//$error_msg=$error_msg."<li>Warehouse not specified</li>";
			//}
			
			
			if($_REQUEST["refno"]=="")
			{
				$error_msg=$error_msg."<li>Reference No. not specified</li>";
			}
			
			
			
			if($error_msg=="")
			{
				
					//calculate amt:
					foreach($_SESSION["PURCHASE_ORDER_ITEM"] as $ts=>$ts_data)
					{
						$header_before_gst_amount = $header_before_gst_amount + $ts_data["BEFORE_GST"];
						$header_gst_amount = $header_gst_amount + $ts_data["GST_AMOUNT"];
						$header_total_amount = $header_total_amount + $ts_data["TOTAL_AMOUNT"];
						
							
					}
					
					
					
					$this_doc_no="ERROR";
					$this_doc_no = mon_register_document_number($this_doc_number_module_code);
					
					//get info:
					$query_whs="select * from t_".$key_table_prefix."_warehouse 
						where whs_code  ='".$_REQUEST["warehouse_code"]."' ";
					$result_whs  = mysqli_query($dbconn,$query_whs);
					if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
					{
						$this_warehouse_id = $line_whs["whs_id"];
					}
					
					
					//get info:
					$query_cus="select * from t_".$key_table_prefix."_customer
						where cus_code  ='".$_REQUEST["customer_code"]."' ";
					$result_cus  = mysqli_query($dbconn,$query_cus);
					if($line_cus = mysqli_fetch_array($result_cus,MYSQLI_ASSOC))
					{
						$this_customer_name = $line_cus["cus_name"];
					}
					
					
					//get info:
					$query_vdr="select * from t_".$key_table_prefix."_vendor
						where vdr_code  ='".$_REQUEST["vendor_code"]."' ";
					$result_vdr  = mysqli_query($dbconn,$query_vdr);
					if($line_vdr = mysqli_fetch_array($result_vdr,MYSQLI_ASSOC))
					{
						$this_vendor_name = $line_vdr["vdr_name"];
					}
						
					
					$query="insert into t_".$key_table_prefix."_purchase_order_header 
							(
							poh_doc_prefix  ,
							poh_doc_no  ,
							poh_module  ,
							poh_date ,
							
							
							poh_vendor_code  ,
							poh_vendor_name  ,
							poh_reference_no  ,
							poh_remark  ,
							poh_status  ,
							poh_before_gst_amount ,
							poh_gst_amount  ,
							poh_total_amount  ,
							
							
							
							created_date  ,
							created_by  ,
							modified_date  ,
							modified_by  ,
							client_id  
							)
							values
							(
							'".$this_doc_prefix."'  ,
							'".$this_doc_no."'  ,
							'".$this_module."'  ,
							'".$_REQUEST["date"]."' ,
							
							
							
							'".$_REQUEST["vendor_code"]."' ,
							'".$this_vendor_name."'  ,
							'".$_REQUEST["refno"]."'  ,
							'".$_REQUEST["remark"]."'  ,
							'POSTED'  ,
							'".($header_before_gst_amount+0)."' ,
							'".($header_gst_amount+0)."' ,
							'".($header_total_amount+0)."' ,
							
							
							
							'".date("Y-m-d H:i:s")."'  ,
							'".$CurrentUserID."'  ,
							'".date("Y-m-d H:i:s")."'  ,
							'".$CurrentUserID."'  ,
							'0'  	
							)
					
						"; 
					$result = mysqli_query($dbconn,$query) or die("Error insert Header".mysqli_error($dbconn));
					$header_id  = mysqli_insert_id($dbconn);
					
					
					
					
						foreach($_SESSION["PURCHASE_ORDER_ITEM"] as $ts=>$ts_data)
						{
							$query_product = "select * 
										from t_".$key_table_prefix."_product 
										where pdt_id = '".$ts_data["ID"]."' ";
							$result_product = mysqli_query($dbconn,$query_product);
							if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
							{
								
								
								$total_old_balance=0;
								$this_price=$ts_data["PRICE"];
								
								
								//by default is the existing cost:
								$new_cost = ($line_product["pdt_cost"]+0); 
								
								
								
								
								
								
								if($line_product["pdt_inventory_costing_mode"]=="MOVING")
								{
									$query_get_balance="select sum(bal_quantity) as total 
													from t_".$key_table_prefix."_balance 
													where bal_product_code = '".$line_product["pdt_code"]."' 
													and bal_quantity >= 0 ";
									$result_get_balance = mysqli_query($dbconn,$query_get_balance);
									if($line_get_balance = mysqli_fetch_array($result_get_balance,MYSQLI_ASSOC))
									{
										$total_old_balance = $line_get_balance["total"];
									}
									
									$old_cost = ($line_product["pdt_cost"]+0);
									
									
									//echo "Old Stock Cost=".$old_cost."<br>";
									//echo "Old Stock Quantity=".$total_old_balance."<br>";
									
									
									
									$this_price = $ts_data["PRICE"]+0;
									
									if($ts_data["GST_PERCENTAGE"]>0)
									{
										//exclude GST
										if($ts_data["GST_MODE"]=="INCLUSIVE")
										{
											$this_price = $ts_data["PRICE"]/(100+$ts_data["GST_PERCENTAGE"])*(100);
										}
										
										if($ts_data["GST_MODE"]=="EXCLUSIVE")
										{
											
											$this_price = $ts_data["PRICE"];
										}
										
										
									}
									
									
									
									
									$old_stock_value = 	$old_cost * $total_old_balance;
									$this_stock_value = $this_price * $ts_data["QTY"];
									$new_stock_value = $old_stock_value+ $this_stock_value;
									
									//echo "Old Stock Value=".$old_stock_value."<br>";
									//echo "This Stock Value=".$this_stock_value."<br>";
									//echo "New Stock Value=".$new_stock_value."<br>";
									
									
									$new_stock_quantity = $total_old_balance + $ts_data["QTY"];
									
									//echo "New Stock Quantity=".$new_stock_quantity."<br>";
									
									
									if($new_stock_quantity==0)
									{
										//if no more quantity , follow the latest price:
										$new_cost = $this_price;
									}
									else {
										$new_cost = round($new_stock_value/$new_stock_quantity,2);	
									}

									//echo "New Cost=".$new_cost."<br>";

								}
								
								
								
								
								
								
								$query_item="insert into t_".$key_table_prefix."_purchase_order_item 
								 		(
											
										  poi_header_id  ,
										  poi_doc_prefix  ,
										  poi_doc_no  ,
										  poi_module  ,
										  poi_date  ,
										  poi_warehouse_code  ,
										  poi_warehouse_id  ,
										  poi_stock_code  ,
										  poi_barcode  ,
										  poi_factory_code  ,
										  poi_stock_name  ,
										  poi_color  ,
										  poi_size  ,
										  poi_pattern  ,
										  poi_uom  ,
										  poi_base_uom  ,
										  poi_quantity  ,
										  poi_base_quantity  ,
										  poi_conversion  ,
										  poi_unit_cost  ,
										  poi_base_unit_cost  ,
										  poi_total_cost  ,
										  poi_gst_mode  ,
										  poi_gst_code  ,
										  poi_gst_percentage  ,
										  poi_remark  ,
										  poi_status  ,
										  poi_before_gst_amount  ,
										  poi_gst_amount  ,
										  poi_total_amount  ,
										  created_date  ,
										  created_by  ,
										  modified_date  ,
										  modified_by  ,
										  client_id  
										)
										values
										(
										'".$header_id."'  ,
										
										'".$this_doc_prefix."'  ,
										'".$this_doc_no."'  ,
										'".$this_module."'  ,
										'".$_REQUEST["date"]."' ,
										'".$_REQUEST["warehouse_code"]."' ,
										'".($this_warehouse_id+0)."'  ,
										
										  '".$ts_data["CODE"]."'  ,
			   							  '".$line_product["pdt_barcode"]."'  ,
			   							  ''  ,
			   							  '".$ts_data["NAME"]."'  ,
			   							  '".$ts_data["COLOR"]."'  ,
			   							  '".$ts_data["SIZE"]."'  ,
			   							  '".$ts_data["PATTERN"]."'  ,
			   							  '".$ts_data["UOM"]."'  ,
			   							  '".$ts_data["UOM"]."'  ,
			   							  '".($ts_data["QTY"]+0)."'  ,
			   							  '".($ts_data["QTY"]+0)."'  ,
			   							  '1'  ,
			   							  '".($ts_data["PRICE"]+0)."'  ,
			   							  '".($ts_data["PRICE"]+0)."'  ,
			   							  '".(($ts_data["PRICE"]*$ts_data["QTY"])+0)."'  ,
			   							  '".$ts_data["GST_MODE"]."'  ,
			   							  '".$ts_data["GST_CODE"]."'  ,
			   							  '".($ts_data["GST_PERCENTAGE"]+0)."'  ,
			   							  ''  ,
			   							  'POSTED'  ,
			   							  '".($ts_data["BEFORE_GST"]+0)."'  ,
			   							  '".($ts_data["GST_AMOUNT"]+0)."'  ,
			   							  '".($ts_data["TOTAL_AMOUNT"]+0)."'  ,
										  '".date("Y-m-d H:i:s")."'  ,
										  
				  						  '".$CurrentUserID."'  ,
					  					  '".date("Y-m-d H:i:s")."'  ,
					  					  '".$CurrentUserID."'  ,
					  					  '0'  	
											
										);
										";
								
								$result_item = mysqli_query($dbconn,$query_item) or die("Error Insert ITEM".mysqli_error($dbconn));
								
								
								
								/*
								if($line_product["pdt_inventory_costing_mode"]=="MOVING")
								{
									
									$query_update_product = "update  t_".$key_table_prefix."_product 
												set pdt_cost = '".($new_cost+0)."'
												where pdt_id = '".$ts_data["ID"]."' ";
									$result_update_product = mysqli_query($dbconn,$query_update_product);
									
								}
								post_inventory_balance(
											'SERVER',
											$_REQUEST["date"],
											$this_doc_no,
											$this_module,
											
											$this_inventory_posting_type, 
											$_REQUEST["warehouse_code"], 
											$ts_data["CODE"], 
											$ts_data["UOM"], 
											$ts_data["QTY"], 
											$ts_data["COLOR"],
											$ts_data["SIZE"], 
											$ts_data["PATTERN"]
											);
								
								*/
								
								
								
								
								
								
							}
							
						}
						
						
					
					
					displaymessage("POSTED","CREATED");
					
					unset($_SESSION["PURCHASE_ORDER_HEADER"]);
					unset($_SESSION["PURCHASE_ORDER_ITEM"]);
					
					
					$_REQUEST["date"] = date("Y-m-d");
					//$_REQUEST["warehouse_code"]="";
					
					//$_REQUEST["customer_code"]="";
					$_REQUEST["vendor_code"]="";
					
					$_REQUEST["refno"] = "";
					$_REQUEST["remark"] = "";
			
			}
			else {
				
				displaymessage($error_msg,"FAIL");
			}
			
		}
		
		
		
		
		
	}
	else {
		
		$_REQUEST["date"] = $_SESSION["PURCHASE_ORDER_HEADER"]["DATE"];
		$_REQUEST["warehouse_code"]=$_SESSION["PURCHASE_ORDER_HEADER"]["WAREHOUSE"];
		$_REQUEST["customer_code"]=$_SESSION["PURCHASE_ORDER_HEADER"]["CUSTOMER"];
		$_REQUEST["vendor_code"]=$_SESSION["PURCHASE_ORDER_HEADER"]["VENDOR"];
		$_REQUEST["refno"] = $_SESSION["PURCHASE_ORDER_HEADER"]["REFNO"];
		$_REQUEST["remark"] = $_SESSION["PURCHASE_ORDER_HEADER"]["REMARK"];
	}

	//============================================================================================

	echo "<div style='float:left;width:96%;padding:3px 2% 3px 2%;'>";
		//echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:left;'  onclick=\"mon_submit_form('".$this_form_name."','".$this_url."')\" >";
			//echo "<i class='fa fa-square fa-stack-2x color_grey'  ></i><i class='fa fa-save fa-stack-1x color_white' ></i>";
		//echo "</div>";
		
		echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:left;'  onclick=\"if(confirm('Are you sure?')){document.".$this_form_name.".ui_action.value='POST';mon_submit_form('".$this_form_name."','".$this_url."')}\" >";
			echo "<i class='fa fa-square fa-stack-2x color_blue'  ></i><i class='fa fa-bolt fa-stack-1x color_white' ></i>";
		echo "</div>";
		
		//echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;'  onclick=\"mon_refresh_tab(mon_current_selected_tab);\" >";
			//echo "<i class='fa fa-square fa-stack-2x color_red'   ></i><i class='fa fa-refresh fa-stack-1x color_white' ></i>";
		//echo "</div>";

	echo "</div>";
	
	
	//============================================================================================
	echo "<div style='float:left;width:96%;padding:3px 2% 3px 2%;'>";
	
		echo "<div class='form_field_separator' >";
			echo "<font size=3><b>".$this_module_title."</b></font><br><br>";
		echo "</div>";
			
		//--------------Date------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Date</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				echo "<input type=\"text\" class=\"textbox textbox-s\" name=\"date\"  value=\"".$_REQUEST["date"]."\"  >";		
				
				echo "<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fPopCalendar(document.".$this_form_name.".date);"."return false;\" HIDEFOCUS>";
				echo " <span class='fa fa-calendar fa-lg' style='color:#000;'></span>";
				echo "</a>"; 
				
			echo "</div>";	
		echo "</div>";	
		
		/*
		//--------------Warehouse------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Warehouse</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				
				echo "<select class='dropdown dropdown-auto' name='warehouse_code'>";
				
					$query_whs = "select * from t_".$key_table_prefix."_warehouse";
					$result_whs = mysqli_query($dbconn,$query_whs);
					while($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
					{
						
						if($_REQUEST["warehouse_code"]==$line_whs["whs_code"])
						{
							echo "<option value='".$line_whs["whs_code"]."' selected >";
						}
						else {
							echo "<option value='".$line_whs["whs_code"]."'>";
						}
						
						echo $line_whs["whs_code"];
						echo " ";
						echo $line_whs["whs_name"];
						
						echo "</option>";
						
						
					}
					
				
				echo "</select>";
				
				
			echo "</div>";	
		echo "</div>";		
		*/
		
		/*
		//--------------Customer------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Customer</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				
				echo "<select class='dropdown dropdown-auto' name='customer_code'>";
					echo "<option value=''></option>";
					$query_cus = "select * from t_".$key_table_prefix."_customer";
					$result_cus = mysqli_query($dbconn,$query_cus);
					while($line_cus = mysqli_fetch_array($result_cus,MYSQLI_ASSOC))
					{
						
						if($_REQUEST["customer_code"]==$line_cus["cus_code"])
						{
							echo "<option value='".$line_cus["cus_code"]."' selected >";
						}
						else {
							echo "<option value='".$line_cus["cus_code"]."'>";
						}
						
						echo $line_cus["cus_code"];
						echo " ";
						echo $line_cus["cus_name"];
						
						echo "</option>";
						
						
					}
					
				
				echo "</select>";
				
				
			echo "</div>";	
		echo "</div>";		
		*/
		
		
		//--------------Vendor------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Vendor</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				
				echo "<select class='dropdown dropdown-auto' name='vendor_code'>";
					echo "<option value=''></option>";
					$query_vdr = "select * from t_".$key_table_prefix."_vendor";
					$result_vdr = mysqli_query($dbconn,$query_vdr);
					while($line_vdr = mysqli_fetch_array($result_vdr,MYSQLI_ASSOC))
					{
						
						if($_REQUEST["vendor_code"]==$line_vdr["vdr_code"])
						{
							echo "<option value='".$line_vdr["vdr_code"]."' selected >";
						}
						else {
							echo "<option value='".$line_vdr["vdr_code"]."'>";
						}
						
						echo $line_vdr["vdr_code"];
						echo " ";
						echo $line_vdr["vdr_name"];
						
						echo "</option>";
						
						
					}
					
				
				echo "</select>";
				
				
			echo "</div>";	
		echo "</div>";		
		
		//--------------Reference No.------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Reference No.</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				echo "<input type=\"text\" class=\"textbox textbox-l\" name=\"refno\"  value=\"".$_REQUEST["refno"]."\"  >";		
			echo "</div>";	
		echo "</div>";	
		
			
		/*	
		//--------------Category------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Category.</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
			echo "<select class='dropdown dropdown-auto' name='category_code'>";
				echo "<option value=''></option>";
				$query_cat = "select * from t_".$key_table_prefix."_stock_transaction_category";
				$result_cat = mysqli_query($dbconn,$query_cat);
				while($line_cat = mysqli_fetch_array($result_cat,MYSQLI_ASSOC))
				{
					
					if($_REQUEST["category_code"]==$line_cat["stc_code"])
					{
						echo "<option value='".$line_cat["stc_code"]."' selected >";
					}
					else {
						echo "<option value='".$line_cat["stc_code"]."'>";
					}
					
					echo $line_cat["stc_code"];
					echo " ";
					echo $line_cat["stc_name"];
					
					echo "</option>";
					
					
				}
				
			
			echo "</select>";
			echo "</div>";	
		echo "</div>";		
		*/
		
		
		//--------------Remark------------------------	
		echo "<div class='form_field_row' >";		
			echo "<div class='form_field_label' >";
				echo "<b>Remark</b>";			
			echo "</div>";
			
			echo "<div class='form_field_input' >";
				echo "<textarea  class=\"textarea textarea-l\" name=\"remark\"  >".$_REQUEST["remark"]."</textarea>";		
			echo "</div>";	
		echo "</div>";		
		
			
			
	//==================Item Level======================================	
			
		echo "<div class='data_display_separator' >";
			echo "<font size=3><b>Transaction Item</b></font>";
			
			//add item button
			echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;' onclick=\"document.".$this_form_name.".ui_action.value='ADD_ITEM1';mon_submit_form('".$this_form_name."','".$this_url."')\" >";
				echo "<i class='fa fa-square fa-stack-2x color_green'   ></i><i class='fa fa-plus fa-stack-1x color_white' ></i>";
			echo "</div>";
			
		echo "</div>";	
	 
	 
	 
		
       //===Header Label =============
       $f_label[1]="Product Code";
       $f_label[2]="Name";
       $f_label[3]="Additional Info";
       $f_label[4]="Unit Price";
       $f_label[5]="Quantity";
       $f_label[6]="Tax";
       $f_label[7]="Before Tax";
       $f_label[8]="Tax Amount";
       $f_label[9]="Total Amount";
	  $f_label[10]="Action";
       
	  
	 
		//===Column width==========
   	  $f_width[1]="100";
   	  $f_width[2]="200";
   	  $f_width[3]="100";
   	  $f_width[4]="80";
   	  $f_width[5]="80";
   	  $f_width[6]="80";
   	  $f_width[7]="80";
   	  $f_width[8]="80";
	  $f_width[9]="80";
	  $f_width[10]="40";

   	  //===Column align==========
	  $f_align[1]="left";
   	  $f_align[2]="left";
   	  $f_align[3]="left";
   	  $f_align[4]="right";
   	  $f_align[5]="left";
   	  $f_align[6]="left";
   	  $f_align[7]="right";
   	  $f_align[8]="right";
	  $f_align[9]="right";
	  $f_align[10]="center";
	  
	 	//--------------Header Labels------------------------
		echo "<div class='view_list_title_row' style='margin-top:20px;'>";
     	
     		
     			echo "<div class='view_list_title_content'>";
				
					$f=1;
				
     				echo "<div style='min-width:20px;' class='view_list_title_row_count'  >";
     					echo "#";
     				echo "</div>";
     				
     				
					foreach($f_label as $key_f=>$value_f)
					{
						echo "<div style='width:".$f_width[$key_f]."px;text-align:".$f_align[$f]."' class='view_list_title_content_object'  >";
								echo "".$f_label[$key_f]."<br>";
						echo "</div>";
						$f++;
						
					}
					
					
     					
     			echo "</div>";
     	echo "</div>";
		
		
		
		
		
		
		
		
		$row_counter=1;
		foreach($_SESSION["PURCHASE_ORDER_ITEM"] as $ts=>$item_data)
		{
			echo "<div class='view_row'>";
		
				$f=1;
				echo "<div style='min-width:20px;' class='view_row_count'  >";
					echo "".$row_counter."";
				echo "</div>";
			
			
			
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo $item_data["CODE"];
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo $item_data["NAME"];
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						if($item_data["COLOR"]!="")
						{
							echo "Color: ".$item_data["COLOR"]."<br>";
						}
						if($item_data["SIZE"]!="")
						{
							echo "Size: ".$item_data["SIZE"]."<br>";
						}
						if($item_data["PATTERN"]!="")
						{
							echo "Pattern: ".$item_data["PATTERN"]."<br>";
						}
						
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text' >";
						echo $item_data["PRICE"];
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo $item_data["QTY"];
						echo " ".$item_data["UOM"];
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						if($item_data["GST_CODE"]!="")
						{
							echo $item_data["GST_CODE"]." ";
							echo "(".$item_data["GST_PERCENTAGE"]."%)<br>";
							echo "<i>".$item_data["GST_MODE"]."</i>";
						}
						
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo number_format($item_data["BEFORE_GST"],2,".",",");
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo number_format($item_data["GST_AMOUNT"],2,".",",");
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						echo number_format($item_data["TOTAL_AMOUNT"],2,".",",");
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				
				
				echo "<div style='width:".$f_width[$f]."px;text-align:".$f_align[$f].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$f_label[$f]."</font>";
					echo "<font class='view_row_content_text'>";
						
						echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;' onclick=\"document.".$this_form_name.".ui_action.value='REMOVE_ITEM';
																						document.".$this_form_name.".remove_item_ts.value='".$ts."';
																						
																						mon_submit_form('".$this_form_name."','".$this_url."')\" >";
							echo "<i class='fa fa-square fa-stack-2x color_red'   ></i><i class='fa fa-times fa-stack-1x color_white' ></i>";
						echo "</div>";
					echo "</font><br>";
				echo "</div>";
				$f++;
				
				$row_counter++;
				
				
			echo "</div>";
		}
			
		
		
		
		
		
		
		
	
	echo "</div>";
echo "</form>";
echo "</div>";


?>


<?php include "../../includes/end_ajax.php";?>