<?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");
}

$function = $_REQUEST['f'];
if($function == "getQtyBreakData"){
    $id = $_REQUEST['id'];
	getQtyBreakData($id);
}elseif($function == "saveQtyBreakData"){
   saveQtyBreakData($_REQUEST);
}elseif($function == "deleteQtyBreak"){
	deleteQtyBreak($_REQUEST['id']);
}
function getQtyBreakData($id){
    global $con;
	$query = "SELECT * FROM QUANTITY_BREAK WHERE id = $id";
	$result = mysqli_query($con, $query);
	if($result){
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		echo json_encode($row);
	}else{
		echo "false";
		
	}
}

function deleteQtyBreak($id){
	global $con;
	$query = "DELETE FROM QUANTITY_BREAK WHERE id = " . $id . ";";
	ob_start();
	getQtyBreakData($id);
	$qtyBreakData = ob_get_contents();
	ob_end_clean();
	$qtyBreakData = json_decode($qtyBreakData);
	
	if(mysqli_query($con, $query)){
		echo "true";
		event_log($_SESSION['Username'], 'Delete Quantity Break', "Quantity Break Data: \n" . print_r($qtyBreakData, true));
	}else{
		echo mysqli_error($con);
	}
}
function saveQtyBreakData($data){
	global $con;
	$return = array();
	
	if($data['IsNew'] == "0"){
		$query = "UPDATE QUANTITY_BREAK SET `Line` = '" . $data['Line'] . "', `SKU` = '" . $data['SKU'] . "', `QtyBreak` = '" . $data['QtyBreak'] . "', `QtyToAdd` = '" . $data['QtyToAdd'] . "', `DiscountRate` = '" . $data['DiscountRate'] . "', `Active` = " . $data['Active'] . " WHERE id = " . $data['QtyBreakID'];
	}else{
		$query = "INSERT INTO QUANTITY_BREAK (`Line`, `SKU`, `QtyBreak`, `QtyToAdd`, `DiscountRate`, `Active`) VALUES ('" . $data['Line'] . "', '" . $data['SKU'] . "', " . $data['QtyBreak'] . ", " . $data['QtyToAdd'] . ", " . $data['DiscountRate'] . ", " . $data['Active'] . ");";
	}
	//var_dump(mysqli_f($con, $query));
	
	if(mysqli_query($con, $query)){
		$return['result'] = true;
		event_log($_SESSION['Username'], 'Add/Edit Quantity Break', "Quantity Break Data: \n" . print_r($data, true));
	}else{
		$return['result'] = false;
		$return['error'] = mysqli_error($con);
		$return['query']= $query;
	}
	echo json_encode($return);
	return $return;
	
}

?>