diff --git a/TODO b/TODO index 08c3420..b297b9d 100755 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ * Notifications by e-mail for users -* Simplify matrix * Mask invoices when user is not concerned in "show all invoices" Improvements : diff --git a/inc/functions.php b/inc/functions.php index 118ae8d..7118f6c 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -97,3 +97,11 @@ return mail ($to, $subject, $msg, $header); } + + // Function to sort an array by abs desc + function sort_array_abs($a, $b) { + if(abs($a) == abs($b)) + return 0; + + return (abs($a) < abs($b)) ? 1 : -1; + } diff --git a/index.php b/index.php index 6168f2e..257660c 100755 --- a/index.php +++ b/index.php @@ -952,6 +952,50 @@ } } + // Now, let's simplify the matrix ! :) + // First, get the total balance by user (gains - debts) + $balances = array(); + $simplified_balances = array(); + foreach($_POST['users_in'] as $user) { + $balances[$user] = 0; + foreach($_POST['users_in'] as $user2) { + if(!empty($users_in[$user][$user2])) { + $balances[$user] -= $users_in[$user][$user2]; + } + if(!empty($users_in[$user2][$user])) { + $balances[$user] += $users_in[$user2][$user]; + } + $simplified_balances[$user][$user2] = 0; + } + } + + // Do while $balances is not identically filled with zeros + while(count(array_unique($balances)) != 1 or $balances[key($balances)] != 0) { + // Sort balances in abs values, desc + uasort($balances, "sort_array_abs"); + + // Get the largest one in abs + // The following largest with opposite sign must pay him back the max + reset($balances); + $user1 = key($balances); + + foreach($balances as $user2=>$value) { + if($value * $balances[$user1] < 0) { + if($balances[$user1] > 0) { + $simplified_balances[$user2][$user1] = abs($value); + $balances[$user1] -= abs($value); + $balances[$user2] += abs($value); + } + else { + $simplified_balances[$user1][$user2] = abs($value); + $balances[$user1] += abs($value); + $balances[$user2] -= abs($value); + } + break; + } + } + } + $global_payback->setUsersIn($users_in); if($global_payback->getUsersIn()->isEmpty()) {