Podpora měsíčních přehledů kasy
[mirrors/SokoMan.git] / assistants / bank.inc.php
index 1435cac4a4ade1bc32fca95e9c55b87663b22890..2b57d422a0a65880c4f7517f866e6ce9c79d5ff1 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 $bank_currency='Kč';
 global $bank_table;
-$bank_table='bank';
+$bank_table='transaction';
 
 function bank_name($name) {
        return strtolower(trim($name));
@@ -26,16 +26,33 @@ function bank_get_accounts($ctx, $all=false) {
        return $accounts;
 }
 
+function bank_get_last_to($ctx, $account) {
+       global $bank_table;
+       $account=$ctx->db->quote(bank_name($account));
+       $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;");
+       return $fetch[0][$bank_table.'_to'];
+}
+
 function bank_add_account($ctx, $name) {
        bank_transaction($ctx, $name, $name, "Created account \"$name\"");
 }
 
-function bank_get_total($ctx, $account, $string=false) {
+function bank_month_sql($ctx, $month=false) {
+       global $bank_table;
+       $month_sql = 'TRUE';
+       if(!is_bool($month)) {
+               $month_q = $ctx->db->quote($month);
+               $month_sql .= " AND DATE_FORMAT(${bank_table}_time, '%Y-%m') = ".$month_q;
+       }
+       return $month_sql;
+}
+
+function bank_get_total($ctx, $account, $month, $string=false) {
        global $bank_table;
        $account_sql=$ctx->db->quote($account);
-       $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_to`=$account_sql;");
+       $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).';');
        $deposits = $result[0]["SUM(${bank_table}_amount)"];
-       $result = $ctx->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) FROM `${bank_table}` WHERE `${bank_table}_from`=$account_sql;");
+       $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).');');
        $withdrawals = $result[0]["SUM(${bank_table}_amount)"];
        if($string) return "$deposits-$withdrawals";
        return $deposits-$withdrawals;
@@ -54,72 +71,112 @@ function bank_rename_account($ctx, $old, $new) {
        );
 }
 
-function bank_get_overview($ctx) {
+function bank_get_overview($ctx,$prefix='',$month=false) {
        global $bank_table;
        $accounts = bank_get_accounts($ctx);
-       foreach($accounts as $acc) $overview[]=array("${bank_table}_account"=>$acc,"${bank_table}_total"=>bank_get_total($ctx, $acc));
+       foreach($accounts as $acc) {
+               $total=bank_get_total($ctx, $acc, $month);
+               $overview['table'][]=array("${prefix}account"=>$acc,"${prefix}total"=>$total);
+               $overview['array'][$acc]=$total;
+       }
        return $overview;
 }
 
+if(isset($bank_json_only) && $bank_json_only) {
+       $overview=bank_get_overview($this,'');
+       die(json_encode(array(
+               'overview'=>$overview['array']
+       )));
+}
+
 if(isset($_POST['create_account'])) {
-       bank_add_account($this, $_POST['account_name']);
-       $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen");
+       $new_account=$_POST['account_name'];
+       bank_add_account($this, $new_account);
+       $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> byl vytvořen!");
 }
 if(isset($_POST['rename_account'])) {
+       $new_account=$_POST['account_new'];
+       $old_account=$_POST['account_old'];
        if(bank_rename_account($this, $_POST['account_old'], $_POST['account_new'])) {
-               $this->post_redirect_get("$URL_INTERNAL","Účet byl upraven");
+               $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$old_account</b> byl přejmenován na <b>$new_account</b>!");
        } else {
-               $this->post_redirect_get("$URL_INTERNAL","Takový účet již existuje!", false);
+               $this->post_redirect_get("$URL_INTERNAL/admin","Účet <b>$new_account</b> již existuje!", false);
        }
 }
 if(isset($_POST['transaction'])) {
-       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);
+       $account_from=$_POST['account_from'];
+       $account_to=$_POST['account_to'];
+       $amount=$_POST['amount'];
        $comment=trim($_POST['comment']);
-       if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Komentář musí mít alespoň 4 znaky!",true);
-       bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']);
-       $this->post_redirect_get("$URL_INTERNAL?account=".$_POST['account_from'],"Transakce byla provedena");
+       if(!is_numeric($amount) || $amount < 0) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Lze převádět jen kladné částky", true);
+       if(strlen($comment)<4) $this->post_redirect_get("$URL_INTERNAL?account=".$account_from,"Komentář musí mít alespoň 4 znaky!",true);
+       bank_transaction($this, $account_from, $account_to, $comment, $amount);
+       $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)");
 }
 
+$month = isset($_GET['month']) ? $_GET['month'] : false;
+
 //bank_add_account($this, 'material');
 echo("<a href='$URL/'>Banka</a> - ");
-echo("<a href='$URL/admin'>Správa účtů</a> - ");
-echo("Účty: ");
+echo("<a href='$URL/admin'>Správa účtů - </a>");
+echo('<span style="float:right;">');
+echo $this->html->form($URL, 'GET', array(
+  array('month',$month,'text',false,'','YYYY-MM:'),
+  array(false,'SELECT BY MONTH','submit')
+));
+echo('</span>');
+echo("Účty: <br />");
 $accounts = bank_get_accounts($this, $SUBPATH[0]=='admin');
-foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, ");
+$lastaccount=false;
+foreach($accounts as $account) {
+       if($lastaccount && $lastaccount[0]!=$account[0] && !preg_match('/[a-zA-Z0-9]/', $lastaccount[0])) echo('<br />');
+       echo("<a href='$URL?account=$account'>$account</a>, ");
+       $lastaccount=$account;
+}
+
+
 
 switch($SUBPATH[0]) {
        default:
 
                if(!isset($_GET['account'])) {
-                       echo("<h1>Banka</h1>");
-                       echo ("<h2>Stav</h2>");
-           $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table};");
-                       echo("Transakcí: ".$result[0]['troughput']."<br />");
-           $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table};");
-                       echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency);
-           $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` ORDER BY ${bank_table}_time DESC;");
-                       echo $this->html->render_item_table(bank_get_overview($this),'bank');
-                       echo ("<h2>Přehled transakcí</h2>");
+                       echo("<h1>Banka $month</h1>");
+                       echo ("<h2>Stav $month</h2>");
+                       $result = $this->db->safe_query_fetch("SELECT COUNT(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
+                       echo("Transakcí $month: ".$result[0]['troughput']."<br />");
+                       $result = $this->db->safe_query_fetch("SELECT SUM(${bank_table}_amount) as troughput FROM ${bank_table} WHERE ".bank_month_sql($this,$month).';');
+                       echo("Obrat $month: ".$result[0]['troughput'].' '.$bank_currency);
+                       $result = $this->db->safe_query_fetch("SELECT * FROM `${bank_table}` WHERE ".bank_month_sql($this,$month)." ORDER BY ${bank_table}_time DESC;");
+                       $overview=bank_get_overview($this,$bank_table.'_',$month);
+                       echo $this->html->render_item_table($overview['table'],'bank');
                } else {
                        $account=bank_name($_GET['account']);
                        $account_sql=$this->db->quote($account);
-                       echo("<h1>Účet: ".$account." (".bank_get_total($this,$account).$bank_currency.")</h1>");
+                       echo("<h1>Účet: ".$account." $month (".bank_get_total($this,$account,$month).$bank_currency.")</h1>");
 
                        ?>
                        <form action="?" method="POST">
                                Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
-                               z účtu <?php echo $account; ?> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
+                               z účtu <b><?php echo $account; ?></b> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
                                na účet <select name='account_to'>
-                                       <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
-                               </select> (pozor, dluhy se převádí opačným směrem než peníze!)<br /><br />
+                                       <?php
+                                               //Ziskat posledni cilovy ucet a presunout na zacatek $accounts
+                                               $last=bank_get_last_to($this,$account);
+                                               unset($accounts[array_search($last,$accounts)]);
+                                               array_unshift($accounts,$last);
+
+                                               foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>");
+                                       ?>
+                               </select> (pozor! zamysli se! převádíš peníze nebo dluhy?!)<br /><br />
                                Důvod: <input type="text" name="comment" maxlength="128" style="width:64em;" />
                                <input type="submit" name="transaction" value="Převést" />
                        </form>
                        <?php
 
-                       echo(bank_get_total($this,$account,true)." $bank_currency");
-           $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;");
+                       echo(bank_get_total($this,$account,$month,true)." $bank_currency");
+                       $result = $this->db->safe_query_fetch("SELECT * 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;");
                }
+               echo ("<h2>Přehled transakcí $month</h2>");
                echo $this->html->render_item_table($result,$bank_table);
 
                break;
This page took 0.158862 seconds and 4 git commands to generate.