<?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);
ini_set("display_errors", 1);
error_reporting(E_ALL);

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

	//$stmt = $con->prepare("INSERT INTO CUSTOMER (`Name`, `SKU`, `Description`, AvgCost, Cost, Qty) VALUES (?,?,?,?,?,?)");
	// if(!$stmt){
	// 	die("error on db: " . mysqli_error($con) );	
	// }
	//$stmt = $con->prepare("UPDATE INVENTORY SET PriceA = ?, PriceB = ?, Cost = ? WHERE SKU = ? AND `Line` = ?");
	$stmtLine = $con->prepare("INSERT INTO `LINE` (`Code`, `Name`, `NameEN`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `Name` = VALUES(`Name`), NameEN = VALUES(`NameEN`)");
	$lineCode = "";
	$lineName = "";
	$stmtLine->bind_param("sss", $lineCode, $lineName, $lineName);

	$sku = "";
	$desc = "";
	$line = "";
	$priceA = "";
	$priceB = "";
	$cost = "";
	$stmt = $con->prepare("INSERT INTO INVENTORY (`Line`, `SKU`, `Description`, PriceA, PriceB, Cost, AvgCost) VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE PriceA = VALUES(PriceA),PriceB = VALUES(PriceB), Cost = VALUES(Cost), Qty = VALUES(Qty), AvgCost = VALUES(AvgCost)");
	$stmt->bind_param("sssdddd", $line, $sku, $desc, $priceA, $priceB, $cost, $cost);
	
	while (($buffer = fgets($handle)) !== false) {
		//var_dump( stripos($buffer, "Run Date") !== false );
		//var_dump(preg_match("/^\d{7} .*$/", $buffer));
		//echo preg_last_error();
		if(stripos($buffer, "PRODUCT LINE:") === 0){			
			
			// $lineCode = substr($buffer,15,4);
			// $lineName = substr($buffer,21,25);
			// //echo "Line: " . $lineCode . "<br>Name: " . $lineName . "<br>";
			// $stmtLine->execute();

		}elseif(preg_match("/^.*Run Date.*$/", $buffer)){
			//$currCustomer = substr($buffer, 0,7);
			echo "1";
			//echo "cus";
		}elseif(preg_match("/^.*I\/M Date.*$/", $buffer) || preg_match("/^.*         .*PROD.*$/", $buffer)){
			//$currCustomer = substr($buffer, 0,7);
			echo "2";	
			//echo "cus";
		}elseif(preg_match("/^\n/", $buffer)){
			//$currCustomer = substr($buffer, 0,7);
			echo "3";
			//echo "cus";
		}elseif(preg_match("/^.*ITEM NUMBER.*$/", $buffer) || preg_match("/^.*    SORTED.*$/", $buffer)){
			//$currCustomer = substr($buffer, 0,7);

			//echo "cus";
		}else{
			

			$sku = trim(substr($buffer,0, 16));
			$desc = trim(substr($buffer,17,30));
			$line = trim(substr($buffer,48,4));
			$priceA = floatval(trim(substr($buffer,68,11)));
			$priceB = floatval(trim(substr($buffer,80,11)));
			$cost = floatval(trim(substr($buffer,110,11)));
			
			//
			$item = new Item("%", $line, $sku);
			$item->PriceA = $priceA;
			$item->PriceB = $priceB;
			$item->Cost = $cost;
			$item->AvgCost = $cost;
			if($item->Exists){
				echo "SKU: " . $sku . " ------- " . $desc . " --------- " . $line . " ----p1--> " . $priceA . " -----p2--> " . $priceB . " -----cst--> " . $cost . "<br>";
				$item->save();
			}else{
				$stmt->execute();
			}
			
			//echo mysqli_stmt_error($stmt);
		}
		
        $index++;
    }
}else{
	echo "Cannot open file";
}
//echo "<pre>";
//var_dump($custData);
//echo "</pre>";

	

// $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));



?>
