<?php
include_once('connect.php');
include_once('functions.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);

$handle = fopen("CUSTOMERS.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) );	
	// }
	// $CustomerName = "";
	// print_r($stmt->bind_param("sssddd", $Line, $SKU, $Description, $AvgCost, $AvgCost, $Qty));
	$custData = [];
	while (($buffer = fgets($handle)) !== false) {
		//var_dump( stripos($buffer, "Run Date") !== false );
		if(stripos($buffer, "Run Date: 03/29/22") !== false){
			if($custBuffer){
				//var_dump($custBuffer);
				array_push($custData, $custBuffer);
			}
			
			$custBuffer = $buffer;

		}else{
			$custBuffer .= $buffer;
		}
	
        $index++;
    }
}else{
	echo "Cannot open file";
}

$stmt = $con->prepare("INSERT INTO CUSTOMER (
	`Name`,
	`ShipToAddress1`, 
	`ShipToAddress2`, 
	`ShipToCity`,
	`ShipToState`, 
	`ShipToZip`, 
	`PriceLevel`, 
	TaxID2, 
	`Salesman`, 
	`Contact`,
	`AccountNumber`,
	`Phone`,
	`Terms`,
	`CreditLimit`
	) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
	
if(!$stmt){
	
	die("error on db: " . mysqli_error($con) );	
}
	$Company = ""; 
	$ShipToAddress1 = ""; 
	$ShipToAddress2 = ""; 
	$ShipToCity = "";
	$ShipToState = ""; 
	$ShipToZip = ""; 
	$PriceLevel = ""; 
	$TaxID2 = ""; 
	$Salesman = ""; 
	$Contact = "";
	$AccountNumber = "";
	$Phone = "";
	$Terms = "";
	$CreditLimit = "";

$stmt->bind_param("sssssssssssssd", 
	$Company, 
	$ShipToAddress1, 
	$ShipToAddress2, 
	$ShipToCity,
	$ShipToState, 
	$ShipToZip, 
	$PriceLevel, 
	$TaxID2, 
	$Salesman, 
	$Contact,
	$AccountNumber,
	$Phone,
	$Terms,
	$CreditLimit
);
foreach($custData as $cust){
	echo "<pre>";
	$fields = [

		[" PHONE NO: ", 21, "Phone"],
		[" NAME:  ", 30, "Company"],
		["CUSTOMER NO:  ", 13, "AccountNumber"],
		["   STATE:  ", 2, "ShipToState"],
		["  FAX:  ", 21, "Fax"],
		["  CONTACT:  ", 40, "Contact"],
		[" TERMS CODE:  ", 2, "Terms"],
		["SALESPERSON: ", 5, "Salesman"],
		[" ZIP CODE: ", 6, "ShipToZip"],
		["  CITY:  ", 25, "ShipToCity"],
		[" ADDRESS:  ", 35, "ShipToAddress1"],
		["  \n              ", 30, "ShipToAddress2"],
		["TAX EXMPT #:  ", 30 , "TaxID2"],
		[" EXT: ", 6, "Extension"],
		[" PRICE LEVEL: ", 2, "PriceLevel"],
		["  COMMENT:  ", 32, "Comment"],
		["  SALES TAX - STATE: ", 3, "TaxCode"],
		[" CREDIT LIMIT:  ", 11, "CreditLimit"],
		["SALESPERSON: ", 35, "SalesmanColumn"],
		

	];
	foreach($fields as $set){
		$matches = [];
		(preg_match("/(?<=" . $set[0] . ").{" . $set[1] . "}/", $cust, $matches));
		$$set[2] = trim($matches[0]);

	}
	if(stripos($Comment, "R.C.") !== false){
		$TaxID2 = trim(substr($Comment,4,100));
	}
	if($PriceLevel == "1"){
		$PriceLevel = "A";
	}elseif($PriceLevel == "2"){
		$PriceLevel = "B";
	}elseif($PriceLevel == "3"){
		$PriceLevel = "C";
	}elseif($PriceLevel == "4"){
		$PriceLevel = "D";
	}
	$CreditLimit = floatval($CreditLimit);
	var_dump($Company);
	$stmt->execute();
	// var_dump($stmt);
	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));



?>
