| 1 | <?php |
| 2 | $bank_currency='Kč'; |
| 3 | global $bank_table; |
| 4 | $bank_table='transaction'; |
| 5 | $recursive=true; //USE RECURSIVE QUERIES??? |
| 6 | $limit=23; |
| 7 | |
| 8 | function bank_name($name) { |
| 9 | return strtolower(trim($name)); |
| 10 | } |
| 11 | |
| 12 | function bank_transaction($ctx, $from, $to, $comment, $amount=0) { |
| 13 | global $bank_table; |
| 14 | $author=$ctx->db->quote($ctx->db->auth->get_user_id()); |
| 15 | $from=$ctx->db->quote(bank_name($from)); |
| 16 | $to=$ctx->db->quote(bank_name($to)); |
| 17 | $amount=$ctx->db->quote($amount); |
| 18 | $comment=$ctx->db->quote(trim($comment)); |
| 19 | |
| 20 | $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);"; |
| 21 | $ctx->db->safe_query($sql); |
| 22 | } |
| 23 | |
| 24 | function bank_get_accounts($ctx, $all=false) { |
| 25 | global $bank_table; |
| 26 | $fetch = $ctx->db->safe_query_fetch("SELECT DISTINCT ${bank_table}_to FROM ${bank_table} ORDER BY ${bank_table}_to;"); |
| 27 | foreach($fetch as $account) if($all || $account[$bank_table.'_to'][0]!='_') $accounts[]=$account[$bank_table.'_to']; |
| 28 | return $accounts; |
| 29 | } |
| 30 | |
| 31 | function bank_get_last_to($ctx, $account) { |
| 32 | global $bank_table; |
| 33 | $account=$ctx->db->quote(bank_name($account)); |
| 34 | $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;"); |
| 35 | return $fetch[0][$bank_table.'_to']; |
| 36 | } |
| 37 | |
| 38 | function bank_add_account($ctx, $name) { |
| 39 | bank_transaction($ctx, $name, $name, "Created account \"$name\""); |
| 40 | } |
| 41 | |
| 42 | function bank_month_sql($ctx, $month=false) { |
| 43 | global $bank_table; |
| 44 | $month_sql = 'TRUE'; |
| 45 | if(!is_bool($month)) { |
| 46 | $month_q = $ctx->db->quote($month); |
| 47 | $month_sql .= " AND DATE_FORMAT(${bank_table}_time, '%Y-%m') = ".$month_q; |
| 48 | } |
| 49 | return $month_sql; |
| 50 | } |
| 51 | |
| 52 | function bank_get_total($ctx, $account, $month, $string=false) { |
| 53 | global $bank_table; |
| 54 | $account_sql=$ctx->db->quote($account); |
| 55 | $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).';'); |
| 56 | $deposits = $result[0]["SUM(${bank_table}_amount)"]; |
| 57 | $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).');'); |
| 58 | $withdrawals = $result[0]["SUM(${bank_table}_amount)"]; |
| 59 | if($string) return "$deposits-$withdrawals"; |
| 60 | return $deposits-$withdrawals; |
| 61 | } |
| 62 | function bank_rename_account($ctx, $old, $new) { |
| 63 | global $bank_table; |
| 64 | if(in_array($new, bank_get_accounts($ctx, true))) return false; |
| 65 | $old=$ctx->db->quote($old); |
| 66 | $new=$ctx->db->quote($new); |
| 67 | |
| 68 | return $ctx->db->safe_query( |
| 69 | "START TRANSACTION;". |
| 70 | "UPDATE ${bank_table} SET `${bank_table}_to`=$new WHERE `${bank_table}_to`=$old;". |
| 71 | "UPDATE ${bank_table} SET `${bank_table}_from`=$new WHERE `${bank_table}_from`=$old;". |
| 72 | "COMMIT;" |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | function bank_get_overview($ctx,$prefix='',$month=false) { |
| 77 | global $bank_table; |
| 78 | $accounts = bank_get_accounts($ctx); |
| 79 | foreach($accounts as $acc) { |
| 80 | $total=bank_get_total($ctx, $acc, $month); |
| 81 | $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total); |
| 82 | $overview['array'][$acc]=$total; |
| 83 | } |
| 84 | return $overview; |
| 85 | } |
| 86 | |
| 87 | if(isset($bank_json_only) && $bank_json_only) { |
| 88 | $overview=bank_get_overview($this,''); |
| 89 | die(json_encode(array( |
| 90 | 'overview'=>$overview['array'] |
| 91 | ))); |
| 92 | } |
| 93 | |
| 94 | if(isset($_POST['create_account'])) { |
| 95 | $new_account=$_POST['account_name']; |
| 96 | bank_add_account($this, $new_account); |
| 97 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!"); |
| 98 | } |
| 99 | if(isset($_POST['rename_account'])) { |
| 100 | $new_account=$_POST['account_new']; |
| 101 | $old_account=$_POST['account_old']; |
| 102 | if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) { |
| 103 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!"); |
| 104 | } else { |
| 105 | $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false); |
| 106 | } |
| 107 | } |
| 108 | if(isset($_POST['transaction'])) { |
| 109 | $account_from=$_POST['account_from']; |
| 110 | $account_to=$_POST['account_to']; |
| 111 | $amount=$_POST['amount']; |
| 112 | $comment=trim($_POST['comment']); |
| 113 | $account_redirect=$account_from; |
| 114 | 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); |
| 115 | if($amount < 0) { |
| 116 | $amount=abs($amount); |
| 117 | list($account_from,$account_to)=array($account_to,$account_from); //swap from/to |
| 118 | } |
| 119 | if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".urlencode($account_from),"Komentář musí mít alespoň 4 znaky!",true); |
| 120 | bank_transaction($this, $account_from, $account_to, $comment, $amount); |
| 121 | $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)"); |
| 122 | } |
| 123 | |
| 124 | $month = isset($_GET['month']) ? $_GET['month'] : false; |
| 125 | |
| 126 | //bank_add_account($this, 'material'); |
| 127 | echo("<a href='$URL/'>Banka</a> - "); |
| 128 | echo("<a href='$URL/admin'>Správa účtů</a> - "); |
| 129 | echo('<span style="float:right;">'); |
| 130 | echo $this->html->form($URL, 'GET', array( |
| 131 | array('month',$month,'text',false,'','YYYY-MM:'), |
| 132 | array(false,'SELECT BY MONTH','submit') |
| 133 | )); |
| 134 | echo('</span>'); |
| 135 | echo("Účty: <br />"); |
| 136 | $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin'); |
| 137 | $lastaccount=false; |
| 138 | foreach($accounts as $account) { |
| 139 | if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />'); |
| 140 | echo("<a href='$URL?account=".urlencode($account)."'>$account</a>, "); |
| 141 | $lastaccount=$account; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | switch($SUBPATH[0]) { |
| 147 | default: |
| 148 | |
| 149 | if(isset($_GET['limit'])) $limit = intval($_GET['limit']); |
| 150 | $limit_sql = $limit==0 ? '' : ' LIMIT '.intval($limit); |
| 151 | |
| 152 | if(!isset($_GET['account'])) { |
| 153 | echo("<h1>Banka $month</h1>"); |
| 154 | echo ("<h2>Stav $month</h2>"); |
| 155 | $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';'); |
| 156 | echo("Transakcí $month: ".$result[0]['troughput']."<br />"); |
| 157 | $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';'); |
| 158 | echo("Obrat $month: ".$result[0]['troughput'].' '.$bank_currency); |
| 159 | $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE ".bank_month_sql($this,$month)." ORDER BY ${bank_table}_time DESC".$limit_sql.";"); |
| 160 | $overview=bank_get_overview($this,$bank_table.'_',$month); |
| 161 | echo $this->html->render_item_table($overview['table'],'bank'); |
| 162 | } else { |
| 163 | $account=bank_name($_GET['account']); |
| 164 | $account_sql=$this->db->quote($account); |
| 165 | echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>"); |
| 166 | |
| 167 | ?> |
| 168 | <form action="?" method="POST"> |
| 169 | Převést <!-- ± --><input type="number" name="amount" value="" /> <?php echo $bank_currency; ?> |
| 170 | z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" /> |
| 171 | na účet <select name='account_to'> |
| 172 | <?php |
| 173 | //Ziskat posledni cilovy ucet a presunout na zacatek $accounts |
| 174 | $last=bank_get_last_to($this,$account); |
| 175 | unset($accounts[array_search($last,$accounts)]); |
| 176 | array_unshift($accounts,$last); |
| 177 | |
| 178 | foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); |
| 179 | ?> |
| 180 | </select> (pozor! zamysli se! převádíš peníze nebo dluhy?! záporná částka = převod v opačném směru.)<br /><br /> |
| 181 | Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" /> |
| 182 | <input type="submit" name="transaction" value="Převést" /> |
| 183 | </form> |
| 184 | <?php |
| 185 | |
| 186 | echo(bank_get_total($this,$account,$month,true)." $bank_currency"); |
| 187 | $subtotal=$recursive?",( |
| 188 | (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) |
| 189 | -(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) |
| 190 | ) as ${bank_table}_subtotal":''; |
| 191 | //(@flux := IF(transaction_to='harvie',IF(transaction_from='harvie',0,1),IF(transaction_from='harvie',-1,0))) as flux |
| 192 | $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".$limit_sql.";"); |
| 193 | } |
| 194 | echo ("<h2>Přehled transakcí $month</h2>"); |
| 195 | echo $this->html->render_item_table($result,$bank_table); |
| 196 | |
| 197 | if(!isset($_GET['limit'])) |
| 198 | echo("<a href='?".$_SERVER['QUERY_STRING']."&limit=0'>zobrazit vše...</a>"); |
| 199 | if($limit == 0) |
| 200 | echo('to je vše.'); |
| 201 | |
| 202 | break; |
| 203 | case 'admin': |
| 204 | ?> |
| 205 | </p> |
| 206 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
| 207 | Account: |
| 208 | <input type="text" name="account_name" /> |
| 209 | <input type="submit" name="create_account" value="Create account" /> |
| 210 | </form> |
| 211 | <form action="<?php echo $ASSISTANT; ?>" method="POST" > |
| 212 | Account: <select name='account_old'> |
| 213 | <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?> |
| 214 | </select> |
| 215 | <input type="text" name="account_new" /> |
| 216 | <input type="submit" name="rename_account" value="Rename account" /> (účty začínající podtržítkem nebudou běžně viditelné) |
| 217 | </form> |
| 218 | </p> |
| 219 | <?php |
| 220 | break; |
| 221 | } |