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

?>

<?php

//important================================
include "config_form.php"; //important
//important================================



$form_table_name="t_".$key_table_prefix."_invoice_header ";
$form_key_id="ivh_id";
$this_url_edit="mod/picking_list/edit.php";





if ($_SERVER[REQUEST_METHOD] == 'POST') {
$error_msg="";
$password_confirmation=false;
$password_temp="";

	
		
	$query_ivi = "select * 
				from t_".$key_table_prefix."_invoice_item
				where ivi_header_id ='".$_REQUEST["ivh_id"]."' 
				 ";
	$result_ivi = mysqli_query($dbconn,$query_ivi);

	while($line_ivi = mysqli_fetch_array($result_ivi,MYSQLI_ASSOC))
	{
		if($_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]]=="")
		{
			$error_msg=$error_msg."<li>Quantity is empty.</li>";	  	
		}
		else {
			
			
			if(!is_numeric($_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]]))
			{
				$error_msg=$error_msg."<li>Quantity must be number value only.</li>";	  
				
			}
					
					
		
			//$_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]];
		}				
		
	}

	if($error_msg=="")
	{
		
		
		
		$query_ivi = "select * 
					from t_".$key_table_prefix."_invoice_item
					where ivi_header_id ='".$_REQUEST["ivh_id"]."' 
					 ";
		$result_ivi = mysqli_query($dbconn,$query_ivi);
	
		while($line_ivi = mysqli_fetch_array($result_ivi,MYSQLI_ASSOC))
		{
			
			
			
			$unit_price = $line_ivi["ivi_price"];
			$new_quantity = $_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]]+0;
			
			$discount_percentage = $line_ivi["ivi_discount_percentage"];
			$discount_value = $line_ivi["ivi_discount_value"];
			
			$total_addon_amount = $line_ivi["ivi_total_addon"];
			
			
			$product_amount =  $unit_price * $new_quantity;
			$product_amount = $product_amount + $total_addon_amount;
			
			
			$total_discount = number_format(($product_amount * $discount_percentage / 100) + $discount_value,2,".","");
			

			$before_gst_amount = $product_amount - $total_discount;
			$gst_percentage = $line_ivi["ivi_gst_percentage"];


			if($gst_percentage>0)
			{
				if($line_ivi["ivi_mode"]=="INCLUSIVE")
				{
					//inclusive:
					
					$gst_amount = number_format($before_gst_amount * $gst_percentage / (100 + $gst_percentage),2,".","");
                      	$total_amount = number_format($before_gst_amount,2,".","");
                      	$before_gst_amount = $total_amount - $gst_amount;
				}
				else {
					
					//exclusive:
					
					$gst_amount = number_format($before_gst_amount * $gst_percentage / 100,2,".","");
                         $total_amount = number_format($before_gst_amount + $gst_amount,2,".","");
					
				}
				
			}
			else {
				$gst_amount = 0;
			     $total_amount = number_format($before_gst_amount,2,".","");
			}
			
			
			
			$query_update="update t_".$key_table_prefix."_invoice_item 
						set 
						ivi_quantity = '".($new_quantity)."' ,
						
						
						ivi_total_addon = '".($total_addon_amount+0)."',
						ivi_discount_percentage = '".($discount_percentage+0)."',
						ivi_discount_value = '".($discount_value+0)."',
						ivi_discount_total = '".($total_discount+0)."',
						ivi_total_before_gst = '".($before_gst_amount+0)."',
						
						ivi_gst_amount = '".($gst_amount+0)."',
						ivi_total_amount = '".($total_amount+0)."',
						
						modified_date = '".date("Y-m-d H:i:s")."'
						
						
						where ivi_id = '".$line_ivi["ivi_id"]."'
						";
			$result_update = mysqli_query($dbconn, $query_update) or die("Error: ".mysqli_error());
			
			
			$sum_discount = $sum_discount + $total_discount;
			$sum_before_gst_amount = $sum_before_gst_amount + $before_gst_amount;
						
			$sum_gst_amount = $sum_gst_amount + $gst_amount;			
			
			$sum_total_amount = $sum_total_amount + $total_amount;
			
			
			
			
			
			
		}
		
		
		
	     $total_amount_rounded = mon_fivecent_round($sum_total_amount);
		
		$sum_rounding_amount = $total_amount_rounded - $sum_total_amount;
		
		
		//---------recompute header amount:-----------------------------
		$query_update="update t_".$key_table_prefix."_invoice_header
					set 
					ivh_discount_total = '".($sum_discount+0)."',
					ivh_total_before_gst = '".($sum_before_gst_amount+0)."',
					ivh_gst_amount = '".($sum_gst_amount+0)."',
					ivh_total_amount = '".($sum_total_amount+0)."',
					
					
					ivh_rounding_amount = '".($sum_rounding_amount+0)."',
					ivh_total_amount_rounded = '".($total_amount_rounded+0)."',
					
					
					modified_date = '".date("Y-m-d H:i:s")."'
					
					
					where ivh_id = '".$_REQUEST["ivh_id"]."'
					";
		$result_update = mysqli_query($dbconn, $query_update) or die("Error: ".mysqli_error());
		
		
		
		
		
		
		
		displaymessage("Saved","CREATED");
			
	}
	else 
	{
		displaymessage($error_msg,"FAIL");
	}
	
			
}










//------------------data display----------------------
echo "<div class='data_display_content'>";	
	
	
	
	
	
	
	
	
	
$query_display="SELECT * FROM $form_table_name ";
$query_display=$query_display." WHERE $form_key_id = '".$_REQUEST[$form_key_id]."';";
$result_display=mysqli_query($dbconn,$query_display)or die("Get Data failed- Err msg  ". mysqli_error($dbconn));
			
if($line_display=mysqli_fetch_array($result_display,MYSQLI_ASSOC))
{
	  	
			
		
		
	//--------------------------------save button----------------------------------	
	//echo "<span class='fa-stack fa-lg' style='cursor:pointer;float:left;margin:10px;' onclick=\"mon_send_get('".$this_url_edit."','?ivh_id=".$_REQUEST["ivh_id"]."','div_display')\" >";
	echo "<span class='fa-stack fa-lg' style='cursor:pointer;float:left;margin:10px;' onclick=\"mon_submit_form_display('edit_picking_list_".$_REQUEST["ivh_id"]."','mod/picking_list/edit.php')\" >";
		echo "<i class='fa fa-square fa-stack-2x' style='color:#1ABC9C;' ></i>";
		echo "<i class='fa fa-save fa-stack-1x'  style='color:#ffffff;'></i>";
	echo "</span>";
			
			
			
			
		$query_item="select count(ivi_id) as total 
					from t_".$key_table_prefix."_invoice_item
					where ivi_header_id = '".$line_display["ivh_id"]."' 
					and not ifnull(ivi_inventory_posting,'') = 'POSTED'
					";
		$result_item = mysqli_query($dbconn,$query_item);
		if($line_item = mysqli_fetch_array($result_item,MYSQLI_ASSOC))
		{
			if($line_item["total"]>0)
			{
				
			}
			else {
				echo "<div style='cursor:pointer;float:right;margin:10px;padding:5px;border:1px solid #BDC3C7;border-radius:5px;' >";
					echo "<span class='fa fa-cubes fa-lg'  ></span>";	
					echo " Inventory Posted";
					echo " <span class='fa fa-check fa-lg' style='color:#16A085;' ></span>";	
				echo "</div>";
				
			}
		}
		
		
	
		if($line_display["ivh_delivery_status"]=="DELIVERED")
		{
			echo "<div style='cursor:pointer;float:right;margin:10px;padding:5px;border:1px solid #BDC3C7;border-radius:5px;' >";
				echo "<span class='fa fa-truck fa-lg'  ></span>";	
				echo " Delivery Completed";
				echo " <span class='fa fa-check fa-lg' style='color:#16A085;' ></span>";	
			echo "</div>";	
		}
		
		
		
		
		
	for($i=1;$i<=count($form_field);$i++)
	{
		
	
	
		if($form_field_separator[$i]!="")
		{
			echo "<div class='data_display_separator' >";
			echo "<font size=3><b>".$form_field_separator[$i]."</b></font>";
			echo "</div>";
		}

		
		
		
		
		echo "<div class='data_display_row'>";
		
			
			echo "<div class='data_display_label'>";
				echo $form_field_label[$i];
			echo "</div>";
		
			echo "<div class='data_display_value'>";
		
				if($form_field_type[$i]=="TEXT")
				{
					echo $line_display[$form_field[$i]]."";
				}
			
				if($form_field_type[$i]=="QUANTITY")
				{
					echo $line_display[$form_field[$i]]."";
				}
			
				if($form_field_type[$i]=="PRICE")
				{
					echo $line_display[$form_field[$i]]."";
				}
				
				if($form_field_type[$i]=="TIME")
				{
					echo $line_display[$form_field[$i]]."";
				}
				
				if($form_field_type[$i]=="DATE")
				{
					echo $line_display[$form_field[$i]]."";
				}
				
				if($form_field_type[$i]=="DATE_START")
				{
					echo $line_display[$form_field[$i]]."";
				}
				
				if($form_field_type[$i]=="DATE_END")
				{
					echo $line_display[$form_field[$i]]."";
				}
				
				if($form_field_type[$i]=="PASSWORD")
				{
					echo "******";
				}
					
				if($form_field_type[$i]=="TEXTAREA")
				{
					echo str_replace(chr(10),"<br>",$line_display[$form_field[$i]])."";
				}
				
				if($form_field_type[$i]=="DROPDOWN")
				{
				
					if($form_field_dropdown_option[$i]=="KEYWORD")
					{
					 
						echo "".$mon_keyword_list[$form_field_dropdown_option_values[$i]][$line_display[$form_field[$i]]]."";
					 
					
						
					}
					if($form_field_dropdown_option[$i]=="SQL")
					{
					 
						$query_select=$form_field_dropdown_option_values[$i];
						$query_select=$query_select." where ".$form_field_dropdown_option_values_key[$i]."='".$line_display[$form_field[$i]]."';";
						$result_select=mysqli_query($dbconn,$query_select) or die("Retrieval failed- Err msg  ". mysqli_error($dbconn));
						if($line_select=mysqli_fetch_array($result_select,MYSQLI_ASSOC))
						{
							echo "".$line_select["text"].""; 
						}
														
					}
				
				
				
				
					}
					
					if($form_field_type[$i]=="FILE_PHOTO")
					{
						$query_file="select fil_id, fil_storage_method from t_".$key_table_prefix."_file 
							where fil_mod='".$form_key_id."' 
							and fil_mod_id='".$_REQUEST[$form_key_id]."'
							and fil_mod_field='".$form_field[$i]."'
							";
			
						$result_file=mysqli_query($dbconn,$query_file) or die("Retrieval failed- Err msg  ". mysqli_error($dbconn));
						if($line_file=mysqli_fetch_array($result_file,MYSQLI_ASSOC))
						{
							if($line_file["fil_storage_method"]=="OS")
							{
								echo "<img width=250 src='".$file_storage_folder.$line_file["fil_id"].".jpg' >";	
							}
							else
							{
								echo "<img width=250 src='mod/uploader/file.php?id=".$line_file["fil_id"]."' >";	
							}
							
						} 
					}
					
					if($form_field_type[$i]=="FILE")
					{
						$query_file="select fil_name, fil_id, fil_storage_method from t_".$key_table_prefix."_file 
										where fil_mod='".$form_key_id."' 
										and fil_mod_id='".$_REQUEST[$form_key_id]."'
										and fil_mod_field='".$form_field[$i]."'
										";
						
						$result_file=mysqli_query($dbconn,$query_file) or die("Retrieval failed- Err msg  ". mysqli_error($dbconn));
						if($line_file=mysqli_fetch_array($result_file,MYSQLI_ASSOC))
						{
							$fileName_array=explode(".",$line_file["fil_name"]);
							$ext=$fileName_array[count($fileName_array)-1];
							if($line_file["fil_storage_method"]=="OS")
							{
								echo "<a href='".$file_storage_folder.$line_file["fil_id"].".".$ext."' target=_blank >".$line_file["fil_name"]."</a>";	
							}
							else
							{
								echo "<a href='mod/fuploader/file.php?id=".$line_file["fil_id"]."' >".$line_file["fil_name"]."</a>";	
							}
						} 
					}
		
				echo "</div>"; // end value division
				
				
				
		echo "</div>"; //end row division
	 
	}
	
	
	echo "<div class='data_display_row'>";
	
		
		echo "<div class='data_display_label'>";
			echo "Payment Mode";
		echo "</div>";
	
		echo "<div class='data_display_value'>";
			$query_ivp = "select * 
						from t_".$key_table_prefix."_invoice_payment 
						where ivp_ivh_id ='".$line_display["ivh_ivh_id"]."' 
						and device_id  ='".$line_display["device_id"]."' ";
			$result_ivp = mysqli_query($dbconn,$query_ivp);
			while($line_ivp = mysqli_fetch_array($result_ivp,MYSQLI_ASSOC))
			{
				echo "<b>".$line_ivp["ivp_payment_code"]."</b> ";
				echo $line_ivp["ivp_payment_name"];
				
				echo ": ".number_format($line_ivp["ivp_amount"]-$line_ivp["ivp_change"],2,".",",");
				
				if($line_ivp["ivp_reference_no"]!="")
				{
					echo " Reference: ".$line_ivp["ivp_reference_no"];	
				}
				if($line_ivp["ivp_approval_no"]!="")
				{
					echo " Approval Code: ".$line_ivp["ivp_approval_no"];
				}
				
				
				
				echo "<br>";
			
			}
		
		
		
		echo "</div>"; // end value division
	echo "</div>"; //end row division
	
	
	
	//========================================================================================================================
	echo "<div class='data_display_separator' >";
	echo "<font size=3><b>Transaction Item</b></font>";
	echo "</div>";
	
		
		
		
		
	  //===Header Label =============
	  $listing_header[1]="Product Code";
	  $listing_header[2]="Barcode";
	  $listing_header[3]="Product Name";
	  $listing_header[4]="Serial No.";
	  $listing_header[5]="Remark";
	  $listing_header[6]="Price";
	  $listing_header[7]="Quantity";
	  $listing_header[8]="Addon Amount";
	  $listing_header[9]="Discount";
	  $listing_header[10]="Before GST";
	  $listing_header[11]="GST Mode";
	  $listing_header[12]="GST Code";
	  $listing_header[13]="GST %";
	  $listing_header[14]="GST Amount";
	  $listing_header[15]="Total Amount";
	  
	  
		  
	  //===Database Fields==========
	  $listing_content[1]="ivi_product_code";
	  $listing_content[2]="ivi_product_barcode";
	  $listing_content[3]="ivi_product_name";
	  $listing_content[4]="ivi_serial_no";
	  $listing_content[5]="ivi_remark";
	  $listing_content[6]="ivi_price";
	  $listing_content[7]="ivi_quantity";
	  $listing_content[8]="ivi_total_addon";
	  $listing_content[9]="ivi_discount_total";
	  $listing_content[10]="ivi_total_before_gst";
	  $listing_content[11]="ivi_gst_mode";
	  $listing_content[12]="ivi_gst_code";
	  $listing_content[13]="ivi_gst_percentage";
	  $listing_content[14]="ivi_gst_amount";
	  $listing_content[15]="ivi_total_amount";
	  
	  
	  
	  
	  //===Column width==========
	  $listing_width[1]="60";
	  $listing_width[2]="60";
	  $listing_width[3]="120";
	  $listing_width[4]="60";
	  $listing_width[5]="60";
	  $listing_width[6]="40";
	  $listing_width[7]="70";
	  $listing_width[8]="40";
	  $listing_width[9]="40";
	  $listing_width[10]="40";
	  $listing_width[11]="60";
	  $listing_width[12]="40";
	  $listing_width[13]="40";
	  $listing_width[14]="40";
	  $listing_width[15]="40";
	  
	  
	  
	  
	  //===Column align==========
	  $listing_align[1]="left";
	  $listing_align[2]="left";
	  $listing_align[3]="left";
	  $listing_align[4]="left";
	  $listing_align[5]="left";
	  $listing_align[6]="right";
	  $listing_align[7]="right";
	  $listing_align[8]="right";
	  $listing_align[9]="right";
	  $listing_align[10]="right";
	  $listing_align[11]="left";
	  $listing_align[12]="left";
	  $listing_align[13]="right";
	  $listing_align[14]="right";
	  $listing_align[15]="right";
	  
	  
	echo "<div class='view_list_title_row'>";
	
	
			echo "<div class='view_list_title_content'>";
				echo "<div style='min-width:20px;' class='view_list_title_row_count'  >";
					echo "#";
				echo "</div>";
				
				
				for($i=1;$i<=count($listing_header);$i++)
				{
					if($listing_content[$i]=="modified_date")
					{
						
					}
					else
					{
						echo "<div style='width:".$listing_width[$i]."px;' class='view_list_title_content_object'  >";
							echo "".$listing_header[$i]."<br>";
						echo "</div>";
					}
					
				}
				
			echo "</div>";
	echo "</div>";
			
			
			
			
	echo "<form name='edit_picking_list_".$line_display["ivh_id"]."' >";
	echo "<input type=hidden name='ivh_id' value='".$line_display["ivh_id"]."'>";
	
	
	$count=1;
	$query_ivi = "select * 
				from t_".$key_table_prefix."_invoice_item
				where ivi_doc_no ='".$line_display["ivh_doc_no"]."' 
				and device_id  ='".$line_display["device_id"]."' ";
	$result_ivi = mysqli_query($dbconn,$query_ivi);
	
	while($line_ivi = mysqli_fetch_array($result_ivi,MYSQLI_ASSOC))
	{
		
		echo "<div class='view_row'>";
		
		
			echo "<div style='min-width:20px;' class='view_row_count'  >";
			echo "".$count."";
			echo "</div>";
		
			for($i=1;$i<=count($listing_header);$i++)
			{
				
				echo "<div style='width:".$listing_width[$i]."px;text-align:".$listing_align[$i].";' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$listing_header[$i]."</font>";
				
				
					echo "<font class='view_row_content_text'>";
					
						if($listing_content[$i]=="ivi_quantity")
						{
							if($_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]]=="")
							{
								$_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]] = $line_ivi["ivi_quantity"];
							}
							
							echo "<input type=text class='textbox textbox-s' name='ivi_quantity_".$line_ivi["ivi_id"]."' value='".$_REQUEST["ivi_quantity_".$line_ivi["ivi_id"]]."' >";
							
							if($line_ivi["ivi_picking_status"]=="PICKED")
							{
								if($line_ivi["ivi_picking_quantity"]==$line_ivi["ivi_quantity"])
								{
									echo "<br>";
									echo "<font color=#049372 >Picked:".$line_ivi["ivi_picking_quantity"]."</font>";
								}
								else {
									
									echo "<br>";
									echo "<font color=#cf000f >Picked:".$line_ivi["ivi_picking_quantity"]."</font>";
									
								}
								
							}
							
						
							
							
							
							
						}	
						else {
							$this_text=$line_ivi[$listing_content[$i]];
							$this_text=str_replace(chr(10),"<br>",$this_text);
							$this_text = str_replace($search_word, "<span class='view_search_hightlight' >".$search_word."</span>", $this_text);
							echo "".$this_text."";
							
								
						}
					
					
						
						
						
					echo "</font><br>";
				
				echo "</div>";
			
			}
		
		
		
		echo "</div>";
	
		$count++;
	}
	
	
	echo "</form>";
	
	
		
		
						
}	
		
	
echo "</div>";
echo "<div style='float:left;height:200px;'></div>";





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

?>