zapujceny HW
[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 $account_redirect=$account_from;
113 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);
114 if($amount < 0) {
115 $amount=abs($amount);
116 list($account_from,$account_to)=array($account_to,$account_from); //swap from/to
117 }
118 if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Komentář musí mít alespoň 4 znaky!",true);
119 bank_transaction($this, $account_from, $account_to, $comment, $amount);
120 $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)");
121 }
122
123 $month = isset($_GET['month']) ? $_GET['month'] : false;
124
125 //bank_add_account($this, 'material');
126 echo("<a href='$URL/'>Banka</a> - ");
127 echo("<a href='$URL/admin'>Správa účtů</a> - ");
128 echo('<span style="float:right;">');
129 echo $this->html->form($URL, 'GET', array(
130 array('month',$month,'text',false,'','YYYY-MM:'),
131 array(false,'SELECT BY MONTH','submit')
132 ));
133 echo('</span>');
134 echo("Účty: <br />");
135 $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
136 $lastaccount=false;
137 foreach($accounts as $account) {
138 if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />');
139 echo("<a href='$URL?account=".urlencode($account)."'>$account</a>, ");
140 $lastaccount=$account;
141 }
142
143
144
145 switch($SUBPATH[0]) {
146 default:
147
148 if(!isset($_GET['account'])) {
149 echo("<h1>Banka $month</h1>");
150 echo ("<h2>Stav $month</h2>");
151 $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
152 echo("Transakcí $month: ".$result[0]['troughput']."<br />");
153 $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
154 echo("Obrat $month: ".$result[0]['troughput'].' '.$bank_currency);
155 $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE ".bank_month_sql($this,$month)." ORDER BY ${bank_table}_time DESC;");
156 $overview=bank_get_overview($this,$bank_table.'_',$month);
157 echo $this->html->render_item_table($overview['table'],'bank');
158 } else {
159 $account=bank_name($_GET['account']);
160 $account_sql=$this->db->quote($account);
161 echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>");
162
163 ?>
164 <form action="?" method="POST">
165 Převést <!-- &plusmn; --><input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
166 z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
167 na účet <select name='account_to'>
168 <?php
169 //Ziskat posledni cilovy ucet a presunout na zacatek $accounts
170 $last=bank_get_last_to($this,$account);
171 unset($accounts[array_search($last,$accounts)]);
172 array_unshift($accounts,$last);
173
174 foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>");
175 ?>
176 </select> (pozor! zamysli se! převádíš peníze nebo dluhy?! záporná částka = převod v opačném směru.)<br /><br />
177 Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" />
178 <input type="submit" name="transaction" value="Převést" />
179 </form>
180 <?php
181
182 echo(bank_get_total($this,$account,$month,true)." $bank_currency");
183 $subtotal=$recursive?",(
184 (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)
185 -(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)
186 ) as ${bank_table}_subtotal":'';
187 //(@flux := IF(transaction_to='harvie',IF(transaction_from='harvie',0,1),IF(transaction_from='harvie',-1,0))) as flux
188 $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;");
189 }
190 echo ("<h2>Přehled transakcí $month</h2>");
191 echo $this->html->render_item_table($result,$bank_table);
192
193 break;
194 case 'admin':
195 ?>
196 </p>
197 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
198 Account:
199 <input type="text" name="account_name" />
200 <input type="submit" name="create_account" value="Create account" />
201 </form>
202 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
203 Account: <select name='account_old'>
204 <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
205 </select>
206 <input type="text" name="account_new" />
207 <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné)
208 </form>
209 </p>
210 <?php
211 break;
212 }
This page took 0.540603 seconds and 4 git commands to generate.