Skip to content

Commit

Permalink
Merge pull request #13 from jevuu/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
AlistairCG committed Aug 6, 2018
2 parents cec26b5 + 5847ba3 commit 392d94e
Show file tree
Hide file tree
Showing 61 changed files with 1,483 additions and 1,463 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ oldPHP
PHP/conn.php
app/google-services.json
.git.zip
oldPHP
52 changes: 0 additions & 52 deletions PHP/OLDgetAllReceipts.php

This file was deleted.

83 changes: 9 additions & 74 deletions PHP/addReceipt.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

require "conn.php";

require "functions.php";

$json = file_get_contents('php://input');
$obj = json_decode($json);
Expand All @@ -11,93 +11,28 @@
$tax = $obj->{'tax'};
$businessName = $obj->{'businessName'};
$items = $obj->{'items'};
$categoryName = $obj->{'categoryName'};

//
$uID = $userID;
$rID = 0;
$iID = array();

//For testing purposes
/*$userName = "johnDoe";
$receiptDate = date("Y-m-d");
$totalCost = 100;
$tax = 13;
$items = array(
array("name"=>"Sample Item", "description"=>"Sample description", "price"=>"100"),
array("name"=>"Sample Item", "description"=>"Sample description", "price"=>"100")
);
$itemCount = count($items);
foreach($items as $key=>$itm){
echo $itm['name'];
if($key < $itemCount - 1){
echo ", ";
}
}
*/

/*// GET USER ID
$qUsersString = "SELECT userID FROM users WHERE userName = '$userName'";
$result = mysqli_query($conn, $qUsersString);
$row = mysqli_fetch_row($result);
$uID = $row[0];
//echo "User ID found: " . $uID;
*/
// INSERT RECEIPT
$qReceiptString = "INSERT INTO receipts (`userID`, `businessName`, `creationDate`, `totalCost`, `tax`)
VALUES('$uID', '$businessName', '$receiptDate', " . $totalCost . ", " . $tax . ")";
$qReceiptString = "INSERT INTO receipts (`userID`, `businessName`, `creationDate`, `totalCost`, `tax`, `categoryName`)
VALUES('$userID', '$businessName', '$receiptDate', " . $totalCost . ", " . $tax . ", '$categoryName')";
if(mysqli_query($conn, $qReceiptString) === FALSE){
echo "An error occured creating the receipt. \n '$businessName'";
echo "An error occured creating the receipt. \n";
}

// GET receiptID
$qReceiptString = "SELECT receiptID FROM receipts WHERE userID = '$uID' ORDER BY receiptID DESC LIMIT 1";
$qReceiptString = "SELECT receiptID FROM receipts WHERE userID = '$userID' ORDER BY receiptID DESC LIMIT 1";
$result = mysqli_query($conn, $qReceiptString);
$row = mysqli_fetch_row($result);
$rID = $row[0];
$receiptID = $row[0];
//echo "Receipt ID found: " . $rID;


//Add items by first building the query string.
$qItemsString = "INSERT INTO items(`userID`, `name`, `description`, `price`) VALUES";
$itemCount = count($items); //Loop through $items and append data from it into the string
/*for($itm = 0; $itm < $itemCount; $itm++){
print_r($items);
echo $items[$itm]->price;
}*/
foreach($items as $key => $itm){
$itemName = $itm->itemName;
$itemDesc = $itm->itemDesc;
$itemPrice = $itm->itemPrice;
$qItemsString = $qItemsString . "('$uID', '$itemName', '$itemDesc', " . $itemPrice . ")";
if($key < $itemCount - 1){
$qItemsString = $qItemsString . ", ";
}
}
echo $qItemsString;

if(mysqli_query($conn, $qItemsString) === FALSE){
echo "An error has occured. Unable to insert items into database.";
}

//Get the itemIDs of the new items
$qString = "SELECT itemID FROM items WHERE userID = '$uID' ORDER BY itemID DESC LIMIT " . $itemCount;
$result = mysqli_query($conn, $qString);
while($row = mysqli_fetch_row($result)){
array_push($iID, $row[0]);
//echo "Item ID found: " . $row[0];
}

//Link tables
$qLinkString = "INSERT INTO receiptItems(`receiptID`, `itemID`) VALUES";
for($i = 0; $i < $itemCount; $i++){
$qLinkString .= "(" . $rID . "," . $iID[$i] . ")";
//echo "Linking receipt: " . $rID . " with item: " . $iID[$i];
if($i < $itemCount - 1){
$qLinkString .= ", ";
}
}
if(mysqli_query($conn, $qLinkString) === FALSE){
echo "An error has occured. Unable to link items to receipt.";

add_item($conn, $userID, $receiptID, $itemName, $itemDesc, $itemPrice);
}

?>
139 changes: 139 additions & 0 deletions PHP/category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

require "conn.php";

//Get from JSON object and the option
$json = file_get_contents('php://input');
$obj = json_decode($json);
$option = $obj->{'option'};

/*JSON shape for each option:
getAllCategory: userID
getOneCategory: categoryID
addCategory: userID
categoryName
deleteCategory: categoryID
editCategory: categoryID
categoryName
Data types:
categoryID: int
userID: string
categoryName: string
*/

//GET ALL CATEGORIES
if($option == "getAllCategory"){
$userID = $obj->{'userID'};
$categoryList = array();

if($userID != ""){
$qGetAllQuery = "SELECT categoryID, name FROM categories WHERE userID = '$userID'";
$result = mysqli_query($conn, $qGetAllQuery);
while($categoryDB = mysqli_fetch_row($result)){
$tempCategory = [
'categoryID' => $categoryDB[0],
'categoryName' => $categoryDB[1],
];
array_push($categoryList, $tempCategory);
}
echo json_encode($categoryList);
}else{
errMsg("No userID.");
}
//GET CATEGORY BY ID
}else if($option == "getOneCategory"){
$categoryID = $obj->{'categoryID'};
//$categoryList = array();

if($categoryID != ""){
$qGetOneQuery = "SELECT name FROM categories WHERE categoryID = " . $categoryID;
$result = mysqli_query($conn, $qGetOneQuery);
$categoryDB = mysqli_fetch_row($result);
$categoryList = [
'categoryName' => $categoryDB[0]
];
echo json_encode($categoryList);
}else{
errMsg("Incorrect categoryID.");
}
//ADD CATEGORY
}else if($option == "addCategory"){
$userID = $obj->{'userID'};
$catName = $obj->{'categoryName'};

if($userID == ""){
errMsg("No userID.");
}else if($catName == ""){
errMsg("No category name.");
}else if($userID != "" && $catName != ""){
//First check if category name doesn't already exist.
//If it doesn't, then insert
$qCheckQuery = "SELECT name FROM categories WHERE userID = '$userID' AND name = '$catName'";
$result = mysqli_query($conn, $qCheckQuery);
$row = mysqli_fetch_row($result);

if (count($row) > 0){
errMsg("Category name (" . $catName . ") already exists. ");
}else{
$qAddQuery = "INSERT INTO categories (`userID`, `name`) VALUES ('$userID', '$catName')";
if(mysqli_query($conn, $qAddQuery) === false){
errMsg("Query failed. \n Query string: " . $qAddQuery);
}
}
}else{
errMsg("Unexpected error adding category.");
}
//DELETE CATEGORY
}else if($option == "deleteCategory"){
$categoryID = $obj->{'categoryID'};

if($categoryID != ""){
$qDeleteQuery = "DELETE FROM categories WHERE categoryID = " . $categoryID;
mysqli_query($conn, $qDeleteQuery);
}else{
errMsg("Incorrect categoryID.");
}
//EDIT CATEGORY
}else if($option == "editCategory"){
$categoryID = $obj->{'categoryID'};
$catName = $obj->{'categoryName'};

if($categoryID != "" && $categoryName != ""){

//Test to see data before change
//$q = "SELECT * FROM categories WHERE categoryID = " . $categoryID;
//$result = mysqli_query($conn, $q);
//$row = mysqli_fetch_row($result);
//echo $row[0] . ", " . $row[1] . ", " . $row[2] . "\n";

$qEditQuery = "UPDATE categories SET name = '$catName' WHERE categoryID = " . $categoryID;
mysqli_query($conn, $qEditQuery);

//Test to see data after change
//$q = "SELECT * FROM categories WHERE categoryID = " . $categoryID;
//$result = mysqli_query($conn, $q);
//$row = mysqli_fetch_row($result);
//echo $row[0] . ", " . $row[1] . ", " . $row[2];
}else if($categoryID == "" && $categoryName == ""){
errMsg("No categoryID or name.");
}else if($categoryID == ""){
errMsg("No categoryID.");
}else if($categoryName == ""){
errMsg("No category name.");
}else{
errMsg("Unrecognized error editing category.");
}
}else{
echo "Error: Unrecognized option.";
}

function errMsg(string $msg){
echo "Error: " . $msg;
}
//$userID = $obj->{'userID'};
?>
69 changes: 69 additions & 0 deletions PHP/editReceipt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

require "conn.php";
require "functions.php";

$json = file_get_contents('php://input');
$obj = json_decode($json);
$userID = $obj->{'userID'};
$receiptID = $obj->{'receiptID'};
$receiptDate = $obj->{'date'}; //This must be a date object
$totalCost = $obj->{'totalCost'};
$tax = $obj->{'tax'};
$businessName = $obj->{'businessName'};
$items = $obj->{'items'};
$categoryName = $obj->{'categoryName'};

//Update Receipt
//Test to see data before change
$q = "SELECT * FROM receipts WHERE receiptID = " . $receiptID;
$result = mysqli_query($conn, $q);
$row = mysqli_fetch_row($result);
//echo "Receipt before: " . $row[0] . ", " . $row[1] . ", " . $row[2] . ", " . $row[3] . ", " . $row[4] . ", " . $row[5] . ", " . $row[6] . ", " . $row[7] . "\n";

$qReceiptString = "UPDATE receipts SET creationDate = '$receiptDate', businessName = '$businessName', totalCost = " . $totalCost . ", tax = " . $tax . ", categoryName = '$categoryName'
WHERE receiptID = " . $receiptID;
mysqli_query($conn, $qReceiptString);

//Test to see data after change
$q = "SELECT * FROM receipts WHERE receiptID = " . $receiptID;
$result = mysqli_query($conn, $q);
$row = mysqli_fetch_row($result);
//echo "Receipt after: " . $row[0] . ", " . $row[1] . ", " . $row[2] . ", " . $row[3] . ", " . $row[4] . ", " . $row[5] . ", " . $row[6] . ", " . $row[7] . "\n\n === \n\n";

//Update Items
$receivedItemIDs = array();
foreach($items as $key => $itm){
$itemID = $itm->itemID;
$itemName = $itm->itemName;
$itemDesc = $itm->itemDesc;
$itemPrice = $itm->itemPrice;

if($itemID == -1){
$itemID = add_item($conn, $userID, $receiptID, $itemName, $itemDesc, $itemPrice);
}else{
edit_item($conn, $userID, $receiptID, $itemID, $itemName, $itemDesc, $itemPrice);
}

array_push($receivedItemIDs, $itemID);
}

//Delete Items
///Compare number of items in JSON receipt with same receipt in DB, and record IDs that are missing from JSON receipt
///These missing IDs will be marked for removal.
$q = "SELECT itemID FROM receiptItems WHERE receiptID = " . $receiptID;
$result = mysqli_query($conn, $q);
$removeItems = array();
$itemsFlipped = array_flip($receivedItemIDs);
while($row = mysqli_fetch_row($result)){
if(!isset($itemsFlipped[$row[0]])){
//echo "item " . $row[0] . " is not in received list. marked for removal. \n";
array_push($removeItems, $row[0]);
}
}
///Now delete them from database
foreach($removeItems as $i){
$q = "DELETE FROM items WHERE itemID = " . $i;
mysqli_query($conn, $q);
}
?>
Loading

0 comments on commit 392d94e

Please sign in to comment.