From c294ba4289d15382b0889be57d63a9297e10291d Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Tue, 4 Dec 2018 15:57:22 +0000 Subject: [PATCH] Refactoring expense related things for subgroups. --- WEB-INF/lib/ttExpenseHelper.class.php | 36 ++++++++++++++++++--------- WEB-INF/templates/footer.tpl | 2 +- expense_delete.php | 2 +- expenses.php | 2 +- mobile/expense_delete.php | 2 +- mobile/expenses.php | 2 +- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/WEB-INF/lib/ttExpenseHelper.class.php b/WEB-INF/lib/ttExpenseHelper.class.php index b11e71fd..bfc3ab72 100644 --- a/WEB-INF/lib/ttExpenseHelper.class.php +++ b/WEB-INF/lib/ttExpenseHelper.class.php @@ -29,8 +29,7 @@ // The ttExpenseHelper is a class to help with expense items. class ttExpenseHelper { // insert - inserts an entry into tt_expense_items table. - static function insert($fields) - { + static function insert($fields) { global $user; $mdb2 = getConnection(); @@ -57,11 +56,13 @@ class ttExpenseHelper { } // update - updates a record in tt_expense_items table. - static function update($fields) - { + static function update($fields) { global $user; $mdb2 = getConnection(); + $group_id = $user->getGroup(); + $org_id = $user->org_id; + $id = (int) $fields['id']; $date = $fields['date']; $user_id = (int) $fields['user_id']; @@ -77,34 +78,45 @@ class ttExpenseHelper { } $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id; - $sql = "UPDATE tt_expense_items set date = ".$mdb2->quote($date).", user_id = $user_id, client_id = ".$mdb2->quote($client_id). + $sql = "update tt_expense_items set date = ".$mdb2->quote($date).", user_id = $user_id, client_id = ".$mdb2->quote($client_id). ", project_id = ".$mdb2->quote($project_id).", name = ".$mdb2->quote($name). ", cost = ".$mdb2->quote($cost)."$paid_part $modified_part, invoice_id = ".$mdb2->quote($invoice_id). - " WHERE id = $id"; + " where id = $id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); } // markDeleted - marks an item as deleted in tt_expense_items table. - static function markDeleted($id, $user_id) { + static function markDeleted($id) { + global $user; $mdb2 = getConnection(); - $sql = "update tt_expense_items set status = NULL where id = $id and user_id = $user_id"; + $user_id = $user->getUser(); + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "update tt_expense_items set status = null". + " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id"; $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); } // getTotalForDay - gets total expenses for a user for a specific date. - static function getTotalForDay($user_id, $date) { + static function getTotalForDay($date) { global $user; - $mdb2 = getConnection(); - $sql = "select sum(cost) as sm from tt_expense_items where user_id = $user_id and date = ".$mdb2->quote($date)." and status = 1"; + $user_id = $user->getUser(); + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $sql = "select sum(cost) as sm from tt_expense_items". + " where user_id = $user_id and group_id = $group_id and org_id = $org_id". + " and date = ".$mdb2->quote($date)." and status = 1"; $res = $mdb2->query($sql); if (!is_a($res, 'PEAR_Error')) { $val = $res->fetchRow(); - $val['sm'] = str_replace('.', $user->decimal_mark, $val['sm']); + $val['sm'] = str_replace('.', $user->getDecimalMark(), $val['sm']); return $val['sm']; } return false; diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index a4ddf7cc..05f357bc 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.29.4581 | Copyright © Anuko | +  Anuko Time Tracker 1.18.29.4582 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/expense_delete.php b/expense_delete.php index fcb039f0..32b9ab67 100644 --- a/expense_delete.php +++ b/expense_delete.php @@ -59,7 +59,7 @@ if ($request->isPost()) { if ($err->no()) { // Mark the record as deleted. - if (ttExpenseHelper::markDeleted($cl_id, $user->getUser())) { + if (ttExpenseHelper::markDeleted($cl_id)) { header('Location: expenses.php'); exit(); } else diff --git a/expenses.php b/expenses.php index 1ff638fb..eea55d1c 100644 --- a/expenses.php +++ b/expenses.php @@ -211,7 +211,7 @@ if ($request->isPost()) { } } -$smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getUser(), $cl_date)); +$smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date)); $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getUser(), $cl_date)); $smarty->assign('predefined_expenses', $predefined_expenses); $smarty->assign('client_list', $client_list); diff --git a/mobile/expense_delete.php b/mobile/expense_delete.php index c0fa032e..93d6d59f 100644 --- a/mobile/expense_delete.php +++ b/mobile/expense_delete.php @@ -59,7 +59,7 @@ if ($request->isPost()) { if ($err->no()) { // Mark the record as deleted. - if (ttExpenseHelper::markDeleted($cl_id, $user->getUser())) { + if (ttExpenseHelper::markDeleted($cl_id)) { header('Location: expenses.php'); exit(); } else diff --git a/mobile/expenses.php b/mobile/expenses.php index a39bf4a9..656b20e0 100644 --- a/mobile/expenses.php +++ b/mobile/expenses.php @@ -217,7 +217,7 @@ if ($request->isPost()) { $smarty->assign('next_date', $next_date); $smarty->assign('prev_date', $prev_date); -$smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getUser(), $cl_date)); +$smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date)); $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getUser(), $cl_date)); $smarty->assign('predefined_expenses', $predefined_expenses); $smarty->assign('client_list', $client_list); -- 2.20.1