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



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



$query_tab_main="SELECT *  
			FROM 
			t_".$key_table_prefix."_product ";
$query_tab_main=$query_tab_main." WHERE pdt_id = '".$_REQUEST["pdt_id"]."';";


$result_tab_main=mysqli_query($dbconn, $query_tab_main)or die("Get Data failed- Err msg  ". mysqli_error($dbconn));
if($line_tab_main=mysqli_fetch_array($result_tab_main,MYSQLI_ASSOC))
{

	include "tab_config.php";

	//part 1: information:--------------------------

	echo "<div class='profile_info_panel' >";
	
	
		
		
		//save button:
		echo "<div class='fa-stack fa-lg'  style='cursor:pointer;float:right;margin-right :10px;clear:both;' onclick=\"if(confirm('Are you sure?')){mon_submit_form('form_balance_adjustment_".$line_tab_main["pdt_id"]."','mod/balance_adjustment/index.php')}\" >";
			echo '<i class="fa fa-square fa-stack-2x " style="color:#3498DB;"  ></i><i class="fa fa-save fa-stack-1x color_white" ></i>';
		echo "</div>";
	
	
	
	
		echo "<div style='width:50px;height:70px;text-align:center;float:left;margin:5px;background:none;border:0px solid #2C3E50;border-radius:5px 5px;color:#000;'>";
			echo '<span class="fa fa-cube fa-3x " ></span>';
		echo "</div>";
	
	
	
	
	
		
	
		echo "<font size=2 color=#1fbad6 ><b>". $line_tab_main["pdt_code"]."</b></font><br>";
		echo "<font size=4><b>". $line_tab_main["pdt_name"]."</b></font><br>";
		echo "<font size=1 color=#7F8C8D ><b>". $line_tab_main["pdt_barcode"]."</b></font><br>";
		
		
		
		
		
		echo "<font size=1 color=#000 ><b> Select Color:</b> ". $line_tab_main["pdt_select_color"]."</font><br>";
		echo "<font size=1 color=#000 ><b> Select Size:</b> ". $line_tab_main["pdt_select_size"]."</font><br>";
		echo "<font size=1 color=#000 ><b> Select Pattern:</b> ". $line_tab_main["pdt_select_pattern"]."</font><br>";
		
		
		
		//echo "<input type=button class='button button-red' value='Refresh' onclick=\"mon_refresh_tab(mon_current_selected_tab)\" >";
		
		
		
		
	echo "</div>";






	//part 3: tabs:--------------------------
	echo "<div class='profile_content_panel' >";

	


	
	
		echo "<form name='form_balance_adjustment_".$line_tab_main["pdt_id"]."'>";
	
		echo "<input type=hidden name='pdt_id' value='".($line_tab_main["pdt_id"]+0)."' >";
	
	
	
			
	

				echo "<table style='margin:2%;background:#BDC3C7' cellpadding=7 cellspacing=1 >";

				$query_warehouse="select * 
							from t_".$key_table_prefix."_warehouse 
				 			order by whs_code ";
				$result_warehouse = mysqli_query($dbconn, $query_warehouse);
				while($line_warehouse = mysqli_fetch_array($result_warehouse,MYSQLI_ASSOC))
				{
						$warehouse_name[$line_warehouse["whs_code"]] = $line_warehouse["whs_name"];
				}
					
					
					
				echo "<tr>";
					echo "<td bgcolor=#ECF0F1 >";
						echo "<b>Warehouse</b>";
					echo "</td>";
					
					
					//color
					echo "<td bgcolor=#ECF0F1>";
						echo "<b>Color</b>";
					echo "</td>";
					
							
					//size
					echo "<td bgcolor=#ECF0F1>";
						echo "<b>Size</b>";
					echo "</td>";
							
							
					//pattern
					echo "<td bgcolor=#ECF0F1>";
						echo "<b>Pattern</b>";
					echo "</td>";
						   
					//pattern
					echo "<td bgcolor=#ECF0F1 >";
						echo "<b>Current Quantity</b>";
					echo "</td>";
					
					
					echo "<td bgcolor=#ECF0F1 >";
						echo "<b>Adjustment Quantity</b>";
					echo "</td>";
					
					
					echo "<td bgcolor=#ECF0F1 >";
						echo "<b>Final Quantity</b>";
					echo "</td>";
					
					
					echo "<td bgcolor=#ECF0F1 >";
						echo "<b>Remark</b>";
					echo "</td>";
					
					
					
				echo "</tr>";
				
				
					
					
				$query_balance = "select * from t_".$key_table_prefix."_balance 
				   where bal_product_id = '".$line_tab_main["pdt_id"]."' ";
				$result_balance = mysqli_query($dbconn,$query_balance);
				while($line_balance = mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
				{
						
						
					if($_REQUEST["bal_".$line_balance["bal_id"]]=="")	
					{
						
						$_REQUEST["bal_".$line_balance["bal_id"]]=$line_balance["bal_quantity"];
					}
						
						
					$this_remark="";	
					
					
					
						
					if ($_SERVER[REQUEST_METHOD] == 'POST') {
					
						if($_REQUEST["bal_".$line_balance["bal_id"]]!=$line_balance["bal_quantity"])
						{
							
							
							if($_REQUEST["bal_".$line_balance["bal_id"]]>$line_balance["bal_quantity"])
							{
								//increase
								
								$qty_to_adjust = $_REQUEST["bal_".$line_balance["bal_id"]]-$line_balance["bal_quantity"];
								
								post_inventory_balance(
											'SERVER',
											date("Y-m-d"),
											"ADJ".date("YmdHis")." ".$CurrentUserFullName,
											0,
											
											"STOCK_ADJUST",
									
											"PLUS",
											$line_balance["bal_warehouse_code"], 
											$line_tab_main["pdt_code"], 
											$line_tab_main["pdt_uom"], 
											$qty_to_adjust, 
											$line_balance["bal_color"],
											$line_balance["bal_size"],
											$line_balance["bal_pattern"]
										 );
								
								
								$line_balance["bal_quantity"] = $_REQUEST["bal_".$line_balance["bal_id"]];
								
								
							}
							elseif($_REQUEST["bal_".$line_balance["bal_id"]]<$line_balance["bal_quantity"])
							{
								//decrease
								$qty_to_adjust = abs($_REQUEST["bal_".$line_balance["bal_id"]] - $line_balance["bal_quantity"]);
								
								post_inventory_balance(
											'SERVER',
											date("Y-m-d"),
											"ADJ".date("YmdHis")." ".$CurrentUserFullName,
											0,
											"STOCK_ADJUST",
											"MINUS",
											$line_balance["bal_warehouse_code"], 
											$line_tab_main["pdt_code"], 
											$line_tab_main["pdt_uom"], 
											$qty_to_adjust, 
											$line_balance["bal_color"],
											$line_balance["bal_size"],
											$line_balance["bal_pattern"]
										 );
								
								
								
								$line_balance["bal_quantity"] = $_REQUEST["bal_".$line_balance["bal_id"]];
								
							}
							else {
								
								//do nothin
								
							}
							
							
							
							
							
							
							
							$this_remark="<font color=#00AA00 >New Balance Quantity Saved</font>";	
						}
					
						
					}
							
						
						
						
					echo "<tr>";
						echo "<td bgcolor=#ECF0F1 >";
							echo "<b>".$line_balance["bal_warehouse_code"]."</b> ";
							echo $warehouse_name[$line_balance["bal_warehouse_code"]];
						echo "</td>";
						
						
						//color
						echo "<td bgcolor=#ECF0F1>";
							echo "".$line_balance["bal_color"]."";
						echo "</td>";
						
								
						//size
						echo "<td bgcolor=#ECF0F1>";
							echo "".$line_balance["bal_size"]."";
						echo "</td>";
								
								
						//pattern
						echo "<td bgcolor=#ECF0F1>";
							echo "".$line_balance["bal_pattern"]."";
						echo "</td>";
							   
						//quantity
						echo "<td bgcolor=#fff align=right >";
							echo "".$line_balance["bal_quantity"]."";
						echo "</td>";
						
						
						//Adjustment
						echo "<td bgcolor=#fff align=center >";
							echo "<span id='bal_adj_preview_".$line_tab_main["pdt_id"]."_".$line_balance["bal_id"]."'>";
							
							
							if($_REQUEST["bal_".$line_balance["bal_id"]]>=0)
							{
								echo $_REQUEST["bal_".$line_balance["bal_id"]] - $line_balance["bal_quantity"];
							}
							else {
								echo $_REQUEST["bal_".$line_balance["bal_id"]] + abs($line_balance["bal_quantity"]);
							}
							
							
							
							echo "</span>";
						echo "</td>";
						
						
						//Final Quantity
						echo "<td bgcolor=#fff >";
						
						
						
							if($line_balance["bal_quantity"]>=0)
							{
								
								echo "<input type=text class='textbox textbox-s' 
									name='bal_".$line_balance["bal_id"]."' 
									value='".$_REQUEST["bal_".$line_balance["bal_id"]]."' 
									style='text-align:center;'
									onkeyup=\"document.getElementById('bal_adj_preview_".$line_tab_main["pdt_id"]."_".$line_balance["bal_id"]."').innerHTML = (this.value*1)-".($line_balance["bal_quantity"]+0)." \"
									>";
									
								
							}
							else {
								echo "<input type=text class='textbox textbox-s' 
									name='bal_".$line_balance["bal_id"]."' 
									value='".$_REQUEST["bal_".$line_balance["bal_id"]]."' 
									style='text-align:center;'
									onkeyup=\"document.getElementById('bal_adj_preview_".$line_tab_main["pdt_id"]."_".$line_balance["bal_id"]."').innerHTML = (this.value*1)+".abs($line_balance["bal_quantity"]+0)." \"
									>";
									
							}
							
								
								
						echo "</td>";
						
						
						
						//remark
						echo "<td bgcolor=#fff >";
							echo $this_remark;
						echo "</td>";
						
						
						
					echo "</tr>";
					
				
				}
							
							
				echo "</table>";


				echo "<b>Note:</b> If you dont see any balance slot for specific warehouse you want. You need to initiated a inventory posting to that particulater warehouse before you can adjust it.";



	echo "</form>";



	echo "</div>";



}

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