From e5014cb7f9304c785915b479f5b7da3b724c393f Mon Sep 17 00:00:00 2001 From: Phyks Date: Wed, 25 Sep 2013 20:28:09 +0200 Subject: [PATCH] Global paybacks should be ok. To be tested --- TODO | 5 --- inc/GlobalPaybacks.class.php | 15 ++++++- inc/Invoices.class.php | 2 +- inc/UsersInGlobalPayback.class.php | 17 +++++++- index.php | 17 +++++++- tpl/default_en/manage_paybacks.html | 4 +- tpl/default_en/see_paybacks.html | 60 ++++++++++++++++------------ tpl/default_fr/manage_paybacks.html | 4 +- tpl/default_fr/see_paybacks.html | 61 +++++++++++++++++------------ 9 files changed, 119 insertions(+), 66 deletions(-) diff --git a/TODO b/TODO index bc64363..914df59 100755 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ Issues : ======== * Check CSRF everywhere -* Close a global payback Improvements : ============== @@ -11,7 +10,3 @@ Improvements : * API * cf TODO in files * README file - -Manage paybacks : -================= -* TODO : Global payback diff --git a/inc/GlobalPaybacks.class.php b/inc/GlobalPaybacks.class.php index 011a0cd..d3e576b 100644 --- a/inc/GlobalPaybacks.class.php +++ b/inc/GlobalPaybacks.class.php @@ -4,7 +4,7 @@ require_once('UsersInGlobalPayback.class.php'); class GlobalPayback extends Storage { - protected $id = 0, $date, $users_in; + protected $id = 0, $date, $users_in, $closed; // date is a DateTime object // buyer is a User object // users_in is a UsersIn objects @@ -12,11 +12,13 @@ protected $fields = array( 'id'=>'key', 'date'=>'date', + 'closed'=>'bool' ); public function __construct() { parent::__construct(); $this->users_in = new UsersInGlobalPayback(); + $this->date = new Datetime(); } // Getters @@ -36,6 +38,10 @@ return $this->users_in; } + public function getClosed() { + return (bool) $this->closed; + } + // Setters // ======= public function setId($id) { @@ -44,7 +50,7 @@ } public function setDate($minute, $hour, $day, $month, $year) { - if((int) $minute < 10) $minute = '0'.$minute; + if((int) $minute < 10) $minute = '0'.(int) $minute; $this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute); } @@ -54,6 +60,10 @@ $this->users_in->set($users_in); } + public function setClosed($closed) { + $this->closed = (bool) $closed; + } + // Maps htmlspecialchars on the class before display // ================================================= public function secureDisplay() { @@ -71,6 +81,7 @@ $this->setId($data['id']); $this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']); + $this->setClosed($data['closed']); } // Override parent load() method diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index b5d6bc1..16d7611 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -59,7 +59,7 @@ } public function setDate($minute, $hour, $day, $month, $year) { - if((int) $minute < 10) $minute = '0'.$minute; + if((int) $minute < 10) $minute = '0'.(int) $minute; $this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute); } diff --git a/inc/UsersInGlobalPayback.class.php b/inc/UsersInGlobalPayback.class.php index c3547c6..35b4c42 100644 --- a/inc/UsersInGlobalPayback.class.php +++ b/inc/UsersInGlobalPayback.class.php @@ -55,6 +55,19 @@ return $this; } + // Test if the payback should be closed + // ==================================== + public function isEmpty() { + foreach($this->users_list as $user1=>$temp) { + foreach($temp as $user2=>$amount) { + if($amount != 0) { + return false; + } + } + } + return true; + } + // Override load() method // ====================== public function load() { @@ -85,7 +98,7 @@ public function save() { // TODO : Optimize ? - $query = 'SELECT COUNT(payback_id) FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE payback_id=:payback_id'; + $query = 'SELECT COUNT(global_payback_id) FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE global_payback_id=:payback_id'; $query = $this->getConnection()->prepare($query); $query->bindParam(':payback_id', $this->payback_id); $query->execute(); @@ -94,7 +107,7 @@ if($count != 0) { // If there are already some records, delete them first - $query = $this->getConnection()->prepare('DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE payback_id=:payback_id'); + $query = $this->getConnection()->prepare('DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE global_payback_id=:payback_id'); $query->bindParam(':payback_id', $this->payback_id); $query->execute(); } diff --git a/index.php b/index.php index b812642..97c147b 100644 --- a/index.php +++ b/index.php @@ -719,13 +719,20 @@ $global_payback->setUsersIn($users_in); + if($global_payback->getUsersIn()->isEmpty()) { + $global_payback->setClosed(true); + } + else { + $global_payback->setClosed(false); + } + $global_payback->save(); // Clear the cache ($cached_files = glob(raintpl::$cache_dir."*.rtpl.php")) or ($cached_files = array()); array_map("unlink", $cached_files); - header('location: see_paybacks.php?id='.(int)$_GET['payback_id']); + header('location: ?do=see_paybacks&id='.(int)$_GET['payback_id']); exit(); } @@ -839,6 +846,14 @@ } $global_payback->setUsersIn($users_in); + + if($global_payback->getUsersIn()->isEmpty()) { + $global_payback->setClosed(true); + } + else { + $global_payback->setClosed(false); + } + $global_payback->setDate(date('i'), date('G'), date('j'), date('n'), date('Y')); $global_payback->save(); diff --git a/tpl/default_en/manage_paybacks.html b/tpl/default_en/manage_paybacks.html index bd8360a..5b14909 100644 --- a/tpl/default_en/manage_paybacks.html +++ b/tpl/default_en/manage_paybacks.html @@ -9,7 +9,7 @@
{loop="$global_paybacks"}
{$value->getDate()}
-
Payback n°{$value->getId()}
+
{if condition="$value->getClosed() === true"}[Closed] {/if}Payback n°{$value->getId()}
{/loop}
{/if} @@ -36,7 +36,7 @@ {/loop}

- +

{/if} diff --git a/tpl/default_en/see_paybacks.html b/tpl/default_en/see_paybacks.html index 5bd5368..c26df0e 100644 --- a/tpl/default_en/see_paybacks.html +++ b/tpl/default_en/see_paybacks.html @@ -6,42 +6,52 @@
{loop="$global_paybacks"}
{$value->getDate()}
-
Payback n°{$value->getId()}
+
+ {if condition="$value->getClosed() !== false"} + Payback n°{$value->getId()} + {else} + [Closed] Payback n°{$value->getId()} + {/if} +
{/loop}
{else}

No global paybacks available.

{/if} {else} -

Payback n°{$global_paybacks->getId()}

+ {if condition="$global_paybacks === false"} + Payback doesn't exist. + {else} +

Payback n°{$global_paybacks->getId()}

- {$table=$global_paybacks->getUsersIn()->get()} - - - - {loop="$global_paybacks->getUsersIn()->get()"} - - {/loop} - - {loop="$global_paybacks->getUsersIn()->get()"} + {$table=$global_paybacks->getUsersIn()->get()} +
Owes / To{$users[$key1]->getDisplayName()}
- + {loop="$global_paybacks->getUsersIn()->get()"} - {if condition="$key1 == $key2"} - - {else} - - {/if} + {/loop} - {/loop} -
{$users[$key1]->getDisplayName()}Owes / To - {if condition="$table[$key1][$key2] != 0"} - {$table[$key1][$key2]} - {else} - - - {/if} - {$users[$key1]->getDisplayName()}
+ {loop="$global_paybacks->getUsersIn()->get()"} + + {$users[$key1]->getDisplayName()} + {loop="$global_paybacks->getUsersIn()->get()"} + {if condition="$key1 == $key2"} + + {else} + + {if condition="$table[$key1][$key2] != 0"} + {$table[$key1][$key2]} + {else} + - + {/if} + + {/if} + {/loop} + + {/loop} + + {/if}

Go back to global paybacks list

{/if} diff --git a/tpl/default_fr/manage_paybacks.html b/tpl/default_fr/manage_paybacks.html index d194180..a87a171 100644 --- a/tpl/default_fr/manage_paybacks.html +++ b/tpl/default_fr/manage_paybacks.html @@ -9,7 +9,7 @@
{loop="$global_paybacks"}
{$value->getDate()}
-
Remboursement n°{$value->getId()}
+
{if condition="$value->getClosed() === true"}[Terminé] {/if}Remboursement n°{$value->getId()}
{/loop}
{/if} @@ -36,7 +36,7 @@ {/loop}

- +

{/if} diff --git a/tpl/default_fr/see_paybacks.html b/tpl/default_fr/see_paybacks.html index 1267be3..3af7476 100644 --- a/tpl/default_fr/see_paybacks.html +++ b/tpl/default_fr/see_paybacks.html @@ -6,42 +6,51 @@
{loop="$global_paybacks"}
{$value->getDate()}
-
Remboursement n°{$value->getId()}
+
+ {if condition="$value->getClosed() !== false"} + Remboursement n°{$value->getId()} + {else} + [Terminé] Remboursement n°{$value->getId()} + {/if} +
{/loop}
{else}

Aucun remboursement global disponible pour l'instant.

{/if} {else} -

Remboursement n°{$global_paybacks->getId()}

- - {$table=$global_paybacks->getUsersIn()->get()} - - - - {loop="$global_paybacks->getUsersIn()->get()"} - - {/loop} - - {loop="$global_paybacks->getUsersIn()->get()"} + {if condition="$global_paybacks === false"} + Remboursement inexistant. + {else} +

Remboursement n°{$global_paybacks->getId()}

+ {$table=$global_paybacks->getUsersIn()->get()} +
Doit / À{$users[$key1]->getDisplayName()}
- + {loop="$global_paybacks->getUsersIn()->get()"} - {if condition="$key1 == $key2"} - - {else} - - {/if} + {/loop} - {/loop} -
{$users[$key1]->getDisplayName()}Doit / À - {if condition="$table[$key1][$key2] != 0"} - {$table[$key1][$key2]} - {else} - - - {/if} - {$users[$key1]->getDisplayName()}
+ {loop="$global_paybacks->getUsersIn()->get()"} + + {$users[$key1]->getDisplayName()} + {loop="$global_paybacks->getUsersIn()->get()"} + {if condition="$key1 == $key2"} + + {else} + + {if condition="$table[$key1][$key2] != 0"} + {$table[$key1][$key2]} + {else} + - + {/if} + + {/if} + {/loop} + + {/loop} + + {/if}

Retour à la liste des remboursements

{/if}