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

$for = $_REQUEST['for'];
if($for != "salesmanquota"){
	include("../session.php");	
}

include_once("functions.php");

$target_dir = "uploads/";
$target_file = $target_dir . $_REQUEST['for'] . ".csv";

$a = (isset($_REQUEST['a']))?$_REQUEST['a']:"";
$file = $target_file;
$output['result'] = false;
$output['for'] = $for;
$output['a'] = $a;

if($for == "inventory"){
	$params = array();
	if ($a == "append") {
		$params['AddToAltas'] = true;
	}elseif ($a == "replace") {
		mysqli_query($con, "TRUNCATE INVENTORY");
		$params['AddToAltas'] = false;
	}
	$output['result'] = CSVImport("INVENTORY", array("`Group`", "`Line`", "`SKU`", "`Description`", "`Notes`", "`Interchange`",  "`Qty`", "`AvgCost`", "`Core`", "`PriceA`", "`PriceB`", "`PriceC`", "`PriceD`", "`PriceE`"), "uploads/inventory.csv", $params);
	//$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`, `Cost`, `Core`, `Interchange`)");
}
if($for == "customers"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE CUSTOMER");
	
	$output['result'] = CSVImport("CUSTOMER", array("`Name`", "`City`", "`AccountNumber`", "`Email`","`Salesman`","`Phone`"), "uploads/customers.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 == "salesmen"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE SALESMAN");
		
	$output['result'] = CSVImport("SALESMAN", array("`ID`", "`FullName`", "`Username`", "`Email`"), "uploads/salesmen.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 == "alta"){
	if($a == "replace")
		mysqli_query($con, "DELETE FROM ALTAS_DETAIL WHERE AltaID = " . $_REQUEST['id']);
		
	$output['result'] = CSVImport("ALTAS_DETAIL", array("`AltaID`","`Group`", "`Line`", "`SKU`"), "uploads/alta.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 == "invoice"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE INVOICE");
		
	$output['result'] = CSVImport("INVOICE", array("`ST`", "`InvoiceDate`", "`AccountNumber`", "`Job`", "`InvoiceNumber`", "`Reference`", "`Subtotal`","`SalesTax`", "`Terms`"), "uploads/invoice.csv");
}
if($for == "credit"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE CREDIT");
		
	$output['result'] = CSVImport("CREDIT", array("`ST`", "`CreditDate`", "`AccountNumber`", "`Job`", "`CreditNumber`", "`Reference`", "`Subtotal`","`SalesTax`"), "uploads/credit.csv");
}
if($for == "special"){
	if($a == "replace")
		mysqli_query($con, "TRUNCATE SPECIAL");
		
	$output['result'] = CSVImport("SPECIAL", array("`Line`", "`SKU`", "`RegularPrice`"), "uploads/special.csv");
}
if($for == "salesmanquota"){
	//this will allways truncate the temp table before importing the data.
	mysqli_query($con, "TRUNCATE SALESMAN_QUOTA_TEMP");
	
	$output['result'] = CSVImport("SALESMAN_QUOTA_TEMP", array("`SalesmanID`", "`SalesmanName`", "`MTD`", "`TransactionCounter`"), "uploads/salesmanquota.csv");
	
	$query = "DELETE FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = '' OR SalesmanID LIKE 'SAL';\n";
	mysqli_query($con, $query);
	$importDate = time();
	if(isset($_REQUEST['impDate'])){
		$importDate = strtotime($_REQUEST['impDate']);
	}
	$query = "INSERT INTO `SALESMAN_QUOTA` (Salesman, `Month`, `Year`, `MTD`) (SELECT SalesmanID, " . date("m", $importDate-(60*60*24*1)) . ", " . date("Y", $importDate-(60*60*24*1)) . ", MTD FROM SALESMAN_QUOTA_TEMP) ON DUPLICATE KEY UPDATE `MTD` = VALUES(`MTD`);";
	mysqli_query($con, $query);
	$query = "UPDATE SALESMAN_QUOTA SET MTD = (SELECT SUM(MTD) FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = 17 || SalesmanID = 37) WHERE Salesman = 37 AND `Month` = " . date("m", $importDate-(60*60*24*1)) . " AND `Year` = " . date("Y",$importDate-(60*60*24*1)) . ";";
	$query .= "UPDATE SALESMAN_QUOTA SET MTD = (SELECT SUM(MTD) FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = 18 || SalesmanID = 38) WHERE Salesman = 38 AND `Month` = " . date("m", $importDate-(60*60*24*1)) . " AND `Year` = " . date("Y",$importDate-(60*60*24*1)) . ";";
	$query .= "UPDATE SALESMAN_QUOTA SET MTD = (SELECT SUM(MTD) FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = 15 || SalesmanID = 35) WHERE Salesman = 35 AND `Month` = " . date("m",$importDate-(60*60*24*1)) . " AND `Year` = " . date("Y",$importDate-(60*60*24*1)) . ";";
	$query .= "UPDATE SALESMAN_QUOTA SET MTD = (SELECT SUM(MTD) FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = 'S1' || SalesmanID = 'ST') WHERE Salesman = 'S1' AND `Month` = " . date("m",$importDate-(60*60*24*1)) . " AND `Year` = " . date("Y",$importDate-(60*60*24*1)) . ";";
	$query .= "UPDATE SALESMAN_QUOTA SET MTD = (SELECT SUM(MTD) FROM SALESMAN_QUOTA_TEMP WHERE SalesmanID = 'A1' || SalesmanID = 'AT') WHERE Salesman = 'A1' AND `Month` = " . date("m",$importDate-(60*60*24*1)) . " AND `Year` = " . date("Y",$importDate-(60*60*24*1)) . ";";
	mysqli_multi_query($con, $query);
	$query = "TRUNCATE SALESMAN_QUOTA_TEMP;";
	mysqli_query($con, $query);
	
}
if($for == "salesmanquotagoal"){
	if($a == "replace"){
		//mysqli_query($con, "TRUNCATE SALESMAN_QUOTA");
	}
	$output['result'] = CSVImport("SALESMAN_QUOTA", array("`Salesman`", "`Month`", "`Year`", "`Goal`"), "uploads/salesmanquotagoal.csv");
}
if($for == "pickingincentive"){
	if($a == "replace"){
		//mysqli_query($con, "");
	}
	$output['result'] = CSVImport("PICKING_INCENTIVE", array("`ST`", "`TransactionDate`", "`PO`", "`InvoiceNumber`", "`LineNum`", "`Description`", "`NetSale`", "`Units`"), "uploads/pickingincentive.csv");
	$query = "DELETE FROM PICKING_INCENTIVE WHERE Description LIKE '*****************%'";
	mysqli_query($con, $query);
}
if($for == "pricingplanlistcustomer"){
	if($a == "replace"){
		mysqli_query($con, "TRUNCATE PRICE_PLAN_LIST_CUSTOMER");
	}
	$output['result'] = CSVImport("PRICE_PLAN_LIST_CUSTOMER", array("`PriceListID`", "`AccountNumber`"), "uploads/pricingplanlistcustomer.csv");
}
if($for == "pricingplanlistparts"){
	if($a == "replace"){
		mysqli_query($con, "TRUNCATE PRICE_PLAN_LIST_DETAIL");
	}
	$output['result'] = CSVImport("PRICE_PLAN_LIST_DETAIL", array("`PriceListID`", "`Line`", "`SKU`", "`Price`"), "uploads/pricingplanlistparts.csv");
}
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);

?>