<?php



$servername1 = "localhost";
$username1 = "sooggoo";
$password1= "4Compassion*";
$name1 = "tebarameats";
// Create connection
$remote = mysqli_connect($servername1, $username1, $password1, $name1);
// Check connection
if (!$remote) {
    die("Connection failed: " . mysqli_connect_error());
}



$currentdate = date('Y-m-d h:i:s', time());


//first sales data


//function the_file() {
    $files = glob("retailshop-nausori/counter1/sales/*.csv");
   // return $files[0]; // return the first filename
//}

//$file = the_file();
//get file handle

if (!empty($files)) {
   // die("Error: The directory already has a .csv file");


foreach($files as $file) {
  
	
	

	$FileHandle = fopen($file, 'r');

	


	//loop through file rows
	$LineNumber = 0;
	while ( ($myrow = fgetcsv($FileHandle, 10000, ',')) !== FALSE ) {

		
		$salesid = $myrow[0];
		$saletime = $myrow[1];
		$employeeid= $myrow[2];
		
		//$date1 = str_replace("/", "-", $transdate);

	
		//$date = date('Y-m-d',strtotime($date1));
		//$longdate = date('Y-m-d H:i:s',strtotime($date1));
		//$yeardate = date('Y',strtotime($date1));

		//$monthdate =date('m',strtotime($date1));
		
		
		//open vms trans and check if duplicate trans shows
		
		
			
			$SQL = "INSERT INTO sales_retailshops (sale_id,
									counter,
									loccode,
									sale_time,
									username)
				VALUES (
					'" . $salesid. "',
					'counter1',
					'3',
					
					'" . $saletime . "',
					'tebara1')";
		
		$result = $remote->query($SQL)  or die($remote->error);
         
		
//update systypes 0 journal so it gives tallies
		$LineNumber++;
	}

//put this file into processed folder
fclose($FileHandle);
	
}
}

	
	

	$Ignore = array(".","..",".ftpquota");
$OriginalFileRoot = "retailshop-nausori/counter1/sales";
$OriginalFiles = scandir($OriginalFileRoot);
$DestinationRoot = "retailshop-nausori/counter1/sales/processed";

foreach($OriginalFiles as $OriginalFile){
    if(!in_array($OriginalFile,$Ignore)){
        //$FileExt = pathinfo($OriginalFileRoot."\\".$OriginalFile, PATHINFO_EXTENSION); // Get the file extension
        $Filename = basename($OriginalFile, ".csv"); // Get the filename
        $DestinationFile = $DestinationRoot."/".$Filename.".csv"; // Create the destination filename 
        rename($OriginalFileRoot."/".$OriginalFile, $DestinationFile); // rename the file            
    }
}

            

	

	

    $files2 = glob("retailshop-nausori/counter1/salesdetails/*.csv");
   // return $files[0]; // return the first filename
//}

//$file = the_file();
//get file handle

if (!empty($files2)) {
   // die("Error: The directory already has a .csv file");


foreach($files2 as $file2) {
  
	
	

	$FileHandle2 = fopen($file2, 'r');

	





	//loop through file rows
	$LineNumber2 = 0;
	while ( ($myrow = fgetcsv($FileHandle2, 10000, ',')) !== FALSE ) {

		
		$salesid = $myrow[0];
		$itemid = $myrow[1];
		$lineid= $myrow[2];
		$qty= $myrow[5];
		$unitprice= $myrow[7];
		$discount =$myrow[8];
		
		//$date1 = str_replace("/", "-", $transdate);

	
		//$date = date('Y-m-d',strtotime($date1));
		//$longdate = date('Y-m-d H:i:s',strtotime($date1));
		//$yeardate = date('Y',strtotime($date1));

		//$monthdate =date('m',strtotime($date1));
		
		
		//open vms trans and check if duplicate trans shows
		

					
			$SQL = "INSERT INTO salesitems_retailshops (sale_id,item_id,line,quantity_purchased,item_unit_price,discount_percent,
									
									loccode,counter)
				VALUES (
					'" . $salesid. "',
					'" . $itemid. "',
					'" . $lineid. "',
					
					'" . $qty . "',
					'" . $unitprice. "',
					'" . $discount. "',
					'3', 'counter1')";
		
		$result = $remote->query($SQL)  or die($remote->error);
         
		
//update systypes 0 journal so it gives tallies
		$LineNumber2++;
	}

//put this file into processed folder
fclose($FileHandle2);
	
}
}

	
	

	$Ignore = array(".","..",".ftpquota");
$OriginalFileRoot = "retailshop-nausori/counter1/salesdetails";
$OriginalFiles = scandir($OriginalFileRoot);
$DestinationRoot = "retailshop-nausori/counter1/salesdetails/processed";

foreach($OriginalFiles as $OriginalFile){
    if(!in_array($OriginalFile,$Ignore)){
        //$FileExt = pathinfo($OriginalFileRoot."\\".$OriginalFile, PATHINFO_EXTENSION); // Get the file extension
        $Filename = basename($OriginalFile, ".csv"); // Get the filename
        $DestinationFile = $DestinationRoot."/".$Filename.".csv"; // Create the destination filename 
        rename($OriginalFileRoot."/".$OriginalFile, $DestinationFile); // rename the file            
    }
}

            



mysqli_close($remote);



echo "done";

?>