<?php
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("../session.php");
}else{
	include_once("connect.php");
	include_once("session.php");
}


if(isset($_REQUEST['f'])){
	$function = $_REQUEST['f'];
	if($function == "getPricingPlanLists"){
		getPricingPlanLists();
	}elseif($function == "savePricePlanList"){
	   savePricePlanList($_REQUEST['ID'], $_REQUEST['Name']);
	}elseif($function == "getPriceListDefinitionInfo"){
	   getPriceListDefinitionInfo($_REQUEST['id']);
	}elseif($function == "savePricePlanListItem"){
		savePricePlanListItem($_REQUEST['ppline'], $_REQUEST['ppsku'], $_REQUEST['ppPrice'], $_REQUEST['ppid']);
	}elseif($function == "deletePricePlanListItem"){
		deletePricePlanListItem($_REQUEST['ppline'], $_REQUEST['ppsku'], $_REQUEST['ppid']);
	}elseif($function == "getPricePlanListItem"){
		getPricePlanListItem($_REQUEST['ppline'], $_REQUEST['ppsku'], $_REQUEST['ppid']);
	}elseif($function == "savePricePlanListCustomer"){
		savePricePlanListCustomer($_REQUEST['ppan'], $_REQUEST['ppid']);
	}elseif($function == "deletePricePlanListCustomer"){
		deletePricePlanListCustomer($_REQUEST['ppan'], $_REQUEST['ppid']);
	}elseif($function == "deletePriceListDefinition"){
		deletePriceListDefinition($_REQUEST['id']);
	}
}
function getPriceListDefinitionInfo($ID, $return = false) {
	global $con;
	$isAdmin = $_SESSION['isAdmin'];
	if (!$isAdmin) {
		//return false;
	}
	$query = "SELECT * FROM `PRICE_PLAN_LIST_DEF` WHERE `id` = '$ID'";
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_array($result, MYSQL_ASSOC);
	if ($return) {
		return $row;
	}else {
		echo json_encode($row);	
	}
	

}
function getPricingPlanLists(){
	global $con;
	$query = "SELECT PRICE_LIST_DEF.*, COUNT(PRICE_LIST_DETAIL.ID) as ItemCount, COUNT(PRICE_LIST_CUSTOMER.ID) as CustomerCount FROM PRICE_LIST_DEF, PRICE_LIST_DETAIL, PRICE_LIST_CUSTOMER WHERE PRICE_LIST_DEF.AccountNumber LIKE '' AND PRICE_LIST_DEF.id = PRICE_LIST_DETAIL.priceListID AND PRICE_LIST_DEF.id = PRICE_LIST_CUSTOMER.PriceListID";
	$result = mysqli_query($con, $query);
	$data = [];
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		array_push($data, $row);
	}
	echo json_encode($data);
}
function savePricePlanListItem($Line, $SKU, $Price, $PPListID){
	global $con;
	$query = "INSERT INTO PRICE_PLAN_LIST_DETAIL (PriceListID, Line, SKU, Price) VALUES ($PPListID, '$Line', '$SKU', $Price) ON DUPLICATE KEY UPDATE Price = $Price;";
	
	if(mysqli_query($con, $query)){
		event_log($_SESSION['Username'], 'ADD/EDIT Price Plan List Item', "Added/updated Item: $Line - $SKU to Price List $PPListID -> Price: $Price");
		echo("true");
		return;
	}else{
		echo("false");
		return;
	}
	
}
function deletePricePlanListItem($Line, $SKU, $PPListID){
	global $con;
	$query = "DELETE FROM PRICE_PLAN_LIST_DETAIL WHERE PriceListID = $PPListID AND `Line` = '$Line' AND `SKU` = '$SKU'";
	
	if(mysqli_query($con, $query)){
		echo("true");
		event_log($_SESSION['Username'], 'Delete Price Plan List Item', "Deleted Item: $Line - $SKU from Price List $PPListID");
		return;
	}else{
		echo("false: " . mysqli_error($con));
		return;
	}
	
}
function savePricePlanList($ID, $Name){
	global $con;
	if($ID == ""){
		$ID = 0;
	}
	$query = "INSERT INTO PRICE_PLAN_LIST_DEF (id, Name) VALUES ($ID, '$Name') ON DUPLICATE KEY UPDATE Name = '$Name'";
	$return = array();
	
	if(mysqli_query($con, $query)){
		$return['result'] = true;
		$return['ID'] = mysqli_insert_id($con);
		event_log($_SESSION['Username'], 'Add/Edit Price Plan List', "Price List ID: $ID \n Price List Name: $Name");
		echo json_encode($return);
		return;
	}else{
		$return['result'] = false;
		$return['error'] = mysqli_error($con);
		$return['query'] = $query;
		echo json_encode($return);
		return;
	}
}
function deletePricePlanList($ID){
	global $con;
	$query = "DELETE FROM PRICE_PLAN_LIST_DEF WHERE id = $ID";
	if(mysqli_query($con, $query)){
		event_log($_SESSION['Username'], 'Deleted Price Plan List', "Price List ID: $ID");
		echo("true");
		return;
	}else{
		echo("false: " . mysqli_error($con));
		return;
	}
}

function savePricePlanListCustomer($AccountNumber, $PPListID){
	global $con;
	$query = "INSERT INTO PRICE_PLAN_LIST_CUSTOMER (PriceListID, AccountNumber) VALUES ($PPListID, '$AccountNumber') ON DUPLICATE KEY UPDATE AccountNumber = $AccountNumber;";
	
	if(mysqli_query($con, $query)){
		echo("true");
		event_log($_SESSION['Username'], 'Add/Edit Price Plan Customer', "Account Number: $AccountNumber\n List ID: $PPListID");
		return;
	}else{
		echo("false");
		return;
	}
	
}
function deletePricePlanListCustomer($AccountNumber, $PPListID){
	global $con;
	$query = "DELETE FROM PRICE_PLAN_LIST_CUSTOMER WHERE PriceListID = $PPListID AND `AccountNumber` LIKE '$AccountNumber'";
	
	if(mysqli_query($con, $query)){
		echo("true");
		event_log($_SESSION['Username'], 'Deleted Price Plan List Customer', "Customer: $AccountNumber \n From Price List ID: $PPListID");
		return;
	}else{
		echo("false: " . mysqli_error($con));
		return;
	}
	
}
function deletePriceListDefinition($ID) {
	global	$con;
	$priceList = getPriceListDefinitionInfo($ID, true);
	$query = "DELETE FROM `PRICE_PLAN_LIST_DETAIL` WHERE `PriceListID` = '$ID'";
	$result = mysqli_query($con, $query);
	$query = "DELETE FROM `PRICE_PLAN_LIST_DEF` WHERE `id` = '$ID'";
	$return['result'] = $result;
	$return['query'] = $query;
	event_log($_SESSION['Username'], "DELETE PRICE LIST DEFINITION", "Price List: " . print_r($priceList, true));
			
	echo(json_encode($return));
}
?>