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