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??? |
f94aa899 TM |
6 | |
7 | function bank_name($name) { | |
8 | return strtolower(trim($name)); | |
9 | } | |
10 | ||
11 | function bank_transaction($ctx, $from, $to, $comment, $amount=0) { | |
8044dfef | 12 | global $bank_table; |
f94aa899 TM |
13 | $author=$ctx->db->quote($ctx->db->auth->get_user_id()); |
14 | $from=$ctx->db->quote(bank_name($from)); | |
15 | $to=$ctx->db->quote(bank_name($to)); | |
399c761f | 16 | $amount=$ctx->db->quote($amount); |
68f2420e | 17 | $comment=$ctx->db->quote(trim($comment)); |
f94aa899 | 18 | |
8044dfef | 19 | $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 |
20 | $ctx->db->safe_query($sql); |
21 | } | |
22 | ||
7a7ee029 | 23 | function bank_get_accounts($ctx, $all=false) { |
8044dfef TM |
24 | global $bank_table; |
25 | $fetch = $ctx->db->safe_query_fetch("SELECT DISTINCT ${bank_table}_to FROM ${bank_table} ORDER BY ${bank_table}_to;"); | |
26 | foreach($fetch as $account) if($all || $account[$bank_table.'_to'][0]!='_') $accounts[]=$account[$bank_table.'_to']; | |
f94aa899 TM |
27 | return $accounts; |
28 | } | |
29 | ||
048fadfc TM |
30 | function bank_get_last_to($ctx, $account) { |
31 | global $bank_table; | |
32 | $account=$ctx->db->quote(bank_name($account)); | |
33 | $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;"); | |
34 | return $fetch[0][$bank_table.'_to']; | |
35 | } | |
36 | ||
f94aa899 TM |
37 | function bank_add_account($ctx, $name) { |
38 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); | |
39 | } | |
40 | ||
6e9dc519 TM |
41 | function bank_month_sql($ctx, $month=false) { |
42 | global $bank_table; | |
43 | $month_sql = 'TRUE'; | |
44 | if(!is_bool($month)) { | |
45 | $month_q = $ctx->db->quote($month); | |
46 | $month_sql .= " AND DATE_FORMAT(${bank_table}_time, '%Y-%m') = ".$month_q; | |
47 | } | |
48 | return $month_sql; | |
49 | } | |
50 | ||
51 | function bank_get_total($ctx, $account, $month, $string=false) { | |
8044dfef | 52 | global $bank_table; |
98437b3b | 53 | $account_sql=$ctx->db->quote($account); |
6e9dc519 | 54 | $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 | 55 | $deposits = $result[0]["SUM(${bank_table}_amount)"]; |
6e9dc519 | 56 | $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 | 57 | $withdrawals = $result[0]["SUM(${bank_table}_amount)"]; |
98437b3b TM |
58 | if($string) return "$deposits-$withdrawals"; |
59 | return $deposits-$withdrawals; | |
60 | } | |
7a7ee029 | 61 | function bank_rename_account($ctx, $old, $new) { |
8044dfef | 62 | global $bank_table; |
7a7ee029 TM |
63 | if(in_array($new, bank_get_accounts($ctx, true))) return false; |
64 | $old=$ctx->db->quote($old); | |
65 | $new=$ctx->db->quote($new); | |
66 | ||
67 | return $ctx->db->safe_query( | |
68 | "START TRANSACTION;". | |
8044dfef TM |
69 | "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;". |
70 | "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;". | |
7a7ee029 TM |
71 | "COMMIT;" |
72 | ); | |
73 | } | |
98437b3b | 74 | |
6e9dc519 | 75 | function bank_get_overview($ctx,$prefix='',$month=false) { |
8044dfef | 76 | global $bank_table; |
98437b3b | 77 | $accounts = bank_get_accounts($ctx); |
5a4b2116 | 78 | foreach($accounts as $acc) { |
6e9dc519 | 79 | $total=bank_get_total($ctx, $acc, $month); |
5a4b2116 TM |
80 | $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total); |
81 | $overview['array'][$acc]=$total; | |
82 | } | |
98437b3b TM |
83 | return $overview; |
84 | } | |
85 | ||
089bea60 | 86 | if(isset($bank_json_only) && $bank_json_only) { |
6e9dc519 | 87 | $overview=bank_get_overview($this,''); |
089bea60 TM |
88 | die(json_encode(array( |
89 | 'overview'=>$overview['array'] | |
90 | ))); | |
91 | } | |
90f6601d | 92 | |
f94aa899 | 93 | if(isset($_POST['create_account'])) { |
51ca86c4 TM |
94 | $new_account=$_POST['account_name']; |
95 | bank_add_account($this, $new_account); | |
bd3726b4 | 96 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!"); |
f94aa899 | 97 | } |
7a7ee029 | 98 | if(isset($_POST['rename_account'])) { |
51ca86c4 TM |
99 | $new_account=$_POST['account_new']; |
100 | $old_account=$_POST['account_old']; | |
7a7ee029 | 101 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { |
bd3726b4 | 102 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!"); |
7a7ee029 | 103 | } else { |
bd3726b4 | 104 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false); |
7a7ee029 TM |
105 | } |
106 | } | |
f94aa899 | 107 | if(isset($_POST['transaction'])) { |
51ca86c4 TM |
108 | $account_from=$_POST['account_from']; |
109 | $account_to=$_POST['account_to']; | |
110 | $amount=$_POST['amount']; | |
68f2420e | 111 | $comment=trim($_POST['comment']); |
51ca86c4 TM |
112 | if(!is_numeric($amount) || $amount < 0) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Lze převádět jen kladné částky", true); |
113 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Komentář musí mít alespoň 4 znaky!",true); | |
114 | bank_transaction($this, $account_from, $account_to, $comment, $amount); | |
bd3726b4 | 115 | $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"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 |
116 | } |
117 | ||
6e9dc519 TM |
118 | $month = isset($_GET['month']) ? $_GET['month'] : false; |
119 | ||
f94aa899 | 120 | //bank_add_account($this, 'material'); |
98437b3b | 121 | echo("<a href='$URL/'>Banka</a> - "); |
587ba49a | 122 | echo("<a href='$URL/admin'>Správa účtů</a> - "); |
6e9dc519 TM |
123 | echo('<span style="float:right;">'); |
124 | echo $this->html->form($URL, 'GET', array( | |
125 | array('month',$month,'text',false,'','YYYY-MM:'), | |
126 | array(false,'SELECT BY MONTH','submit') | |
127 | )); | |
128 | echo('</span>'); | |
7da3f2f4 | 129 | echo("Účty: <br />"); |
7a7ee029 | 130 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
7da3f2f4 TM |
131 | $lastaccount=false; |
132 | foreach($accounts as $account) { | |
133 | if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />'); | |
134 | echo("<a href='$URL?account=$account'>$account</a>, "); | |
135 | $lastaccount=$account; | |
136 | } | |
f94aa899 | 137 | |
6e9dc519 TM |
138 | |
139 | ||
f94aa899 TM |
140 | switch($SUBPATH[0]) { |
141 | default: | |
142 | ||
143 | if(!isset($_GET['account'])) { | |
6e9dc519 TM |
144 | echo("<h1>Banka $month</h1>"); |
145 | echo ("<h2>Stav $month</h2>"); | |
146 | $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';'); | |
147 | echo("Transakcí $month: ".$result[0]['troughput']."<br />"); | |
148 | $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';'); | |
149 | echo("Obrat $month: ".$result[0]['troughput'].' '.$bank_currency); | |
150 | $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE ".bank_month_sql($this,$month)." ORDER BY ${bank_table}_time DESC;"); | |
151 | $overview=bank_get_overview($this,$bank_table.'_',$month); | |
089bea60 | 152 | echo $this->html->render_item_table($overview['table'],'bank'); |
f94aa899 TM |
153 | } else { |
154 | $account=bank_name($_GET['account']); | |
155 | $account_sql=$this->db->quote($account); | |
6e9dc519 | 156 | echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>"); |
f94aa899 TM |
157 | |
158 | ?> | |
159 | <form action="?" method="POST"> | |
160 | Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> | |
1d0a5f85 | 161 | z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
f94aa899 | 162 | na účet <select name='account_to'> |
048fadfc TM |
163 | <?php |
164 | //Ziskat posledni cilovy ucet a presunout na zacatek $accounts | |
165 | $last=bank_get_last_to($this,$account); | |
166 | unset($accounts[array_search($last,$accounts)]); | |
167 | array_unshift($accounts,$last); | |
168 | ||
169 | foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); | |
170 | ?> | |
1d2cac6a | 171 | </select> (pozor! zamysli se! převádíš peníze nebo dluhy?!)<br /><br /> |
114949ad | 172 | Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" /> |
f94aa899 TM |
173 | <input type="submit" name="transaction" value="Převést" /> |
174 | </form> | |
175 | <?php | |
176 | ||
6e9dc519 | 177 | echo(bank_get_total($this,$account,$month,true)." $bank_currency"); |
0e44ea7a TM |
178 | $subtotal=$recursive?",( |
179 | (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) | |
180 | -(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) | |
181 | ) as ${bank_table}_subtotal":''; | |
e862fa82 TM |
182 | //(@flux := IF(transaction_to='harvie',IF(transaction_from='harvie',0,1),IF(transaction_from='harvie',-1,0))) as flux |
183 | $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;"); | |
f94aa899 | 184 | } |
6e9dc519 | 185 | echo ("<h2>Přehled transakcí $month</h2>"); |
8044dfef | 186 | echo $this->html->render_item_table($result,$bank_table); |
f94aa899 | 187 | |
f94aa899 TM |
188 | break; |
189 | case 'admin': | |
190 | ?> | |
98437b3b | 191 | </p> |
f94aa899 | 192 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
7a7ee029 | 193 | Account: |
f94aa899 TM |
194 | <input type="text" name="account_name" /> |
195 | <input type="submit" name="create_account" value="Create account" /> | |
196 | </form> | |
7a7ee029 TM |
197 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
198 | Account: <select name='account_old'> | |
199 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> | |
200 | </select> | |
201 | <input type="text" name="account_new" /> | |
202 | <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné) | |
203 | </form> | |
98437b3b | 204 | </p> |
f94aa899 TM |
205 | <?php |
206 | break; | |
207 | } |