Payback base system now working

This commit is contained in:
Phyks 2013-09-11 00:51:45 +02:00
parent 359c706e94
commit 4824b05219
6 changed files with 130 additions and 17 deletions

4
TODO
View File

@ -1,7 +1,9 @@
* Don't cache the username * Don't cache the username
* JSON output => do index view * JSON output => do index view
* API * API
* Reattribute all invoices / paybacks to unknown user when deleting a user * Reattribute all invoices / paybacks to unknown user when deleting a user ?
* Check that all is ok when deleting an invoice
* Use PHP constant in tpl instead of variables
* User groups * User groups
* cf TODO in files * cf TODO in files

View File

@ -3,7 +3,7 @@
require_once('Storage.class.php'); require_once('Storage.class.php');
class Payback extends Storage { class Payback extends Storage {
protected $id = 0, $date, $invoice_id, $amount, $from, $to; protected $id = 0, $date, $invoice_id, $amount, $from_user, $to_user;
protected $TABLE_NAME = "Paybacks"; protected $TABLE_NAME = "Paybacks";
protected $fields = array( protected $fields = array(
'id'=>'key', 'id'=>'key',
@ -41,11 +41,11 @@
} }
public function getFrom() { public function getFrom() {
return (int) $this->from; return (int) $this->from_user;
} }
public function getTo() { public function getTo() {
return (int) $this->to; return (int) $this->to_user;
} }
// Setters // Setters
@ -70,11 +70,11 @@
} }
public function setFrom($from) { public function setFrom($from) {
$this->from = (int) $from; $this->from_user = (int) $from;
} }
public function setTo($to) { public function setTo($to) {
$this->to = (int) $to; $this->to_user = (int) $to;
} }
// Restores object from array // Restores object from array
@ -87,8 +87,8 @@
$this->setId($data['id']); $this->setId($data['id']);
$this->setInvoice($data['invoice_id']); $this->setInvoice($data['invoice_id']);
$this->setAmount($data['amount']); $this->setAmount($data['amount']);
$this->setFrom($data['from']); $this->setFrom($data['from_user']);
$this->setTo($data['to']); $this->setTo($data['to_user']);
$this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']); $this->date = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']);
} }
@ -100,7 +100,9 @@
$this->id = (int) $this->id; $this->id = (int) $this->id;
$this->invoice_id = (int) $this->invoice_id; $this->invoice_id = (int) $this->invoice_id;
$this->amount = (float) $this->amount; $this->amount = (float) $this->amount;
$this->from = (int) $this->from; $this->from = (int) $this->from_user;
$this->to = (int) $this->to; $this->to = (int) $this->to_user;
return $this;
} }
} }

View File

@ -34,6 +34,12 @@
} }
public function set($users_in) { public function set($users_in) {
foreach($users_in as $user=>$guest) {
if($guest < 0)
$users_in[$user] = 0;
else
$users_in[$user] = (int) $guest;
}
$this->users_list = $users_in; $this->users_list = $users_in;
} }

View File

@ -474,10 +474,84 @@
} }
break; break;
case 'confirm_payback':
if(!empty($_GET['from']) && !empty($_GET['to']) && !empty($_GET['invoice_id'])) {
if($_GET['to'] == $current_user->getId() || $current_user->getAdmin()) {
$invoice = new Invoice();
$invoice = $invoice->load(array('id'=>(int) $_GET['invoice_id']), true);
$payback = new Payback();
if(!empty($_GET['payback_id'])) {
$payback = $payback->load(array('id'=>(int) $_GET['payback_id']), true);
if($payback->getFrom() != $_GET['from'] || $payback->getTo() != $_GET['to']) {
$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->setFrom($_GET['from']);
$payback->setTo($_GET['to']);
$payback->save();
// Clear the cache
$tmp_files = glob(raintpl::$cache_dir."*.rtpl.php");
if(is_array($tmp_files)) {
array_map("unlink", $tmp_files);
}
header('location: index.php');
exit();
}
else {
$tpl->assign('error', $errors['unauthorized']);
$tpl->draw('index');
}
}
else {
header('location: index.php?'.$get_redir);
}
break;
case 'delete_payback':
if(!empty($_GET['from']) && !empty($_GET['to']) && !empty($_GET['invoice_id'])) {
if($_GET['to'] == $current_user->getId() || $current_user->getAdmin()) {
$paybacks = new Payback();
$paybacks = $paybacks->load(array('to_user'=>(int) $_GET['to'], 'from_user'=> (int) $_GET['from'], 'invoice_id'=> (int) $_GET['invoice_id']));
foreach($paybacks as $payback) {
$payback->delete();
}
// Clear the cache
$tmp_files = glob(raintpl::$cache_dir."*.rtpl.php");
if(is_array($tmp_files)) {
array_map("unlink", $tmp_files);
}
header('location: index.php');
exit();
}
else {
$tpl->assign('error', $errors['unauthorized']);
$tpl->draw('index');
}
}
else {
header('location: index.php');
exit();
}
default: default:
$use_cache = false;
// Display cached page in priority // Display cached page in priority
if($use_cache && $cache = $tpl->cache('index', $expire_time = 600, $cache_id = $current_user->getLogin())) { if($cache = $tpl->cache('index', $expire_time = 600, $cache_id = $current_user->getLogin())) {
echo $cache; echo $cache;
} }
else { else {
@ -492,7 +566,7 @@
$paybacks = array(); $paybacks = array();
foreach($invoices_list as $invoice) { foreach($invoices_list as $invoice) {
$paybacks[$invoice->getId()] = new Payback(); $paybacks[$invoice->getId()] = new Payback();
$paybacks[$invoice->getId()] = $paybacks[$invoice->getId()]->load(array('invoice_id'=>$invoice->getId())); $paybacks[$invoice->getId()] = $paybacks[$invoice->getId()]->load(array('invoice_id'=>$invoice->getId()), false, 'from_user');
} }
$tpl->assign('users', secureDisplay($users_list)); $tpl->assign('users', secureDisplay($users_list));

View File

@ -14,12 +14,12 @@
<ul> <ul>
<li><a href="index.php?do=new_invoice">Add a bill</a></li> <li><a href="index.php?do=new_invoice">Add a bill</a></li>
<li><a href="index.php?do=password">Change your password</a></li> <li><a href="index.php?do=password">Change your password</a></li>
<li><a href="index.php?do=paybacks">See paybacks</a></li> <li><a href="index.php?do=paybacks">See global paybacks</a></li>
<li><a href="index.php?do=disconnect">Disconnect</a></li> <li><a href="index.php?do=disconnect">Disconnect</a></li>
</ul> </ul>
{if condition="$current_user->getAdmin() == 1"} {if condition="$current_user->getAdmin() == 1"}
<ul> <ul>
<li><a href="index.php?do=manage_paybacks">Manage paybacks</a></li> <li><a href="index.php?do=manage_paybacks">Manage global paybacks</a></li>
<li><a href="index.php?do=edit_users">Edit users</a></li> <li><a href="index.php?do=edit_users">Edit users</a></li>
<li><a href="index.php?do=edit_notice">Edit notice on homepage</a></li> <li><a href="index.php?do=edit_notice">Edit notice on homepage</a></li>
<li><a href="index.php?do=settings">Settings</a></li> <li><a href="index.php?do=settings">Settings</a></li>

View File

@ -40,7 +40,7 @@
</tr> </tr>
{loop="invoices"} {loop="invoices"}
<tr> <tr>
<td>{$value->getDate('d-m-Y A')}</td> <td>{$value->getDate('d/m/Y A')}</td>
<td>{$users[$value->getBuyer()]->getDisplayName()}</td> <td>{$users[$value->getBuyer()]->getDisplayName()}</td>
<td> <td>
{loop="$value->getUsersIn()->get()"} {loop="$value->getUsersIn()->get()"}
@ -50,10 +50,39 @@
{elseif condition="$value2 == 1"} {elseif condition="$value2 == 1"}
({$value2} guest) ({$value2} guest)
{/if} {/if}
-
{if condition="$paybacks[$value1->getId()] === false"}
{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->getAmount()} {$currency}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
</a>
{/if}
{else}
{* TODO : partial payback *}
{if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmount()"}
{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}
Paid
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
</a>
{/if}
{else}
{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->getAmount() - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency}
{if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
</a>
{/if}
{/if}
{/if}
<br/> <br/>
{/loop} {/loop}
</td> </td>
<td>{$value->getAmount()}</td> <td>{$value->getAmount()}{$currency}</td>
<td>{$value->getWhat()}</td> <td>{$value->getWhat()}</td>
<td><a href="index.php?do=edit_invoice&id={$value->getId()}">Edit</a></td> <td><a href="index.php?do=edit_invoice&id={$value->getId()}">Edit</a></td>
<td><a href="index.php?do=delete_invoice&id={$value->getId()}">Delete</a></td> <td><a href="index.php?do=delete_invoice&id={$value->getId()}">Delete</a></td>