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




if($_REQUEST["y"]!="")
{
	$_SESSION["stock_delegate_calendar_y"]=$_REQUEST["y"];
}

if($_REQUEST["m"]!="")
{
	$_SESSION["stock_delegate_calendar_m"]=$_REQUEST["m"];
}

if($_REQUEST["d"]!="")
{
	$_SESSION["stock_delegate_calendar_d"]=$_REQUEST["d"];
}






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

if($_SESSION["stock_delegate_calendar_y"]=="")
{
	$_SESSION["stock_delegate_calendar_y"]=date("Y");
}

if($_SESSION["stock_delegate_calendar_m"]=="")
{
	$_SESSION["stock_delegate_calendar_m"]=date("m");
}

if($_SESSION["stock_delegate_calendar_d"]=="")
{
	$_SESSION["stock_delegate_calendar_d"]=date("d");
}






$selected_date_start=$_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]." 00:00:00";
$selected_date_end=$_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]." 23:59:59";


$selected_date_start_simple=$_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";
$selected_date_end_simple=$_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";



$selected_date = $_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";




    echo "<section class='view_toolbar'>";
                    echo "<div class='view_toolbar_left' style='width:50%;'>";
                            echo "<font size=5 ><span class='fa fa-share-alt fa-1x'></span> <b>Daily Stock Delegation</b></font>";

                            echo "<br>";
                           
                    echo "</div>";
                    echo "<div class='view_toolbar_right'>";




				
						
                              echo " <input type=button class='button button-blue' value='Refresh' onclick=\"mon_send_get('mod/stock_delegation/index.php','','div_main')\">";


                    echo "</div>";
    echo "</section>";







if ($_SERVER[REQUEST_METHOD] == 'POST') {
	
	$error_message="";
	
	
	
	if($_REQUEST["product_id"]=="")
	{
		$error_message=$error_message."<li>Invalid Product</li>";
	}
	
	if($_REQUEST["quantity"]=="")
	{
		$error_message=$error_message."<li>Quantity not specify</li>";
	}
	
	if($_REQUEST["user_action"]=="")
	{
		$error_message=$error_message."<li>Invalid Action. Close Panel and try again</li>";
	}
	
	if($error_message=="")
	{
		
		
		$_SESSION["delegate_draft_".$selected_date_start_simple][$_REQUEST["product_id"]][$_REQUEST["user_action"]] = abs($_REQUEST["quantity"]);
		
		
		//hide window
		$_SESSION["selected_delegate_product"]="";
		
	}
	else {
		
		displaymessage($error_message,"FAIL");
		
	}
	
}




if(($_REQUEST["remove_pdt_id"]!="")and($_REQUEST["remove_action"]!=""))
{
	unset($_SESSION["delegate_draft_".$selected_date_start_simple][$_REQUEST["remove_pdt_id"]][$_REQUEST["remove_action"]]);
	
}








if($_REQUEST["post_document"]!="")
{
	
	$error_message="";
	
	
	
	
	if($_SESSION["list_filter"]["filter_stock_delegate_from"]=="")
	{
		$error_message=$error_message."<li>From Warehouse not specify</li>";
	}
	
	
	if($_SESSION["list_filter"]["filter_stock_delegate_to"]=="")
	{
		$error_message=$error_message."<li>Delegate to Warehouse not specify</li>";
	}
	

	
	//collecting data:
	foreach($_SESSION["delegate_draft_".$selected_date_start_simple] as $pdt_id => $pdt_data)
	{
	
		foreach($_SESSION["delegate_draft_".$selected_date_start_simple][$pdt_id] as $action_key => $user_quantity)
		{	
			$to_post[$action_key][$pdt_id]["QTY"]=$user_quantity;
		}	
		
	}
	
	
	$item_found=false;

	if(count($to_post["PLUS"])>0)
	{
		$item_found = true;
		
	}
	
	
	if(count($to_post["MINUS"])>0)
	{
		$item_found = true;
		
	}



	if($item_found)
	{
		
		
	}
	else {
		
		$error_message=$error_message."<li>No Quantity Input</li>";
		
	}



	if($error_message=="")
	{
		
	
					$this_doc_prefix="ST";
					$this_doc_number_module_code="ST"; //stock transfer
					$this_module="STOCK_TRANSFER";
				
				
					$this_inventory_posting_type="MINUS";
					$this_inventory_posting_type2="PLUS";
					
					
					
					$display_message="";
				
					if(count($to_post["PLUS"])>0)
					{
						
						
						$this_posting_date = $_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";
						
				
						$this_warehouse_from_id=0;
						$this_warehouse_to_id=0;
						
						
						
						//get info:
						$query_whs="select * from t_".$key_table_prefix."_warehouse 
							where whs_code  ='".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ";
						$result_whs  = mysqli_query($dbconn,$query_whs);
						if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
						{
							$this_warehouse_from_id = $line_whs["whs_id"];
						}
						
						//get info:
						$query_whs="select * from t_".$key_table_prefix."_warehouse 
							where whs_code  ='".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ";
						$result_whs  = mysqli_query($dbconn,$query_whs);
						if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
						{
							$this_warehouse_to_id = $line_whs["whs_id"];
						}
						
						
						
						
						$this_doc_no = mon_register_document_number($this_doc_number_module_code);
						
						
						$query="insert into t_".$key_table_prefix."_stock_transaction_header 
								(
								sth_doc_prefix  ,
								sth_doc_no  ,
								sth_module  ,
								sth_date ,
								
								sth_warehouse_code  ,
								sth_warehouse_id  ,
								
								sth_warehouse_code2  ,
								sth_warehouse_id2  ,
								
								sth_customer_code  ,
								sth_customer_name  ,
								sth_vendor_code  ,
								sth_vendor_name  ,
								sth_reference_no  ,
								sth_remark  ,
								
								sth_category_code  ,
								
								
								sth_status  ,
								sth_before_gst_amount ,
								sth_gst_amount  ,
								sth_total_amount  ,
								
								
								sth_driver_code, 
								sth_staff1_code, 
								sth_staff2_code, 
								
								created_date  ,
								created_by  ,
								modified_date  ,
								modified_by  ,
								client_id  
								)
								values
								(
								'".$this_doc_prefix."'  ,
								'".$this_doc_no."'  ,
								'".$this_module."'  ,
								'".$this_posting_date."' ,
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ,
								'".$this_warehouse_from_id."'  ,
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ,
								'".$this_warehouse_to_id."'  ,
								
								
								'' ,
								''  ,
								'' ,
								''  ,
								'Daily Stock Delegation'  ,
								'Daily Stock Delegation'  ,
								
								'".$_REQUEST["category"]."'  ,
								
								
								'POSTED'  ,
								'0' ,
								'0' ,
								'0' ,
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_driver"]."',
								'".$_SESSION["list_filter"]["filter_stock_delegate_assistant1"]."',
								'".$_SESSION["list_filter"]["filter_stock_delegate_assistant2"]."',
								
								
								'".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($to_post["PLUS"] as $pdt_id => $data1)
						{
							
							
							
							$query_product = "select * 
										from t_".$key_table_prefix."_product 
										where pdt_id = '".$pdt_id."' ";
							$result_product = mysqli_query($dbconn,$query_product);
							if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
							{
								
								
								
								
								$query_item="insert into t_".$key_table_prefix."_stock_transaction_item 
										(
											
										  sti_header_id  ,
										  sti_doc_prefix  ,
										  sti_doc_no  ,
										  sti_module  ,
										  sti_date  ,
										  sti_warehouse_code  ,
										  sti_warehouse_id  ,
										  
										  sti_warehouse_code2  ,
										  sti_warehouse_id2  ,
										  
										  
										  sti_stock_code  ,
										  sti_barcode  ,
										  sti_factory_code  ,
										  sti_stock_name  ,
										  sti_color  ,
										  sti_size  ,
										  sti_pattern  ,
										  sti_uom  ,
										  sti_base_uom  ,
										  sti_quantity  ,
										  sti_base_quantity  ,
										  sti_conversion  ,
										  sti_unit_cost  ,
										  sti_base_unit_cost  ,
										  sti_total_cost  ,
										  sti_gst_mode  ,
										  sti_gst_code  ,
										  sti_gst_percentage  ,
										  sti_remark  ,
										  sti_status  ,
										  sti_before_gst_amount  ,
										  sti_gst_amount  ,
										  sti_total_amount  ,
										  
										  sti_storage_location  ,
										  sti_storage_location2  ,
										  
										  
										  created_date  ,
										  created_by  ,
										  modified_date  ,
										  modified_by  ,
										  client_id  
										)
										values
										(
										'".$header_id."'  ,
										
										'".$this_doc_prefix."'  ,
										'".$this_doc_no."'  ,
										'".$this_module."'  ,
										'".$this_posting_date."' ,
										'".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ,
										'".$this_warehouse_from_id."'  ,
										
										'".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ,
										'".$this_warehouse_to_id."'  ,
										
										
										  '".$line_product["pdt_code"]."'  ,
										  '".$line_product["pdt_barcode"]."'  ,
										  ''  ,
										  '".$line_product["pdt_name"]."'  ,
										  ''  ,
										  ''  ,
										  ''  ,
										  '".$line_product["pdt_uom"]."'  ,
										  '".$line_product["pdt_uom"]."'  ,
										  '".($to_post["PLUS"][$pdt_id]["QTY"]+0)."'  ,
										  '".($to_post["PLUS"][$pdt_id]["QTY"]+0)."'  ,
										  '1'  ,
										  '".($line_product["pdt_cost"]+0)."'  ,
										  '".($line_product["pdt_cost"]+0)."'  ,
										  '".(($line_product["pdt_cost"]*$to_post["PLUS"][$pdt_id]["QTY"])+0)."'  ,
										  ''  ,
										  ''  ,
										  '".(0)."'  ,
										  ''  ,
										  'POSTED'  ,
										  '".(($line_product["pdt_cost"]*$to_post["PLUS"][$pdt_id]["QTY"])+0)."'  ,
										  '".(0)."'  ,
										  '".(($line_product["pdt_cost"]*$to_post["PLUS"][$pdt_id]["QTY"])+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));
								$last_item_id = mysqli_insert_id($dbconn);
								
								
								
								//------------------------Part 1: minus:-------------------------
								post_inventory_balance(
										'SERVER',
										$this_posting_date,
										$this_doc_no, 
										$last_item_id,
										
										$this_module, 
										
										$this_inventory_posting_type, 
										$_SESSION["list_filter"]["filter_stock_delegate_from"], 
										$line_product["pdt_code"], 
										$line_product["pdt_uom"], 
										$to_post["PLUS"][$pdt_id]["QTY"], 
										"",
										"", 
										""
										);
										
												
												
												
								//------------------------Part 2: plus:-------------------------
								post_inventory_balance(
										'SERVER',
										$this_posting_date,
										$this_doc_no, 
										$last_item_id,
										
										$this_module, 
										
										$this_inventory_posting_type2, 
										$_SESSION["list_filter"]["filter_stock_delegate_to"], 
										$line_product["pdt_code"], 
										$line_product["pdt_uom"], 
										$to_post["PLUS"][$pdt_id]["QTY"], 
										"",
										"", 
										""
										);
								
								
								
								
							}
							
											
							
						}
						
						
						
						
						
						
						
						
						
						
						$display_message=$display_message."here1";
						
						
					}
					
					
					if(count($to_post["MINUS"])>0)
					{
						
						
						$this_posting_date = $_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";
						
				
						$this_warehouse_from_id=0;
						$this_warehouse_to_id=0;
						
						
						
						//get info:
						$query_whs="select * from t_".$key_table_prefix."_warehouse 
							where whs_code  ='".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ";
						$result_whs  = mysqli_query($dbconn,$query_whs);
						if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
						{
							$this_warehouse_from_id = $line_whs["whs_id"];
						}
						
						//get info:
						$query_whs="select * from t_".$key_table_prefix."_warehouse 
							where whs_code  ='".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ";
						$result_whs  = mysqli_query($dbconn,$query_whs);
						if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
						{
							$this_warehouse_to_id = $line_whs["whs_id"];
						}
						
						
						
						
						$this_doc_no = mon_register_document_number($this_doc_number_module_code);
						
						
						$query="insert into t_".$key_table_prefix."_stock_transaction_header 
								(
								sth_doc_prefix  ,
								sth_doc_no  ,
								sth_module  ,
								sth_date ,
								
								sth_warehouse_code  ,
								sth_warehouse_id  ,
								
								sth_warehouse_code2  ,
								sth_warehouse_id2  ,
								
								sth_customer_code  ,
								sth_customer_name  ,
								sth_vendor_code  ,
								sth_vendor_name  ,
								sth_reference_no  ,
								sth_remark  ,
								
								sth_category_code  ,
								
								sth_status  ,
								sth_before_gst_amount ,
								sth_gst_amount  ,
								sth_total_amount  ,
								
								
								sth_driver_code, 
								sth_staff1_code, 
								sth_staff2_code, 
								
								
								created_date  ,
								created_by  ,
								modified_date  ,
								modified_by  ,
								client_id  
								)
								values
								(
								'".$this_doc_prefix."'  ,
								'".$this_doc_no."'  ,
								'".$this_module."'  ,
								'".$this_posting_date."' ,
								
								
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ,
								'".$this_warehouse_to_id."'  ,
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ,
								'".$this_warehouse_from_id."'  ,
								
								
								'' ,
								''  ,
								'' ,
								''  ,
								'Daily Stock Delegation (Return)'  ,
								'Daily Stock Delegation (Return)'  ,
								
								'".$_REQUEST["category"]."'  ,
								
								'POSTED'  ,
								'0' ,
								'0' ,
								'0' ,
								
								
								
								'".$_SESSION["list_filter"]["filter_stock_delegate_driver"]."',
								'".$_SESSION["list_filter"]["filter_stock_delegate_assistant1"]."',
								'".$_SESSION["list_filter"]["filter_stock_delegate_assistant2"]."',
								
								
								'".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($to_post["MINUS"] as $pdt_id => $data1)
						{
							
							
							
							$query_product = "select * 
										from t_".$key_table_prefix."_product 
										where pdt_id = '".$pdt_id."' ";
							$result_product = mysqli_query($dbconn,$query_product);
							if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
							{
								
								
								
								
								$query_item="insert into t_".$key_table_prefix."_stock_transaction_item 
										(
											
										  sti_header_id  ,
										  sti_doc_prefix  ,
										  sti_doc_no  ,
										  sti_module  ,
										  sti_date  ,
										  sti_warehouse_code  ,
										  sti_warehouse_id  ,
										  
										  sti_warehouse_code2  ,
										  sti_warehouse_id2  ,
										  
										  
										  sti_stock_code  ,
										  sti_barcode  ,
										  sti_factory_code  ,
										  sti_stock_name  ,
										  sti_color  ,
										  sti_size  ,
										  sti_pattern  ,
										  sti_uom  ,
										  sti_base_uom  ,
										  sti_quantity  ,
										  sti_base_quantity  ,
										  sti_conversion  ,
										  sti_unit_cost  ,
										  sti_base_unit_cost  ,
										  sti_total_cost  ,
										  sti_gst_mode  ,
										  sti_gst_code  ,
										  sti_gst_percentage  ,
										  sti_remark  ,
										  sti_status  ,
										  sti_before_gst_amount  ,
										  sti_gst_amount  ,
										  sti_total_amount  ,
										  
										  sti_storage_location  ,
										  sti_storage_location2  ,
										  
										  
										  created_date  ,
										  created_by  ,
										  modified_date  ,
										  modified_by  ,
										  client_id  
										)
										values
										(
										'".$header_id."'  ,
										
										'".$this_doc_prefix."'  ,
										'".$this_doc_no."'  ,
										'".$this_module."'  ,
										'".$this_posting_date."' ,
										
										
										'".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' ,
										'".$this_warehouse_to_id."'  ,
										
										'".$_SESSION["list_filter"]["filter_stock_delegate_from"]."' ,
										'".$this_warehouse_from_id."'  ,
										
										
										  '".$line_product["pdt_code"]."'  ,
										  '".$line_product["pdt_barcode"]."'  ,
										  ''  ,
										  '".$line_product["pdt_name"]."'  ,
										  ''  ,
										  ''  ,
										  ''  ,
										  '".$line_product["pdt_uom"]."'  ,
										  '".$line_product["pdt_uom"]."'  ,
										  '".($to_post["MINUS"][$pdt_id]["QTY"]+0)."'  ,
										  '".($to_post["MINUS"][$pdt_id]["QTY"]+0)."'  ,
										  '1'  ,
										  '".($line_product["pdt_cost"]+0)."'  ,
										  '".($line_product["pdt_cost"]+0)."'  ,
										  '".(($line_product["pdt_cost"]*$to_post["MINUS"][$pdt_id]["QTY"])+0)."'  ,
										  ''  ,
										  ''  ,
										  '".(0)."'  ,
										  ''  ,
										  'POSTED'  ,
										  '".(($line_product["pdt_cost"]*$to_post["MINUS"][$pdt_id]["QTY"])+0)."'  ,
										  '".(0)."'  ,
										  '".(($line_product["pdt_cost"]*$to_post["MINUS"][$pdt_id]["QTY"])+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));
								$last_item_id = mysqli_insert_id($dbconn);
								
								
								
								//------------------------Part 1: minus:-------------------------
								post_inventory_balance(
										'SERVER',
										$this_posting_date,
										$this_doc_no, 
										$last_item_id,
										
										$this_module, 
										
										$this_inventory_posting_type, 
										$_SESSION["list_filter"]["filter_stock_delegate_to"], 
										$line_product["pdt_code"], 
										$line_product["pdt_uom"], 
										$to_post["MINUS"][$pdt_id]["QTY"], 
										"",
										"", 
										""
										);
										
												
												
												
								//------------------------Part 2: plus:-------------------------
								post_inventory_balance(
										'SERVER',
										$this_posting_date,
										$this_doc_no, 
										$last_item_id,
										
										$this_module, 
										
										$this_inventory_posting_type2, 
										$_SESSION["list_filter"]["filter_stock_delegate_from"], 
										$line_product["pdt_code"], 
										$line_product["pdt_uom"], 
										$to_post["MINUS"][$pdt_id]["QTY"], 
										"",
										"", 
										""
										);
								
								
								
								
							}
							
											
							
						}
						
						
						
						
						
						
						
						
						
						$display_message=$display_message."here2";
						
					}

		
					unset($_SESSION["delegate_draft_".$selected_date_start_simple]);
				
					
					displaymessage("Posted","CREATED");
					
		
	}
	else {
		
		displaymessage($error_message,"FAIL");
	}
	
	
	



	
	
	
}















if($_REQUEST["selected_delegate_product"]!="")
{
	if($_REQUEST["selected_delegate_product"]=="CANCEL")
	{
		$_SESSION["selected_delegate_product"]="";
	}
	else {
		$_SESSION["selected_delegate_product"]=$_REQUEST["selected_delegate_product"];
	}
	
}


if($_REQUEST["warehouse_from"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_from"] = $_REQUEST["warehouse_from"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_from"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_from"] = "";
}


if($_REQUEST["warehouse_to"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_to"] = $_REQUEST["warehouse_to"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_to"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_to"] = "";
}




if($_REQUEST["driver"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_driver"] = $_REQUEST["driver"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_driver"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_driver"] = "";
}



if($_REQUEST["assistant1"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_assistant1"] = $_REQUEST["assistant1"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_assistant1"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_assistant1"] = "";
}




if($_REQUEST["assistant2"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_assistant2"] = $_REQUEST["assistant2"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_assistant2"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_assistant2"] = "";
}



if($_REQUEST["category"]!="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_category"] = $_REQUEST["category"];
}
if($_SESSION["list_filter"]["filter_stock_delegate_category"]=="")
{
	$_SESSION["list_filter"]["filter_stock_delegate_category"] = "";
}








if(($_SESSION["list_filter"]["filter_stock_delegate_from"]!="")and($_SESSION["list_filter"]["filter_stock_delegate_to"]!=""))
{
	
	if($_SESSION["list_filter"]["filter_stock_delegate_from"]==$_SESSION["list_filter"]["filter_stock_delegate_to"])
	{
		displaymessage("From and To warehouse cannot be the same.","FAIL");
		$_SESSION["list_filter"]["filter_stock_delegate_to"]="";
	}
	
	
}





    echo "<section class='view_header'>";

    //echo $_SESSION["data_site"];
    echo "</section>";


    echo "<section class='view_content'>";



$month_text[1]="JAN";
$month_text[2]="FEB";
$month_text[3]="MAR";
$month_text[4]="APR";
$month_text[5]="MAY";
$month_text[6]="JUN";
$month_text[7]="JUL";
$month_text[8]="AUG";
$month_text[9]="SEP";
$month_text[10]="OCT";
$month_text[11]="NOV";
$month_text[12]="DEC";



$month_text2[1]="January";
$month_text2[2]="February";
$month_text2[3]="March";
$month_text2[4]="April";
$month_text2[5]="May";
$month_text2[6]="June";
$month_text2[7]="July";
$month_text2[8]="August";
$month_text2[9]="September";
$month_text2[10]="October";
$month_text2[11]="November";
$month_text2[12]="December";


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




//device library:
$query_device="select * from t_".$key_table_prefix."_device ";
$result_device = mysqli_query($dbconn,$query_device);
while($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
{
	
	$device_data[$line_device["dev_android_id"]]=$line_device["dev_name"];
}



//device library:
$query_staff="select * from t_".$key_table_prefix."_staff ";
$result_staff = mysqli_query($dbconn,$query_staff);
while($line_staff = mysqli_fetch_array($result_staff,MYSQLI_ASSOC))
{
	
	$staff_data[$line_staff["stf_code"]]=$line_staff["stf_name"];
}








echo "<div class='cal_menu_panel'>";
    echo "<div class='cal_menu_box'>";
    for($y=2010;$y<=2030;$y++)
    {
        if($_SESSION["stock_delegate_calendar_y"]==$y)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/stock_delegation/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&y=".$y."".$tab_extra_url."','div_main')\">".$y."</div>";
    }
    echo "</div>";

    echo "<div class='cal_menu_box'>";
    for($m=1;$m<=12;$m++)
    {
        if($_SESSION["stock_delegate_calendar_m"]==$m)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/stock_delegation/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&m=".substr($m+100,1,2)."".$tab_extra_url."','div_main')\" >".$month_text[$m]."</div>";
    }
    echo "</div>";


    echo "<div class='cal_menu_box'>";
    for($d=1;$d<=date("t",mktime(0,0,0,$_REQUEST["m"]+0,1,$_REQUEST["y"]));$d++)
    {

        if($_SESSION["stock_delegate_calendar_d"]==$d)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/stock_delegation/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&d=".substr($d+100,1,2)."".$tab_extra_url."','div_main')\" >".$d."</div>";
    }
    echo "</div>";



echo "</div>";




echo "<font size=1 color=#34495E style='float:left;width:100%;' >Showing data of period ".$selected_date_start." - ".$selected_date_end."</font><br><br><br>";


echo "<br>&nbsp;";


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

	//Staff assignment:
	echo "<table border=0 bgcolor=#e4e9ed  width=98%  cellpadding=5 cellspacing=1 align=center >";
		echo "<tr>";
			echo "<td align=left  >";
				echo "<span class='fa fa-building-o fa-lg'></span> From: ";
				echo "<select class='dropdown dropdown-auto' name='warehouse_from' 
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?warehouse_from='+this.value,'div_main')\"
						>";
				
					
					
					
					
					echo "<option value=''>-- Select Warehouse --</option>";
					
					
					$query_whs="select * from t_".$key_table_prefix."_warehouse order by whs_code ; ";
					$result_whs =mysqli_query($dbconn,$query_whs);
					
					while($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
					{
						
						if($_SESSION["list_filter"]["filter_stock_delegate_from"]==$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 "</td>";
			
			
			echo "<td width=50 >";
				echo "<span class='fa fa-angle-double-right'></span>";
			echo "</td>";
			
			echo "<td align=right   >";
					
					echo "<span class='fa fa-truck fa-lg'></span> Delegate to: ";
					echo "<select class='dropdown dropdown-auto' name='warehouse_to' 
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?warehouse_to='+this.value,'div_main')\"
						>";
					
						
						
						
						
						echo "<option value=''>-- Select Warehouse --</option>";
						
						
						$query_whs="select * from t_".$key_table_prefix."_warehouse order by whs_code ; ";
						$result_whs =mysqli_query($dbconn,$query_whs);
						
						while($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
						{
							
							if($_SESSION["list_filter"]["filter_stock_delegate_to"]==$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 "</td>";
			
			
			echo "<td align=right width=300>";
				
					
					echo "<span class='fa fa-tags fa-lg '></span> Category: ";
					echo "<select class='dropdown dropdown-auto' name='category' id='stock_delegate_category'
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?category='+this.value,'div_main')\"
							>";
					$query_stock_category="select * 
									from t_".$key_table_prefix."_stock_transaction_category ";
					$result_stock_category = mysqli_query($dbconn,$query_stock_category);
					
					while($line_stock_category= mysqli_fetch_array($result_stock_category,MYSQLI_ASSOC))
					{
						if($_SESSION["list_filter"]["filter_stock_delegate_category"]==$line_stock_category["stc_code"])
						{
							echo "<option value='".$line_stock_category["stc_code"]."' selected >";
						}
						else {
							echo "<option value='".$line_stock_category["stc_code"]."'>";
							
						}
						
						
							echo $line_stock_category["stc_name"];
						echo "</option>";
						
					}
					
					echo "</select>";
					
					
			echo "</td>";
			
			
			
			
			echo "<td width=200>";
				
				
			
				echo " <input type=button class='button button-blue' value='Post' onclick=\"if(confirm('Are you sure?')){mon_send_get('mod/stock_delegation/index.php','?post_document=YES&category='+document.getElementById('stock_delegate_category').value,'div_main')}\">";

			echo "</td>";
			
			
			
			
		echo "</tr>";
		
	echo "</table>";





	echo "<br>";
//=====================================================================================================================


	//Staff assignment:

	echo "<table border=0 bgcolor=#e4e9ed  width=98%  cellpadding=5 cellspacing=1 align=center >";
		echo "<tr>";
			echo "<td align=left width=250 >";
				echo "<span class='fa fa-male fa-lg'></span> Driver: ";
				
				echo "<select class='dropdown dropdown-auto' name='driver' id='driver'
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?driver='+this.value,'div_main')\"
						>";
				$query_staff="select * 
								from t_".$key_table_prefix."_staff ";
				$result_staff = mysqli_query($dbconn,$query_staff);
				
				while($line_staff = mysqli_fetch_array($result_staff,MYSQLI_ASSOC))
				{
					if($_SESSION["list_filter"]["filter_stock_delegate_driver"]==$line_staff["stf_code"])
					{
						echo "<option value='".$line_staff["stf_code"]."' selected >";
					}
					else {
						echo "<option value='".$line_staff["stf_code"]."'>";
					}
					
					
						echo $line_staff["stf_code"];
						echo " - ";
						echo $line_staff["stf_name"];
					echo "</option>";
					
				}
				
				echo "</select>";
				
				
				
			echo "</td>";
			
			echo "<td align=left width=250 >";
				echo "<span class='fa fa-male fa-lg'></span> Staff 1: ";
				
				echo "<select class='dropdown dropdown-auto' name='driver' id='driver'
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?assistant1='+this.value,'div_main')\"
						>";
				$query_staff="select * 
								from t_".$key_table_prefix."_staff ";
				$result_staff = mysqli_query($dbconn,$query_staff);
				
				while($line_staff = mysqli_fetch_array($result_staff,MYSQLI_ASSOC))
				{
					if($_SESSION["list_filter"]["filter_stock_delegate_assistant1"]==$line_staff["stf_code"])
					{
						echo "<option value='".$line_staff["stf_code"]."' selected >";
					}
					else {
						echo "<option value='".$line_staff["stf_code"]."'>";
					}
					
					
						echo $line_staff["stf_code"];
						echo " - ";
						echo $line_staff["stf_name"];
					echo "</option>";
					
				}
				
				echo "</select>";
				
				
			echo "</td>";
			
			
			echo "<td align=left width=250 >";
				echo "<span class='fa fa-male fa-lg'></span> Staff 2: ";
				
				echo "<select class='dropdown dropdown-auto' name='driver' id='driver'
						onchange=\"mon_send_get('mod/stock_delegation/index.php','?assistant2='+this.value,'div_main')\"
						>";
				$query_staff="select * 
								from t_".$key_table_prefix."_staff ";
				$result_staff = mysqli_query($dbconn,$query_staff);
				
				while($line_staff = mysqli_fetch_array($result_staff,MYSQLI_ASSOC))
				{
					if($_SESSION["list_filter"]["filter_stock_delegate_assistant2"]==$line_staff["stf_code"])
					{
						echo "<option value='".$line_staff["stf_code"]."' selected >";
					}
					else {
						echo "<option value='".$line_staff["stf_code"]."'>";
					}
					
					
						echo $line_staff["stf_code"];
						echo " - ";
						echo $line_staff["stf_name"];
					echo "</option>";
					
				}
				
				echo "</select>";
				
				
			echo "</td>";
		echo "</tr>";
	echo "</table>";
			
	echo "<br>";
//=====================================================================================================================



	
		echo "<table border=0 bgcolor=#e4e9ed  width=98% cellpadding=5 cellspacing=1 align=center >";
			
				
			echo "<tr>";
				echo "<td bgcolor=#e4e9ed width=200 align=center >";
					echo "<span class='fa fa-cubes fa-lg '></span><br><b>Product</b><br>";	
				echo "</td>";
				
				
				echo "<td bgcolor=#e4e9ed width=100  align=center >";
					echo "<span class='fa fa-balance-scale fa-lg '></span><br><b>Openning Balance on<br>".$selected_date_start_simple."</b><br>";	
				echo "</td>";
				
				echo "<td bgcolor=#e4e9ed  align=center >";
					echo "<span class='fa fa-exchange fa-lg '></span><br><b>Stock Movement</b><br>";	
				echo "</td>";
				
				
				echo "<td bgcolor=#e4e9ed width=100  align=center >";
					echo "<span class='fa fa-balance-scale fa-lg '></span><br><b>Balance on<br>".$selected_date_start_simple."</b><br>";	
				echo "</td>";
				
				
			echo "</tr>";
			
			
			
			
			//---------------------------START get balance and stock card-----------------------------------------------------------------
			
			
			
			
			$query_balance="select 
						sum(bal_quantity) as total_quantity ,
						bal_product_code 
						from t_".$key_table_prefix."_balance
						where   bal_warehouse_code = '".$_SESSION["list_filter"]["filter_stock_delegate_to"]."' 
						group by bal_product_code 
						";
			$result_balance=mysqli_query($dbconn,$query_balance);
			while($line_balance=mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
			{
				$lib_balance[$line_balance["bal_product_code"]] = $line_balance["total_quantity"];				
				$lib_open_balance[$line_balance["bal_product_code"]] = $line_balance["total_quantity"];				
			}
			
			
			
			
			$query_card="select 
					sum(abs(stc_base_quantity)) as total_quantity,
					stc_type,
					stc_stock_code,
					stc_doc_no
			
					from t_".$key_table_prefix."_stock_card 
					where  stc_date = '".$selected_date_start_simple."'
					and stc_warehouse_code = '".$_SESSION["list_filter"]["filter_stock_delegate_to"]."'
					group by stc_type ,stc_stock_code,stc_doc_no
					";
			
			$result_card =mysqli_query($dbconn,$query_card);
			while($line_card = mysqli_fetch_array($result_card,MYSQLI_ASSOC))
			{	
				$lib_stock_cart[$line_card["stc_stock_code"]][$line_card["stc_doc_no"]][$line_card["stc_type"]] = $line_card["total_quantity"]+0;
				
				
				if($line_card["stc_type"]=="PLUS")
				{
					$lib_open_balance[$line_card["stc_stock_code"]] = $lib_open_balance[$line_card["stc_stock_code"]] - $line_card["total_quantity"];
					
				}
				if($line_card["stc_type"]=="MINUS")
				{
					$lib_open_balance[$line_card["stc_stock_code"]] = $lib_open_balance[$line_card["stc_stock_code"]] + $line_card["total_quantity"];
					
				}
				
				
			}
			
			
			
			
			
			
			$query_card_after_end ="select 
					sum(abs(stc_base_quantity)) as total_quantity,
					stc_type,
					stc_stock_code
					
			
					from t_".$key_table_prefix."_stock_card 
					where  stc_date > '".$selected_date_start_simple."'
					and stc_warehouse_code = '".$_SESSION["list_filter"]["filter_stock_delegate_to"]."'
					group by stc_type ,stc_stock_code 
					";
			
			$result_card_after_end =mysqli_query($dbconn,$query_card_after_end);
			while($line_card_after_end = mysqli_fetch_array($result_card_after_end,MYSQLI_ASSOC))
			{	
				if($line_card_after_end["stc_type"]=="PLUS")
				{
					$lib_balance[$line_card_after_end["stc_stock_code"]] = $lib_balance[$line_card_after_end["stc_stock_code"]] - $line_card_after_end["total_quantity"];
					$lib_open_balance[$line_card_after_end["stc_stock_code"]] = $lib_open_balance[$line_card_after_end["stc_stock_code"]] - $line_card_after_end["total_quantity"];
					
					
				}
				if($line_card_after_end["stc_type"]=="MINUS")
				{
					$lib_balance[$line_card_after_end["stc_stock_code"]] = $lib_balance[$line_card_after_end["stc_stock_code"]] + $line_card_after_end["total_quantity"];
					$lib_open_balance[$line_card_after_end["stc_stock_code"]] = $lib_open_balance[$line_card_after_end["stc_stock_code"]] + $line_card_after_end["total_quantity"];
					
				}
				
				
				
			}
			
			
			
				
			//---------------------------END get balance and stock card-----------------------------------------------------------------
		
		
			
			
			$query_product="select * from t_".$key_table_prefix."_product
						order by pdt_code ";
			$result_product = mysqli_query($dbconn,$query_product);
			
			while($line_product= mysqli_fetch_array($result_product,MYSQLI_ASSOC))
			{
				$this_product_decimal = $line_product["pdt_decimal_quantity"]+0;
			
				echo "<tr>";
					echo "<td bgcolor=#ffffff width=200 >";
						
						echo "<b>".$line_product["pdt_name"]."</b><br>";
						echo "<font color=#1fbad6 >".$line_product["pdt_code"]."</font>";
					echo "</td>";
					
					
					echo "<td bgcolor=#ffffff width=100 align=right >";
						echo number_format($lib_open_balance[$line_product["pdt_code"]]+0,$this_product_decimal,".",",");
					echo "</td>";
					
					
					
					echo "<td bgcolor=#ffffff >";
					
					
						//-------------------display stock card---------------------------------------------------
					
						foreach($lib_stock_cart[$line_product["pdt_code"]] as $doc_no => $data1)
						{
								
							if($data1["PLUS"]>0)
							{
								echo "<div onclick=\"mon_send_get('mod/stock_transaction/display.php','?sth_doc_no=".$doc_no."','div_display')\" style='float:left;max-width:60px;text-align:center;margin:1px;padding:3px; border-radius:10px;background:#019875;border:2px solid #019875;color:#ffffff;cursor:pointer;'>";
								echo "+";
								echo $lib_stock_cart[$line_product["pdt_code"]][$doc_no]["PLUS"]+0;
								
								echo "</div>";
							}
							
							if($data1["MINUS"]>0)
							{
								echo "<div onclick=\"mon_send_get('mod/stock_transaction/display.php','?sth_doc_no=".$doc_no."','div_display')\" style='float:left;max-width:60px;text-align:center;margin:1px;padding:3px; border-radius:10px;background:#cf000f;border:2px solid #cf000f;color:#ffffff;cursor:pointer;'>";
								echo "-";
								echo $lib_stock_cart[$line_product["pdt_code"]][$doc_no]["MINUS"]+0;
								echo "</div>";
							}
							
						}
						
					
					
						
						//-------------------display stock card---------------------------------------------------
					
						foreach($_SESSION["delegate_draft_".$selected_date_start_simple][$line_product["pdt_id"]] as $action=>$input_quantity)
						{
							if($action=="PLUS")
							{
								echo "<div style='float:left;max-width:60px;text-align:center;margin:1px;padding:3px; border-radius:10px;background:#ffffff;border:2px dotted #019875;'>";
									echo "+".$input_quantity;
									echo " <span class='fa fa-times-circle fa-lg ' style='color:#bdc3c7;' onclick=\"mon_send_get('mod/stock_delegation/index.php','?remove_pdt_id=".$line_product["pdt_id"]."&remove_action=PLUS','div_main')\" ></span>";								
								echo "</div>";
							}
							
							if($action=="MINUS")
							{
								echo "<div style='float:left;max-width:60px;text-align:center;margin:1px;padding:3px; border-radius:10px;background:#ffffff;border:2px dotted #cf000f;'>";
									echo "-".$input_quantity;
									echo " <span class='fa fa-times-circle fa-lg ' style='color:#bdc3c7;' onclick=\"mon_send_get('mod/stock_delegation/index.php','?remove_pdt_id=".$line_product["pdt_id"]."&remove_action=MINUS','div_main')\" ></span>";								
								echo "</div>";
							}
							
						}
						
						//$_SESSION["delegate_draft_".$selected_date_start_simple][$_REQUEST["product_id"]][$_REQUEST["user_action"]] = abs($_REQUEST["quantity"]);
					
					
						
					
					
						echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;'  onclick=\"mon_send_get('mod/stock_delegation/index.php','?selected_delegate_product=".$line_product["pdt_id"]."&user_action=MINUS','div_main');\" >";
						   	echo "<i class='fa fa-square fa-stack-2x color_red'   ></i><i class='fa fa-minus fa-stack-1x color_white' ></i>";
					   	echo "</div>";
						
						echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;'  onclick=\"mon_send_get('mod/stock_delegation/index.php','?selected_delegate_product=".$line_product["pdt_id"]."&user_action=PLUS','div_main');\" >";
						   	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>";
						   
						
						
						
						
						
						
						if($_SESSION["selected_delegate_product"]==$line_product["pdt_id"])
						{
							echo "<div style='position:absolute;border-radius:10px;padding:10px;background:#e4e9ed;box-shadow:0px 0px 3px #888;width:300px;height:200px;'>";   
							
								echo "<form action='javascript:void(0)' name='form_delegate_pop_add' >";
								
								
								
								echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;'  onclick=\"mon_send_get('mod/stock_delegation/index.php','?selected_delegate_product=CANCEL','div_main');\" >";
									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>";
								
								
								if($_REQUEST["user_action"]=="PLUS")
								{
									echo "<font size=5><b>+</b>PLUS</font>";
								}
								
								if($_REQUEST["user_action"]=="MINUS")
								{
									echo "<font size=5><b>-</b>MINUS</font>";
								}
								
								echo "<br><br>";
								
								//important:
								echo "<input type=hidden name='product_id' value='".$line_product["pdt_id"]."' >";
								echo "<input type=hidden name='user_action' value='".$_REQUEST["user_action"]."' >";
								
								
								
								
								
								echo "<b>".$line_product["pdt_name"]."</b><br>";
								echo "<font color=#1fbad6 >".$line_product["pdt_code"]."</font>";
							
								echo "<br><br>";
							
							
							
								
								if($_SESSION["delegate_draft_".$selected_date_start_simple][$line_product["pdt_id"]][$_REQUEST["user_action"]]!="")
								{
									$_REQUEST["quantity"] = $_SESSION["delegate_draft_".$selected_date_start_simple][$line_product["pdt_id"]][$_REQUEST["user_action"]];
								}
						
						
								echo "Quantity:<br>";
								echo "<input type='text' class='textbox textbox-s' name='quantity' value='".$_REQUEST["quantity"]."' >";
								
								
								echo "<br>";
								echo "<br>";
								/*
							
								echo "<br>";
								echo "<br>";
								*/
							
								echo "<input type=button class='button button-blue ' value='Add' onclick=\"mon_submit_form('form_delegate_pop_add','mod/stock_delegation/index.php')\">";
							
							
								echo "</form>";
								
								
								
							echo "</div>";		
						}
						
					
					
					echo "</td>";
					
					
					
					
					
					echo "<td bgcolor=#ffffff width=100 align=right >";
						
						echo number_format($lib_balance[$line_product["pdt_code"]]+0,$this_product_decimal,".",",");
						
					echo "</td>";
					
					
					
					
				echo "</tr>";
				
				
				
				
				
			}
			
			
			
		echo "</table>";




		echo "<br><br><br><br><br><br>";



    echo "</section>";





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