diff --git a/phpInterface/shoppingList.php b/phpInterface/shoppingList.php index 6803929..af9134e 100644 --- a/phpInterface/shoppingList.php +++ b/phpInterface/shoppingList.php @@ -26,10 +26,18 @@ } } - if(isset($_GET['command']) && $_GET['command'] == "printProductList" || $_GET['command'] == "printRhasspyProductList") + if(isset($_GET['command'])) { - displayProductList(); - exit(0); + if($_GET['command'] == "printProductList" || $_GET['command'] == "printRhasspyProductList") + { + displayProductList(); + exit(0); + } + else if($_GET['command'] == "printUnitList") + { + displayUnitList(); + exit(0); + } } ?> @@ -95,12 +103,16 @@ { case "addToList": $productId = $_POST['productToAdd']; - addToList($productId); + $amount = $_POST['amountToAdd']; + $unitId = $_POST['unitToAdd']; + addToList($productId, $amount, $unitId); break; case "removeFromList": $productId = $_POST['productToRemove']; $listId = $_POST['listId']; - removeFromList($productId, $listId); + $amount = $_POST['productAmountToRemove']; + $unit = $_POST['productUnitToRemove']; + removeFromList($productId, $listId, $amount, $unit); break; case "displayShoppingList": displayShoppingList(); @@ -139,34 +151,79 @@ mysqli_close(DBLink::getDbLink()); - function addToList($productId) + function createList() { - if($productId > 0) + $SQL_command = "INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).")"; + if (DBLink::getDbLink()->query($SQL_command)) { - $SQL_command; + echo "List created.
"; + return true; + } + + return false; + } - if(oneListExists()) - $SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.")"; - else - $SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).") RETURN @id, ".$productId."))"; - if (DBLink::getDbLink()->query($SQL_command)) - echo "Product added to list.
"; + function addToList($productId, $amount, $unitId) + { + if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0) + { + if(!oneListExists()) + createList(); + + if(!entryCombinationExists($productId, $unitId)) + { + $SQL_command = "INSERT IGNORE INTO listEntries (listId, productId, amount, unit) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.", ".$amount.", ".$unitId.")"; + + if (DBLink::getDbLink()->query($SQL_command)) + echo "Product added to list.
"; + else + echo "Product could not be added to list.
"; + } else - echo "Product could not be added to list.
"; + { + /* + update command that adds the amount to the already existing amount + if amount is negative -> subtract from existing amount. if resulting amount <= 0 -> + */ + + $SQL_command = "SELECT amount FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + if ($row = mysqli_fetch_object($mysqli_result)) + { + if($row->amount + $amount > 0) + { + $SQL_command = "UPDATE listEntries set amount=".($row->amount + $amount)." WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId; + + if (DBLink::getDbLink()->query($SQL_command)) + echo "Product added to existing entry on list.
"; + else + echo "Product could not be added to existing entry on list.
"; + } + else + { + $SQL_command = "DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId; + if (DBLink::getDbLink()->query($SQL_command)) + echo "Product removed from list as too many pieces of this item have been removed.
"; + else + echo "Product could not be removed from list (too many pieces of this itme have been removed).
"; + } + } + } } else - echo "Select a product."; + echo "Specifiy product, amount and unit."; } - function removeFromList($productId, $listId) + function removeFromList($productId, $listId, $amount, $unitId) { - if($productId > 0) + if($productId > 0 && $amount != null && $unitId > 0) { if(oneListExists()) { - $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId; + $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId." AND amount=".$amount." AND unit=".$unitId; if (DBLink::getDbLink()->query($SQL_command)) echo "Product removed from list.
"; @@ -179,6 +236,21 @@ } + function entryCombinationExists($productId, $unitId) + { + $SQL_command = "SELECT COUNT(listId) as entryAmount FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + if ($row = mysqli_fetch_object($mysqli_result)) + { + if($row->entryAmount > 0) + return true; + } + + return false; + } + + function oneListExists() { $SQL_command = "SELECT COUNT(id) as listAmount FROM lists"; @@ -207,7 +279,9 @@ function displayShoppingList($listId, $justCreatedProductId) { - echo "
+ echo "

Shopping list

+ + + + + +
"; @@ -260,47 +355,70 @@ echo "
"; } - $SQL_command = "SELECT * FROM listEntries INNER JOIN products ON listEntries.productId=products.id INNER JOIN storeTypes ON products.storeTypeId = storeTypes.id WHERE listEntries.listId=".$listId." ORDER BY storeTypes.name, products.name ASC"; + $SQL_command = "SELECT * FROM listEntries + INNER JOIN products ON listEntries.productId=products.id + INNER JOIN storeTypes ON products.storeTypeId = storeTypes.id + INNER JOIN units ON units.id = listEntries.unit + WHERE listEntries.listId=".$listId." ORDER BY storeTypes.name, products.name ASC"; $mysqli_result = DBLink::getDbLink()->query($SQL_command); $lastShop=""; + $found = false; while ($row = mysqli_fetch_array($mysqli_result)) { - if(isset($lastShop) && $lastShop != "" && $row[7] != $lastShop) + if(!$found) + $found = true; + + if(isset($lastShop) && $lastShop != "" && $row[9] != $lastShop) echo ""; - if(!isset($lastShop) || $row[7] != $lastShop) + if(!isset($lastShop) || $row[9] != $lastShop) { - $lastShop = $row[7]; + $lastShop = $row[9]; echo $lastShop."
================"; echo ""; } - echo " - + echo ""; + + if($row[14] == 1) + echo ""; + else + echo " + + + "; + + echo " "; } - echo "
".$row[3]."
".$row[5]."".$row[2]."".$row[12]."".$row[5]."
- + + +
"; + + if($found) + echo ""; + else + echo "No entries in this list, yet."; } else - echo "No list exists, yet."; + echo "No list exists, yet."; } function showDataMaintenance() { echo "
"; - echo "

Data maintenance

+ echo "

Data maintenance

Create new list

@@ -389,6 +507,33 @@ } + function displayUnitList() + { + $SQL_Befehl = "SELECT name FROM units ORDER BY name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_Befehl); + + $lastShop=""; + + while ($row = mysqli_fetch_object($mysqli_result)) + { + $unitArray = explode(";", $row->name); + if(count($unitArray) == 1) + echo $unitArray[0]."\n"; + else + { + $tbp = ""; + for($i=1; $i&2 + exit 1 + fi + ;; + --shoppingUnit) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + UNIT=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; --shoppingProduct) if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then SHOPPINGITEM=$2 @@ -53,7 +73,21 @@ then echo "Was soll ich auf die Liste setzen?" exit 1 else - java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM + lengthAmount=${#AMOUNT} + lengthUnit=${#UNIT} + + if [ $lengthAmount -gt 0 ] + then + if [ $lengthUnit -gt 0 ] + then + java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingAmount $AMOUNT --shoppingUnit $UNIT --shoppingProduct $SHOPPINGITEM + else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingAmount $AMOUNT --shoppingProduct $SHOPPINGITEM + fi + else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM + fi + if [ "$?" -eq "0" ] then exit 0 diff --git a/shellScripts/ShoppingListRemove.sh b/shellScripts/ShoppingListRemove.sh index 24286f4..07d177d 100644 --- a/shellScripts/ShoppingListRemove.sh +++ b/shellScripts/ShoppingListRemove.sh @@ -1,12 +1,32 @@ #!/bin/bash SHOPPINGITEM="" +AMOUNT="" +UNIT="" SITEID="" PARAMS="" SESSIONID="" while (( "$#" )); do case "$1" in + --shoppingAmount) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + AMOUNT=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + --shoppingUnit) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + UNIT=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; --shoppingProduct) if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then SHOPPINGITEM=$2 @@ -53,11 +73,25 @@ then echo "Was soll ich von der Liste streichen?" exit 1 else - java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM - if [ "$?" -eq "0" ] - then - exit 0 - else - exit 1 - fi + lengthAmount=${#AMOUNT} + lengthUnit=${#UNIT} + + if [ $lengthAmount -gt 0 ] + then + if [ $lengthUnit -gt 0 ] + then + java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingAmount $AMOUNT --shoppingUnit $UNIT --shoppingProduct $SHOPPINGITEM + else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingAmount $AMOUNT --shoppingProduct $SHOPPINGITEM + fi + else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM + fi + + if [ "$?" -eq "0" ] + then + exit 0 + else + exit 1 + fi fi