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 | ||
98437b3b | 40 | function bank_get_total($ctx, $account, $string=false) { |
8044dfef | 41 | global $bank_table; |
98437b3b | 42 | $account_sql=$ctx->db->quote($account); |
8044dfef TM |
43 | $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql;"); |
44 | $deposits = $result[0]["SUM(${bank_table}_amount)"]; | |
45 | $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_from`=$account_sql;"); | |
46 | $withdrawals = $result[0]["SUM(${bank_table}_amount)"]; | |
98437b3b TM |
47 | if($string) return "$deposits-$withdrawals"; |
48 | return $deposits-$withdrawals; | |
49 | } | |
7a7ee029 | 50 | function bank_rename_account($ctx, $old, $new) { |
8044dfef | 51 | global $bank_table; |
7a7ee029 TM |
52 | if(in_array($new, bank_get_accounts($ctx, true))) return false; |
53 | $old=$ctx->db->quote($old); | |
54 | $new=$ctx->db->quote($new); | |
55 | ||
56 | return $ctx->db->safe_query( | |
57 | "START TRANSACTION;". | |
8044dfef TM |
58 | "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;". |
59 | "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;". | |
7a7ee029 TM |
60 | "COMMIT;" |
61 | ); | |
62 | } | |
98437b3b | 63 | |
90f6601d | 64 | function bank_get_overview($ctx,$prefix='') { |
8044dfef | 65 | global $bank_table; |
98437b3b | 66 | $accounts = bank_get_accounts($ctx); |
5a4b2116 TM |
67 | foreach($accounts as $acc) { |
68 | $total=bank_get_total($ctx, $acc); | |
69 | $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total); | |
70 | $overview['array'][$acc]=$total; | |
71 | } | |
98437b3b TM |
72 | return $overview; |
73 | } | |
74 | ||
90f6601d | 75 | if(isset($bank_json_only) && $bank_json_only) die(json_encode(array( |
5a4b2116 | 76 | 'overview'=>bank_get_overview($this)['array'] |
90f6601d TM |
77 | ))); |
78 | ||
f94aa899 TM |
79 | if(isset($_POST['create_account'])) { |
80 | bank_add_account($this, $_POST['account_name']); | |
81 | $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen"); | |
82 | } | |
7a7ee029 TM |
83 | if(isset($_POST['rename_account'])) { |
84 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { | |
85 | $this->post_redirect_get("$URL_INTERNAL","Účet byl upraven"); | |
86 | } else { | |
87 | $this->post_redirect_get("$URL_INTERNAL","Takový účet již existuje!", false); | |
88 | } | |
89 | } | |
f94aa899 | 90 | if(isset($_POST['transaction'])) { |
68f2420e TM |
91 | if(!is_numeric($_POST['amount']) || $_POST['amount'] < 0) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Lze převádět jen kladné částky", true); |
92 | $comment=trim($_POST['comment']); | |
93 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Komentář musí mít alespoň 4 znaky!",true); | |
f94aa899 | 94 | bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']); |
68f2420e | 95 | $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Transakce byla provedena"); |
f94aa899 TM |
96 | } |
97 | ||
98 | //bank_add_account($this, 'material'); | |
98437b3b TM |
99 | echo("<a href='$URL/'>Banka</a> - "); |
100 | echo("<a href='$URL/admin'>Správa účtů</a> - "); | |
101 | echo("Účty: "); | |
7a7ee029 | 102 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
98437b3b | 103 | foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, "); |
f94aa899 TM |
104 | |
105 | switch($SUBPATH[0]) { | |
106 | default: | |
107 | ||
108 | if(!isset($_GET['account'])) { | |
109 | echo("<h1>Banka</h1>"); | |
98437b3b | 110 | echo ("<h2>Stav</h2>"); |
8044dfef | 111 | $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table};"); |
98437b3b | 112 | echo("Transakcí: ".$result[0]['troughput']."<br />"); |
8044dfef | 113 | $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table};"); |
f94aa899 | 114 | echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency); |
8044dfef | 115 | $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` ORDER BY ${bank_table}_time DESC;"); |
5a4b2116 | 116 | echo $this->html->render_item_table(bank_get_overview($this, $bank_table.'_')['table'],'bank'); |
98437b3b | 117 | echo ("<h2>Přehled transakcí</h2>"); |
f94aa899 TM |
118 | } else { |
119 | $account=bank_name($_GET['account']); | |
120 | $account_sql=$this->db->quote($account); | |
98437b3b | 121 | echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>"); |
f94aa899 TM |
122 | |
123 | ?> | |
124 | <form action="?" method="POST"> | |
125 | Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> | |
1d0a5f85 | 126 | z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
f94aa899 | 127 | na účet <select name='account_to'> |
048fadfc TM |
128 | <?php |
129 | //Ziskat posledni cilovy ucet a presunout na zacatek $accounts | |
130 | $last=bank_get_last_to($this,$account); | |
131 | unset($accounts[array_search($last,$accounts)]); | |
132 | array_unshift($accounts,$last); | |
133 | ||
134 | foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); | |
135 | ?> | |
1d2cac6a | 136 | </select> (pozor! zamysli se! převádíš peníze nebo dluhy?!)<br /><br /> |
114949ad | 137 | Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" /> |
f94aa899 TM |
138 | <input type="submit" name="transaction" value="Převést" /> |
139 | </form> | |
140 | <?php | |
141 | ||
98437b3b | 142 | echo(bank_get_total($this,$account,true)." $bank_currency"); |
8044dfef | 143 | $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql OR `${bank_table}_from`=$account_sql ORDER BY ${bank_table}_time DESC;"); |
f94aa899 | 144 | } |
8044dfef | 145 | echo $this->html->render_item_table($result,$bank_table); |
f94aa899 | 146 | |
f94aa899 TM |
147 | break; |
148 | case 'admin': | |
149 | ?> | |
98437b3b | 150 | </p> |
f94aa899 | 151 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
7a7ee029 | 152 | Account: |
f94aa899 TM |
153 | <input type="text" name="account_name" /> |
154 | <input type="submit" name="create_account" value="Create account" /> | |
155 | </form> | |
7a7ee029 TM |
156 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
157 | Account: <select name='account_old'> | |
158 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> | |
159 | </select> | |
160 | <input type="text" name="account_new" /> | |
161 | <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné) | |
162 | </form> | |
98437b3b | 163 | </p> |
f94aa899 TM |
164 | <?php |
165 | break; | |
166 | } |