forked from jevuu/e-ceipt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jevuu/master
Merge
- Loading branch information
Showing
61 changed files
with
1,483 additions
and
1,463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ oldPHP | |
PHP/conn.php | ||
app/google-services.json | ||
.git.zip | ||
oldPHP |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
?> |
Oops, something went wrong.