48ca6799720b694e0fcf1c071888dc448836f23e
[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 /* }}} */
55
56
57 function validate_expense($userdata, &$dbdata, &$validation) {/*{{{*/
58     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
59     $fields = array(
60         'id'              => 'number',
61         'date'            => 'date',
62         'type'            => $EXPENSE_TYPES,
63         'account'         => $ACCOUNT_TYPES,
64         'member_id'       => 'member',
65         'value'           => 'money',
66         'description'     => 'text',
67 //      'status'          -  See below...
68     );
69     $orig = $dbdata;
70     $dbdata['status'] = empty($userdata['status']) ? 'open' : 'paid';
71     foreach ($fields as $name => $type) {
72         if (!isset($userdata[$name])) continue;
73         $dbdata[$name] = $userdata[$name];
74     }
75
76     if (empty($dbdata['date'])) {
77         $validation[] = 'Bitte ein Datum angeben.';
78     }
79     if ((float)ui_money2float($dbdata['value']) == 0) {
80         $validation[] = 'Bitte einen Betrag angeben.';
81     }
82
83     return (count($validation) == 0);
84 }/*}}}*/
85
86
87 function action_create_expense() {/*{{{*/
88     $expense = array();
89     $expense['date']      = format_date(time());
90     $expense['status']    = 'paid';
91     $log_messages = array();
92     if (isset($_POST['submit'])) {
93         unset($_POST['id']);
94         if (validate_expense($_POST, $expense, $log_messages)) {
95             if (db_create_expense($expense)) {
96                 redirect(link_to('finances'));
97             }
98             echo db_error();
99         }
100     }
101     form_expense($expense, 'create', $log_messages);
102 }/*}}}*/
103
104
105 function action_change_expense() {/*{{{*/
106     if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
107     $expense = db_get_expense_with_id($_REQUEST['id']);
108     $expense['date'] = format_date(db_date2unixtime($expense['date']));
109     $expense['value']= format_money($expense['value']);
110     if (!isset($expense)) redirect(link_to('finances'));
111     $log_messages = array();
112     if (isset($_POST['submit'])) {
113         if (validate_expense($_POST, $expense, $log_messages)) {
114             if (db_change_expense($expense)) {
115                 redirect(link_to('finances'));
116             }
117             echo db_error();
118         }
119     }
120
121     form_expense($expense, 'change', $log_messages);
122 }/*}}}*/
123
124
125 function form_expense($expense = array(), $task, $log_messages) {/*{{{*/
126     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
127
128     $readonly = ($task == 'delete');
129
130     ?>
131     <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 ?>">
132         <?php if (isset($expense['id'])) : ?><?=html_hidden_field('id', $expense)?><?php endif ?>
133         <fieldset>
134         <?php if     ($task == 'create') : ?>
135             <legend>Neue Ausgabe</legend>
136         <?php elseif ($task == 'delete') : ?>
137             <legend>Ausgabe l&ouml;schen</legend>
138         <?php elseif ($task == 'change') : ?>
139             <legend>Ausgabe &auml;ndern</legend>
140         <?php endif ?>
141             <?php log_messages($log_messages); ?>
142             <?=html_text_field('Datum', 'date', $expense, $readonly)?>
143             <?=html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly)?>
144             <?=html_member_list_box('Mitglied', 'member_id', $expense, $readonly)?>
145             <?=html_money_field('Betrag', 'value', $expense, $readonly)?>
146             <?=html_text_field('Beschreibung', 'description', $expense, $readonly)?>
147             <?=html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly)?>
148             <?=html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly)?>
149         </fieldset>
150     <?php if ($task == 'delete') : ?>
151         <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
152     <?php else : ?>
153         <input class="submit" type="submit" name="submit" value="Speichern"/>
154     <?php endif ?>
155     </form>
156     <?php if ($task == 'change') : ?>
157         <br/>
158         <a href="<?=html_escape(link_to('delete_expense', array('id' => $expense['id'])))?>#expense_form">Ausgabe l&ouml;schen</a>
159         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
160     <?php elseif ($task == 'delete') : ?>
161         <br/>
162         <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
163     <?php endif ?>
164     <?php
165 }/*}}}*/
166
167