<?php include "../../includes/start_ajax.php";
$client_id=0;
?>

<div  align=left   >
<?php


$cell_width="50";

$this_url="mod/stock_balance/index.php";


echo "<form name='form_import_sales_transaction' >";



echo "<section class='view_toolbar'>";
			 echo "<div class='view_toolbar_left' style='width:50%;'>";
				    echo "<font size=5 ><span class='fa fa-cloud-upload fa-1x'></span> <b>Import Sales Transaction</b></font>";

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

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



	

//=====gst==========
$query_gst = "select * from t_".$key_table_prefix."_gst ";												
$result_gst = mysqli_query($dbconn, $query_gst);
while($line_gst = mysqli_fetch_array($result_gst,MYSQLI_ASSOC))
{
	$lib_gst[$line_gst["gst_code"]] = ($line_gst["gst_percentage"]+0);
	
}



//=====lib==========
$query_customer="select * from t_".$key_table_prefix."_customer ";												
$result_customer = mysqli_query($dbconn, $query_customer);
while($line_customer = mysqli_fetch_array($result_customer,MYSQLI_ASSOC))
{
	$lib_customer[$line_customer["cus_code"]] = $line_customer["cus_code"];
	
}



$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_code"];
	
	if($line_product["pdt_barcode"]!="")
	{
		$lib_product[$line_product["pdt_barcode"]] = $line_product["pdt_barcode"];
	}
	
}


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




	
	echo "<div class='data_display_content' style='padding:20px;'>";	
	
	
					
				echo "<input type=hidden name='confirm_upload' value='' >";
				echo "<table width='80%' bgcolor=#ECF0F1 cellpadding=10 cellspacing=1 >";
				    
				    
				echo "<tr>";
				    echo "<td bgcolor=#ECF0F1 >";
				    	echo "<b>Before you start:</b> Upload Guildline:<br>";
					   
				    echo "</td>";
				echo "</tr>";
				
				
				echo "<tr>";
				    echo "<td bgcolor=#fff >";
				    
						
						 echo "<ul>";
							 echo "<li>File must be in CSV format (Comma-Separated Values)</li>";
							 echo "<li>First row must be the Column/Field Header Name</li>";
							 echo "<li>Column/Field Header sequence is not important</li>";
							 echo "<li>Column/Field Header accepted as below:</li>";
							 
							 echo "<br>";
							 echo "<table cellpadding=8 cellspacing=1 bgcolor=#bdc3c7 >";
							 
								 echo "<tr>";
								 
									 echo "<td bgcolor=#dadfe1 valign=top >";
										 echo "Header/Column Info";
									 echo "</td>";
									 
									 
									 echo "<td bgcolor=#ffffff valign=top >";
										 echo "<b>Transaction Date</b><br>";
										 echo "The transaction date in YYYY-MM-DD format.";
										 echo "<br><font color=#ff0000 >Same to all rows</font>";
									 echo "</td>";
									 
									 echo "<td bgcolor=#ffffff valign=top>";
										 echo "<b>Customer Code</b><br>";
										 echo "Customer Code that match the Customer Profile Setting in this system.";
										 echo "<br><font color=#ff0000 >Same to all rows</font>";
									 echo "</td>";
									 
									 
									 echo "<td bgcolor=#ffffff valign=top>";
										 echo "<b>Product Code</b><br>";
										 echo "Product Code that match the Product Setting in this system. <br>Code can be either product code or barcode.";
									 echo "</td>";
									 
									 echo "<td bgcolor=#ffffff valign=top>";
										 echo "<b>Product Quantity</b><br>";
										 echo "Quantity must be numeric value (no thousand seperator). <br>Decimal value is accepted.<br>Negative symbol will be ignored";
									 echo "</td>";
									 
									 echo "<td bgcolor=#ffffff valign=top>";
										 echo "<b>Product Unit Price</b><br>";
										 echo "Price must be numeric value (no thousand seperator). <br>Decimal value is accepted.<br>Negative symbol will be ignored";
									 echo "</td>";
									 
									 
								 echo "</tr>";
								 
								 
								 echo "<tr>";
									 echo "<td bgcolor=#dadfe1 valign=top >";
										 echo "First Row";
									 echo "</td>";
								 
									 echo "<td bgcolor=#e4e9ed>";
										 echo "DATE";
									 echo "</td>";
									 
									 echo "<td bgcolor=#e4e9ed>";
										 echo "CUSTOMER";
									 echo "</td>";
									 
									 
									 echo "<td bgcolor=#e4e9ed>";
										 echo "CODE";
									 echo "</td>";
									 
									 echo "<td bgcolor=#e4e9ed>";
										 echo "QUANTITY";
									 echo "</td>";
									 
									 echo "<td bgcolor=#e4e9ed>";
										 echo "PRICE";
									 echo "</td>";
									 
									 
								 echo "</tr>";
								 
								 
								 
								 
								 echo "<tr>";
								 
									 echo "<td bgcolor=#dadfe1>";
										 echo "Sample Value";
									 echo "</td>";
									 
								
									 
									 echo "<td bgcolor=#f4f9fd>";
									     echo "".date("Y-m-d");
								      echo "</td>";
									
									 echo "<td bgcolor=#f4f9fd>";
									 
									 	$query_customer="select * from t_".$key_table_prefix."_customer 
													order by rand() limit 0, 1";												
					  					$result_customer = mysqli_query($dbconn, $query_customer);
					  					if($line_customer = mysqli_fetch_array($result_customer,MYSQLI_ASSOC))
					  					{
											echo $line_customer["cus_code"];
										}
										else {
											echo "ABC123";
										}
										 
									 echo "</td>";
									 
									 
									 echo "<td bgcolor=#f4f9fd>";
										 $query_product="select * from t_".$key_table_prefix."_product
													order by rand() limit 0, 1";												
										$result_product = mysqli_query($dbconn, $query_product);
										if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
										{
											echo $line_product["pdt_code"];
										}
										else {
											echo "ABC123";
										}
									 echo "</td>";
									 
									 echo "<td bgcolor=#f4f9fd>";
										 echo "9999.99";
									 echo "</td>";
									 
									 echo "<td bgcolor=#f4f9fd>";
										 echo "9999.99";
									 echo "</td>";
									 
									 
								 echo "</tr>";
							 echo "</table>";
							 
						 echo "</ul>";
				    
				    
						
				    echo "</td>";
				echo "</tr>";
				
				    echo "<tr>";
				        echo "<td bgcolor=#ECF0F1 >";
				            echo "<b>Step 1:</b> Upload Data<br>";
				        echo "</td>";
				    echo "</tr>";
				    
				    
				    echo "<tr>";
				        echo "<td bgcolor=#fff >";
					   
					   	
					   
				              echo "<iframe style='border:0px solid #000;' class=\"upload_iframe\" src=\"mod/fuploader/index.php?mod=SALES_UPLOAD&mod_id=0&mod_field=data_file\" frameborder=0 ></iframe>";
				        echo "</td>";
				    echo "</tr>";
				    
				    
				    
				    
				    
				     echo "<tr>";
				        echo "<td bgcolor=#ECF0F1 >";
				            echo "<b>Step 2:</b> Preview Upload Result<br>";
				        echo "</td>";
				    	echo "</tr>";
				    
				    	echo "<tr>";
				        echo "<td bgcolor=#fff >";
				            echo "<input type='button' class='button button-blue' value='Preview' onclick=\"mon_submit_form('form_import_sales_transaction','mod/import_sales_transaction/index.php')\" >";
				            
				            
				            echo "<br><br>";
				            
				            	$data_found=false;
				                    
					        	$query="Select fil_id, fil_name, fil_size, fil_type, fil_storage_method 
					                FROM t_".$key_table_prefix."_file 
					                        where fil_mod='SALES_UPLOAD' 
					                        and fil_mod_id='0' 
					                        and fil_mod_field='data_file' ;"; 
					                  
					        	//$result = mysqli_query($dbconn,$query) or die("Unable to get data! - Err msg ". mysqli_error($dbconn));
					        	$result = mysqli_query($dbconn, $query);
					        
					        	echo mysqli_error($dbconn);
					        
					        	if($line = mysqli_fetch_array($result,MYSQLI_ASSOC))
					        	{
				            
				            		$fileName_array=explode(".",$line["fil_name"]);
					    			$ext=$fileName_array[count($fileName_array)-1];
				            
				            
				            		$checking_header_date_found=false;
								$checking_header_customer_found=false;
								$checking_header_product_found=false;
								$checking_header_quantity_found=false;
								$checking_header_price_found=false;
								
								
								$column_header_date_index="-1";
								$column_header_customer_index="-1";
								$column_header_product_index="-1";
								$column_header_quantity_index="-1";
								$column_header_price_index="-1";
								
								
								
								
						  		$valid_header["DATE"]="DATE";
								$valid_header["CUSTOMER"]="CUSTOMER";
								$valid_header["CODE"]="CODE";
								$valid_header["QUANTITY"]="QUANTITY";
								$valid_header["PRICE"]="PRICE";
						  
						  
				            
				                	if (($handle = fopen("../../".$file_storage_folder.$line["fil_id"].".".$ext, "r")) !== FALSE)
				                	{
									
									$data_found=true;
									
									$total_error=0;
									$upload_total_amount=0;
									
									
									$header_total_before_gst=0;
									$header_total_gst = 0;
									$header_total =0; 
									
									
									
									if($_REQUEST["confirm_upload"]=="YES")
									{
										
									
							    			
							    		
										
										
									}
										
									
					                    echo "<table bgcolor=#BDC3C7 cellspacing=1 cellpadding=5 >";
					                    
				                        
								    		$row_counter=1;
								    		$header_created=false;
										
						                    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
						                        
										    
										    
										    if($row_counter==1)
										    {
											    foreach($data as $key=>$value)
											    {
												    if($value=="DATE")
												    {
													    $checking_header_date_found = true;
													    
													    $column_header_date_index=$key;
													    
													    
													    
													    
												    }
												    
												    if($value=="CUSTOMER")
												    {
													    $checking_header_customer_found = true;
													    
													    $column_header_customer_index=$key;
													    
												    }
												    
												    if($value=="CODE")
												    {
													    $checking_header_product_found = true;
													    
													    $column_header_product_index=$key;
													    
												    }
												    
												    if($value=="QUANTITY")
												    {
													    $checking_header_quantity_found = true;
													    
													    $column_header_quantity_index=$key;
													    
												    }
												    
												    if($value=="PRICE")
												    {
													    $checking_header_price_found = true;
													    
													    $column_header_price_index=$key;
												    }
												    
											     }
											    
											    
											    
											    
											    
											 	$check_header_message="";
												
												if(!$checking_header_date_found)
												{
													$check_header_message=$check_header_message."<li>DATE not found</li>";
												}
												
												if(!$checking_header_customer_found)
												{
													$check_header_message=$check_header_message."<li>CUSTOMER not found</li>";
												}
												
												if(!$checking_header_product_found)
												{
													$check_header_message=$check_header_message."<li>CODE not found</li>";
												}
												
												if(!$checking_header_quantity_found)
												{
													$check_header_message=$check_header_message."<li>QUANTITY not found</li>";
												}
												
												if(!$checking_header_price_found)
												{
													$check_header_message=$check_header_message."<li>PRICE not found</li>";
												}
												
									
									
									
	 						                        echo "<tr>";
											    
												     echo "<td bgcolor=#ECF0F1 >";
													 	echo $row_counter;
												     echo "</td>";
												    
	 						                         
												   
												   foreach($data as $key=>$value)
	    											    {
													    
											
													    
													    
													    echo "<td bgcolor=#ffffff>";
													    		echo "".$value."";
															
															if(in_array($value,$valid_header))
															{
															    echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";
															    
															}
															else {
																echo "<span class='fa fa-eye-slash fa-lg' style='color:#cf000f; '></span>";
															 
															}
															
													    echo "</td>";
												    }
												    
												    	echo "<td bgcolor=#ECF0F1 >";
												    				
														if($check_header_message!="")
														{
															echo "Column not correct<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
															echo $check_header_message;
															
															$total_error++;
														}		
																									    
														
													echo "</td>";
												   
												   
													if($_REQUEST["confirm_upload"]=="YES")
 												    {
 													    echo "<td bgcolor=#2abb9b >";
													    	echo "Upload Result";
 													    echo "</td>";
 													    
 												    }
 												    
 												    
	 						                          
	 						                        echo "</tr>";	
											    
											    
											    
											    
										    }
										    else {
										    	
											
											    echo "<tr>";
												    echo "<td bgcolor=#ECF0F1 >";
													 echo $row_counter;
												    echo "</td>";
												
													$this_transaction_date = "";
													$this_customer_code = "";
													$this_product_code = "";
													
												  
											    		$this_unit_price=0;
													$this_quantity=0;
											    
											    
											    
												    foreach($data as $key=>$value)
													{
														
														
														echo "<td bgcolor=#ffffff>";
														 echo "".$value."";
														 
														 
														
															if($column_header_date_index==$key)
															{
																//echo " -date";
																$valid_date = true;
																
																$date_array = explode("-",$value);
																if(count($date_array)==3)
																{
																	if(($date_array[0]+0>=2010)and($date_array[0]+0<=2050))
																	{
																		
																	}
																	else {
																		$valid_date = false;
																	}
																	
																	
																	if(($date_array[1]+0>=1)and($date_array[1]+0<=12))
																	{
																		
																	}
																	else {
																		$valid_date = false;
																	}
																	
																	
																	if(($date_array[2]+0>=1)and($date_array[2]+0<=31))
																	{
																		
																	}
																	else {
																		$valid_date = false;
																	}
																	
																	
																	
																	
																	
																	
																}
																else {
																	$valid_date = false;
																}
																
																
																
																
																
																
																
																
																
																if($valid_date)
																{
																	$this_transaction_date = $value;
																
																	echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";	
																}
																else {
																	echo "<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
																	$total_error++;
																}
																
															}
															
															
															if($column_header_customer_index==$key)
															{
																//echo " -cust";
																
																if(in_array($value,$lib_customer))
																{
																	
																	$this_customer_code = $value;
																	
																	echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";
      															    
      															}
      															else {
      																echo "<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
																	$total_error++;
      															 
      															}
																
															}
															
															
															if($column_header_product_index==$key)
															{
																//echo " -pdt";
																
																if(in_array($value,$lib_product))
																{
																	
																	$this_product_code = $value;
																	
																	
																	echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";
      															    
      															}
      															else {
      																echo "<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
																	$total_error++;
      															 
      															}
															}
															
															
															if($column_header_quantity_index==$key)
															{
																//echo " -qty";
																
																if(is_numeric($value))
																{
																	$this_quantity=$value;
																	echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";	
																}
																else {
																	echo "<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
																	$total_error++;
																}
																
																
															}
															
															if($column_header_price_index==$key)
															{
																//echo " -price";
																
																if(is_numeric($value))
																{
																	$this_unit_price=$value;
																	echo "<span class='fa fa-check fa-lg' style='color:#049372; '></span>";	
																}
																else {
																	echo "<span class='fa fa-times fa-lg' style='color:#cf000f; '></span>";
																	$total_error++;
																}
																
																
																
																
															}
															
															
															
															
														echo "</td>";
													}
												    
													
												   	echo "<td bgcolor=#ECF0F1 >";
													    
													    
												   			echo "Row Amount: <b>".number_format($this_unit_price * $this_quantity,2,".",",")."</b>";
													    
													    
												    			$upload_total_amount = $upload_total_amount + ($this_unit_price * $this_quantity);
												    echo "</td>";
												  
												    
												    
												    
												    //=============================================================================================================
												    if($_REQUEST["confirm_upload"]=="YES")
												    {
													    echo "<td bgcolor=#ffffff >";
													    
													    
													    
													    
													    	if(!$header_created)
														{
															
															//----get customer-----
															
															$this_customer_name = "";
															$this_customer_gst_no = "";
															$this_customer_address_billing = "";
															$this_customer_address_delivery = "";
															$this_customer_discount_percentage = "";
															
															$query_customer = "select * 
																	from t_".$key_table_prefix."_customer
																	where cus_code = '".$this_customer_code."'
																	";
															
															$result_customer = mysqli_query($dbconn,$query_customer) or die("Error ".mysqli_error($dbconn));
															if($line_customer = mysqli_fetch_array($result_customer,MYSQLI_ASSOC))
															{
																
																//$this_customer_code
																$this_customer_name = $line_customer["cus_name"];
																$this_customer_gst_no = $line_customer["cus_gst_no"];
																$this_customer_address_billing = $line_customer["cus_address_billing"];
																$this_customer_address_delivery = $line_customer["cus_address_delivery"];
																$this_customer_discount_percentage = $line_customer["cus_discount_percentage"];
																
															}
															
															
															
															
															
															//------get document no. -----
															$max_no=10000;
															$query_max = "select max(ivh_ivh_id) as max_no 
																	from t_".$key_table_prefix."_invoice_header 
																	where device_id = 'SERVER' 
																	
																	
																	";
															
															$result_max = mysqli_query($dbconn,$query_max) or die("Error ".mysqli_error($dbconn));
															if($line_max = mysqli_fetch_array($result_max,MYSQLI_ASSOC))
															{
																$max_no = $line_max["max_no"]+0;	
															}
															
															if($max_no==0)
															{
																$max_no=10000;
															}
															
															$next_no = $max_no+1;
															
															
													 
															   $query_header ="INSERT INTO
																		    t_".$key_table_prefix."_invoice_header
																		    (
																			
																			device_id  ,
																			ivh_doc_no  ,
																			
																			ivh_ivh_id  ,
																			ivh_customer_code  ,
																			ivh_customer_name  ,
																			ivh_customer_address_billing  ,
																			ivh_customer_address_delivery  ,
																			ivh_customer_gst_no  ,
																			ivh_customer_discount  ,
																			ivh_date  ,
																			ivh_discount_total  ,
																			ivh_total_before_gst  ,
																			ivh_gst_amount  ,
																			ivh_total_amount  ,
																			ivh_rounding_amount  ,
																			ivh_total_amount_rounded  ,
																			ivh_payment_amount  ,
																			ivh_change_amount  ,
																			
																			ivh_latitude  ,
																			ivh_longitude  ,
																			
																			ivh_status  ,
																			
																			ivh_operation_mode  ,
																			
																			ivh_partial_paid  ,
																			ivh_amount_paid  ,
																			
																			
																			created_date ,
																			
																			modified_date ,
																			client_id 
																		    )
																		    VALUES
																		    (
																			
																			'SERVER',
																			
																			'".$next_no."',	
																			'".$next_no."',
																		
																			'".$this_customer_code."',
																			'".$this_customer_name."',
																			'".$this_customer_address_billing."',
																			'".$this_customer_address_delivery."',
																			'".$this_customer_gst_no."',
																			'".($this_customer_discount_percentage+0)."',
																			'".$this_transaction_date."',
																			
																			'0.00',
																			'0.00',
																			'0.00',
																			'0.00',
																			'0.00',
																			'0.00',
																			'0.00',
																			'0.00',
																			
																			'',
																			'',
																			
																			'POSTED',
																			
																			'ORDER',
																			
																			'0.00',
																			'0.00',
																			
																			
																			'".$this_transaction_date." 00:00:00',
																			
																			'".date("Y-m-d H:i:s")."' ,		
																			
																			'".$client_id."'
																		    )
																		   
																			
																		    ;
																		    ";
															   	$result_header = mysqli_query($dbconn,$query_header) or die("Error ".mysqli_error($dbconn));
																//$result_header = mysqli_query($dbconn,$query_header);
																//echo mysqli_error($dbconn);
																
																
																
																
																
															   	$this_header_id = mysqli_insert_id($dbconn);
															   
																	
																$header_created=true;
																	
																	
															
															
														}
													    
													    
													    
													    
													     $ivi_unique_id = $this_header_id.substr(10000+($row_counter-1),1,4);
													    
													    
													    	//get product info:
														
														
														
														$query_get_product="select * from 
																t_".$key_table_prefix."_product 
																where pdt_code= '".$this_product_code."'
																or pdt_barcode= '".$this_product_code."'
																
																";												
														$result_get_product = mysqli_query($dbconn, $query_get_product);
														if($line_get_product = mysqli_fetch_array($result_get_product,MYSQLI_ASSOC))
														{
														
														
															
															$total_before_gst = number_format($this_unit_price * $this_quantity,2,".","");
															 
															 $gst_percentage  =  $lib_gst[$line_get_product["pdt_gst_code"]];
															 
															 if($gst_percentage>0)
															 {
																 if($line_get_product["pdt_gst_mode"]=="INCLUSIVE")
																 {
																	 
																	$total_amount = number_format($this_unit_price * $this_quantity,2,".","");  
																	$gst_amount = number_format($total_amount/(100+$gst_percentage)*100,2,".","");   
																	
  																	$total_before_gst = $total_amount - $gst_amount;
																	
																	 
																	 
																 }
																 else
																 {
																	 
																	$gst_amount = number_format($total_before_gst * $gst_percentage/100,2,".","");   
 																	$total_amount = $total_before_gst + $gst_amount;
																	 
																	 
																 }
																 
															 }
															 else {
															 	
																$gst_amount = 0;
																$total_amount = $total_before_gst;
 																 
																
																
															 }
															 
															 
															 
															
															$query_item = " INSERT INTO
														                      t_".$key_table_prefix."_invoice_item
														                      (
														  					
														  					
														  			 		ivi_ivi_id,
														  					device_id, 
														  					
														  			 		ivi_doc_no ,
																			
																			ivi_header_id ,
																			
																			
																			
														  			 		ivi_product_code, 
														  			 		ivi_product_barcode, 
														  			 		ivi_product_name,
														  			 		ivi_serial_no, 
														  			 		ivi_remark,
	
														  			 		ivi_price,  
														  			 		ivi_cost, 
														  			 		ivi_quantity,
														  			 		ivi_uom,
														  					
														  			 		ivi_attribute,  
														  			 		ivi_size,
														  					ivi_color,
														  					ivi_pattern,
														  					
														  			 		ivi_total_addon,
	
														  			 		ivi_discount_percentage,
														  			 		ivi_discount_value,
														  		 			ivi_discount_total,  
														  			 		ivi_total_before_gst, 
	
														  			 		ivi_gst_mode, 
														  			 		ivi_gst_code,  
														  			 		ivi_gst_percentage, 
														  			 		ivi_gst_amount,  
	
														  			 		ivi_total_amount,  
														  			 		ivi_status,
														  					
														  					ivi_salesperson,
																			
																			ivi_storage_location, 
	
														  					created_date ,					
														  					modified_date ,
														  					client_id 
														                      )
														                      VALUES
														                      (
														  	                    
														  					'".$ivi_unique_id."',
														  					'SERVER', 
														  					
														  			 		'".$next_no."' ,
																			
																			'".$this_header_id."' ,
																			
																			
														  			 		'".$line_get_product["pdt_code"]."', 
														  			 		'".$line_get_product["pdt_barcode"]."', 
														  			 		'".$line_get_product["pdt_name"]."', 
														  			 		'', 
														  			 		'',
	
														  			 		'".$this_unit_price."',  
														  			 		'".$line_get_product["pdt_cost"]."', 
														  			 		'".$this_quantity."',
														  			 		'".$line_get_product["pdt_uom"]."',
														  					
														  			 		'',  
														  			 		'',
														  					'',
														  					'',
														  					
														  					
														  			 		'0.00',
	
														  			 		'0.00',
														  			 		'0.00',
														  		 			'0.00',
														  			 		'".($total_before_gst+0)."', 
	
														  			 		'".$line_get_product["pdt_gst_mode"]."', 
														  			 		'".$line_get_product["pdt_gst_code"]."',  
														  			 		'".($gst_percentage+0)."', 
														  			 		'".($gst_amount+0)."',  
	
														  			 		'".($total_amount+0)."',  
														  			 		'POSTED',
														  					
														  					'',
	
																			'".$line_get_product["pdt_storage_location"]."',
	
	
														  					'".$this_transaction_date." 00:00:00',				
														  					'".date("Y-m-d H:i:s")."' ,					
														  					'".$client_id."' 
														  					
														                      )
														                     ;
														                      ";
														      $result_item = mysqli_query($dbconn,$query_item) or die("Error ".mysqli_error($dbconn));
														      
															 
															 
															 $header_total_before_gst =  $header_total_before_gst + $total_before_gst;
															 $header_total_gst = $header_total_gst +  $gst_amount;
															 $header_total = $header_total + $total_amount;
																  
																  
													    	
														}
													    
													    
													          
													  			
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    
													    		echo "<font color=#2abb9b >";
															
																echo "<span class='fa fa-cloud-upload fa-lg' style='color:#2abb9b; '></span> ";
													    			echo "Uploaded";
													    		echo "</font>";
													    
													    echo "</td>";
													    
													    
													    
												    }
												    //===============================================================================================================
												    
												    
												    
												    
										
											    
											echo "</tr>";
										 
											
										    }
										    
									
											    
										    
										   
					                          
											   
											   
											$row_counter++;
						                        
						                        
						                    }
				                    
				                    	echo "</table>";
				                    
				                    	fclose($handle);
									
									echo "<br><font size=3>Total Amount: <b>".number_format($upload_total_amount,2,".",",")."</b></font>";
									
									
									
									
									
									
									if($_REQUEST["confirm_upload"]=="YES")
									{
										
										
										
												
										$query_refresh_header = "update t_".$key_table_prefix."_invoice_header
												
												set 
												
												ivh_total_before_gst='".($header_total_before_gst+0)."',
												ivh_gst_amount='".($header_total_gst+0)."',
												ivh_total_amount='".($header_total+0)."',
												ivh_rounding_amount='".($header_total+0)."',
												ivh_total_amount_rounded='".($header_total+0)."'
												
													
												where ivh_id = '".$this_header_id."'
												";
												
										$result_refresh_header = mysqli_query($dbconn,$query_refresh_header) or die("Error ".mysqli_error($dbconn));
										
										
										
											
											
										
										
									}
									
									
									
									
				                	}
				            
				            
				        		}
				            
				            
						  
						  
				            
				        echo "</td>";
				    echo "</tr>";
				    
				 
				 
				     echo "<tr>";
				        echo "<td bgcolor=#ECF0F1 >";
				            echo "<b>Step 3:</b> Upload Confirmation<br>";
				            
				            
				            
				            
				            
				            
				        echo "</td>";
				    echo "</tr>";
				    
				    echo "<tr>";
				        echo "<td bgcolor=#fff >";
				          
						
						   if($_REQUEST["confirm_upload"]=="YES")
						   {
							   
							   echo "<span class='fa fa-cloud-upload fa-lg' style='color:#2abb9b; '></span> ";
								 echo "Uploaded";
							   
							   
						   }
						   else {
						   	
							
							   		if($data_found)
			 				            {
			 				                
			 							 
			 							 
			 							 if($total_error>0)
			 							 {
			 								 echo "<font color=#ff0000  ><span class='fa fa-times fa-lg' style='color:#cf000f; '></span> Total of <b>".($total_error+0)."</b> error(s) found. Please rectify the file before you can upload.</font>";
			 								 
			 							 }
			 							 else {
			 								 echo "<input type='button' class='button button-green' value='Upload Data' onclick=\"if(confirm('Are you sure?')){document.form_import_sales_transaction.confirm_upload.value='YES';mon_submit_form('form_import_sales_transaction','mod/import_sales_transaction/index.php')}\" >";
			 							 
			 							   
			 								
			 							 }
			 				                
			 				              
			 				                
			 				            }
			 				            else
			 				            {
			 				                echo "Please Upload Data File";    
			 				                
			 				            }
			 				            
						   }
						
						
				         
				            
				        echo "</td>";
				    echo "</tr>";
				    
				 
				echo "</table>";
	
	
	echo "</div>";
	
	
echo "</form>";

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

?>
