| 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)); |
| 12 | $amount=$ctx->db->quote($amount); |
| 13 | $comment=$ctx->db->quote(trim($comment)); |
| 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 | |
| 19 | function bank_get_accounts($ctx) { |
| 20 | $fetch = $ctx->db->safe_query_fetch('SELECT DISTINCT bank_to FROM bank ORDER BY bank_to;'); |
| 21 | foreach($fetch as $account) $accounts[]=$account['bank_to']; |
| 22 | return $accounts; |
| 23 | } |
| 24 | |
| 25 | function bank_add_account($ctx, $name) { |
| 26 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); |
| 27 | } |
| 28 | |
| 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 | } |
| 38 | |
| 39 | function bank_get_overview($ctx) { |
| 40 | $accounts = bank_get_accounts($ctx); |
| 41 | foreach($accounts as $acc) $overview[]=array("bank_account"=>$acc,"bank_total"=>bank_get_total($ctx, $acc)); |
| 42 | return $overview; |
| 43 | } |
| 44 | |
| 45 | if(isset($_POST['create_account'])) { |
| 46 | bank_add_account($this, $_POST['account_name']); |
| 47 | $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen"); |
| 48 | } |
| 49 | if(isset($_POST['transaction'])) { |
| 50 | 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); |
| 51 | $comment=trim($_POST['comment']); |
| 52 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Komentář musí mít alespoň 4 znaky!",true); |
| 53 | bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']); |
| 54 | $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Transakce byla provedena"); |
| 55 | } |
| 56 | |
| 57 | //bank_add_account($this, 'material'); |
| 58 | echo("<a href='$URL/'>Banka</a> - "); |
| 59 | echo("<a href='$URL/admin'>Správa účtů</a> - "); |
| 60 | echo("Účty: "); |
| 61 | $accounts = bank_get_accounts($this); |
| 62 | foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, "); |
| 63 | |
| 64 | switch($SUBPATH[0]) { |
| 65 | default: |
| 66 | |
| 67 | if(!isset($_GET['account'])) { |
| 68 | echo("<h1>Banka</h1>"); |
| 69 | echo ("<h2>Stav</h2>"); |
| 70 | $result = $this->db->safe_query_fetch("SELECT COUNT(bank_amount) as troughput FROM bank;"); |
| 71 | echo("Transakcí: ".$result[0]['troughput']."<br />"); |
| 72 | $result = $this->db->safe_query_fetch("SELECT SUM(bank_amount) as troughput FROM bank;"); |
| 73 | echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency); |
| 74 | $result = $this->db->safe_query_fetch("SELECT * FROM `bank` ORDER BY bank_time DESC;"); |
| 75 | echo $this->html->render_item_table(bank_get_overview($this),'bank'); |
| 76 | echo ("<h2>Přehled transakcí</h2>"); |
| 77 | } else { |
| 78 | $account=bank_name($_GET['account']); |
| 79 | $account_sql=$this->db->quote($account); |
| 80 | echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>"); |
| 81 | |
| 82 | ?> |
| 83 | <form action="?" method="POST"> |
| 84 | Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> |
| 85 | z účtu <?php echo $account; ?> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
| 86 | na účet <select name='account_to'> |
| 87 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> |
| 88 | </select> (pozor, dluhy se převádí opačným směrem než peníze!)<br /><br /> |
| 89 | Důvod: <input type="text" name="comment" style="width:64em;" /> |
| 90 | <input type="submit" name="transaction" value="Převést" /> |
| 91 | </form> |
| 92 | <?php |
| 93 | |
| 94 | echo(bank_get_total($this,$account,true)." $bank_currency"); |
| 95 | $result = $this->db->safe_query_fetch("SELECT * FROM `bank` WHERE `bank_to`=$account_sql OR `bank_from`=$account_sql ORDER BY bank_time DESC;"); |
| 96 | } |
| 97 | echo $this->html->render_item_table($result,'bank'); |
| 98 | |
| 99 | break; |
| 100 | case 'admin': |
| 101 | ?> |
| 102 | </p> |
| 103 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
| 104 | Account name: |
| 105 | <input type="text" name="account_name" /> |
| 106 | <input type="submit" name="create_account" value="Create account" /> |
| 107 | </form> |
| 108 | </p> |
| 109 | <?php |
| 110 | break; |
| 111 | } |