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.
280 lines
8.0 KiB
PHP
280 lines
8.0 KiB
PHP
<?
|
|
include_once("../php/include.php");
|
|
|
|
initDatabaseConnection();
|
|
|
|
$categories = array();
|
|
$rows = dbQuery("SELECT * FROM category_ext WHERE household_id = ?", getHouseholdId());
|
|
foreach($rows as $row) {
|
|
$name1 = $row['category_group_name']." - ".$row['name'];
|
|
$name2 = $name1." [".($row['expense'] ? "expense" : "income").($row['exclude'] ? ", excluded" : "")."]";
|
|
$categories[$row['id']] = $name2;
|
|
|
|
if($row['system']) {
|
|
if($row['expense']) {
|
|
$defaultCategoryNameExpense = $name1;
|
|
} else {
|
|
$defaultCategoryNameIncome = $name1;
|
|
}
|
|
}
|
|
}
|
|
|
|
$accounts = array(
|
|
"" => "<any>"
|
|
);
|
|
$rows = dbQuery("SELECT * FROM account WHERE household_id = ?", getHouseholdId());
|
|
foreach($rows as $row) {
|
|
$accounts[$row['id']] = $row['name'];
|
|
}
|
|
?>
|
|
|
|
<table class="list-c">
|
|
<tr>
|
|
<th>Regex</th>
|
|
<th>Sort order</th>
|
|
<th>Category</th>
|
|
<th>Account</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
<?
|
|
$rows = dbQuery("SELECT * FROM rule_sort WHERE household_id = ?", getHouseholdId());
|
|
foreach($rows as $row) {
|
|
$id = $row['id'];
|
|
?>
|
|
<tr>
|
|
<td><input id="regex<?=$id?>" type="text" style="width: 250px;" placeholder="Regular expression (SQL like)" value="<?=$row['regex']?>" autocomplete="off" /></td>
|
|
<td>
|
|
<input id="sortOrderAuto<?=$id?>" type="checkbox" <?=($row['sort_order'] == null ? "checked" : "")?> title="Auto sort" onchange="$('#sortOrder<?=$id?>').prop('readonly', this.checked); $('#sortOrder<?=$id?>').css('background', this.checked ? '#ccc' : 'none');" autocomplete="off" />
|
|
<input id="sortOrder<?=$id?>" type="number" style="width: 50px; <?=($row['sort_order'] == null ? "background: #ccc;" : "")?>" placeholder="Sort order" value="<?=$row['sort_order_auto']?>" <?=($row['sort_order'] == null ? "readonly" : "")?> autocomplete="off" />
|
|
</td>
|
|
<td>
|
|
<select id="categoryId<?=$id?>" autocomplete="off">
|
|
<?
|
|
foreach($categories as $cid => $cname) {
|
|
echo "<option value='$cid'".($cid == $row['category_id'] ? " selected" : "").">$cname</option>";
|
|
}
|
|
?>
|
|
</select>
|
|
</td>
|
|
<td>
|
|
<select id="accountId<?=$id?>" autocomplete="off">
|
|
<?
|
|
foreach($accounts as $aid => $aname) {
|
|
echo "<option value='$aid'".($aid == $row['account_id'] ? " selected" : "").">$aname</option>";
|
|
}
|
|
?>
|
|
</select>
|
|
</td>
|
|
<td>
|
|
<button onclick="saveRule(this, <?=$id?>);">Save</button>
|
|
<button onclick="deleteRule(this, <?=$id?>);">Delete</button>
|
|
<button id="run-button-<?=$id?>" onclick="showMore(this, <?=$id?>);">Run +</button>
|
|
</td>
|
|
</tr>
|
|
<?
|
|
}
|
|
$id = "null";
|
|
?>
|
|
<tr class="list-b">
|
|
<td class="list-b"><input id="regex<?=$id?>" type="text" style="width: 250px;" placeholder="Regular expression (SQL like)" value="" /></td>
|
|
<td class="list-b"></td>
|
|
<td class="list-b">
|
|
<select id="categoryId<?=$id?>">
|
|
<?
|
|
foreach($categories as $cid => $cname) {
|
|
echo "<option value='$cid'>$cname</option>";
|
|
}
|
|
?>
|
|
</select>
|
|
</td>
|
|
<td class="list-b">
|
|
<select id="accountId<?=$id?>">
|
|
<?
|
|
foreach($accounts as $aid => $aname) {
|
|
echo "<option value='$aid'>$aname</option>";
|
|
}
|
|
?>
|
|
</select>
|
|
</td>
|
|
<td class="list-b">
|
|
<button onclick="saveRule(this, <?=$id?>)">Add new rule</button>
|
|
<button onclick="runAllRules(this)">Run all</button>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br/><br/>
|
|
|
|
<table class="list-b">
|
|
<tr>
|
|
<th class="list-b" colspan="6">Records in the default categories</th>
|
|
</tr>
|
|
<tr>
|
|
<th class="list-b">Label</th>
|
|
<th class="list-b">#</th>
|
|
<th class="list-b">Type</th>
|
|
<th class="list-b">Span</th>
|
|
<th class="list-b">Sum</th>
|
|
<th class="list-b">Records</th>
|
|
</tr>
|
|
<?
|
|
$rows = dbQuery("SELECT * FROM record_default_category WHERE occurences > 1 AND household_id = ?", getHouseholdId());
|
|
foreach($rows as $row) {
|
|
?>
|
|
<tr class="list-b">
|
|
<td class="list-b"><?=$row['label']?></td>
|
|
<td class="list-b"><?=$row['occurences']?></td>
|
|
<td class="list-b" style="text-align: center;"><?=insertExpenseHtml($row['expense'])?></td>
|
|
<td class="list-b" style="text-align: right;"><?=intval($row['amount_min'])?> - <?=intval($row['amount_max'])?> <?=getHouseholdCurrency()?></td>
|
|
<td class="list-b" style="text-align: right;"><?=intval($row['amount_sum'])?> <?=getHouseholdCurrency()?></td>
|
|
<td class="list-b" style="text-align: center;">
|
|
<button onclick="showRecords(this, '<?=$row['label']?>', <?=$row['expense']?>);">+</button>
|
|
</td>
|
|
</tr>
|
|
<?
|
|
}
|
|
?>
|
|
</table>
|
|
|
|
<?
|
|
closeDatabaseConnection();
|
|
?>
|
|
|
|
<script>
|
|
var reloadPageFunc = function(success, status, response) {
|
|
if(success) {
|
|
reloadCurrentPage();
|
|
}
|
|
};
|
|
var lastMoreDivId = null;
|
|
|
|
function saveRule(button, id) {
|
|
var requestData = {
|
|
action: "save-rule",
|
|
id: id,
|
|
regex: $('#regex' + id).val(),
|
|
sortOrder: ($('#sortOrderAuto' + id).is(':checked') ? null : $('#sortOrder' + id).val()),
|
|
categoryId: $('#categoryId' + id + ' option:selected').val(),
|
|
accountId: $('#accountId' + id + ' option:selected').val()
|
|
};
|
|
sendRequest("rule", requestData, button, (id == null ? reloadPageFunc : null));
|
|
}
|
|
|
|
function deleteRule(button, id) {
|
|
var requestData = {
|
|
action: "delete-rule",
|
|
id: id
|
|
};
|
|
sendRequest("rule", requestData, button, reloadPageFunc);
|
|
}
|
|
|
|
function runRule(button, id, system, divId) {
|
|
var completeFunc = function(success, status, response) {
|
|
if(success) {
|
|
closeMore();
|
|
showMore("run-button-" + id, id);
|
|
}
|
|
}
|
|
var requestData = {
|
|
action: "run-rule",
|
|
id: id,
|
|
system: system
|
|
};
|
|
sendRequest("rule", requestData, button, completeFunc);
|
|
}
|
|
|
|
function runAllRules(button) {
|
|
var requestData = {
|
|
action: "run-all-rules"
|
|
};
|
|
sendRequest("rule", requestData, button, reloadPageFunc);
|
|
}
|
|
|
|
|
|
function showMore(button, id) {
|
|
closeMore();
|
|
|
|
var messageFunc = function(success, status, response) {
|
|
if(success) {
|
|
return "Data fetched";
|
|
} else {
|
|
return response;
|
|
}
|
|
};
|
|
var completeFunc = function(success, status, response) {
|
|
if(success) {
|
|
var json = JSON.parse(response);
|
|
var html = "";
|
|
html += "<table class='list-c'>";
|
|
html += " <tr>";
|
|
html += " <th>Category</th>";
|
|
html += " <th>Matches</th>";
|
|
html += " <th>Action</th>";
|
|
html += " </tr>";
|
|
html += " <tr>";
|
|
html += " <td>" + (json.expense ? "<?=$defaultCategoryNameExpense?>" : "<?=$defaultCategoryNameIncome?>") + "</td>";
|
|
html += " <td>" + json.matches_default_category + "</td>";
|
|
html += " <td><button onclick='runRule(this, " + id + ", true, " + divId + ");'>Update</button></td>";
|
|
html += " </tr>";
|
|
html += " <tr>";
|
|
html += " <td>" + json.category_group_name + " - " + json.category_name + "</td>";
|
|
html += " <td>" + json.matches_current_category + "</td>";
|
|
html += " <td></td>";
|
|
html += " </tr>";
|
|
html += " <tr>";
|
|
html += " <td><i>Other</i></td>";
|
|
html += " <td>" + json.matches_other_category + "</td>";
|
|
html += " <td><button onclick='runRule(this, " + id + ", false, " + divId + ");'>Update</button></td>";
|
|
html += " </tr>";
|
|
html += " <tr>";
|
|
html += " <td colspan='2'></td>";
|
|
html += " <td colspan='1'><button onclick='closeMore();'>Close</button></td>";
|
|
html += " </tr>";
|
|
html += "</table>";
|
|
|
|
var divId = createDivRelative("loadingDiv", button);
|
|
$('#' + divId).html(html);
|
|
$('#' + divId).animate(
|
|
{
|
|
opacity: 1,
|
|
left: "+=30"
|
|
},
|
|
200,
|
|
null
|
|
);
|
|
lastMoreDivId = divId;
|
|
}
|
|
};
|
|
var requestData = {
|
|
action: "get-rule-stats",
|
|
id: id
|
|
};
|
|
sendRequestAdv("rule", requestData, button, completeFunc, messageFunc, 0, false);
|
|
}
|
|
|
|
function closeMore() {
|
|
if(lastMoreDivId != null) {
|
|
$('#' + lastMoreDivId).animate(
|
|
{
|
|
opacity: 0,
|
|
left: "+=50"
|
|
},
|
|
200,
|
|
function() {
|
|
removeDiv(lastMoreDivId);
|
|
lastMoreDivId = null;
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
function showRecords(button, label, expense) {
|
|
var requestData = {
|
|
action: "show-records",
|
|
label: label,
|
|
expense: expense
|
|
};
|
|
sendRequestAdv("rule", requestData, button, null, null, 7000, false);
|
|
}
|
|
</script>
|