Bug correction in payback system

This commit is contained in:
Phyks 2013-09-14 23:21:49 +02:00
parent a8dcea0625
commit 970553a599
7 changed files with 75 additions and 23 deletions

1
TODO
View File

@ -6,6 +6,7 @@
* cf TODO in files
* French template
* Favicon
* Dépenses du dernier mois
Manage paybacks :
=================

View File

@ -13,7 +13,7 @@
'id'=>'key',
'date'=>'date',
'buyer'=>'int',
'amount'=>'float',
'amount'=>'int',
'what'=>'text'
);
@ -44,7 +44,7 @@
}
public function getAmount() {
return $this->amount;
return (float) $this->amount / 100; // Amount is stored in cents
}
public function getWhat() {
@ -73,7 +73,7 @@
}
public function setAmount ($amount) {
$this->amount = (float) $amount;
$this->amount = (int) ($amount * 100); // Amount is stored in cents
}
public function setWhat($what) {
@ -87,8 +87,12 @@
// Get the amount to pay by person
// ===============================
public function getAmountPerPerson() {
return round($this->amount / count($this->users_in->get()), 2);
public function getAmountPerPerson($id) {
$guests = $this->users_in->get();
$guests = $guests[(int) $id];
// Amount is stored in cents
return round($this->amount / 100 / (count($this->users_in->get()) + $guests), 2);
}
// Maps htmlspecialchars on the class before display
@ -111,7 +115,7 @@
$this->setId($data['id']);
$this->setWhat($data['what']);
$this->setAmount($data['amount']);
$this->amount = (int) $data['amount'];
$this->setBuyer($data['buyer']);
$this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']);

View File

@ -9,7 +9,7 @@
'id'=>'key',
'date'=>'date',
'invoice_id'=>'int',
'amount'=>'float',
'amount'=>'int',
'from_user'=>'int',
'to_user'=>'int'
);
@ -37,7 +37,7 @@
}
public function getAmount() {
return (float) $this->amount;
return (float) $this->amount / 100; // Amount is stored in cents
}
public function getFrom() {
@ -66,7 +66,7 @@
}
public function setAmount($amount) {
$this->amount = (float) $amount;
$this->amount = (int) ($amount * 100); // Amount is stored in cents
}
public function setFrom($from) {
@ -86,7 +86,7 @@
$this->setId($data['id']);
$this->setInvoice($data['invoice_id']);
$this->setAmount($data['amount']);
$this->amount = (int) $data['amount'];
$this->setFrom($data['from_user']);
$this->setTo($data['to_user']);

View File

@ -262,7 +262,7 @@
$user->save();
$_SESSION['current_user'] = $user->sessionStore();
header('location: index.php'.$get_redir);
header('location: index.php?do=password&'.$get_redir);
exit();
break;
@ -272,6 +272,44 @@
$user->setId($_GET['user_id']);
$user->delete();
// Update concerned invoices
$invoices = new Invoice();
$invoices = $invoices->load();
if($invoices !== FALSE) {
foreach($invoices as $invoice) {
if($invoice->getBuyer() == $_GET['user_id']) {
$invoice->setBuyer(0);
$invoice->save();
}
if($invoice->getUsersIn()->inUsersIn($_GET['user_id'])) {
$users_in = $invoice->getUsersIn()->get();
$users_in[0] = $users_in[$_GET['user_id']];
unset($users_in[$_GET['user_id']]);
$invoice->setUsersIn($users_in);
$invoice->save();
}
}
}
// Update paybacks
$paybacks = new Payback();
$paybacks = $paybacks->load(array('from_user'=>(int) $_GET['user_id']));
if($paybacks !== FALSE) {
foreach($paybacks as $payback) {
$payback->setFrom(0);
$payback->save();
}
}
$paybacks = $paybacks->load(array('to_user'=>(int) $_GET['user_id']));
if($paybacks !== FALSE) {
foreach($paybacks as $payback) {
$payback->setTo(0);
$payback->save();
}
}
// Clear the cache
array_map("unlink", glob(raintpl::$cache_dir."*.rtpl.php"));
@ -489,10 +527,16 @@
$payback = new Payback();
}
}
else {
$payback = $payback->load(array('invoice_id'=>(int) $_GET['invoice_id'], 'to_user'=>(int) $_GET['to'], 'from_user'=>(int) $_GET['from']), true);
if($payback == false)
$payback = new Payback();
}
$payback->setDate(date('i'), date('G'), date('j'), date('n'), date('Y'));
$payback->setInvoice($_GET['invoice_id']);
$payback->setAmount($invoice->getAmount());
$payback->setAmount($invoice->getAmountPerPerson($_GET['from']));
$payback->setFrom($_GET['from']);
$payback->setTo($_GET['to']);
@ -525,9 +569,11 @@
$paybacks = $paybacks->load(array('to_user'=>(int) $_GET['to'], 'from_user'=> (int) $_GET['from'], 'invoice_id'=> (int) $_GET['invoice_id']));
if($paybacks == false) {
foreach($paybacks as $payback) {
$payback->delete();
}
}
// Clear the cache
$tmp_files = glob(raintpl::$cache_dir."*.rtpl.php");
@ -535,6 +581,7 @@
array_map("unlink", $tmp_files);
}
exit();
header('location: index.php');
exit();
}
@ -597,7 +644,7 @@
if($invoices_list_balances !== false) {
foreach($invoices_list_balances as $invoice) {
if($invoice->getUsersIn()->inUsersIn($user1->getId())) {
$balances[$user1->getId()][$user2->getId()] += $invoice->getAmountPerPerson();
$balances[$user1->getId()][$user2->getId()] += $invoice->getAmountPerPerson($user1->getId());
}
}
}
@ -608,7 +655,7 @@
if($invoices_list_balances !== false) {
foreach($invoices_list_balances as $invoice) {
if($invoice->getUsersIn()->inUsersIn($user2->getId())) {
$balances[$user1->getId()][$user2->getId()] -= $invoice->getAmountPerPerson();
$balances[$user1->getId()][$user2->getId()] -= $invoice->getAmountPerPerson($user2->getId());
}
}
}

View File

@ -28,13 +28,13 @@
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(255), display_name VARCHAR(255), password VARCHAR(130), admin TINYINT(1), json_token VARCHAR(32)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
//Create table "Invoices"
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, buyer INT(11), amount FLOAT, what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, buyer INT(11), amount INT(11), what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
//Create table "Users_in_invoices"
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users_in_invoices (invoice_id INT(11) NOT NULL, KEY invoice_id (invoice_id), user_id INT(11), KEY user_id (user_id), guests INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
//Create table "Paybacks"
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Paybacks (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, invoice_id INT(11), KEY invoice_id (invoice_id), amount FLOAT, from_user INT(11), to_user INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
$db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Paybacks (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date DATETIME, invoice_id INT(11), KEY invoice_id (invoice_id), amount INT(11), from_user INT(11), to_user INT(11)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
} catch (PDOException $e) {
$error = 'Unable to connect to database and create database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.';
}

View File

@ -58,7 +58,7 @@
{elseif condition="$view == 'password'"}
<h2>Edit your password</h2>
<form method="post" action="index.php?do=password" id="edit_password_form">
<p><label for="password" class="label-block">New password : </label><input type="password" id="password" name="password"/></p>
<p><label for="password" class="label-block">New password : </label><input type="password" id="password" name="password"/> <a href="" onclick="toggle_password('password'); return false;"><img src="img/toggleVisible.png" alt="Toggle visible"/></a></p>
<p><label for="password_confirm" class="label-block">Confirm new password : </label><input type="password" id="password_confirm" name="password_confirm"/> <a href="" onclick="toggle_password('password_confirm'); return false;"><img src="img/toggleVisible.png" alt="Toggle visible"/></a></p>
<p class="center"><input type="submit" value="Update"/><input type="hidden" name="token" value="{$token}"</p>
</form>

View File

@ -56,16 +56,16 @@
({$value2} guest)
{/if}
-
{if condition="$paybacks[$value1->getId()] === false"}
{if condition="$paybacks[$value1->getId()] === false || !in_array($key2, array_keys($paybacks[$value1->getId()]))"}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
<a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}" title="Confirm payback">
{/if}
Remains {$value1->getAmountPerPerson()} {$currency}
Remains {$value1->getAmountPerPerson($key2)} {$currency}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
</a>
{/if}
{else}
{if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson()"}
{if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson($key2)"}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
<a href="?do=delete_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}" title="Delete payback">
{/if}
@ -77,7 +77,7 @@
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
<a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;payback_id={$paybacks[$value1->getId()][$key2]->getId()}" title="Confirm payback">
{/if}
Remains {$value1->getAmountPerPerson() - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency}
Remains {$value1->getAmountPerPerson($key2) - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
</a>
{/if}