Fehler beim Prüfen des angegebenen Betrags bei Beträgen bis 1 Euro.
[memberdb.git] / include / expenses.php
1 <?php
2
3 $SQL_CREATE_EXPENSES = sprintf('
4     CREATE TABLE IF NOT EXISTS `%1$s` (
5       `id` int(10) unsigned NOT NULL auto_increment,
6       `date` date NOT NULL,
7       `type` enum(\'invoice\', \'gains_tax\') NULL,
8       `status` enum(\'open\',\'paid\') NOT NULL,
9       `account` enum(\'bank\',\'cash\') NOT NULL,
10       `value` DECIMAL(10,2) NULL,
11       `member_id` int(10) unsigned NOT NULL,
12       `description` TEXT NOT NULL,
13       `created_at` datetime NOT NULL,
14       `modified_at` datetime NOT NULL,
15       PRIMARY KEY  (`id`),
16       INDEX `idx_member_id` (`member_id`),
17       INDEX `idx_date` (`date`)
18     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
19     ', DB_TABLE_EXPENSES, DB_CHARSET);
20
21 $EXPENSE_TYPES = array(
22     'invoice'   => 'Rechnung',
23     'gains_tax' => 'Kapitalertragssteuer'
24 );
25
26
27 /* DB functions {{{ */
28
29 function db_get_expense_with_id($expense_id) {
30     $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
31     return db_select_single(sprintf($sql, DB_TABLE_EXPENSES, db_escape($expense_id)));
32 }
33
34 function db_create_expense($expense) {
35     unset($expense['id']);
36     $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
37     $expense['value']       = ui_money2float($expense['value']);
38     $expense['created_at']  = db_unixtime2datetime(time());
39     $expense['modified_at'] = db_unixtime2datetime(time());
40     return (db_insert_single(DB_TABLE_EXPENSES, $expense));
41 }
42
43 function db_change_expense($expense) {
44     $id = $expense['id'];
45     unset($expense['id']);
46     $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
47     $expense['value']       = ui_money2float($expense['value']);
48     $expense['modified_at'] = db_unixtime2datetime(time());
49     return db_update_single(DB_TABLE_EXPENSES, $expense, sprintf('id=\'%1$d\'', db_escape($id)));
50 }
51
52 /* }}} */
53
54
55 function validate_expense($userdata, &$dbdata, &$validation) {/*{{{*/
56     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
57     $fields = array(
58         'id'              => 'number',
59         'date'            => 'date',
60         'type'            => $EXPENSE_TYPES,
61         'account'         => $ACCOUNT_TYPES,
62         'member_id'       => 'member',
63         'value'           => 'money',
64         'description'     => 'text',
65 //      'status'          -  See below...
66     );
67     $orig = $dbdata;
68     $dbdata['status'] = empty($userdata['status']) ? 'open' : 'paid';
69     foreach ($fields as $name => $type) {
70         if (!isset($userdata[$name])) continue;
71         $dbdata[$name] = $userdata[$name];
72     }
73
74     if (empty($dbdata['date'])) {
75         $validation[] = 'Bitte ein Datum angeben.';
76     }
77     if ((float)ui_money2float($dbdata['value']) == 0) {
78         $validation[] = 'Bitte einen Betrag angeben.';
79     }
80
81     return (count($validation) == 0);
82 }/*}}}*/
83
84
85 function action_create_expense() {/*{{{*/
86     $expense = array();
87     $expense['date']      = format_date(time());
88     $expense['status']    = 'paid';
89     $log_messages = array();
90     if (isset($_POST['submit'])) {
91         unset($_POST['id']);
92         if (validate_expense($_POST, $expense, $log_messages)) {
93             if (db_create_expense($expense)) {
94                 redirect(link_to('finances'));
95             }
96             echo db_error();
97         }
98     }
99     form_expense($expense, 'create', $log_messages);
100 }/*}}}*/
101
102
103 function action_change_expense() {/*{{{*/
104     if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
105     $expense = db_get_expense_with_id($_REQUEST['id']);
106     $expense['date'] = format_date(db_date2unixtime($expense['date']));
107     $expense['value']= format_money($expense['value']);
108     if (!isset($expense)) redirect(link_to('finances'));
109     $log_messages = array();
110     if (isset($_POST['submit'])) {
111         if (validate_expense($_POST, $expense, $log_messages)) {
112             if (db_change_expense($expense)) {
113                 redirect(link_to('finances'));
114             }
115             echo db_error();
116         }
117     }
118
119     form_expense($expense, 'change', $log_messages);
120 }/*}}}*/
121
122
123 function form_expense($expense = array(), $task, $log_messages) {/*{{{*/
124     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
125
126     $readonly = ($task == 'delete');
127
128     ?>
129     <form action="<?=link_to($task . '_expense')?>" method="post" class="clearfix" id="expense_form" style="<?php if ($_REQUEST['action'] == 'finances') : ?>float: left<?php else :?>margin-top: 2em<?php endif ?>">
130         <?php if (isset($expense['id'])) : ?><?=html_hidden_field('id', $expense)?><?php endif ?>
131         <fieldset>
132         <?php if     ($task == 'create') : ?>
133             <legend>Neue Ausgabe</legend>
134         <?php elseif ($task == 'delete') : ?>
135             <legend>Ausgabe l&ouml;schen</legend>
136         <?php elseif ($task == 'change') : ?>
137             <legend>Ausgabe &auml;ndern</legend>
138         <?php endif ?>
139             <?php log_messages($log_messages); ?>
140             <?=html_text_field('Datum', 'date', $expense, $readonly)?>
141             <?=html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly)?>
142             <?=html_member_list_box('Mitglied', 'member_id', $expense, $readonly)?>
143             <?=html_money_field('Betrag', 'value', $expense, $readonly)?>
144             <?=html_text_field('Beschreibung', 'description', $expense, $readonly)?>
145             <?=html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly)?>
146             <?=html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly)?>
147         </fieldset>
148     <?php if ($task == 'delete') : ?>
149         <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
150     <?php else : ?>
151         <input class="submit" type="submit" name="submit" value="Speichern"/>
152     <?php endif ?>
153     </form>
154     <?php if ($task == 'change') : ?>
155         <br/>
156         <a href="<?=html_escape(link_to('delete_expense', array('id' => $expense['id'])))?>#expense_form">Ausgabe l&ouml;schen</a>
157         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
158     <?php elseif ($task == 'delete') : ?>
159         <br/>
160         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
161     <?php endif ?>
162     <?php
163 }/*}}}*/
164
165