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