<?php 
include_once("../connect.php");
global $con;
include_once("../session.php");


$target_dir = "uploads/";
$target_file = $target_dir . $_REQUEST['for'] . ".csv";
$for = $_REQUEST['for'];
$a = (isset($_REQUEST['t']))?$_REQUEST['t']:"";
$file = $target_file;
$output['result'] = false;
$output['for'] = $for;
$output['t'] = $a;
if($for == "sales"){
	$params = array();
	if ($a == "append") {
		
	}elseif ($a == "replace") {
		mysqli_query($con, "TRUNCATE COMMISSION_SALES_RPT");
	}
	
	$inputFile = "uploads/sales.csv";
	$outputFile = "uploads/sales_split_";
	
	$splitSize = 500;
	
	$in = fopen($inputFile, 'r');
	
	$rowCount = 0;
	$fileCount = 1;
	while (!feof($in)) {
		if (($rowCount % $splitSize) == 0) {
			if ($rowCount > 0) {
				fclose($out);
			}
			$out = fopen($outputFile . $fileCount++ . '.csv', 'w');
		}
		$data = fgetcsv($in);
		if ($data)
			fputcsv($out, $data);
		$rowCount++;
	}
	
	fclose($out);
	$output['result']['trueCount'] = 0;
	$output['result']['rowCount'] = 0;
	$output['result']['falseCount'] = 0;
	$output['result']['error'] = array();
	$output['result']['result'] = true;
	for($i = 1; $i < $fileCount; $i++){
		$result = CSVImport("COMMISSION_SALES_RPT", array("`Branch`", "`Salesman`", "`Price`", "`GrossMargin`", "`MarginPercent`", "`PartNumber`",  "`Description`", "`InvoiceNumber`", "`TransactionDate`", "`AccountNumber`", "`CustomerName`", "`LineCode`"), "uploads/sales_split_" . ($i) . ".csv");
		$output['result']['trueCount'] += $result['trueCount'];
		$output['result']['rowCount'] += $result['rowCount'];
		$output['result']['falseCount'] += $result['falseCount'];
		$output['result']['result'] = $output['result']['result'] && $result['result'];
		
	}
	
	//$output['result'] = mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE INVENTORY FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'  (`warehouse`, `Group`, `Line`, `SKU`, `Description`, `Notes`,  `Qty`, `Price`, `Core`, `Interchange`)");
}
if($for == "payments"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE COMMISSION_PAID_INVOICE_RPT");
	
	$output['result'] = CSVImport("COMMISSION_PAID_INVOICE_RPT", array("`Salesman`", "`Branch`", "`AccountNumber`", "`CustomerName`","`InvoiceNumber`","`PaidDate`","`PaymentAmt`","`Discount`","`Tax`", "`Job`"), "uploads/payments.csv");
	$output['for'] = $for;
	$output['a'] = $a;
	//mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE CUSTOMER FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\r'  (`Name`, `City`, `AccountNumber`, `Email`, `Salesman`)");
}
if($for == "specials"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE COMMISSION_SPECIAL");
		
	$output['result'] = CSVImport("COMMISSION_SPECIAL", array("`Line`", "`SKU`"), "uploads/specials.csv");
	//$output['result'] = mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE SALESMAN FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'  (`ID`, `FullName`, `Username`, `Password`, `Email`)");
}
if($for == "specialCustomer"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE COMMISSION_SELECTIVE_SPECIAL_CUSTOMER");
		
	$output['result'] = CSVImport("COMMISSION_SELECTIVE_SPECIAL_CUSTOMER", array("`AccountNumber`"), "uploads/specialCustomer.csv");
	//$output['result'] = mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE SALESMAN FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'  (`ID`, `FullName`, `Username`, `Password`, `Email`)");
}
if($for == "specialCustomerItems"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE COMMISSION_SELECTIVE_SPECIAL_ITEM");
		
	$output['result'] = CSVImport("COMMISSION_SELECTIVE_SPECIAL_ITEM", array("`Line`", "`SKU`"), "uploads/specialCustomerItems.csv");
	//$output['result'] = mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE SALESMAN FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'  (`ID`, `FullName`, `Username`, `Password`, `Email`)");
}
if($for == "salesHeaders"){
	if($a == "replace")
		//mysqli_query($con, "TRUNCATE SALES_HISTORY");
		
	$output['result'] = CSVImport("SALES_HISTORY", array("`ST`", "`AccountNumber`", "`Job`", "`InvoiceNumber`", "`Reference`", "`Subtotal`", "`IVU`", "`Month`"), "uploads/salesHeaders.csv");
	//$output['result'] = mysqli_query($con, "load data local infile '".$target_file."' INTO TABLE SALESMAN FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'  (`ID`, `FullName`, `Username`, `Password`, `Email`)");
}
header('Content-Type: application/json');
$eventDetail = $output;
unset($eventDetail['error']);
event_log($_SESSION['Username'], strtoupper($a . " " . $for), print_r($eventDetail,true));
echo json_encode($output);

?>