Commit | Line | Data |
---|---|---|
f94aa899 TM |
1 | <?php |
2 | $bank_currency='Kč'; | |
8044dfef | 3 | global $bank_table; |
ee2e7415 | 4 | $bank_table='transaction'; |
0e44ea7a | 5 | $recursive=true; //USE RECURSIVE QUERIES??? |
9787a5ca | 6 | $limit=23; |
f94aa899 TM |
7 | |
8 | function bank_name($name) { | |
9 | return strtolower(trim($name)); | |
10 | } | |
11 | ||
12 | function bank_transaction($ctx, $from, $to, $comment, $amount=0) { | |
8044dfef | 13 | global $bank_table; |
f94aa899 TM |
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)); | |
399c761f | 17 | $amount=$ctx->db->quote($amount); |
68f2420e | 18 | $comment=$ctx->db->quote(trim($comment)); |
f94aa899 | 19 | |
8044dfef | 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);"; |
f94aa899 TM |
21 | $ctx->db->safe_query($sql); |
22 | } | |
23 | ||
7a7ee029 | 24 | function bank_get_accounts($ctx, $all=false) { |
8044dfef TM |
25 | global $bank_table; |
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']; | |
f94aa899 TM |
28 | return $accounts; |
29 | } | |
30 | ||
048fadfc TM |
31 | function bank_get_last_to($ctx, $account) { |
32 | global $bank_table; | |
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']; | |
36 | } | |
37 | ||
f94aa899 TM |
38 | function bank_add_account($ctx, $name) { |
39 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); | |
40 | } | |
41 | ||
6e9dc519 TM |
42 | function bank_month_sql($ctx, $month=false) { |
43 | global $bank_table; | |
44 | $month_sql = 'TRUE'; | |
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; | |
48 | } | |
49 | return $month_sql; | |
50 | } | |
51 | ||
52 | function bank_get_total($ctx, $account, $month, $string=false) { | |
8044dfef | 53 | global $bank_table; |
98437b3b | 54 | $account_sql=$ctx->db->quote($account); |
6e9dc519 | 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).';'); |
8044dfef | 56 | $deposits = $result[0]["SUM(${bank_table}_amount)"]; |
6e9dc519 | 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).');'); |
8044dfef | 58 | $withdrawals = $result[0]["SUM(${bank_table}_amount)"]; |
98437b3b TM |
59 | if($string) return "$deposits-$withdrawals"; |
60 | return $deposits-$withdrawals; | |
61 | } | |
7a7ee029 | 62 | function bank_rename_account($ctx, $old, $new) { |
8044dfef | 63 | global $bank_table; |
7a7ee029 TM |
64 | if(in_array($new, bank_get_accounts($ctx, true))) return false; |
65 | $old=$ctx->db->quote($old); | |
66 | $new=$ctx->db->quote($new); | |
67 | ||
68 | return $ctx->db->safe_query( | |
69 | "START TRANSACTION;". | |
8044dfef TM |
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;". | |
7a7ee029 TM |
72 | "COMMIT;" |
73 | ); | |
74 | } | |
98437b3b | 75 | |
6e9dc519 | 76 | function bank_get_overview($ctx,$prefix='',$month=false) { |
8044dfef | 77 | global $bank_table; |
98437b3b | 78 | $accounts = bank_get_accounts($ctx); |
5a4b2116 | 79 | foreach($accounts as $acc) { |
6e9dc519 | 80 | $total=bank_get_total($ctx, $acc, $month); |
5a4b2116 TM |
81 | $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total); |
82 | $overview['array'][$acc]=$total; | |
83 | } | |
98437b3b TM |
84 | return $overview; |
85 | } | |
86 | ||
089bea60 | 87 | if(isset($bank_json_only) && $bank_json_only) { |
6e9dc519 | 88 | $overview=bank_get_overview($this,''); |
089bea60 TM |
89 | die(json_encode(array( |
90 | 'overview'=>$overview['array'] | |
91 | ))); | |
92 | } | |
90f6601d | 93 | |
f94aa899 | 94 | if(isset($_POST['create_account'])) { |
51ca86c4 TM |
95 | $new_account=$_POST['account_name']; |
96 | bank_add_account($this, $new_account); | |
bd3726b4 | 97 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!"); |
f94aa899 | 98 | } |
7a7ee029 | 99 | if(isset($_POST['rename_account'])) { |
51ca86c4 TM |
100 | $new_account=$_POST['account_new']; |
101 | $old_account=$_POST['account_old']; | |
7a7ee029 | 102 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { |
bd3726b4 | 103 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!"); |
7a7ee029 | 104 | } else { |
bd3726b4 | 105 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false); |
7a7ee029 TM |
106 | } |
107 | } | |
f94aa899 | 108 | if(isset($_POST['transaction'])) { |
51ca86c4 TM |
109 | $account_from=$_POST['account_from']; |
110 | $account_to=$_POST['account_to']; | |
111 | $amount=$_POST['amount']; | |
68f2420e | 112 | $comment=trim($_POST['comment']); |
ffd3556b TM |
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); | |
115 | if($amount < 0) { | |
116 | $amount=abs($amount); | |
117 | list($account_from,$account_to)=array($account_to,$account_from); //swap from/to | |
118 | } | |
a2100946 | 119 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Komentář musí mít alespoň 4 znaky!",true); |
51ca86c4 | 120 | bank_transaction($this, $account_from, $account_to, $comment, $amount); |
ffd3556b | 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)"); |
f94aa899 TM |
122 | } |
123 | ||
6e9dc519 TM |
124 | $month = isset($_GET['month']) ? $_GET['month'] : false; |
125 | ||
f94aa899 | 126 | //bank_add_account($this, 'material'); |
98437b3b | 127 | echo("<a href='$URL/'>Banka</a> - "); |
587ba49a | 128 | echo("<a href='$URL/admin'>Správa účtů</a> - "); |
6e9dc519 TM |
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') | |
133 | )); | |
134 | echo('</span>'); | |
7da3f2f4 | 135 | echo("Účty: <br />"); |
7a7ee029 | 136 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
7da3f2f4 TM |
137 | $lastaccount=false; |
138 | foreach($accounts as $account) { | |
139 | if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />'); | |
a2100946 | 140 | echo("<a href='$URL?account=".urlencode($account)."'>$account</a>, "); |
7da3f2f4 TM |
141 | $lastaccount=$account; |
142 | } | |
f94aa899 | 143 | |
6e9dc519 TM |
144 | |
145 | ||
f94aa899 TM |
146 | switch($SUBPATH[0]) { |
147 | default: | |
148 | ||
9787a5ca TM |
149 | if(isset($_GET['limit'])) $limit = intval($_GET['limit']); |
150 | $limit_sql = $limit==0 ? '' : ' LIMIT '.intval($limit); | |
151 | ||
f94aa899 | 152 | if(!isset($_GET['account'])) { |
6e9dc519 TM |
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); | |
9787a5ca | 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.";"); |
6e9dc519 | 160 | $overview=bank_get_overview($this,$bank_table.'_',$month); |
089bea60 | 161 | echo $this->html->render_item_table($overview['table'],'bank'); |
f94aa899 TM |
162 | } else { |
163 | $account=bank_name($_GET['account']); | |
164 | $account_sql=$this->db->quote($account); | |
6e9dc519 | 165 | echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>"); |
f94aa899 TM |
166 | |
167 | ?> | |
168 | <form action="?" method="POST"> | |
ffd3556b | 169 | Převést <!-- ± --><input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> |
1d0a5f85 | 170 | z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
f94aa899 | 171 | na účet <select name='account_to'> |
048fadfc TM |
172 | <?php |
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); | |
177 | ||
178 | foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); | |
179 | ?> | |
ffd3556b | 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 /> |
114949ad | 181 | Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" /> |
f94aa899 TM |
182 | <input type="submit" name="transaction" value="Převést" /> |
183 | </form> | |
184 | <?php | |
185 | ||
6e9dc519 | 186 | echo(bank_get_total($this,$account,$month,true)." $bank_currency"); |
0e44ea7a TM |
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":''; | |
e862fa82 | 191 | //(@flux := IF(transaction_to='harvie',IF(transaction_from='harvie',0,1),IF(transaction_from='harvie',-1,0))) as flux |
9787a5ca | 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.";"); |
f94aa899 | 193 | } |
6e9dc519 | 194 | echo ("<h2>Přehled transakcí $month</h2>"); |
8044dfef | 195 | echo $this->html->render_item_table($result,$bank_table); |
f94aa899 | 196 | |
9787a5ca TM |
197 | if(!isset($_GET['limit'])) |
198 | echo("<a href='?".$_SERVER['QUERY_STRING']."&limit=0'>zobrazit vše...</a>"); | |
199 | if($limit == 0) | |
200 | echo('to je vše.'); | |
201 | ||
f94aa899 TM |
202 | break; |
203 | case 'admin': | |
204 | ?> | |
98437b3b | 205 | </p> |
f94aa899 | 206 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
7a7ee029 | 207 | Account: |
f94aa899 TM |
208 | <input type="text" name="account_name" /> |
209 | <input type="submit" name="create_account" value="Create account" /> | |
210 | </form> | |
7a7ee029 TM |
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>"); ?> | |
214 | </select> | |
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é) | |
217 | </form> | |
98437b3b | 218 | </p> |
f94aa899 TM |
219 | <?php |
220 | break; | |
221 | } |