Prefill form for modifications of bills

This commit is contained in:
Phyks 2013-08-17 19:16:16 +02:00
parent 53e7d93670
commit 52f6b7fc52
8 changed files with 74 additions and 27 deletions

View File

@ -3,7 +3,7 @@
require_once('Storage.class.php');
class Invoice extends Storage {
protected $id, $date, $users_in, $buyer, $amount, $what;
protected $id = 0, $date, $users_in, $buyer, $amount, $what;
protected $TABLE_NAME = "Invoices";
protected $fields = array(
'id'=>'key',
@ -80,4 +80,22 @@
return $return;
}
public function load_invoice($fields = NULL) {
$fetch = $this->load($fields);
if(count($fetch) > 0) {
$this->setId($fetch[0]['id']);
$this->setWhat($fetch[0]['what']);
$this->setAmount($fetch[0]['amount']);
$this->setBuyer($fetch[0]['buyer']);
$this->setUsersIn($fetch[0]['users_in']);
$this->setDate($fetch[0]['date']);
return true;
}
else {
return false;
}
}
}

View File

@ -158,7 +158,7 @@ class Storage {
$query = $this->connection->prepare($query);
foreach($this->fields as $field=>$type) {
if(!empty($this->$field)) {
if(isset($this->$field)) {
$query->bindParam(':'.$field, $this->$field);
}
}

View File

@ -3,7 +3,7 @@ require_once('data/config.php');
require_once('Storage.class.php');
class User extends Storage {
protected $id, $login, $display_name, $password, $admin;
protected $id = 0, $login, $display_name, $password, $admin;
protected $TABLE_NAME = "Users";
protected $fields = array(
'id'=>'key',

View File

@ -203,8 +203,33 @@
break;
case 'new_invoice':
case 'edit_invoice':
if(!empty($_GET['id'])) {
$invoice = new Invoice();
$invoice->load_invoice(array('id'=>(int) $_GET['id']));
$date_day = '';
$date_month = '';
$date_year = '';
$amount = $invoice->getAmount();
$what = $invoice->getWhat();
$users_in = explode(',', $invoice->getUsersIn());
$guests = array();
}
if(!empty($_POST['what'])) $what = $_POST['what'];
if(!empty($_POST['amount'])) $amount = $_POST['amount'];
if(!empty($_POST['date_day'])) $date_day = $_POST['date_day'];
if(!empty($_POST['date_month'])) $date_month = $_POST['date_month'];
if(!empty($_POST['date_year'])) $date_year = $_POST['date_year'];
if(!empty($_POST['users_in'])) $users_in = $_POST['users_in'];
if(!empty($_POST['what']) && !empty($_POST['amount']) && (float) $_POST['amount'] != 0 && !empty($_POST['date_day']) && !empty($_POST['date_month']) && !empty($_POST['date_year']) && !empty($_POST['users_in'])) {
$invoice = new Invoice();
if(!empty($_POST['id']))
$invoice->setId($_POST['id']);
$invoice->setWhat($_POST['what']);
$invoice->setAmount($_POST['amount']);
$invoice->setBuyer($current_user->getId());
@ -219,9 +244,9 @@
}
$invoice->setUsersIn($users_in);
//$invoice->save();
// header('location: index.php');
// exit();
$invoice->save();
header('location: index.php');
exit();
}
$users_list = new User();
@ -231,14 +256,15 @@
$tpl->assign('months', range(1, 12));
$tpl->assign('years', range(date('Y') - 1, date('Y') + 1));
$tpl->assign('day_post', (!empty($_POST['date_day']) ? (int) $_POST['date_day'] : (int) date('d')));
$tpl->assign('month_post', (!empty($_POST['date_month']) ? (int) $_POST['date_month'] : (int) date('m')));
$tpl->assign('year_post', (!empty($_POST['date_year']) ? (int) $_POST['date_year'] : (int) date('Y')));
$tpl->assign('amount_post', (!empty($_POST['amount']) ? (float) $_POST['amount'] : 0));
$tpl->assign('what_post', (!empty($_POST['what']) ? htmlspecialchars($_POST['what']) : ''));
$tpl->assign('day_post', (!empty($date_day) ? (int) $date_day : (int) date('d')));
$tpl->assign('month_post', (!empty($date_month) ? (int) $date_month : (int) date('m')));
$tpl->assign('year_post', (!empty($date_year) ? (int) $date_year : (int) date('Y')));
$tpl->assign('amount_post', (!empty($amount) ? (float) $amount : 0));
$tpl->assign('what_post', (!empty($what) ? htmlspecialchars($what) : ''));
$tpl->assign('users', $users_list);
$tpl->assign('users_in', (!empty($_POST['users_in']) ? $_POST['users_in'] : array()));
$tpl->assign('users_in', (!empty($users_in) ? $users_in : array()));
$tpl->assign('guests', (!empty($guests) ? $guests : array()));
$tpl->assign('id', (!empty($_GET['id']) ? (int) $_GET['id'] : 0));
$tpl->draw('new_invoice');
break;
@ -246,7 +272,7 @@
$users_list = new User();
$users_list = $users_list->load_users();
$invoices_list = new Invoices();
$invoices_list = new Invoice();
$invoices_list = $invoices_list->load_invoices();
$tpl->assign('users', $users_list);

View File

@ -49,13 +49,13 @@
<?php $counter1=-1; if( isset($invoices) && is_array($invoices) && sizeof($invoices) ) foreach( $invoices as $key1 => $value1 ){ $counter1++; ?>
<tr>
<td><?php echo $value1->getDate;?></td>
<td><?php echo $value1->getBuyer;?></td>
<td><?php echo $value1->getUsersIn;?></td>
<td><?php echo $value1->getAmount;?></td>
<td><?php echo $value1->getWhat;?></td>
<td><a href="index.php?do=edit_bill&id=<?php echo $value1->getId();?>">Edit</a></td>
<td><a href="index.php?do=delete_bill&id=<?php echo $value1->getId();?>">Delete</a></td>
<td><?php echo $value1->getDate();?></td>
<td><?php echo $value1->getBuyer();?></td>
<td><?php echo $value1->getUsersIn();?></td>
<td><?php echo $value1->getAmount();?></td>
<td><?php echo $value1->getWhat();?></td>
<td><a href="index.php?do=edit_invoice&id=<?php echo $value1->getId();?>">Edit</a></td>
<td><a href="index.php?do=delete_invoice&id=<?php echo $value1->getId();?>">Delete</a></td>
</tr>
<?php } ?>

View File

@ -47,6 +47,8 @@
</p>
<p>
<input type="submit" value="Add"/>
<?php if( $id != 0 ){ ?><input type="hidden" name="id" value="<?php echo $id;?>"/><?php } ?>
</p>
</form>

View File

@ -39,13 +39,13 @@
</tr>
{loop="invoices"}
<tr>
<td>{$value->getDate}</td>
<td>{$value->getBuyer}</td>
<td>{$value->getUsersIn}</td>
<td>{$value->getAmount}</td>
<td>{$value->getWhat}</td>
<td><a href="index.php?do=edit_bill&id={$value->getId()}">Edit</a></td>
<td><a href="index.php?do=delete_bill&id={$value->getId()}">Delete</a></td>
<td>{$value->getDate()}</td>
<td>{$value->getBuyer()}</td>
<td>{$value->getUsersIn()}</td>
<td>{$value->getAmount()}</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=delete_invoice&id={$value->getId()}">Delete</a></td>
</tr>
{/loop}
</table>

View File

@ -37,6 +37,7 @@
</p>
<p>
<input type="submit" value="Add"/>
{if condition="$id != 0"}<input type="hidden" name="id" value="{$id}"/>{/if}
</p>
</form>