<?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("VENDORS.TXT", "r");
if($handle){
	$index = 0;

	
	// if(!$stmt){
	// 	die("error on db: " . mysqli_error($con) );	
	// }
	$stmt = $con->prepare("INSERT INTO VENDOR (`VendorID`, `Name`, PostalAddress1, PostalAddress2, PostalCity, PostalState, PostalZip, PostalCountry,MainPhone, Fax,Contact, PaymentTerms ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
	echo mysqli_error($con);
	$stmt->bind_param("isssssssssss",
		$VendorID,
		$CompanyName,
		$Address1,
		$Address2,
		$City,
		$State,
		$ZipCode,
		$PostalCountry,
		$Phone,
		$Fax,
		$Contact,
		$Terms);
	
		
	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, "VENDOR NO: ") === 2){			
			$vendor = new stdClass();
			$VendorID = intval(substr($buffer,14,7));
			$CompanyName = substr($buffer,34,30);
			//advance 2 lines
			$buffer = fgets($handle);
			$buffer = fgets($handle);
			$Address1 = substr($buffer,14,37);
			//advance 1 line
			$buffer = fgets($handle);
			$Address2 = substr($buffer,14,37);
			//advance 1 line
			$buffer = fgets($handle);
			$City = substr($buffer,14,37);
			//advance 1 line
			$buffer = fgets($handle);
			$State = substr($buffer,14,2);
			$ZipCode = substr($buffer,34,10);
			//advance 1 line
			$buffer = fgets($handle);
			$Phone = substr($buffer,14,32);
			//advance 1 line
			$buffer = fgets($handle);
			$Fax = substr($buffer,14,32);
			//advance 1 line
			$buffer = fgets($handle);
			$Contact = substr($buffer,14,10);
			$PostalCountry = substr($buffer,24,15);
			//advance 3 lines
			$buffer = fgets($handle);
			$buffer = fgets($handle);
			$buffer = fgets($handle);
			$Terms = substr($buffer,14,34);
			$stmt->execute();

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



?>
