Skip to content

Commit

Permalink
Loads of changes and fiddles
Browse files Browse the repository at this point in the history
  • Loading branch information
lillem4n committed Jul 17, 2014
1 parent cc3fe82 commit 7122470
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 94 deletions.
158 changes: 106 additions & 52 deletions classes/controller/admin/accounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,127 @@ public function before()

public function action_index()
{
$accounting_node = $this->xml_content->appendChild($this->dom->createElement('accounting'));
if ( ! isset($_GET['from_date'])) $_GET['from_date'] = '1970-01-01';
if ( ! isset($_GET['to_date'])) $_GET['to_date'] = date('Y-m-d');
if ( ! isset($_GET['cash_position'])) $_GET['cash_position'] = 'All';

xml::to_XML(Transactions::get(NULL, 'IF(transfer_date = 0,1,0),transfer_date;', NULL, TRUE), $accounting_node, 'entry', 'id');
}
$_GET['from_date'] = date('Y-m-d', strtotime($_GET['from_date']));
$_GET['to_date'] = date('Y-m-d', strtotime($_GET['to_date']));

public function action_dlvouchers()
{
set_time_limit(0);
$files = array();
$where = '1';
$pdo = Kohana_Pdo::instance();

/** /
if (date('m') < 4) $quarter = 1;
elseif (date('m') < 7) $quarter = 2;
elseif (date('m') < 10) $quarter = 3;
else $quarter = 4;
if (isset($_GET['dlvouchers']))
{
set_time_limit(0);
$files = array();

if ($quarter == 1) $year = intval(date('Y') - 1);
else $year = intval(date('Y'));
/** /
if (date('m') < 4) $quarter = 1;
elseif (date('m') < 7) $quarter = 2;
elseif (date('m') < 10) $quarter = 3;
else $quarter = 4;
$where = 'YEAR(accounting_date) = '.$year.' AND MONTH(accounting_date) BETWEEN ';
if ($quarter == 1) $year = intval(date('Y') - 1);
else $year = intval(date('Y'));
if ($quarter == 1) $where .= '10 AND 12';
elseif ($quarter == 2) $where .= '1 AND 3';
elseif ($quarter == 3) $where .= '4 AND 6';
elseif ($quarter == 4) $where .= '7 AND 9';
/**/
$where = 'YEAR(accounting_date) = '.$year.' AND MONTH(accounting_date) BETWEEN ';
if ( ! isset($_GET['from_date'])) $_GET['from_date'] = '1970-01-01';
if ( ! isset($_GET['to_date'])) $_GET['to_date'] = date('Y-m-d');
if ($quarter == 1) $where .= '10 AND 12';
elseif ($quarter == 2) $where .= '1 AND 3';
elseif ($quarter == 3) $where .= '4 AND 6';
elseif ($quarter == 4) $where .= '7 AND 9';
/**/

$from_date = date('Y-m-d', strtotime($_GET['from_date']));
$to_date = date('Y-m-d', strtotime($_GET['to_date']));
$from_date = date('Y-m-d', strtotime($_GET['from_date']));
$to_date = date('Y-m-d', strtotime($_GET['to_date']));

$where = 'transfer_date >= \''.$from_date.'\' AND transfer_date <= \''.$to_date.'\'';
$where = 'transfer_date >= \''.$from_date.'\' AND transfer_date <= \''.$to_date.'\'';

foreach (Transactions::get(NULL, 'IF(transfer_date = 0,1,0),transfer_date;', $where, TRUE) as $transaction)
if ( ! empty($transaction['vouchers']))
foreach ($transaction['vouchers'] as $voucher_file)
$files[date('Y-m-d', strtotime($transaction['transfer_date']))][] = APPPATH.'user_content/vouchers/'.$transaction['id'].'/'.$voucher_file;
foreach (Transactions::get(NULL, 'IF(transfer_date = 0,1,0),transfer_date;', $where, TRUE) as $transaction)
if ( ! empty($transaction['vouchers']))
foreach ($transaction['vouchers'] as $voucher_file)
$files[date('Y-m-d', strtotime($transaction['transfer_date']))][] = APPPATH.'user_content/vouchers/'.$transaction['id'].'/'.$voucher_file;

if (count($files))
if (count($files))
{
$zipname = APPPATH.'user_content/'.$from_date.'_-_'.$to_date.'_vouchers.zip';
$zip = new ZipArchive;
if ($zip->open($zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === TRUE)
{
foreach ($files as $date => $files)
foreach ($files as $file)
if (file_exists($file))
$zip->addFile($file, $date.' - '.pathinfo($file, PATHINFO_BASENAME));

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.pathinfo($zipname, PATHINFO_BASENAME));
header('Content-Length: '.filesize($zipname));
echo file_get_contents($zipname);
unlink($zipname);
die();
} else die('error opening ziparchive');
}
else die('no vouchers found');
}

$accounting_node = $this->xml_content->appendChild($this->dom->createElement('accounting'));

if ( ! isset($_GET['order_by']))
$_GET['order_by'] = 'transfer_date';

if ($_GET['order_by'] == 'accounting_date')
{
$zipname = APPPATH.'user_content/'.$from_date.'_-_'.$to_date.'_vouchers.zip';
$zip = new ZipArchive;
if ($zip->open($zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === TRUE)
$where = 'accounting_date <= \''.$_GET['to_date'].'\'';
$order_by = 'IF(accounting_date = 0,1,0),accounting_date;';
}
else
{
$where = 'transfer_date <= \''.$_GET['to_date'].'\'';
$order_by = 'IF(transfer_date = 0,1,0),transfer_date;';
}

if ($_GET['cash_position'] != 'All')
$where .= ' AND cash_position = '.$pdo->quote($_GET['cash_position']);

$transactions = Transactions::get(NULL, $order_by, $where, TRUE);
$balances = array(
'balance' => 0,
'balance_all' => 0,
'vat_balance' => 0,
'vat_balance_all' => 0,
);
foreach ($transactions as $nr => $transaction)
{
if ($_GET['order_by'] == 'accounting_date' && strtotime($transaction['accounting_date']) < strtotime($_GET['from_date']))
unset($transactions[$nr]);

elseif ($_GET['order_by'] == 'transfer_date' && strtotime($transaction['transfer_date']) < strtotime($_GET['from_date']))
unset($transactions[$nr]);

else
{
foreach ($files as $date => $files)
foreach ($files as $file)
if (file_exists($file))
$zip->addFile($file, $date.' - '.pathinfo($file, PATHINFO_BASENAME));

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.pathinfo($zipname, PATHINFO_BASENAME));
header('Content-Length: '.filesize($zipname));
echo file_get_contents($zipname);
unlink($zipname);
die();
} else die('error opening ziparchive');
$balances['vat_balance'] += $transaction['vat'];
$balances['balance'] += $transaction['sum'];
}

$balances['vat_balance_all'] += $transaction['vat'];
$balances['balance_all'] += $transaction['sum'];
}
else die('no vouchers found');

xml::to_XML($transactions, $accounting_node, 'entry', 'id');
xml::to_XML(array('balances' => $balances), $this->xml_content);
xml::to_XML(Transactions::get_cash_positions(), $this->xml_content->appendChild($this->dom->createElement('cash_positions')), 'cash_position');
}

public function action_entry()
{
// Set employees node
$employees_node = $this->xml_content->appendChild($this->dom->createElement('employees'));
$employees = array('0option'=>array('@value'=>'0', 'None'));
$counter = 1;
$employees_node = $this->xml_content->appendChild($this->dom->createElement('employees'));
$employees = array('0option' => array('@value' => '0', 'None'));
$counter = 1;
foreach (Employees::get() as $employee)
{
$employees[$counter.'option'] = array(
Expand Down Expand Up @@ -111,6 +162,8 @@ public function action_entry()
'vat' => $post->get('vat'),
'sum' => $post->get('sum'),
'employee_id' => $post->get('employee_id'),
'cash_position' => $post->get('cash_position'),
'account' => $post->get('account'),
);

if (isset($_POST['rm_voucher']))
Expand All @@ -124,7 +177,8 @@ public function action_entry()
{
$transaction = new Transaction(NULL, $new_transaction_data);
$this->add_message('Transaction '.$transaction->get_id().' added');
if (isset($_FILES['voucher'])) $transaction->add_voucher($_FILES['voucher']);
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
Expand Down
13 changes: 11 additions & 2 deletions classes/model/transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __construct($id = NULL, $data = NULL, $voucher = FALSE)
* 'vat' => amount of money, 0 or above OPTIONAL
* 'sum' => amount of money, above 0
* 'employee_id' => int or NULL OPTIONAL
* 'cash_position' => str or NULL OPTIONAL
* 'account' => int or NULL OPTIONAL
* )
* @param arr $voucher - from a file form field
* @return int
Expand All @@ -45,9 +47,14 @@ private function add($data)
if ( ! isset($data['journal_id'])) $data['journal_id'] = NULL;
if ( ! isset($data['vat'])) $data['vat'] = 0;
if ( ! isset($data['employee_id'])) $data['employee_id'] = NULL;
if ( ! isset($data['cash_position'])) $data['cash_position'] = '';
if ( ! isset($data['account'])) $data['account'] = 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(?,?,?,?,?,?,?)');
{
$sql = 'INSERT INTO transactions (accounting_date, transfer_date, description, journal_id, vat, sum, employee_id, cash_position, account) VALUES(?,?,?,?,?,?,?,?,?)';
self::$prepared_insert = $this->pdo->prepare($sql);
}

self::$prepared_insert->execute(array(
$data['accounting_date'],
Expand All @@ -56,7 +63,9 @@ private function add($data)
$data['journal_id'],
$data['vat'],
$data['sum'],
$data['employee_id']
$data['employee_id'],
$data['cash_position'],
$data['account']
));

return $this->pdo->lastInsertId();
Expand Down
25 changes: 21 additions & 4 deletions classes/model/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static function get($search = NULL, $order_by = 'accounting_date', $where

$where = substr($where, 0, strlen($where) - 4);
}
elseif ($where === NULL) $where = '1';
elseif ($where === NULL)
$where = '1';

$sql = '
SELECT
Expand All @@ -45,10 +46,10 @@ public static function get($search = NULL, $order_by = 'accounting_date', $where
FROM transactions
LEFT JOIN employees ON employees.id = transactions.employee_id
WHERE '.$where.'
ORDER BY '.$order_by;
// ORDER BY NEEDS TO BE SECURED!!!!!0101=!11111ett
ORDER BY '.$order_by; // ORDER BY NEEDS TO BE SECURED!!!!!0101=!11111ett

$transactions = array();
$balance = 0;
foreach ($pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $transaction)
{
foreach (glob(APPPATH.'user_content/vouchers/'.$transaction['id'].'/*') as $nr => $voucher)
Expand All @@ -59,10 +60,26 @@ public static function get($search = NULL, $order_by = 'accounting_date', $where
$transaction['vouchers'][] = pathinfo($voucher, PATHINFO_BASENAME);
}

$balance += $transaction['sum'];
$transaction['balance'] = $balance;

$transactions[] = $transaction;
}

return $transactions;
}

}
public static function get_cash_positions()
{
$pdo = Kohana_pdo::instance();

$sql = 'SELECT DISTINCT cash_position FROM transactions ORDER BY cash_position;';

$cash_positions = array('All');
foreach ($pdo->query($sql) as $row)
$cash_positions[] = $row['cash_position'];

return $cash_positions;
}

}
3 changes: 2 additions & 1 deletion config/larv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return array
(
/*
'soc_fee_levels' => array(
array(
'start_age' => 0,
Expand All @@ -18,7 +19,7 @@
'end_age' => 73,
'level' => 10.21,
),
),
),*/
'email' => array(
'from' => '[email protected]',
'from_name' => 'Larv IT AB',
Expand Down
9 changes: 6 additions & 3 deletions js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ $(document).ready(function() {
var currentTime = new Date()
var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
if (month.toString().length == 1) month = '0'+month;
var day = currentTime.getDate();
if (day.toString().length == 1) day = '0'+day;
var day = currentTime.getDate();

if (month.toString().length == 1)
month = '0'+month;

if (day.toString().length == 1)
day = '0'+day;

////////////////////////////
// Calculations for wages //
Expand Down
18 changes: 0 additions & 18 deletions js/jquery-1.6.2.min.js

This file was deleted.

Loading

0 comments on commit 7122470

Please sign in to comment.