Index balance table working

This commit is contained in:
Phyks 2013-09-13 19:18:49 +02:00
parent 3696a6566a
commit f67a9a6e2f
5 changed files with 53 additions and 4 deletions

View File

@ -43,6 +43,12 @@
$this->users_list = $users_in; $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 // Maps htmlspecialchars on the class before display
// ================================================= // =================================================
public function secureDisplay() { public function secureDisplay() {

View File

@ -575,9 +575,43 @@
if($user1->getId() == $user2->getId()) { if($user1->getId() == $user2->getId()) {
$balances[$user1->getId()][$user2->getId()] = 'X'; $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 { else {
// TODO : Balances between users // TODO : Optimize ?
$balances[$user1->getId()][$user2->getId()] = ''; $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();
}
}
}
} }
} }
} }

View File

@ -70,6 +70,10 @@ input[type="checkbox"] {
width: 200px; width: 200px;
} }
.cell-disabled {
background-color: rgb(221, 221, 221);
}
input[type=submit] { input[type=submit] {
background-color: green; background-color: green;
color: white; color: white;

View File

@ -18,7 +18,12 @@
<tr> <tr>
<th>{$value->getDisplayName()}</th> <th>{$value->getDisplayName()}</th>
{loop="users"} {loop="users"}
<td>{$balances[$value1->getId()][$value2->getId()]}</td> {if condition="$balances[$value1->getId()][$value2->getId()] == 'X'"}
<td class="cell-disabled"></td>
{else}
<td>{$balances[$value1->getId()][$value2->getId()]}</td>
{/if}
{/loop} {/loop}
</tr> </tr>
{/loop} {/loop}