4 $bank_table='transaction';
5 $recursive=true; //USE RECURSIVE QUERIES???
8 function bank_name($name) {
9 return strtolower(trim($name));
12 function bank_transaction($ctx, $from, $to, $comment, $amount=0) {
14 $author=$ctx->db
->quote($ctx->db
->auth
->get_user_id());
15 $from=$ctx->db
->quote(bank_name($from));
16 $to=$ctx->db
->quote(bank_name($to));
17 $amount=$ctx->db
->quote($amount);
18 $comment=$ctx->db
->quote(trim($comment));
20 $sql="INSERT INTO `${bank_table}` (`${bank_table}_time`, `${bank_table}_from`, `${bank_table}_to`, `${bank_table}_amount`, `${bank_table}_author`, `${bank_table}_comment`) VALUES (now(), $from, $to, $amount, $author, $comment);";
21 $ctx->db
->safe_query($sql);
24 function bank_get_accounts($ctx, $all=false) {
26 $fetch = $ctx->db
->safe_query_fetch("SELECT DISTINCT ${bank_table}_to FROM ${bank_table} ORDER BY ${bank_table}_to;");
27 foreach($fetch as $account) if($all ||
$account[$bank_table.'_to'][0]!='_') $accounts[]=$account[$bank_table.'_to'];
31 function bank_get_last_to($ctx, $account) {
33 $account=$ctx->db
->quote(bank_name($account));
34 $fetch = $ctx->db
->safe_query_fetch("SELECT ${bank_table}_to FROM ${bank_table} WHERE ${bank_table}_from=$account ORDER BY ${bank_table}_time DESC LIMIT 1;");
35 return $fetch[0][$bank_table.'_to'];
38 function bank_add_account($ctx, $name) {
39 bank_transaction($ctx, $name, $name, "Created account \"$name\"");
42 function bank_month_sql($ctx, $month=false) {
45 if(!is_bool($month)) {
46 $month_q = $ctx->db
->quote($month);
47 $month_sql .= " AND DATE_FORMAT(${bank_table}_time, '%Y-%m') = ".$month_q;
52 function bank_get_total($ctx, $account, $month, $string=false) {
54 $account_sql=$ctx->db
->quote($account);
55 $result = $ctx->db
->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql AND ".bank_month_sql($ctx,$month).';');
56 $deposits = $result[0]["SUM(${bank_table}_amount)"];
57 $result = $ctx->db
->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_from`=$account_sql AND (".bank_month_sql($ctx,$month).');');
58 $withdrawals = $result[0]["SUM(${bank_table}_amount)"];
59 if($string) return "$deposits-$withdrawals";
60 return $deposits-$withdrawals;
62 function bank_rename_account($ctx, $old, $new) {
64 if(in_array($new, bank_get_accounts($ctx, true))) return false;
65 $old=$ctx->db
->quote($old);
66 $new=$ctx->db
->quote($new);
68 return $ctx->db
->safe_query(
70 "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;".
71 "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;".
76 function bank_get_overview($ctx,$prefix='',$month=false) {
78 $accounts = bank_get_accounts($ctx);
79 foreach($accounts as $acc) {
80 $total=bank_get_total($ctx, $acc, $month);
81 $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total);
82 $overview['array'][$acc]=$total;
87 if(isset($bank_json_only) && $bank_json_only) {
88 $overview=bank_get_overview($this,'');
89 die(json_encode(array(
90 'overview'=>$overview['array']
94 if(isset($_POST['create_account'])) {
95 $new_account=$_POST['account_name'];
96 bank_add_account($this, $new_account);
97 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!");
99 if(isset($_POST['rename_account'])) {
100 $new_account=$_POST['account_new'];
101 $old_account=$_POST['account_old'];
102 if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) {
103 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!");
105 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false);
108 if(isset($_POST['transaction'])) {
109 $account_from=$_POST['account_from'];
110 $account_to=$_POST['account_to'];
111 $amount=$_POST['amount'];
112 $comment=trim($_POST['comment']);
113 $account_redirect=$account_from;
114 if(!is_numeric($amount)) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Převáděnou částkou musí být celé číslo.", true);
116 $amount=abs($amount);
117 list($account_from,$account_to)=array($account_to,$account_from); //swap from/to
119 if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Komentář musí mít alespoň 4 znaky!",true);
120 bank_transaction($this, $account_from, $account_to, $comment, $amount);
121 $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_redirect),"Transakce byla provedena:<br />Převod <b>$amount $bank_currency</b> z účtu <b>$account_from</b> na účet <b>$account_to</b>.<br />($comment)");
124 $month = isset($_GET['month']) ?
$_GET['month'] : false;
126 //bank_add_account($this, 'material');
127 echo("<a href='$URL/'>Banka</a> - ");
128 echo("<a href='$URL/admin'>Správa účtů</a> - ");
129 echo('<span style="float:right;">');
130 echo $this->html
->form($URL, 'GET', array(
131 array('month',$month,'text',false,'','YYYY-MM:'),
132 array(false,'SELECT BY MONTH','submit')
135 echo("Účty: <br />");
136 $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
138 foreach($accounts as $account) {
139 if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />');
140 echo("<a href='$URL?account=".urlencode($account)."'>$account</a>, ");
141 $lastaccount=$account;
146 switch($SUBPATH[0]) {
149 if(isset($_GET['limit'])) $limit = intval($_GET['limit']);
150 $limit_sql = $limit==0 ?
'' : ' LIMIT '.intval($limit);
152 if(!isset($_GET['account'])) {
153 echo("<h1>Banka $month</h1>");
154 echo ("<h2>Stav $month</h2>");
155 $result = $this->db
->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
156 echo("Transakcí $month: ".$result[0]['troughput']."<br />");
157 $result = $this->db
->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
158 echo("Obrat $month: ".$result[0]['troughput'].' '.$bank_currency);
159 $result = $this->db
->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE ".bank_month_sql($this,$month)." ORDER BY ${bank_table}_time DESC".$limit_sql.";");
160 $overview=bank_get_overview($this,$bank_table.'_',$month);
161 echo $this->html
->render_item_table($overview['table'],'bank');
163 $account=bank_name($_GET['account']);
164 $account_sql=$this->db
->quote($account);
165 echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>");
168 <form action
="?" method
="POST">
169 Převést
<!-- ±
; --><input type
="number" name
="amount" value
="" /> <?php
echo $bank_currency; ?
>
170 z účtu
<b
><?php
echo $account; ?
></b
> <input type
="hidden" name
="account_from" value
="<?php echo $account; ?>" />
171 na účet
<select name
='account_to'>
173 //Ziskat posledni cilovy ucet a presunout na zacatek $accounts
174 $last=bank_get_last_to($this,$account);
175 unset($accounts[array_search($last,$accounts)]);
176 array_unshift($accounts,$last);
178 foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>");
180 </select
> (pozor
! zamysli se
! převádíš peníze nebo dluhy?
! záporná částka
= převod v opačném směru
.)<br
/><br
/>
181 Důvod
: <input type
="text" name
="comment" maxlength
="128" style
="width:64em;" />
182 <input type
="submit" name
="transaction" value
="Převést" />
186 echo(bank_get_total($this,$account,$month,true)." $bank_currency");
187 $subtotal=$recursive?
",(
188 (SELECT SUM(${bank_table}_amount) FROM ${bank_table} x WHERE ${bank_table}_to=$account_sql AND x.${bank_table}_id<=${bank_table}.${bank_table}_id)
189 -(SELECT SUM(${bank_table}_amount) FROM ${bank_table} x WHERE ${bank_table}_from=$account_sql AND x.${bank_table}_id<=${bank_table}.${bank_table}_id)
190 ) as ${bank_table}_subtotal":'';
191 //(@flux := IF(transaction_to='harvie',IF(transaction_from='harvie',0,1),IF(transaction_from='harvie',-1,0))) as flux
192 $result = $this->db
->safe_query_fetch("SELECT *${subtotal} FROM `${bank_table}` WHERE (`${bank_table}_to`=$account_sql OR `${bank_table}_from`=$account_sql) AND (".bank_month_sql($this,$month).") ORDER BY ${bank_table}_time DESC".$limit_sql.";");
194 echo ("<h2>Přehled transakcí $month</h2>");
195 echo $this->html
->render_item_table($result,$bank_table);
197 if(!isset($_GET['limit']))
198 echo("<a href='?".$_SERVER['QUERY_STRING']."&limit=0'>zobrazit vše...</a>");
206 <form action
="<?php echo $ASSISTANT; ?>" method
="POST" >
208 <input type
="text" name
="account_name" />
209 <input type
="submit" name
="create_account" value
="Create account" />
211 <form action
="<?php echo $ASSISTANT; ?>" method
="POST" >
212 Account
: <select name
='account_old'>
213 <?php
foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?
>
215 <input type
="text" name
="account_new" />
216 <input type
="submit" name
="rename_account" value
="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné
)