Prasacka parametrizace tabulky transakci
[mirrors/SokoMan.git] / assistants / bank.inc.php
CommitLineData
f94aa899
TM
1<?php
2$bank_currency='Kč';
8044dfef
TM
3global $bank_table;
4$bank_table='bank';
f94aa899
TM
5
6function bank_name($name) {
7 return strtolower(trim($name));
8}
9
10function 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 22function 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
29function bank_add_account($ctx, $name) {
30 bank_transaction($ctx, $name, $name, "Created account \"$name\"");
31}
32
98437b3b 33function bank_get_total($ctx, $account, $string=false) {
8044dfef 34 global $bank_table;
98437b3b 35 $account_sql=$ctx->db->quote($account);
8044dfef
TM
36 $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql;");
37 $deposits = $result[0]["SUM(${bank_table}_amount)"];
38 $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_from`=$account_sql;");
39 $withdrawals = $result[0]["SUM(${bank_table}_amount)"];
98437b3b
TM
40 if($string) return "$deposits-$withdrawals";
41 return $deposits-$withdrawals;
42}
7a7ee029 43function bank_rename_account($ctx, $old, $new) {
8044dfef 44 global $bank_table;
7a7ee029
TM
45 if(in_array($new, bank_get_accounts($ctx, true))) return false;
46 $old=$ctx->db->quote($old);
47 $new=$ctx->db->quote($new);
48
49 return $ctx->db->safe_query(
50 "START TRANSACTION;".
8044dfef
TM
51 "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;".
52 "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;".
7a7ee029
TM
53 "COMMIT;"
54 );
55}
98437b3b
TM
56
57function bank_get_overview($ctx) {
8044dfef 58 global $bank_table;
98437b3b 59 $accounts = bank_get_accounts($ctx);
8044dfef 60 foreach($accounts as $acc) $overview[]=array("${bank_table}_account"=>$acc,"${bank_table}_total"=>bank_get_total($ctx, $acc));
98437b3b
TM
61 return $overview;
62}
63
f94aa899
TM
64if(isset($_POST['create_account'])) {
65 bank_add_account($this, $_POST['account_name']);
66 $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen");
67}
7a7ee029
TM
68if(isset($_POST['rename_account'])) {
69 if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) {
70 $this->post_redirect_get("$URL_INTERNAL","Účet byl upraven");
71 } else {
72 $this->post_redirect_get("$URL_INTERNAL","Takový účet již existuje!", false);
73 }
74}
f94aa899 75if(isset($_POST['transaction'])) {
68f2420e
TM
76 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);
77 $comment=trim($_POST['comment']);
78 if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Komentář musí mít alespoň 4 znaky!",true);
f94aa899 79 bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']);
68f2420e 80 $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Transakce byla provedena");
f94aa899
TM
81}
82
83//bank_add_account($this, 'material');
98437b3b
TM
84echo("<a href='$URL/'>Banka</a> - ");
85echo("<a href='$URL/admin'>Správa účtů</a> - ");
86echo("Účty: ");
7a7ee029 87$accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
98437b3b 88foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, ");
f94aa899
TM
89
90switch($SUBPATH[0]) {
91 default:
92
93 if(!isset($_GET['account'])) {
94 echo("<h1>Banka</h1>");
98437b3b 95 echo ("<h2>Stav</h2>");
8044dfef 96 $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table};");
98437b3b 97 echo("Transakcí: ".$result[0]['troughput']."<br />");
8044dfef 98 $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table};");
f94aa899 99 echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency);
8044dfef 100 $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` ORDER BY ${bank_table}_time DESC;");
a5770923 101 echo $this->html->render_item_table(bank_get_overview($this),'bank');
98437b3b 102 echo ("<h2>Přehled transakcí</h2>");
f94aa899
TM
103 } else {
104 $account=bank_name($_GET['account']);
105 $account_sql=$this->db->quote($account);
98437b3b 106 echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>");
f94aa899
TM
107
108 ?>
109 <form action="?" method="POST">
110 Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
111 z účtu <?php echo $account; ?> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
112 na účet <select name='account_to'>
113 <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
98437b3b 114 </select> (pozor, dluhy se převádí opačným směrem než peníze!)<br /><br />
114949ad 115 Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" />
f94aa899
TM
116 <input type="submit" name="transaction" value="Převést" />
117 </form>
118 <?php
119
98437b3b 120 echo(bank_get_total($this,$account,true)." $bank_currency");
8044dfef 121 $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 122 }
8044dfef 123 echo $this->html->render_item_table($result,$bank_table);
f94aa899 124
f94aa899
TM
125 break;
126 case 'admin':
127?>
98437b3b 128 </p>
f94aa899 129 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
7a7ee029 130 Account:
f94aa899
TM
131 <input type="text" name="account_name" />
132 <input type="submit" name="create_account" value="Create account" />
133 </form>
7a7ee029
TM
134 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
135 Account: <select name='account_old'>
136 <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
137 </select>
138 <input type="text" name="account_new" />
139 <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné)
140 </form>
98437b3b 141 </p>
f94aa899
TM
142<?php
143 break;
144}
This page took 0.471115 seconds and 4 git commands to generate.