Lepsi generovani etiket
[mirrors/SokoMan.git] / assistants / bank.inc.php
CommitLineData
f94aa899
TM
1<?php
2$bank_currency='Kč';
8044dfef 3global $bank_table;
ee2e7415 4$bank_table='transaction';
0e44ea7a 5$recursive=true; //USE RECURSIVE QUERIES???
f94aa899
TM
6
7function bank_name($name) {
8 return strtolower(trim($name));
9}
10
11function 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 23function 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
30function 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
37function bank_add_account($ctx, $name) {
38 bank_transaction($ctx, $name, $name, "Created account \"$name\"");
39}
40
6e9dc519
TM
41function 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
51function 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 61function 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 75function 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 86if(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 93if(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 98if(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 107if(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']);
ffd3556b
TM
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 }
a2100946 118 if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Komentář musí mít alespoň 4 znaky!",true);
51ca86c4 119 bank_transaction($this, $account_from, $account_to, $comment, $amount);
ffd3556b 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)");
f94aa899
TM
121}
122
6e9dc519
TM
123$month = isset($_GET['month']) ? $_GET['month'] : false;
124
f94aa899 125//bank_add_account($this, 'material');
98437b3b 126echo("<a href='$URL/'>Banka</a> - ");
587ba49a 127echo("<a href='$URL/admin'>Správa účtů</a> - ");
6e9dc519
TM
128echo('<span style="float:right;">');
129echo $this->html->form($URL, 'GET', array(
130 array('month',$month,'text',false,'','YYYY-MM:'),
131 array(false,'SELECT BY MONTH','submit')
132));
133echo('</span>');
7da3f2f4 134echo("Účty: <br />");
7a7ee029 135$accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
7da3f2f4
TM
136$lastaccount=false;
137foreach($accounts as $account) {
138 if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />');
a2100946 139 echo("<a href='$URL?account=".urlencode($account)."'>$account</a>, ");
7da3f2f4
TM
140 $lastaccount=$account;
141}
f94aa899 142
6e9dc519
TM
143
144
f94aa899
TM
145switch($SUBPATH[0]) {
146 default:
147
148 if(!isset($_GET['account'])) {
6e9dc519
TM
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);
089bea60 157 echo $this->html->render_item_table($overview['table'],'bank');
f94aa899
TM
158 } else {
159 $account=bank_name($_GET['account']);
160 $account_sql=$this->db->quote($account);
6e9dc519 161 echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>");
f94aa899
TM
162
163 ?>
164 <form action="?" method="POST">
ffd3556b 165 Převést <!-- &plusmn; --><input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
1d0a5f85 166 z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
f94aa899 167 na účet <select name='account_to'>
048fadfc
TM
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 ?>
ffd3556b 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 />
114949ad 177 Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" />
f94aa899
TM
178 <input type="submit" name="transaction" value="Převést" />
179 </form>
180 <?php
181
6e9dc519 182 echo(bank_get_total($this,$account,$month,true)." $bank_currency");
0e44ea7a
TM
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":'';
e862fa82
TM
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;");
f94aa899 189 }
6e9dc519 190 echo ("<h2>Přehled transakcí $month</h2>");
8044dfef 191 echo $this->html->render_item_table($result,$bank_table);
f94aa899 192
f94aa899
TM
193 break;
194 case 'admin':
195?>
98437b3b 196 </p>
f94aa899 197 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
7a7ee029 198 Account:
f94aa899
TM
199 <input type="text" name="account_name" />
200 <input type="submit" name="create_account" value="Create account" />
201 </form>
7a7ee029
TM
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>
98437b3b 209 </p>
f94aa899
TM
210<?php
211 break;
212}
This page took 0.536345 seconds and 4 git commands to generate.