<?php
//die("hre");
class POS {
	public $Salesman;
	public function __construct($Salesman) {
		$query = "SELECT SHOPPING_CART.*, INVENTORY.Description FROM SHOPPING_CART JOIN INVENTORY USING(Line, SKU) WHERE AccountNumber LIKE '$Salesman' ORDER BY SHOPPING_CART.id";
		global $con;
		
		$this->Items = [];
		$this->Salesman = $Salesman;
		$result = mysqli_query($con, $query);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
			array_push($this->Items,$row);
		}
	}
	
	public function addToCart($Line, $SKU, $Qty){
		global $con;
		$item = new Item("%", $Line, $SKU);
		$price = getCustomerPrice($item->Group, $item->Line, $item->SKU);
		$query = "INSERT INTO SHOPPING_CART (AccountNumber, `Group`, Line, SKU, Description, Qty, Price, Taxable1, Taxable2) VALUES ('"
			. mysqli_real_escape_string($con, $this->Salesman) . "', '"
			. mysqli_real_escape_string($con, $item->Group) . "', '"
			. mysqli_real_escape_string($con, $item->Line) . "', '"
			. mysqli_real_escape_string($con, $item->SKU) . "', '"
			. mysqli_real_escape_string($con, $item->Description) . "', "
			. mysqli_real_escape_string($con, $Qty) . ", "
			. mysqli_real_escape_string($con, $price) . ", "
			. $item->Taxable1 . ", "
			. $item->Taxable2 . ") ON DUPLICATE KEY UPDATE Qty = Qty + $Qty";
		$result = mysqli_query($con, $query);
		if($result){
			return true;
		}else{
			echo $query;
			
		}
	}
	public function recalculatePrices(){
		foreach($this->Items as $key=>$value){
			//var_dump($value);
			$item = new POSItem($value['id']);
			
			$item->recalculatePrice();
		}
	}
	public function clearCart(){
		global $con;
		$query = "DELETE FROM SHOPPING_CART WHERE AccountNumber LIKE '{$this->Salesman}'";
		//echo $query;
		if(mysqli_query($con, $query)){
			return true;
		}else{
			return false;
		}
	}
	public function editCartItem($itemId, $Description, $Qty, $Price, $Discount, $Taxable1, $Taxable2){
		global $con;
		$Description = mysqli_real_escape_string($con, $Description);
		$posItem = new POSItem($itemId);
		$posItem->Description = $Description;
		$posItem->Qty = $Qty;
		$posItem->Price = $Price;
		$posItem->Discount = $Discount;
		$posItem->Taxable1 = $Taxable1;
		$posItem->Taxable2 = $Taxable2;
		
		//$query = "UPDATE SHOPPING_CART SET Description = '$Description', Qty = $Qty, Price = $Price, Discount = $Discount WHERE id = $itemId";
		//var_dump();
		if($posItem->save()){
			return true;
		}else{
			return false;
		}
	}
	public function deleteCartItem($itemId){
		global $con;
		$query = "DELETE FROM SHOPPING_CART WHERE id = $itemId";
		if(mysqli_query($con, $query)){
			return true;
		}else{
			return mysqli_error($con);
		}
	}
}

class POSItem{
	private $hasQuotes, $PRIVATE_KEY, $TABLE_NAME;
	public function __construct($id){
		global $con;
		//
		//	Private Key
		//
		$this->PRIVATE_KEY = "id";
		//
		// Table name
		//
		$this->TABLE_NAME = "SHOPPING_CART";
		//
		// Get Table Column and add to object.
		//
		$query = "SELECT * FROM `{$this->TABLE_NAME}` WHERE `{$this->PRIVATE_KEY}` = $id";
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		foreach($row as $key => $value){
				$this->$key = $value;
		}
		//
		// Get Columns Names and either if they need quotes in a query or not.
		//
		$this->hasQuotes = [];
		$query = "SHOW COLUMNS FROM `{$this->TABLE_NAME}`";
		
		$result = mysqli_query($con,$query);
		
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
			if(stripos($row['Type'], "int") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "tinyint") === 0 || stripos($row['Type'], "numeric") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "decimal") === 0 || stripos($row['Type'], "float") === 0 || stripos($row['Type'], "bigint") === 0 || stripos($row['Type'], "real") === 0 || stripos($row['Type'], "bool") === 0){
				$this->hasQuotes[$row['Field']] = false;
			}else{
				$this->hasQuotes[$row['Field']] = true;
			}
		}
	}
	public function recalculatePrice(){
		$this->Price = getCustomerPrice($this->Group, $this->Line, $this->SKU);
		$this->save();
	}
	//
	// Function to create a "column = value" string usable by mysql query;
	//
	private function saveString($Column, $Value){
		
		if(is_null($Value)){
			$returnValue = "NULL";	
		}else{
			$returnValue = ($this->hasQuotes[$Column])?"'$Value'":"$Value";	
		}
		return " `$Column` = $returnValue ";
	}
	//
	// function to save all the values in the object to the table.
	//
	public function save(){
		global $con;
		
		$query = "UPDATE `{$this->TABLE_NAME}` SET ";
		$saveQuery = [];
		foreach($this->hasQuotes as $fieldName => $quotes){
			if($fieldName != $this->PRIVATE_KEY){
				$saveQuery[] = $this->saveString($fieldName, $this->$fieldName);	
			}
		}
		$query .= implode(", ", $saveQuery) . " WHERE `{$this->PRIVATE_KEY}` = " . $this->{$this->PRIVATE_KEY};
		$result = mysqli_query($con, $query);
		//echo $query;
		if($result){
			return $result;
		}else{
			return [mysqli_error($con), $query];
		}
	}
}

?>