f16bb21180d7e71a9f747e811e2bdc72984dd94d
[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\', \'bankcharges\', \'account_transfer\', \'member_fee\', \'catering\') 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     'bankcharges' => 'Bankgebühren',
25     'account_transfer'  => 'Umbuchung',
26     'member_fee' => 'Rückbuchung Mitgliedsbeitrag',
27     'catering' => 'Raum - Getraenke & Snacks'
28 );
29
30
31 /* DB functions {{{ */
32
33 function db_get_expense_with_id($expense_id) {
34     $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
35     return db_select_single(sprintf($sql, DB_TABLE_EXPENSES, db_escape($expense_id)));
36 }
37
38 function db_create_expense($expense) {
39     unset($expense['id']);
40     $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
41     $expense['value']       = ui_money2float($expense['value']);
42     $expense['created_at']  = db_unixtime2datetime(time());
43     $expense['modified_at'] = db_unixtime2datetime(time());
44     return (db_insert_single(DB_TABLE_EXPENSES, $expense));
45 }
46
47 function db_change_expense($expense) {
48     $id = $expense['id'];
49     unset($expense['id']);
50     $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
51     $expense['value']       = ui_money2float($expense['value']);
52     $expense['modified_at'] = db_unixtime2datetime(time());
53     return db_update_single(DB_TABLE_EXPENSES, $expense, sprintf('id=\'%1$d\'', db_escape($id)));
54 }
55
56 function db_delete_expense($expense) {
57     return db_delete_single(DB_TABLE_EXPENSES, sprintf('id=\'%1$d\'', db_escape($expense['id'])));
58 }
59
60 /* }}} */
61
62
63 function validate_expense($userdata, &$dbdata, &$validation) {/*{{{*/
64     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
65     $fields = array(
66         'id'              => 'number',
67         'date'            => 'date',
68         'type'            => $EXPENSE_TYPES,
69         'account'         => $ACCOUNT_TYPES,
70         'member_id'       => 'member',
71         'value'           => 'money',
72         'description'     => 'text',
73 //      'status'          -  See below...
74     );
75     $orig = $dbdata;
76     $dbdata['status'] = empty($userdata['status']) ? 'open' : 'paid';
77     foreach ($fields as $name => $type) {
78         if (!isset($userdata[$name])) continue;
79         $dbdata[$name] = $userdata[$name];
80     }
81
82     if (empty($dbdata['date'])) {
83         $validation[] = 'Bitte ein Datum angeben.';
84     }
85     if ((float)ui_money2float($dbdata['value']) == 0) {
86         $validation[] = 'Bitte einen Betrag angeben.';
87     }
88
89     return (count($validation) == 0);
90 }/*}}}*/
91
92
93 function action_create_expense() {/*{{{*/
94     $expense = array();
95     $expense['date']      = format_date(time());
96     $expense['status']    = 'paid';
97     $log_messages = array();
98     if (isset($_POST['submit'])) {
99         unset($_POST['id']);
100         if (validate_expense($_POST, $expense, $log_messages)) {
101             if (db_create_expense($expense)) {
102                 redirect(link_to('finances'));
103             }
104             echo db_error();
105         }
106     }
107     form_expense($expense, 'create', $log_messages);
108 }/*}}}*/
109
110
111 function action_change_expense() {/*{{{*/
112     if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
113     $expense = db_get_expense_with_id($_REQUEST['id']);
114     if (!isset($expense)) redirect(link_to('finances'));
115     $expense['date'] = format_date(db_date2unixtime($expense['date']));
116     $expense['value']= format_money($expense['value']);
117     $log_messages = array();
118     if (isset($_POST['submit'])) {
119         if (validate_expense($_POST, $expense, $log_messages)) {
120             if (db_change_expense($expense)) {
121                 redirect(link_to('finances'));
122             }
123             echo db_error();
124         }
125     }
126
127     form_expense($expense, 'change', $log_messages);
128 }/*}}}*/
129
130
131 function action_delete_expense() {/*{{{*/
132     if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
133     $expense = db_get_expense_with_id($_REQUEST['id']);
134     if (!isset($expense)) redirect(link_to('finances'));
135     $expense['date'] = format_date(db_date2unixtime($expense['date']));
136     $expense['value']= format_money($expense['value']);
137     $log_messages = array();
138     if (isset($_POST['submit'])) {
139         if (db_delete_expense($expense)) {
140             redirect(link_to('finances'));
141         }
142         echo db_error();
143     }
144     form_expense($expense, 'delete', $log_messages);
145 }/*}}}*/
146
147
148 function form_expense($expense = array(), $task, $log_messages) {/*{{{*/
149     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
150
151     $readonly = ($task == 'delete');
152
153     ?>
154     <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 ?>">
155         <?php if (isset($expense['id'])) : ?><?=html_hidden_field('id', $expense)?><?php endif ?>
156         <fieldset>
157         <?php if     ($task == 'create') : ?>
158             <legend>Neue Ausgabe</legend>
159         <?php elseif ($task == 'delete') : ?>
160             <legend>Ausgabe l&ouml;schen</legend>
161         <?php elseif ($task == 'change') : ?>
162             <legend>Ausgabe &auml;ndern</legend>
163         <?php endif ?>
164             <?php log_messages($log_messages); ?>
165             <?=html_text_field('Datum', 'date', $expense, $readonly)?>
166             <?=html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly)?>
167             <?=html_member_list_box('Mitglied', 'member_id', $expense, $readonly)?>
168             <?=html_money_field('Betrag', 'value', $expense, $readonly)?>
169             <?=html_text_field('Beschreibung', 'description', $expense, $readonly)?>
170             <?=html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly)?>
171             <?=html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly)?>
172         </fieldset>
173     <?php if ($task == 'delete') : ?>
174         <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
175     <?php else : ?>
176         <input class="submit" type="submit" name="submit" value="Speichern"/>
177     <?php endif ?>
178     </form>
179     <?php if ($task == 'change') : ?>
180         <br/>
181         <a href="<?=html_escape(link_to('delete_expense', array('id' => $expense['id'])))?>#expense_form">Ausgabe l&ouml;schen</a>
182         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
183     <?php elseif ($task == 'delete') : ?>
184         <br/>
185         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
186     <?php endif ?>
187     <?php
188 }/*}}}*/
189
190