| 1 | <?php |
| 2 | $bank_currency='Kč'; |
| 3 | global $bank_table; |
| 4 | $bank_table='transaction'; |
| 5 | |
| 6 | function bank_name($name) { |
| 7 | return strtolower(trim($name)); |
| 8 | } |
| 9 | |
| 10 | function bank_transaction($ctx, $from, $to, $comment, $amount=0) { |
| 11 | global $bank_table; |
| 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)); |
| 15 | $amount=$ctx->db->quote($amount); |
| 16 | $comment=$ctx->db->quote(trim($comment)); |
| 17 | |
| 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);"; |
| 19 | $ctx->db->safe_query($sql); |
| 20 | } |
| 21 | |
| 22 | function bank_get_accounts($ctx, $all=false) { |
| 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']; |
| 26 | return $accounts; |
| 27 | } |
| 28 | |
| 29 | function bank_get_last_to($ctx, $account) { |
| 30 | global $bank_table; |
| 31 | $account=$ctx->db->quote(bank_name($account)); |
| 32 | $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;"); |
| 33 | return $fetch[0][$bank_table.'_to']; |
| 34 | } |
| 35 | |
| 36 | function bank_add_account($ctx, $name) { |
| 37 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); |
| 38 | } |
| 39 | |
| 40 | function bank_get_total($ctx, $account, $string=false) { |
| 41 | global $bank_table; |
| 42 | $account_sql=$ctx->db->quote($account); |
| 43 | $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql;"); |
| 44 | $deposits = $result[0]["SUM(${bank_table}_amount)"]; |
| 45 | $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_from`=$account_sql;"); |
| 46 | $withdrawals = $result[0]["SUM(${bank_table}_amount)"]; |
| 47 | if($string) return "$deposits-$withdrawals"; |
| 48 | return $deposits-$withdrawals; |
| 49 | } |
| 50 | function bank_rename_account($ctx, $old, $new) { |
| 51 | global $bank_table; |
| 52 | if(in_array($new, bank_get_accounts($ctx, true))) return false; |
| 53 | $old=$ctx->db->quote($old); |
| 54 | $new=$ctx->db->quote($new); |
| 55 | |
| 56 | return $ctx->db->safe_query( |
| 57 | "START TRANSACTION;". |
| 58 | "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;". |
| 59 | "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;". |
| 60 | "COMMIT;" |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | function bank_get_overview($ctx,$prefix='') { |
| 65 | global $bank_table; |
| 66 | $accounts = bank_get_accounts($ctx); |
| 67 | foreach($accounts as $acc) { |
| 68 | $total=bank_get_total($ctx, $acc); |
| 69 | $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total); |
| 70 | $overview['array'][$acc]=$total; |
| 71 | } |
| 72 | return $overview; |
| 73 | } |
| 74 | |
| 75 | if(isset($bank_json_only) && $bank_json_only) { |
| 76 | $overview=bank_get_overview($this); |
| 77 | die(json_encode(array( |
| 78 | 'overview'=>$overview['array'] |
| 79 | ))); |
| 80 | } |
| 81 | |
| 82 | if(isset($_POST['create_account'])) { |
| 83 | $new_account=$_POST['account_name']; |
| 84 | bank_add_account($this, $new_account); |
| 85 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!"); |
| 86 | } |
| 87 | if(isset($_POST['rename_account'])) { |
| 88 | $new_account=$_POST['account_new']; |
| 89 | $old_account=$_POST['account_old']; |
| 90 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { |
| 91 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!"); |
| 92 | } else { |
| 93 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false); |
| 94 | } |
| 95 | } |
| 96 | if(isset($_POST['transaction'])) { |
| 97 | $account_from=$_POST['account_from']; |
| 98 | $account_to=$_POST['account_to']; |
| 99 | $amount=$_POST['amount']; |
| 100 | $comment=trim($_POST['comment']); |
| 101 | if(!is_numeric($amount) || $amount < 0) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Lze převádět jen kladné částky", true); |
| 102 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Komentář musí mít alespoň 4 znaky!",true); |
| 103 | bank_transaction($this, $account_from, $account_to, $comment, $amount); |
| 104 | $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)"); |
| 105 | } |
| 106 | |
| 107 | //bank_add_account($this, 'material'); |
| 108 | echo("<a href='$URL/'>Banka</a> - "); |
| 109 | echo("<a href='$URL/admin'>Správa účtů - </a>"); |
| 110 | echo("Účty: <br />"); |
| 111 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
| 112 | $lastaccount=false; |
| 113 | foreach($accounts as $account) { |
| 114 | if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />'); |
| 115 | echo("<a href='$URL?account=$account'>$account</a>, "); |
| 116 | $lastaccount=$account; |
| 117 | } |
| 118 | |
| 119 | switch($SUBPATH[0]) { |
| 120 | default: |
| 121 | |
| 122 | if(!isset($_GET['account'])) { |
| 123 | echo("<h1>Banka</h1>"); |
| 124 | echo ("<h2>Stav</h2>"); |
| 125 | $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table};"); |
| 126 | echo("Transakcí: ".$result[0]['troughput']."<br />"); |
| 127 | $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table};"); |
| 128 | echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency); |
| 129 | $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` ORDER BY ${bank_table}_time DESC;"); |
| 130 | $overview=bank_get_overview($this,$bank_table.'_'); |
| 131 | echo $this->html->render_item_table($overview['table'],'bank'); |
| 132 | echo ("<h2>Přehled transakcí</h2>"); |
| 133 | } else { |
| 134 | $account=bank_name($_GET['account']); |
| 135 | $account_sql=$this->db->quote($account); |
| 136 | echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>"); |
| 137 | |
| 138 | ?> |
| 139 | <form action="?" method="POST"> |
| 140 | Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> |
| 141 | z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
| 142 | na účet <select name='account_to'> |
| 143 | <?php |
| 144 | //Ziskat posledni cilovy ucet a presunout na zacatek $accounts |
| 145 | $last=bank_get_last_to($this,$account); |
| 146 | unset($accounts[array_search($last,$accounts)]); |
| 147 | array_unshift($accounts,$last); |
| 148 | |
| 149 | foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); |
| 150 | ?> |
| 151 | </select> (pozor! zamysli se! převádíš peníze nebo dluhy?!)<br /><br /> |
| 152 | Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" /> |
| 153 | <input type="submit" name="transaction" value="Převést" /> |
| 154 | </form> |
| 155 | <?php |
| 156 | |
| 157 | echo(bank_get_total($this,$account,true)." $bank_currency"); |
| 158 | $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;"); |
| 159 | } |
| 160 | echo $this->html->render_item_table($result,$bank_table); |
| 161 | |
| 162 | break; |
| 163 | case 'admin': |
| 164 | ?> |
| 165 | </p> |
| 166 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
| 167 | Account: |
| 168 | <input type="text" name="account_name" /> |
| 169 | <input type="submit" name="create_account" value="Create account" /> |
| 170 | </form> |
| 171 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
| 172 | Account: <select name='account_old'> |
| 173 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> |
| 174 | </select> |
| 175 | <input type="text" name="account_new" /> |
| 176 | <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné) |
| 177 | </form> |
| 178 | </p> |
| 179 | <?php |
| 180 | break; |
| 181 | } |