Commit | Line | Data |
---|---|---|
f94aa899 TM |
1 | <?php |
2 | $bank_currency='Kč'; | |
3 | ||
4 | function bank_name($name) { | |
5 | return strtolower(trim($name)); | |
6 | } | |
7 | ||
8 | function bank_transaction($ctx, $from, $to, $comment, $amount=0) { | |
9 | $author=$ctx->db->quote($ctx->db->auth->get_user_id()); | |
10 | $from=$ctx->db->quote(bank_name($from)); | |
11 | $to=$ctx->db->quote(bank_name($to)); | |
399c761f | 12 | $amount=$ctx->db->quote($amount); |
68f2420e | 13 | $comment=$ctx->db->quote(trim($comment)); |
f94aa899 TM |
14 | |
15 | $sql="INSERT INTO `bank` (`bank_time`, `bank_from`, `bank_to`, `bank_amount`, `bank_author`, `bank_comment`) VALUES (now(), $from, $to, $amount, $author, $comment);"; | |
16 | $ctx->db->safe_query($sql); | |
17 | } | |
18 | ||
7a7ee029 | 19 | function bank_get_accounts($ctx, $all=false) { |
f94aa899 | 20 | $fetch = $ctx->db->safe_query_fetch('SELECT DISTINCT bank_to FROM bank ORDER BY bank_to;'); |
7a7ee029 | 21 | foreach($fetch as $account) if($all || $account['bank_to'][0]!='_') $accounts[]=$account['bank_to']; |
f94aa899 TM |
22 | return $accounts; |
23 | } | |
24 | ||
25 | function bank_add_account($ctx, $name) { | |
26 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); | |
27 | } | |
28 | ||
98437b3b TM |
29 | function bank_get_total($ctx, $account, $string=false) { |
30 | $account_sql=$ctx->db->quote($account); | |
31 | $result = $ctx->db->safe_query_fetch("SELECT SUM(bank_amount) FROM `bank` WHERE `bank_to`=$account_sql;"); | |
32 | $deposits = $result[0]['SUM(bank_amount)']; | |
33 | $result = $ctx->db->safe_query_fetch("SELECT SUM(bank_amount) FROM `bank` WHERE `bank_from`=$account_sql;"); | |
34 | $withdrawals = $result[0]['SUM(bank_amount)']; | |
35 | if($string) return "$deposits-$withdrawals"; | |
36 | return $deposits-$withdrawals; | |
37 | } | |
7a7ee029 TM |
38 | function bank_rename_account($ctx, $old, $new) { |
39 | if(in_array($new, bank_get_accounts($ctx, true))) return false; | |
40 | $old=$ctx->db->quote($old); | |
41 | $new=$ctx->db->quote($new); | |
42 | ||
43 | return $ctx->db->safe_query( | |
44 | "START TRANSACTION;". | |
45 | "UPDATE bank SET `bank_to`=$new WHERE `bank_to`=$old;". | |
46 | "UPDATE bank SET `bank_from`=$new WHERE `bank_from`=$old;". | |
47 | "COMMIT;" | |
48 | ); | |
49 | } | |
98437b3b TM |
50 | |
51 | function bank_get_overview($ctx) { | |
52 | $accounts = bank_get_accounts($ctx); | |
53 | foreach($accounts as $acc) $overview[]=array("bank_account"=>$acc,"bank_total"=>bank_get_total($ctx, $acc)); | |
54 | return $overview; | |
55 | } | |
56 | ||
f94aa899 TM |
57 | if(isset($_POST['create_account'])) { |
58 | bank_add_account($this, $_POST['account_name']); | |
59 | $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen"); | |
60 | } | |
7a7ee029 TM |
61 | if(isset($_POST['rename_account'])) { |
62 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { | |
63 | $this->post_redirect_get("$URL_INTERNAL","Účet byl upraven"); | |
64 | } else { | |
65 | $this->post_redirect_get("$URL_INTERNAL","Takový účet již existuje!", false); | |
66 | } | |
67 | } | |
f94aa899 | 68 | if(isset($_POST['transaction'])) { |
68f2420e TM |
69 | 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); |
70 | $comment=trim($_POST['comment']); | |
71 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Komentář musí mít alespoň 4 znaky!",true); | |
f94aa899 | 72 | bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']); |
68f2420e | 73 | $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Transakce byla provedena"); |
f94aa899 TM |
74 | } |
75 | ||
76 | //bank_add_account($this, 'material'); | |
98437b3b TM |
77 | echo("<a href='$URL/'>Banka</a> - "); |
78 | echo("<a href='$URL/admin'>Správa účtů</a> - "); | |
79 | echo("Účty: "); | |
7a7ee029 | 80 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
98437b3b | 81 | foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, "); |
f94aa899 TM |
82 | |
83 | switch($SUBPATH[0]) { | |
84 | default: | |
85 | ||
86 | if(!isset($_GET['account'])) { | |
87 | echo("<h1>Banka</h1>"); | |
98437b3b TM |
88 | echo ("<h2>Stav</h2>"); |
89 | $result = $this->db->safe_query_fetch("SELECT COUNT(bank_amount) as troughput FROM bank;"); | |
90 | echo("Transakcí: ".$result[0]['troughput']."<br />"); | |
f94aa899 TM |
91 | $result = $this->db->safe_query_fetch("SELECT SUM(bank_amount) as troughput FROM bank;"); |
92 | echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency); | |
93 | $result = $this->db->safe_query_fetch("SELECT * FROM `bank` ORDER BY bank_time DESC;"); | |
a5770923 | 94 | echo $this->html->render_item_table(bank_get_overview($this),'bank'); |
98437b3b | 95 | echo ("<h2>Přehled transakcí</h2>"); |
f94aa899 TM |
96 | } else { |
97 | $account=bank_name($_GET['account']); | |
98 | $account_sql=$this->db->quote($account); | |
98437b3b | 99 | echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>"); |
f94aa899 TM |
100 | |
101 | ?> | |
102 | <form action="?" method="POST"> | |
103 | Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> | |
104 | z účtu <?php echo $account; ?> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> | |
105 | na účet <select name='account_to'> | |
106 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> | |
98437b3b TM |
107 | </select> (pozor, dluhy se převádí opačným směrem než peníze!)<br /><br /> |
108 | Důvod: <input type="text" name="comment" style="width:64em;" /> | |
f94aa899 TM |
109 | <input type="submit" name="transaction" value="Převést" /> |
110 | </form> | |
111 | <?php | |
112 | ||
98437b3b | 113 | echo(bank_get_total($this,$account,true)." $bank_currency"); |
f94aa899 TM |
114 | $result = $this->db->safe_query_fetch("SELECT * FROM `bank` WHERE `bank_to`=$account_sql OR `bank_from`=$account_sql ORDER BY bank_time DESC;"); |
115 | } | |
0920d9c7 | 116 | echo $this->html->render_item_table($result,'bank'); |
f94aa899 | 117 | |
f94aa899 TM |
118 | break; |
119 | case 'admin': | |
120 | ?> | |
98437b3b | 121 | </p> |
f94aa899 | 122 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
7a7ee029 | 123 | Account: |
f94aa899 TM |
124 | <input type="text" name="account_name" /> |
125 | <input type="submit" name="create_account" value="Create account" /> | |
126 | </form> | |
7a7ee029 TM |
127 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
128 | Account: <select name='account_old'> | |
129 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> | |
130 | </select> | |
131 | <input type="text" name="account_new" /> | |
132 | <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné) | |
133 | </form> | |
98437b3b | 134 | </p> |
f94aa899 TM |
135 | <?php |
136 | break; | |
137 | } |