You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB
PHP

<?
include_once("../php/include-request.php");
verifyAuthorized();
$action = getMandatoryRequestValue("action");
initDatabaseConnection();
if($action == "search") {
$accountId = getOptionalRequestValue("accountId", null);
$categoryMixId = getOptionalRequestValue("categoryMixId", null);
$year = getOptionalRequestValue("year", null);
$label = getOptionalRequestValue("label", null);
$sql = "SELECT * FROM record_ext WHERE household_id = ?";
$inputs = array();
array_push($inputs, getHouseholdId());
if($accountId != null) {
$sql .= " AND account_id = ?";
array_push($inputs, $accountId);
}
if($categoryMixId != null) {
$type = substr($categoryMixId, 0, 1);
$id = substr($categoryMixId, 2);
if($type == "g") {
$sql .= " AND category_group_id = ?";
} else {
$sql .= " AND category_id = ?";
}
array_push($inputs, $id);
}
if($year != null) {
$sql .= " AND year = ?";
array_push($inputs, $year);
}
if($label != null) {
$sql .= " AND label LIKE ?";
array_push($inputs, $label);
}
array_unshift($inputs, $sql); // insert $sql first in $inputs
//print_r($inputs);
$rows = call_user_func_array("dbQuery", $inputs);
if($rows !== false) {
$records = array();
foreach($rows as $row) {
$record = array(
"id" => $row['id'],
"date" => formatDate($row['year'], $row['month'], $row['day']),
"accountId" => $row['account_id'],
"accountName" => $row['account_name'],
"label" => $row['label'],
"expense" => $row['expense'],
"amount" => $row['amount'],
"categoryId" => $row['category_id'],
"comment" => $row['comment']
);
array_push($records, $record);
}
$data = array("recordCount" => count($records), "records" => $records);
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
requestOk($jsonData);
}
} else if($action == "save-category") {
$id = getMandatoryRequestValue("id");
$categoryId = getMandatoryRequestValue("categoryId");
$rowCount = dbUpdate("UPDATE record SET category_id = ? WHERE id = ?", $categoryId, $id);
if($rowCount) {
requestOk("Category set for $rowCount record");
}
} else if($action == "save-comment") {
$id = getMandatoryRequestValue("id");
$comment = getMandatoryRequestValue("comment");
$rowCount = dbUpdate("UPDATE record SET comment = ? WHERE id = ?", $comment, $id);
if($rowCount) {
requestOk("Comment saved for $rowCount record");
}
} else {
requestFail("Unknown request action \"$action\"");
}
closeDatabaseConnection();
?>