<?php 
set_time_limit(300);
include "../../includes/start_ajax.php";

?>

<div  align=left >
<?php


  echo "<section class='view_toolbar'>";
                    echo "<div class='view_toolbar_left' style='width:50%;'>";
						echo "<span class='fa fa-folder-open-o'></span> DBF Product Sync";
                    echo "</div>";
                    echo "<div class='view_toolbar_right'>";	
    
					echo "<span class='fa-stack fa-lg' style='cursor:pointer;' onclick=\"mon_refresh_tab(mon_current_selected_tab);\" >";
						echo "<i class='fa fa-circle fa-stack-2x' style='color:#34495E;' ></i>";
						echo "<i class='fa fa-refresh fa-stack-1x'  style='color:#ffffff;'></i>";
					echo "</span>";
					
			
						
	
                    echo "</div>";
    echo "</section>";
    
    

?>

<?php

$dbf_file_path="";

$query_get="SELECT * FROM t_".$key_table_prefix."_dbf ";
$result_get=mysqli_query($dbconn,$query_get)or die("Get Data failed- Err msg  ". mysqli_error($dbconn));
if($line_get=mysqli_fetch_array($result_get,MYSQLI_ASSOC))
{
    $dbf_file_path = $line_get["dbf_product_path"];	    
}





$this_url="mod/dbf_product_sync/index.php";
$display_url="mod/dbf_product_sync/display.php";
	
	

	//-------------preview dbf----------------------------------------------------



$mapping["ITEMNO"]="pdt_code";
$mapping["DESP"]="pdt_name";
$mapping["DESPA"]="pdt_name2";
$mapping["PRICE"]="pdt_price";



	
	
	
	require_once "../../plugin/phpxbase-2006-10-11/Column.class.php";
	require_once "../../plugin/phpxbase-2006-10-11/Record.class.php";
	require_once "../../plugin/phpxbase-2006-10-11/Table.class.php";
	require_once "../../plugin/phpxbase-2006-10-11/WritableTable.class.php";
	
	
			if(is_readable($dbf_file_path))
			{
				
				
				
				
				
					if(is_writable($dbf_file_path))
					{
						
					}
					else {
						echo "<div style='float:left;border-radius:5px;border:1px solid #16A085; color:#fff; background:#1ABC9C;padding:1%;margin:1%;width:96%;'>";
							echo "File is not writable.";
						echo "</div>";
					}
					
					
					echo "<div style='float:left;border-radius:5px;border:1px solid #16A085; color:#000; background:none;padding:1%;margin:1%;width:96%;'>";
						
						$file_name=$dbf_file_path;
				
						echo "<b>Transaction File Analysis</b><br>";
				       	echo "Openning: ".$file_name."<br>";
				       
				       	$table = new XBaseTable($file_name);
				       	$table->open();
			
			
					
						
						
			  
				          echo "Successfully Openned: ".$file_name."<br>";  
				          echo "Reading File: ".$file_name."<br>";
				           
						 
						 
						 
						 //=============================start sync script==================================================
						 if($_REQUEST["action"]=="SYNC")
						 {
							 
							 
							 
							 while ($record = $table->nextRecord())
					           {
					 			
					 			//reset all column value for this row:
					 			unset($row_insert);
								
								//get each column
					           	foreach ($table->getColumns() as $i=>$c) {
					               	
									if(array_key_exists($c->getName(),$mapping))
									{
										
										if(($c->getType()=="C")or($c->getType()=="D")or($c->getType()=="T")or($c->getType()=="N")or($c->getType()=="L")or($c->getType()=="F"))
	  					                  	{
	  					                      
	  					                      	if($c->getType()=="D")
	  					                      	{    
	  					                          	$data =$record->getString($c);  
	  					                      	}
	  					                      	elseif(($c->getType()=="F"))
	  					                      	{
	  					                          	$data=$record->getFloat($c);
	  					                      	}
	  					                      	elseif(($c->getType()=="N"))
	  					                      	{
	  					                          	$data=$record->getFloat($c);
	  					                      	}
	  					                      	else{
	  					                          	$data=$record->getString($c);
	  					                      	}
	  					                  	}
	  					                  	else
	  					 				{
	  					                      	$data="Unknown";
	  					                  	}
	  					                      
										//setup data for SQL  
										$row_insert[$mapping[$c->getName()]] = str_replace("'","``",$data);
									}
					                }
								 
								 
								 
								 $this_date = date("Y-m-d H:i:s");
								 $client_id=0;
								 
								 $query_sync="INSERT INTO
											  t_".$key_table_prefix."_product
											  (
											  pdt_code,
											  pdt_name,
											  
											  pdt_price,
											  
											  
											  pdt_status,
											  
											  created_date,
											  modified_date,
											  
											  client_id
											  )
											  VALUES
											  (
											  '".$row_insert["pdt_code"]."',
											  '".$row_insert["pdt_name"]." ".$row_insert["pdt_name2"]."',
											  
											  '".$row_insert["pdt_price"]."',
											  'ACTIVE',
											  
											  '".$this_date."',
											  '".$this_date."',
											  
											  '".$client_id."'
											  )
											  ON DUPLICATE KEY UPDATE
											  pdt_name = '".$row_insert["pdt_name"]." ".$row_insert["pdt_name2"]."',
											  pdt_price = '".$row_insert["pdt_price"]."',
											  modified_date = '".$this_date."'
											  
											  ;
											  ";
								 $result_sync = mysqli_query($dbconn,$query_sync) or die("Error ".mysqli_error($dbconn));
								 
								 
								 
					                
					           }
							 
							 
							echo "<div style='float:left;border-radius:5px;border:1px solid #16A085; color:#fff; background:#1ABC9C;padding:1%;margin:1%;width:96%;'>";
			 					 echo "Sync Success";
			 				echo "</div>";
							 
						 }
						 //=============================end sync script==================================================
						 
						 
			     	echo "</div>";
				
					echo "<div style='float:left;border-radius:5px;border:1px solid #95A5A6; background:#ffffff;padding:1%;margin:1%;width:96%;'>";
						
						
						
					echo "<span class='fa-stack fa-2x' style='cursor:pointer;float:right;' onclick=\"if(confirm('Are you sure?')){mon_send_get('mod/dbf_product_sync/index.php','?action=SYNC','div_main');}\" >";
						echo "<i class='fa fa-circle fa-stack-2x' style='color:#3498DB;' ></i>";
						echo "<i class='fa fa-bolt fa-stack-1x'  style='color:#ffffff;'></i>";
					echo "</span>";
					
					
						echo "<table border=0 cellpadding=5 cellspacing=1 bgcolor=#95A5A6 >";
							echo "<tr>";
								echo "<td bgcolor=#ECF0F1 >";
									echo "<b>Field Name</b>";
								echo "</td>";
								
								echo "<td bgcolor=#ECF0F1 >";
									echo "<b>Field Type</b>";
								echo "</td>";
								
								echo "<td bgcolor=#ECF0F1 >";
									echo "<b>Field Length</b>";
								echo "</td>";
								
								
								echo "<td bgcolor=#ECF0F1 >";
									echo "<b>Mapping</b>";
								echo "</td>";
								
								
								echo "<td bgcolor=#ECF0F1 >";
									echo "<b>PServer Field</b>";
								echo "</td>";
								
							echo "</tr>";
					          foreach ($table->getColumns() as $i=>$c) {
								echo "<tr>";
					          		echo "<td bgcolor=#fff >";
										echo $c->getName()."<br>";
									echo "</td>";
									
									echo "<td bgcolor=#fff >";
										echo $c->getType()."<br>";
									echo "</td>";
									
									echo "<td bgcolor=#fff >";
										echo $c->getLength()."<br>";
									echo "</td>";
									
									
									
									if(array_key_exists($c->getName(),$mapping))
									{
										
										echo "<td bgcolor=#fff align=center >";
											echo "<font color=#16A085 ><span class='fa fa-arrow-right fa-lg'></span></font>";
										echo "</td>";
										
										
										echo "<td bgcolor=#fff >";
											echo "".$mapping[$c->getName()];
										echo "</td>";
										
										
										
									}
									else {
										echo "<td bgcolor=#BDC3C7 >";
											echo "<br>";
										echo "</td>";
										
										echo "<td bgcolor=#BDC3C7 >";
											echo "<br>";
										echo "</td>";
									}
									
									
							    	echo "</tr>";
								
											
											
								
					          }
							
						echo "</table>";
						
						
						
						
						
						
						
						
		               echo "</div>";
						
					
		                    
				
			}
			else {
				echo "<div style='float:left;border-radius:5px;border:1px solid #C0392B;color:#fff; background:#E74C3C;padding:1%;margin:1%;width:96%;'>";
					 echo "File Not Found.";
				echo "</div>";
			}
	
			
	                    
	               	
	
	
	?> 


</form>
</div>

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

?>
