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



$this_url="mod/stock_count_posting/index.php";
$tab_key_id="bal_id";



$query_product = "select * from t_".$key_table_prefix."_product ; ";
$result_product =mysqli_query($dbconn,$query_product);

while($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
{
	$lib_product[$line_product["pdt_code"]] = $line_product["pdt_name"];
}





if($_REQUEST["warehouse"]!="")
{
	$_SESSION["list_filter"]["filter_stock_count_warehouse"] = $_REQUEST["warehouse"];
}




if($_REQUEST["device"]!="")
{
	$_SESSION["list_filter"]["filter_stock_count_device"] = $_REQUEST["device"];
}



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



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

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

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






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

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

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

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



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













if ($_SERVER[REQUEST_METHOD] == 'POST') {
	
	$error_message="";
	
	
	
	displaymessage($error_message,"FAIL");		
	
}





echo "<section class='view_toolbar'>";
               echo "<div class='view_toolbar_left' style='width:50%;'>";
                       echo "<font size=5 ><span class='fa fa-cubes fa-1x'></span> <b>Stock Count Posting</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_count_posting/index.php','','div_main')\">";


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



    


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

	echo "<div class='view_toolbar_left' >";
		   
		   
		   	echo "<table>";
		   		echo "<tr>";
					echo "<td>";
					
		   
				   		echo "Warehouse: ";
					
					echo "</td>";
					
					echo "<td>";
								
								
						echo "<select class='dropdown dropdown-auto' name='warehouse' 
								onchange=\"mon_send_get('".$this_url."','?warehouse='+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_count_warehouse"]==$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 "</tr>";
			
			echo "<tr>";
				echo "<td>";
				
	   
					echo " Device : ";
				
				echo "</td>";
				
				echo "<td>";
			
						
						
						
						echo "<select class='dropdown dropdown-auto' name='device' 
								onchange=\"mon_send_get('".$this_url."','?device='+this.value,'div_main')\"
								>";
						
							
							
							
							
							echo "<option value=''>-- Select Device --</option>";
							
							
							$query_device="select * from t_".$key_table_prefix."_device order by dev_name ; ";
							$result_device =mysqli_query($dbconn,$query_device);
							
							while($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
							{
								
								if($_SESSION["list_filter"]["filter_stock_count_device"]==$line_device["dev_android_id"])
								{
									echo "<option value='".$line_device["dev_android_id"]."' selected >";
								}
								else {
									echo "<option value='".$line_device["dev_android_id"]."' >";
								}
								
									echo $line_device["dev_android_id"];
									echo " - ";
									echo $line_device["dev_name"];
								echo "</option>";
								
							}
						
						
						
							echo "</select>";
		
				
					echo "</td>";
				echo "</tr>";
			echo "</table>";
			
		   
		   
		   
		   
		   	
	echo "</div>";
	
	echo "<div class='view_toolbar_right' >";
		   
		   
		 
		   
		   	
	echo "</div>";

echo "</section>";



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



$query_item_count="select count(sct_id) as total , date_format(sct_count_datetime,'%Y-%m-%d') as date_f
 					from t_".$key_table_prefix."_stock_count_item   ";
					
$query_item_count=$query_item_count." where sct_warehouse_code = '".$_SESSION["list_filter"]["filter_stock_count_warehouse"]."' ";
$query_item_count=$query_item_count." and device_id  = '".$_SESSION["list_filter"]["filter_stock_count_device"]."' ";

$query_item_count=$query_item_count." group by date_format(sct_count_datetime,'%Y-%m-%d') ";

$result_item_count =mysqli_query($dbconn,$query_item_count);

while($line_item_count = mysqli_fetch_array($result_item_count,MYSQLI_ASSOC))
{
	$item_count[$line_item_count["date_f"]] = $line_item_count["total"]+0;
	
}


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




echo "<div class='cal_menu_panel'>";
    echo "<div class='cal_menu_box'>";
    for($y=2010;$y<=2030;$y++)
    {
        if($_SESSION["calendar_y"]==$y)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('".$this_url."','?".$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["calendar_m"]==$m)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('".$this_url."','?".$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["calendar_d"]==$d)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('".$this_url."','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&d=".substr($d+100,1,2)."".$tab_extra_url."','div_main')\" >".$d."";
	   
	   
	   
	   $this_count = $item_count[$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".substr($d+100,1,2)];
	   if($this_count>0)
	   {
		   echo "<br><br>";
		   
		   echo "<span style='border-radius:20px;padding:3px;background:#cf000f;color:#fff;margin-top:15px;'>";
		   	echo $this_count;
		   echo "</span>";
	   			   
	   }
	   
	   
	   
	   echo "</div>";
    }
    echo "</div>";



echo "</div>";




$selected_date_start=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]." 00:00:00";
$selected_date_end=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]." 23:59:59";


$selected_date_start_simple=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";
$selected_date_end_simple=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";



$selected_date = $_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";

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



echo "</section>";








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


	echo "<table border=0 bgcolor=#e4e9ed  width=98%  cellpadding=5 cellspacing=1 align=center >";
		echo "<tr>";
			echo "<td align=left  >";
				
			
			
				echo " <input type=button class='button button-blue' value='Post Adjustment' onclick=\"if(confirm('Are you sure?')){mon_send_get('".$this_url."','?post_document=YES','div_main')}\">";


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

	



	echo "<table style='margin:10px;background:#e4e9ed;' width=98%   cellpadding=5 cellspacing=1 >";
	
		
		echo "<tr>";
			
			echo "<td bgcolor=#e4e9ed valign=top >";
				echo "<b>Product</b>";
			echo "</td>";
			
			
			echo "<td bgcolor=#e4e9ed valign=top >";
				echo "<b>Counted Quantity</b>";
			echo "</td>";
			
			
			echo "<td bgcolor=#e4e9ed valign=top >";
				echo "<b>Counted by</b>";
			echo "</td>";
			
			echo "<td bgcolor=#e4e9ed valign=top >";
				echo "<b>Inventory Posting </b>";
			echo "</td>";
			
			echo "<td bgcolor=#e4e9ed valign=top >";
				echo "<b>Balance Prior to Count </b>";
			echo "</td>";
			
			
			echo "<td bgcolor=#e4e9ed valign=top >";
			echo "<b>Quantity to Adjust</b>";
			echo "</td>";
			
			
		echo "</tr>";
	
	
	
		
			//======A balance =======================================================
			$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_count_warehouse"]."'
						group by bal_product_code
						";
			
			
			$result_balance=mysqli_query($dbconn,$query_balance);
			while($line_balance=mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
			{
				$product_balance_before[$line_balance["bal_product_code"]] = $line_balance["total_quantity"]+0;
			}
		
		
			//======B card =======================================================
			$query_card="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_count_warehouse"]."'
					group by stc_stock_code , stc_type 
					";
			
			$result_card =mysqli_query($dbconn,$query_card);
			while($line_card = mysqli_fetch_array($result_card,MYSQLI_ASSOC))
			{
				if($line_card["stc_type"]=="PLUS")
				{
					$product_balance_before[$line_card["stc_stock_code"]]   =  $product_balance_before[$line_card["stc_stock_code"]]  - $line_card["total_quantity"];
				}
				
				if($line_card["stc_type"]=="MINUS")
				{
					$product_balance_before[$line_card["stc_stock_code"]]   =  $product_balance_before[$line_card["stc_stock_code"]]  +  $line_card["total_quantity"];
				}
				
				
			}
			
			
		
		
		
			
			
			$query_item="select * 
			 					from t_".$key_table_prefix."_stock_count_item   ";
								
			$query_item=$query_item." where sct_warehouse_code = '".$_SESSION["list_filter"]["filter_stock_count_warehouse"]."' ";
			$query_item=$query_item." and device_id  = '".$_SESSION["list_filter"]["filter_stock_count_device"]."' ";
			$query_item=$query_item." and sct_count_datetime  >= '".$selected_date_start."' ";
			$query_item=$query_item." and sct_count_datetime  <= '".$selected_date_end."' ";
			
			
			
			$query_item=$query_item." order by  sct_count_datetime  ";


			$result_item =mysqli_query($dbconn,$query_item);
			
			while($line_item = mysqli_fetch_array($result_item,MYSQLI_ASSOC))
			{
				
				
				$this_to_adjust = $line_item["sct_quantity"] - $product_balance_before[$line_item["sct_product_code"]];
					
					
				
				if($_REQUEST["post_document"]=="YES")
				{
				
					if($line_item["sct_inventory_posting"]=="")
					{
						
						$line_item["sct_inventory_posting"]="POSTED";
						
						
						
						if($this_to_adjust>0)
						{
							//PLUS
							post_inventory_balance(
										$_SESSION["list_filter"]["filter_stock_count_device"],
										$selected_date_start_simple,
										"ADJ".date("YmdHis")." app-".$line_item["sct_count_user"],
										0,
										
										"STOCK_ADJUST",
								
										"PLUS",
										$_SESSION["list_filter"]["filter_stock_count_warehouse"], 
										$line_item["sct_product_code"], 
										$line_item["sct_uom"], 
										abs($this_to_adjust), 
										"",
										"",
										""
									 );
							
							
							
						}
						
						if($this_to_adjust<0)
						{
							//MINUS
							post_inventory_balance(
										$_SESSION["list_filter"]["filter_stock_count_device"],
										$selected_date_start_simple,
										"ADJ".date("YmdHis")." app-".$line_item["sct_count_user"],
										0,
										"STOCK_ADJUST",
										"MINUS",
										$_SESSION["list_filter"]["filter_stock_count_warehouse"], 
										$line_item["sct_product_code"], 
										$line_item["sct_uom"], 
										abs($this_to_adjust), 
										"",
										"",
										""
									 );
							
							
							
						}
						
						
						
						$query_update_item="update  t_".$key_table_prefix."_stock_count_item  
									set  sct_inventory_posting = 'POSTED' 
									where sct_id ='".$line_item["sct_id"]."' ";
											
						$result_update_item =mysqli_query($dbconn,$query_update_item);
						
						
						
						
						
						
						
						
					}
				}
				
				
				
				
				
				
				
					
				echo "<tr>";
					
					echo "<td bgcolor=#ffffff valign=top >";
						echo "<font color=#1fbad6 >".$line_item["sct_product_code"]."</font><br>";
						echo $lib_product[$line_item["sct_product_code"]]."<br>";
						echo "<font color=#bdc3c7 size=1 >".$line_item["sct_product_barcode"]."</font><br>";
					echo "</td>";
					
					echo "<td bgcolor=#ffffff  valign=top  align=right>";
						echo $line_item["sct_quantity"]."<br>";
						echo "<font color=#bdc3c7 size=1 >".$line_item["sct_uom"]."</font><br>";
					echo "</td>";
					
					echo "<td bgcolor=#ffffff valign=top  >";
						echo $line_item["sct_count_user"]."<br>";
						echo "<font color=#bdc3c7 size=1 >".$line_item["sct_count_datetime"]."</font><br>";
					echo "</td>";
					
					echo "<td bgcolor=#ffffff valign=top  >";
					
					
						if($line_item["sct_inventory_posting"]=="POSTED")
						{
							echo "<span class='fa fa-check fa-1x'></span> Posted";
							
						}else {
							
							echo "<span class='fa fa-hourglass-half fa-1x'></span> Pending";
						}
						
					
						
					echo "</td>";
					
					
					
					
					
					echo "<td bgcolor=#ffffee valign=top  align=right >";
						echo $product_balance_before[$line_item["sct_product_code"]];
					echo "</td>";
					
					echo "<td bgcolor=#ffffee valign=top  align=right >";
					
					
					
						if($this_to_adjust>0)
						{
							echo "<font color=#049372 >+".($this_to_adjust+0)."</font>";
							
						}
						
						if($this_to_adjust<0)
						{
							echo "<font color=#cf000f >".($this_to_adjust+0)."</font>";
							
						}
						
						
						
						
						
				
					
						
						
					echo "</td>";
					
					
					
					
					
				echo "</tr>";
			
				
				
			}
			
	
	
	
	
	
	echo "</table>";
	
	




		


echo "</section>";









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