Podpora kasy
[mirrors/SokoMan.git] / assistants / bank.inc.php
CommitLineData
f94aa899
TM
1<?php
2$bank_currency='Kč';
3
4function bank_name($name) {
5 return strtolower(trim($name));
6}
7
8function 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
13 $comment=trim($comment);
14 if(strlen($comment)<4) die("Komentář musí mít alespoň 4 znaky!");
15 $comment=$ctx->db->quote($comment);
16
17 $sql="INSERT INTO `bank` (`bank_time`, `bank_from`, `bank_to`, `bank_amount`, `bank_author`, `bank_comment`) VALUES (now(), $from, $to, $amount, $author, $comment);";
18 $ctx->db->safe_query($sql);
19}
20
21function bank_get_accounts($ctx) {
22 $fetch = $ctx->db->safe_query_fetch('SELECT DISTINCT bank_to FROM bank ORDER BY bank_to;');
23 foreach($fetch as $account) $accounts[]=$account['bank_to'];
24 return $accounts;
25}
26
27function bank_add_account($ctx, $name) {
28 bank_transaction($ctx, $name, $name, "Created account \"$name\"");
29}
30
31if(isset($_POST['create_account'])) {
32 bank_add_account($this, $_POST['account_name']);
33 $this->post_redirect_get("$URL_INTERNAL","Účet byl vytvořen");
34}
35if(isset($_POST['transaction'])) {
36 bank_transaction($this, $_POST['account_from'], $_POST['account_to'], $_POST['comment'], $_POST['amount']);
37 $this->post_redirect_get("$URL_INTERNAL","Transakce byla provedena"); //TODO redirect na account_from
38}
39
40//bank_add_account($this, 'material');
41 echo("<a href='$URL/'>Banka</a> - ");
42 echo("<a href='$URL/admin'>Správa účtů</a> - ");
43 echo("Účty: ");
44 $accounts = bank_get_accounts($this);
45 foreach($accounts as $account) echo("<a href='$URL?account=$account'>$account</a>, ");
46
47switch($SUBPATH[0]) {
48 default:
49
50 if(!isset($_GET['account'])) {
51 echo("<h1>Banka</h1>");
52 $result = $this->db->safe_query_fetch("SELECT SUM(bank_amount) as troughput FROM bank;");
53 echo("Obrat: ".$result[0]['troughput'].' '.$bank_currency);
54 $result = $this->db->safe_query_fetch("SELECT * FROM `bank` ORDER BY bank_time DESC;");
55 } else {
56 $account=bank_name($_GET['account']);
57 $account_sql=$this->db->quote($account);
58 $result = $this->db->safe_query_fetch("SELECT SUM(bank_amount) FROM `bank` WHERE `bank_to`=$account_sql;");
59 $deposits = $result[0]['SUM(bank_amount)'];
60 $result = $this->db->safe_query_fetch("SELECT SUM(bank_amount) FROM `bank` WHERE `bank_from`=$account_sql;");
61 $withdrawals = $result[0]['SUM(bank_amount)'];
62 echo("<h1>Účet: ".$_GET['account']." (".($deposits-$withdrawals).$bank_currency.")</h1>");
63
64 ?>
65 <form action="?" method="POST">
66 Převést <input type="number" name="amount" value="" /> <?php echo $bank_currency; ?>
67 z účtu <?php echo $account; ?> <input type="hidden" name="account_from" value="<?php echo $account; ?>" />
68 na účet <select name='account_to'>
69 <?php foreach($accounts as $acc) echo("<option value='$acc'>$acc</option>"); ?>
70 </select> (pozor, dluhy se převádí opačným směrem než peníze!)<br />
71 Důvod: <input type="text" name="comment" style="width:800px;" />
72 <input type="submit" name="transaction" value="Převést" />
73 </form>
74 <?php
75
76 echo("$deposits-$withdrawals $bank_currency");
77 $result = $this->db->safe_query_fetch("SELECT * FROM `bank` WHERE `bank_to`=$account_sql OR `bank_from`=$account_sql ORDER BY bank_time DESC;");
78 }
79 echo $this->html->render_item_table($result);
80
81
82 break;
83 case 'admin':
84?>
85 <form action="<?php echo $ASSISTANT; ?>" method="POST" >
86 Account name:
87 <input type="text" name="account_name" />
88 <input type="submit" name="create_account" value="Create account" />
89 </form>
90<?php
91 break;
92}
This page took 0.137535 seconds and 4 git commands to generate.