4c816cb975abda280ce8e85a5211986b7e7d93f9
[mirrors/SokoMan.git] / assistants / bank.inc.php
1 <?php
2 $bank_currency='Kč';
3 global $bank_table;
4 $bank_table='transaction';
5 $recursive=true; //USE RECURSIVE QUERIES???
6
7 function bank_name($name) {
8 return strtolower(trim($name));
9 }
10
11 function bank_transaction($ctx, $from, $to, $comment, $amount=0) {
12 global $bank_table;
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));
16 $amount=$ctx->db->quote($amount);
17 $comment=$ctx->db->quote(trim($comment));
18
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);";
20 $ctx->db->safe_query($sql);
21 }
22
23 function bank_get_accounts($ctx, $all=false) {
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'];
27 return $accounts;
28 }
29
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
37 function bank_add_account($ctx, $name) {
38 bank_transaction($ctx, $name, $name, "Created account \"$name\"");
39 }
40
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) {
52 global $bank_table;
53 $account_sql=$ctx->db->quote($account);
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).';');
55 $deposits = $result[0]["SUM(${bank_table}_amount)"];
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).');');
57 $withdrawals = $result[0]["SUM(${bank_table}_amount)"];
58 if($string) return "$deposits-$withdrawals";
59 return $deposits-$withdrawals;
60 }
61 function bank_rename_account($ctx, $old, $new) {
62 global $bank_table;
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;".
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;".
71 "COMMIT;"
72 );
73 }
74
75 function bank_get_overview($ctx,$prefix='',$month=false) {
76 global $bank_table;
77 $accounts = bank_get_accounts($ctx);
78 foreach($accounts as $acc) {
79 $total=bank_get_total($ctx, $acc, $month);
80 $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total);
81 $overview['array'][$acc]=$total;
82 }
83 return $overview;
84 }
85
86 if(isset($bank_json_only) && $bank_json_only) {
87 $overview=bank_get_overview($this,'');
88 die(json_encode(array(
89 'overview'=>$overview['array']
90 )));
91 }
92
93 if(isset($_POST['create_account'])) {
94 $new_account=$_POST['account_name'];
95 bank_add_account($this, $new_account);
96 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!");
97 }
98 if(isset($_POST['rename_account'])) {
99 $new_account=$_POST['account_new'];
100 $old_account=$_POST['account_old'];
101 if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) {
102 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!");
103 } else {
104 $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false);
105 }
106 }
107 if(isset($_POST['transaction'])) {
108 $account_from=$_POST['account_from'];
109 $account_to=$_POST['account_to'];
110 $amount=$_POST['amount'];
111 $comment=trim($_POST['comment']);
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);
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)");
116 }
117
118 $month = isset($_GET['month']) ? $_GET['month'] : false;
119
120 //bank_add_account($this, 'material');
121 echo("<a href='$URL/'>Banka</a> - ");
122 echo("<a href='$URL/admin'>Správa účtů</a> - ");
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>');
129 echo("Účty: <br />");
130 $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
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 }
137
138
139
140 switch($SUBPATH[0]) {
141 default:
142
143 if(!isset($_GET['account'])) {
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);
152 echo $this->html->render_item_table($overview['table'],'bank');
153 } else {
154 $account=bank_name($_GET['account']);
155 $account_sql=$this->db->quote($account);
156 echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>");
157
158 ?>
159 <form action="?" method="POST">
160 Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
161 z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
162 na účet <select name='account_to'>
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 ?>
171 </select> (pozor! zamysli se! převádíš peníze nebo dluhy?!)<br /><br />
172 Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" />
173 <input type="submit" name="transaction" value="Převést" />
174 </form>
175 <?php
176
177 echo(bank_get_total($this,$account,$month,true)." $bank_currency");
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":'';
182 $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;"); //TADY
183 }
184 echo ("<h2>Přehled transakcí $month</h2>");
185 echo $this->html->render_item_table($result,$bank_table);
186
187 break;
188 case 'admin':
189 ?>
190 </p>
191 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
192 Account:
193 <input type="text" name="account_name" />
194 <input type="submit" name="create_account" value="Create account" />
195 </form>
196 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
197 Account: <select name='account_old'>
198 <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
199 </select>
200 <input type="text" name="account_new" />
201 <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné)
202 </form>
203 </p>
204 <?php
205 break;
206 }
This page took 0.436156 seconds and 3 git commands to generate.