This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.json.php
78 lines (70 loc) · 2.89 KB
/
transactions.json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
require_once("sql.inc.php");
require_once("login.inc.php");
loginRequire();
function matchFilter($transaction, $filter) {
switch ($filter["type"]) {
case "daterange":
return ($filter["start"] <= $transaction["date"]) && ($filter["end"] >= $transaction["date"]);
case "account":
return in_array($filter["guid"], array_map(create_function('$a','return $a["account_guid"];'), $transaction["splits"]));
case "num":
if (isset($filter["num"]))
return $transaction["num"] == formatNum($filter["num"]);
else
return !empty($transaction["num"]);
case "numStartsWith":
return (strpos($transaction["num"], formatNum($filter["prefix"])) === 0);
case "accountCodeStartsWith":
return array_reduce($transaction["splits"], create_function('$a,$b','return $a || substr($b["account_code"],0,'.strlen($filter["prefix"]).') == "'.intval($filter["prefix"]).'";'), false);
case "descStartsWith":
return (substr($transaction["description"],0,strlen($filter["prefix"])) == $filter["prefix"]);
case "verifiedAbove":
return $transaction["validValidations"] > $filter["count"];
case "failedVerificationsAbove":
return (count($transaction["validations"]) - $transaction["validValidations"]) > $filter["count"];
case "and":
return !in_array(false, array_map("matchFilter", array_fill(0, count($filter["conds"]), $transaction), $filter["conds"]));
case "or":
return in_array(true, array_map("matchFilter", array_fill(0, count($filter["conds"]), $transaction), $filter["conds"]));
case "not":
return !matchFilter($transaction, $filter["cond"]);
case "true":
return true;
default:
case "false":
return false;
}
}
$i = $offset = (isset($_REQUEST["offset"]) ? intval($_REQUEST["offset"]) : 0);
$sorting_field = "post_date";
$sorting_order = "asc";
if (isset($_REQUEST["sorting"])) {
if (in_array($_REQUEST["sorting"]["field"], array("post_date","num"))) {
$sorting_field = $_REQUEST["sorting"]["field"];
}
if (in_array($_REQUEST["sorting"]["order"], array("asc","desc"))) {
$sorting_order = $_REQUEST["sorting"]["order"];
}
}
$transactions = array();
$result = $sql->query("select guid as guid from transactions order by " . $sorting_field . " " . $sorting_order . " limit " . $offset . ",100");
while (($row = $result->fetch_assoc()) && count($transactions) < 20) {
$transaction = sqlGetTransaction($row["guid"]);
$allowed = false;
$splits = array();
foreach ($transaction["splits"] as $split) {
if (isAllowedAccount($split["account_guid"])) {
$splits[] = $split;
}
}
if (count($splits) > 0) {
$transaction["splits"] = $splits;
if (!isset($_REQUEST["filter"]) || matchFilter($transaction, $_REQUEST["filter"])) {
$transactions[] = $transaction;
}
}
$i++;
}
header("Content-Type: application/json; charset=utf-8");
print(json_encode(array("transactions" => $transactions, "num_rows" => $result->num_rows, "nextOffset" => ( ($result->num_rows >= 100 || $offset + $result->num_rows > $i) ? $i : null))));