Skip to content

Commit

Permalink
Added support for vouchers
Browse files Browse the repository at this point in the history
  • Loading branch information
lillem4n committed Dec 8, 2012
1 parent 7bee851 commit c80bd79
Show file tree
Hide file tree
Showing 5 changed files with 462 additions and 407 deletions.
18 changes: 15 additions & 3 deletions classes/controller/admin/accounting.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Admin_Accounting extends Admincontroller {
class Controller_Admin_Accounting extends Admincontroller
{

public function before()
{
$this->xslt_stylesheet = 'admin/accounting';
xml::to_XML(array('admin_page' => 'Accounting'), $this->xml_meta);
xml::to_XML(array('current_date' => date('Y-m-d', time())), $this->xml_meta);
}
Expand All @@ -13,7 +13,7 @@ public function action_index()
{
$accounting_node = $this->xml_content->appendChild($this->dom->createElement('accounting'));

xml::to_XML(Transactions::get(NULL, 'IF(transfer_date = 0,1,0),transfer_date;'), $accounting_node, 'entry', 'id');
xml::to_XML(Transactions::get(NULL, 'IF(transfer_date = 0,1,0),transfer_date;', NULL, TRUE), $accounting_node, 'entry', 'id');
}

public function action_entry()
Expand Down Expand Up @@ -54,17 +54,28 @@ public function action_entry()
'employee_id' => $post->get('employee_id'),
);

if (isset($_POST['rm_voucher']))
{
$new_transaction_data['rm_vouchers'] = array();
foreach (array_keys($_POST['rm_voucher']) as $nr)
$new_transaction_data['rm_vouchers'][] = $_POST['rm_voucher_names'][$nr];
}

if ( ! isset($_GET['id']))
{
$transaction = new Transaction(NULL, $new_transaction_data);
$this->add_message('Transaction '.$transaction->get_id().' added');
if (isset($_FILES['voucher'])) $transaction->add_voucher($_FILES['voucher']);
$this->set_formdata(array('accounting_date' => date('Y-m-d', time()), 'transfer_date' => date('Y-m-d', time())));
}
else
{
$transaction = new Transaction($_GET['id']);
$transaction->set($new_transaction_data);
$this->add_message('Transaction '.$transaction->get_id().' updated');
$this->set_formdata($transaction->get());
if (isset($_FILES['voucher'])) $transaction->add_voucher($_FILES['voucher']);
xml::to_XML($transaction->get('vouchers'), array('vouchers' => $this->xml_content), 'voucher');
}
}
else
Expand All @@ -77,6 +88,7 @@ public function action_entry()
{
$transaction = new Transaction($_GET['id']);
$this->set_formdata($transaction->get());
xml::to_XML($transaction->get('vouchers'), array('vouchers' => $this->xml_content), 'voucher');
}
else
{
Expand Down
65 changes: 46 additions & 19 deletions classes/model/transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ class Model_Transaction extends Model
private $id;
private static $prepared_insert; // PDO prepared insert object

public function __construct($id = NULL, $data = NULL)
public function __construct($id = NULL, $data = NULL, $voucher = FALSE)
{
parent::__construct();

if ($id === NULL && is_array($data))
{
if ($this->id = $this->add($data))
{
$this->data = $data;
}
if ($this->id = $this->add($data, $voucher))
$this->load_entry_data($this->id);
}
elseif ($id > 0)
{
$id = (int) preg_replace("/[^0-9]+/", '', $id);
if ($this->load_entry_data($id))
{
$this->id = $id;
}
if ($this->load_entry_data($id)) $this->id = $id;
}
}

Expand All @@ -40,6 +35,7 @@ public function __construct($id = NULL, $data = NULL)
* 'sum' => amount of money, above 0
* 'employee_id' => int or NULL OPTIONAL
* )
* @param arr $voucher - from a file form field
* @return int
**/
private function add($data)
Expand All @@ -51,9 +47,7 @@ private function add($data)
if ( ! isset($data['employee_id'])) $data['employee_id'] = NULL;

if (self::$prepared_insert == NULL)
{
self::$prepared_insert = $this->pdo->prepare('INSERT INTO transactions (accounting_date, transfer_date, description, journal_id, vat, sum, employee_id) VALUES(?,?,?,?,?,?,?)');
}

self::$prepared_insert->execute(array(
$data['accounting_date'],
Expand All @@ -68,6 +62,27 @@ private function add($data)
return $this->pdo->lastInsertId();
}

/**
* Add voucher to this transaction
*
* @param arr $voucher - from a file form field
* @return boolean
**/
public function add_voucher($voucher)
{
if ( ! ($this->id > 0)) throw new Kohana_Exception('No transaction ID set');

$folder = APPPATH.'user_content/vouchers/'.$this->get_id();
exec('mkdir -p '.$folder);

if (isset($voucher['error']) && $voucher['error'] == 0)
{
move_uploaded_file($voucher['tmp_name'], $folder.'/'.$voucher['name']);
$this->load_entry_data($this->get_id()); // Load the new data to the local data cache
return TRUE;
}
}

/**
* Set data in current transaction
*
Expand All @@ -87,17 +102,20 @@ public function set($data)
{
if ( ! ($this->id > 0)) throw new Kohana_Exception('No transaction ID set');

$sql = 'UPDATE transactions SET';
$sql = 'UPDATE transactions SET';
$columns = array_keys($this->pdo->query('SELECT * FROM transactions LIMIT 1;')->fetch(PDO::FETCH_ASSOC));
if (isset($columns[array_search('id', $columns)]))
unset($columns[array_search('id', $columns)]); // Unset ID, we should never try to update that

foreach ($data as $field => $value)
{
$columns = array_keys($this->pdo->query('SELECT * FROM transactions LIMIT 1;')->fetch(PDO::FETCH_ASSOC));

if (isset($columns[array_search('id', $columns)]))
unset($columns[array_search('id', $columns)]); // Unset ID, we should never try to update that

if (in_array($field, $columns))
$sql .= '`'.$field.'` = '.$this->pdo->quote($value).',';
elseif ($field == 'rm_vouchers')
{
foreach ($value as $filename)
unlink(APPPATH.'user_content/vouchers/'.$this->id.'/'.$filename);
}
}

$sql = substr($sql, 0, strlen($sql) - 1) . ' WHERE id = '.$this->pdo->quote($this->get_id()).' LIMIT 1';
Expand All @@ -121,9 +139,18 @@ public function get_id()
return $this->id;
}

private function load_entry_data($id)
protected function load_entry_data($id)
{
return ($this->data = $this->pdo->query('SELECT * FROM transactions WHERE id = '.$id)->fetch(PDO::FETCH_ASSOC));
if ($this->data = $this->pdo->query('SELECT * FROM transactions WHERE id = '.$id)->fetch(PDO::FETCH_ASSOC))
{
$this->data['vouchers'] = array();
foreach (glob(APPPATH.'user_content/vouchers/'.$id.'/*') as $voucher)
$this->data['vouchers'][] = pathinfo($voucher, PATHINFO_BASENAME);

return TRUE;
}

return FALSE;
}

}
18 changes: 16 additions & 2 deletions classes/model/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function __construct()
parent::__construct();
}

public static function get($search = NULL, $order_by = 'accounting_date', $where = NULL)
public static function get($search = NULL, $order_by = 'accounting_date', $where = NULL, $format_for_XML = FALSE)
{
$pdo = Kohana_pdo::instance();

Expand Down Expand Up @@ -48,7 +48,21 @@ public static function get($search = NULL, $order_by = 'accounting_date', $where
ORDER BY '.$order_by;
// ORDER BY NEEDS TO BE SECURED!!!!!0101=!11111ett

return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$transactions = array();
foreach ($pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $transaction)
{
foreach (glob(APPPATH.'user_content/vouchers/'.$transaction['id'].'/*') as $nr => $voucher)
{
if ($format_for_XML)
$transaction['vouchers'][$nr.'voucher'] = pathinfo($voucher, PATHINFO_BASENAME);
else
$transaction['vouchers'][] = pathinfo($voucher, PATHINFO_BASENAME);
}

$transactions[] = $transaction;
}

return $transactions;
}

}
Loading

0 comments on commit c80bd79

Please sign in to comment.