From f67a9a6e2ff10485bc246deac09812ae07188eb8 Mon Sep 17 00:00:00 2001 From: Phyks Date: Fri, 13 Sep 2013 19:18:49 +0200 Subject: [PATCH] Index balance table working --- inc/Storage.class.php | 2 +- inc/UsersIn.class.php | 6 ++++++ index.php | 38 ++++++++++++++++++++++++++++++++++-- tpl/default_en/css/style.css | 4 ++++ tpl/default_en/index.html | 7 ++++++- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/inc/Storage.class.php b/inc/Storage.class.php index b08c3b6..78eab6c 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -163,7 +163,7 @@ class Storage { } $query->execute(); - + $results = $query->fetchAll(); if(count($results) > 0) { diff --git a/inc/UsersIn.class.php b/inc/UsersIn.class.php index 10cb347..e449f46 100644 --- a/inc/UsersIn.class.php +++ b/inc/UsersIn.class.php @@ -43,6 +43,12 @@ $this->users_list = $users_in; } + // Check if a user is in a specified invoice + // ========================================= + public function inUsersIn($id) { + return in_array($id, array_keys($this->users_list)); + } + // Maps htmlspecialchars on the class before display // ================================================= public function secureDisplay() { diff --git a/index.php b/index.php index a3db930..49c36a1 100644 --- a/index.php +++ b/index.php @@ -575,9 +575,43 @@ if($user1->getId() == $user2->getId()) { $balances[$user1->getId()][$user2->getId()] = 'X'; } + if(!empty($balances[$user2->getId()][$user1->getId()])) { + // If the opposed element in the matrix exists + if(is_float($balances[$user2->getId()][$user1->getId()])) { + if($balances[$user2->getId()][$user1->getId()] >= 0) { + $balances[$user1->getId()][$user2->getId()] = '-'; + } + else { + $balances[$user1->getId()][$user2->getId()] = -$balances[$user2->getId()][$user1->getId()]; + $balances[$user2->getId()][$user1->getId()] = '-'; + } + } + } else { - // TODO : Balances between users - $balances[$user1->getId()][$user2->getId()] = ''; + // TODO : Optimize ? + $balances[$user1->getId()][$user2->getId()] = 0; + + // First, get a list of all invoices paid by user2 and check if user1 was in + $invoices_list_balances = new Invoice(); + $invoices_list_balances = $invoices_list_balances->load(array('buyer'=>$user2->getId())); + if($invoices_list_balances !== false) { + foreach($invoices_list_balances as $invoice) { + if($invoice->getUsersIn()->inUsersIn($user1->getId())) { + $balances[$user1->getId()][$user2->getId()] += $invoice->getAmountPerPerson(); + } + } + } + + // Then search for all invoices paid by 1 and check if user2 was in + $invoices_list_balances = new Invoice(); + $invoices_list_balances = $invoices_list_balances->load(array('buyer'=>$user1->getId())); + if($invoices_list_balances !== false) { + foreach($invoices_list_balances as $invoice) { + if($invoice->getUsersIn()->inUsersIn($user2->getId())) { + $balances[$user1->getId()][$user2->getId()] -= $invoice->getAmountPerPerson(); + } + } + } } } } diff --git a/tpl/default_en/css/style.css b/tpl/default_en/css/style.css index c8e6db5..06ebd14 100644 --- a/tpl/default_en/css/style.css +++ b/tpl/default_en/css/style.css @@ -70,6 +70,10 @@ input[type="checkbox"] { width: 200px; } +.cell-disabled { + background-color: rgb(221, 221, 221); +} + input[type=submit] { background-color: green; color: white; diff --git a/tpl/default_en/index.html b/tpl/default_en/index.html index 5d1c96b..28ac5bc 100755 --- a/tpl/default_en/index.html +++ b/tpl/default_en/index.html @@ -18,7 +18,12 @@ {$value->getDisplayName()} {loop="users"} - {$balances[$value1->getId()][$value2->getId()]} + {if condition="$balances[$value1->getId()][$value2->getId()] == 'X'"} + + {else} + + {$balances[$value1->getId()][$value2->getId()]} + {/if} {/loop} {/loop}