<?php
include_once('connect.php');
include_once('functions.php');
include_once('classes/ClassLoader.php');
include_once('modules/payment/payment.classes.php');
//echo date('Y-m-d H:i:s', time()-4*60*60);


$handle = fopen("INVSTOCK.TXT", "r");
if($handle){
	$index = 0;

	$item = new stdClass();
	$stmt = $con->prepare("INSERT INTO INVENTORY (`Line`, `SKU`, `Description`, AvgCost, Cost, Qty) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE Cost = VALUES(Cost), Qty = VALUES(Qty), AvgCost = VALUES(AvgCost)");
	if(!$stmt){
		die("error on db: " . mysqli_error($con) );	
	}
	$Line = "";
	$SKU = "";
	$Description = "";
	$AvgCost = "";
	$Qty = "";
	print_r($stmt->bind_param("sssddd", $Line, $SKU, $Description, $AvgCost, $AvgCost, $Qty));

	while (($buffer = fgets($handle)) !== false) {
		//
		
		
		$skip = [];
		$skip[] = "Run Date:";
		$skip[] = "I/M Date:";
		$skip[] = "VENDOR NUMBER   PROD LN  ";
		$skip[] = "ITEM NUMBER      DESCRIPTION";
		$skip[] = "SORTED BY ITEM N";
		$skip[] = "STOCK STATUS REPORT";
		foreach($skip as $value){
			
			if(stripos($buffer, $value) !== false){
				//echo "<p>Skipped $value</p>";
				continue 2;
			}else{
				
				
			}
		}
		
		
			
		//echo $index;
		//echo $buffer . "." . ($index%3) . "<br>";
		if($index%3 === 2){
			//$item = new stdClass();
			$cursor = 0;
			$SKU = trim(substr($buffer,0,17));
			$cursor = 17;
			$Description = trim(substr($buffer, $cursor, 94));
			$cursor += 94;
			$cursor+= 11; //skip cycle:
			$AvgCost = floatval(trim(substr($buffer, $cursor, 10)));
			

			//echo $buffer . "<br>";
		}elseif($index%3 == 0){
			$Line = trim(substr($buffer, 18, 4));
			var_dump($Line);
			$cursor = 23;
			$Qty = floatval(trim(substr($buffer, 53,11)));
			
			//echo $buffer . "<br>";
		}else{
			
			$invItem = new Item("%",$Line, $SKU);
			if($invItem->Exists && trim($SKU) != ""){
				$invItem->Cost = $AvgCost;
				$invItem->AvgCost = $AvgCost;
				$invItem->Qty = $Qty;
				$invItem->save();
				echo "<pre>Record Updated: $SKU\n</pre>";
			}else{
				// echo "<pre>";
				// print_r($stmt);
				// echo "</pre>";
				// echo "<br>";
				
				
				if(!$stmt->execute()){
					echo $SKU . "<br>";
					echo mysqli_stmt_error($stmt);
				}					
			}

			


		}
        $index++;
    }
}else{
	echo "Cannot open file";
}

// $customer = new Customer("10012");
// $invoices = $customer->getInvoices();
// var_dump($customer);
// $payment = new Payment("1737");
// $payment->SendPaymentEmail();
// $invoice = new Invoic;e("2556");
// $invoice->sendEmail();

//sendEstimate("10812");
//$customer = new Customer("10012");
//$customer->SendCollectionsEmail();

//$customer->transferARToLoss();


function generateUPC(){
	$item = new Item("%", "BUL", "38888");


	$upc = $item->getUPC("1");
	$originalUPC = $upc;

	$upc = substr($upc,0, strlen($upc)-1);
	if(strlen($upc) == 11){
		$upc = "0" . $upc;
	}
	$upc = "2" . $upc;
	$i = 0;
	$sum = 0;
	for($i = strlen($upc)-1; $i >= 0; $i--){
		//echo $upc[$i];
		if($i % 2 == 0){
			$sum += $upc[$i]*3;
		}else{
			$sum += $upc[$i];
		}

	}
	if(round($sum,-1) > $sum){
		$tenth = round($sum,-1);
	}else{
		$tenth = round($sum,-1)+10;
	}
	$check = $tenth-$sum;

	echo "Item:" . $item->SKU . "<br>";
	echo "Original UPC: " . $originalUPC;
	echo "<br>";
	echo "<br>Master Carton Code (ITF-14): " . $upc . $check;


}


//sendEstimate('10254');
//sendOrder("2191");
?>

<?php
/**
 * Author: Abu Ashraf Masnun
 * URL: http://masnun.me
 */
 /*
class WorkerThreads extends Thread
{
    private $workerId;

    public function __construct($id)
    {
        $this->workerId = $id;
    }

    public function run()
    {
        sleep(rand(0, 3));
        echo "Worker {$this->workerId} ran" . PHP_EOL;
    }
}

// Worker pool
$workers = [];

// Initialize and start the threads
foreach (range(0, 5) as $i) {
    $workers[$i] = new WorkerThreads($i);
    $workers[$i]->start();
}

// Let the threads come back
foreach (range(0, 5) as $i) {
    $workers[$i]->join();
}
*/
 //var_dump(sendEstimate(10028));



?>
