Formatting
[memberdb.git] / include / expenses.php
index 0dbf312..e08a06b 100644 (file)
@@ -1,73 +1,64 @@
 <?php
 
-$SQL_CREATE_EXPENSES = sprintf('
-    CREATE TABLE IF NOT EXISTS `%1$s` (
-      `id` int(10) unsigned NOT NULL auto_increment,
-      `date` date NOT NULL,
-      `type` enum(\'invoice\', \'gains_tax\') NULL,
-      `status` enum(\'open\',\'paid\') NOT NULL,
-      `account` enum(\'bank\',\'cash\') NOT NULL,
-      `value` DECIMAL(10,2) NULL,
-      `member_id` int(10) unsigned NOT NULL,
-      `description` TEXT NOT NULL,
-      `created_at` datetime NOT NULL,
-      `modified_at` datetime NOT NULL,
-      PRIMARY KEY  (`id`),
-      INDEX `idx_member_id` (`member_id`),
-      INDEX `idx_date` (`date`)
-    ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_EXPENSES, DB_CHARSET);
-
 $EXPENSE_TYPES = array(
-    'invoice'   => 'Rechnung',
-    'gains_tax' => 'Kapitalertragssteuer'
+    'invoice'          => 'Rechnung',
+    'gains_tax'        => 'Kapitalertragssteuer',
+    'bankcharges'      => 'Bankgebühren',
+    'account_transfer' => 'Umbuchung',
+    'member_fee'       => 'Rückbuchung Mitgliedsbeitrag',
+    'catering'         => 'Raum - Getraenke & Snacks'
 );
 
-
-/* DB functions {{{ */
-
-function db_get_expense_with_id($expense_id) {
-    $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
-    return db_select_single(sprintf($sql, DB_TABLE_EXPENSES, db_escape($expense_id)));
+// DB functions
+function db_get_expense_with_id($expense_id)
+{
+    $sql = 'SELECT * FROM `expenses` WHERE id=? LIMIT 1';
+    return db_select_single($sql, [$expense_id]);
 }
 
-function db_create_expense($expense) {
+function db_create_expense($expense)
+{
     unset($expense['id']);
-    $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
-    $expense['value']       = ui_money2float($expense['value']);
-    $expense['created_at']  = db_unixtime2datetime(time());
+    $expense['date'] = db_unixtime2datetime(ui_date2unixtime($expense['date']));
+    $expense['value'] = ui_money2float($expense['value']);
+    $expense['created_at'] = db_unixtime2datetime(time());
     $expense['modified_at'] = db_unixtime2datetime(time());
-    return (db_insert_single(DB_TABLE_EXPENSES, $expense));
+    return (db_insert('expenses', $expense));
 }
 
-function db_change_expense($expense) {
+function db_change_expense($expense)
+{
     $id = $expense['id'];
     unset($expense['id']);
-    $expense['date']        = db_unixtime2datetime(ui_date2unixtime($expense['date']));
-    $expense['value']       = ui_money2float($expense['value']);
+    $expense['date'] = db_unixtime2datetime(ui_date2unixtime($expense['date']));
+    $expense['value'] = ui_money2float($expense['value']);
     $expense['modified_at'] = db_unixtime2datetime(time());
-    return db_update_single(DB_TABLE_EXPENSES, $expense, sprintf('id=\'%1$d\'', db_escape($id)));
+    return db_update('expenses', $expense, sprintf('id=%d', (int)$id));
 }
 
-/* }}} */
-
+function db_delete_expense($expense)
+{
+    return db_delete('expenses', 'id=?', $expense['id']);
+}
 
-function validate_expense($userdata, &$dbdata, &$validation) {/*{{{*/
+function validate_expense($userdata, &$dbdata, &$validation)
+{
     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
     $fields = array(
-        'id'              => 'number',
-        'date'            => 'date',
-        'type'            => $EXPENSE_TYPES,
-        'account'         => $ACCOUNT_TYPES,
-        'member_id'       => 'member',
-        'value'           => 'money',
-        'description'     => 'text',
-//      'status'          -  See below...
+        'id'          => 'number',
+        'date'        => 'date',
+        'type'        => $EXPENSE_TYPES,
+        'account'     => $ACCOUNT_TYPES,
+        'member_id'   => 'member',
+        'value'       => 'money',
+        'description' => 'text',
+        //'status'     -  See below...
     );
-    $orig = $dbdata;
     $dbdata['status'] = empty($userdata['status']) ? 'open' : 'paid';
     foreach ($fields as $name => $type) {
-        if (!isset($userdata[$name])) continue;
+        if (!isset($userdata[$name])) {
+            continue;
+        }
         $dbdata[$name] = $userdata[$name];
     }
 
@@ -79,13 +70,13 @@ function validate_expense($userdata, &$dbdata, &$validation) {/*{{{*/
     }
 
     return (count($validation) == 0);
-}/*}}}*/
-
+}
 
-function action_create_expense() {/*{{{*/
+function action_create_expense()
+{
     $expense = array();
-    $expense['date']      = format_date(time());
-    $expense['status']    = 'paid';
+    $expense['date'] = format_date(time());
+    $expense['status'] = 'paid';
     $log_messages = array();
     if (isset($_POST['submit'])) {
         unset($_POST['id']);
@@ -97,15 +88,19 @@ function action_create_expense() {/*{{{*/
         }
     }
     form_expense($expense, 'create', $log_messages);
-}/*}}}*/
-
+}
 
-function action_change_expense() {/*{{{*/
-    if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
+function action_change_expense()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to('finances'));
+    }
     $expense = db_get_expense_with_id($_REQUEST['id']);
+    if (!isset($expense)) {
+        redirect(link_to('finances'));
+    }
     $expense['date'] = format_date(db_date2unixtime($expense['date']));
-    $expense['value']= format_money($expense['value']);
-    if (!isset($expense)) redirect(link_to('finances'));
+    $expense['value'] = format_money($expense['value']);
     $log_messages = array();
     if (isset($_POST['submit'])) {
         if (validate_expense($_POST, $expense, $log_messages)) {
@@ -117,49 +112,69 @@ function action_change_expense() {/*{{{*/
     }
 
     form_expense($expense, 'change', $log_messages);
-}/*}}}*/
+}
 
+function action_delete_expense()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to('finances'));
+    }
+    $expense = db_get_expense_with_id($_REQUEST['id']);
+    if (!isset($expense)) {
+        redirect(link_to('finances'));
+    }
+    $expense['date'] = format_date(db_date2unixtime($expense['date']));
+    $expense['value'] = format_money($expense['value']);
+    $log_messages = array();
+    if (isset($_POST['submit'])) {
+        if (db_delete_expense($expense)) {
+            redirect(link_to('finances'));
+        }
+        echo db_error();
+    }
+    form_expense($expense, 'delete', $log_messages);
+}
 
-function form_expense($expense = array(), $task, $log_messages) {/*{{{*/
+function form_expense($expense = array(), $task, $log_messages)
+{
     global $EXPENSE_TYPES, $ACCOUNT_TYPES;
 
     $readonly = ($task == 'delete');
 
     ?>
-    <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 ?>">
-        <?php if (isset($expense['id'])) : ?><?=html_hidden_field('id', $expense)?><?php endif ?>
+    <form action="<?php echo 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 ?>">
+        <?php if (isset($expense['id'])) : ?><?php echo html_hidden_field('id', $expense) ?><?php endif ?>
         <fieldset>
-        <?php if     ($task == 'create') : ?>
-            <legend>Neue Ausgabe</legend>
-        <?php elseif ($task == 'delete') : ?>
-            <legend>Ausgabe l&ouml;schen</legend>
-        <?php elseif ($task == 'change') : ?>
-            <legend>Ausgabe &auml;ndern</legend>
-        <?php endif ?>
+            <?php if ($task == 'create') : ?>
+                <legend>Neue Ausgabe</legend>
+            <?php elseif ($task == 'delete') : ?>
+                <legend>Ausgabe l&ouml;schen</legend>
+            <?php elseif ($task == 'change') : ?>
+                <legend>Ausgabe &auml;ndern</legend>
+            <?php endif ?>
             <?php log_messages($log_messages); ?>
-            <?=html_text_field('Datum', 'date', $expense, $readonly)?>
-            <?=html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly)?>
-            <?=html_member_list_box('Mitglied', 'member_id', $expense, $readonly)?>
-            <?=html_money_field('Betrag', 'value', $expense, $readonly)?>
-            <?=html_text_field('Beschreibung', 'description', $expense, $readonly)?>
-            <?=html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly)?>
-            <?=html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly)?>
+            <?php echo html_text_field('Datum', 'date', $expense, $readonly) ?>
+            <?php echo html_list_box('Typ', 'type', $EXPENSE_TYPES, $expense, $readonly) ?>
+            <?php echo html_member_list_box('Mitglied', 'member_id', $expense, $readonly) ?>
+            <?php echo html_money_field('Betrag', 'value', $expense, $readonly) ?>
+            <?php echo html_text_field('Beschreibung', 'description', $expense, $readonly) ?>
+            <?php echo html_list_box('Quelle', 'account', $ACCOUNT_TYPES, $expense, $readonly) ?>
+            <?php echo html_checkbox('Bezahlt', 'status', $expense['status'] == 'paid', 1, $readonly) ?>
         </fieldset>
-    <?php if ($task == 'delete') : ?>
-        <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
-    <?php else : ?>
-        <input class="submit" type="submit" name="submit" value="Speichern"/>
-    <?php endif ?>
+        <?php if ($task == 'delete') : ?>
+            <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
+        <?php else : ?>
+            <input class="submit" type="submit" name="submit" value="Speichern"/>
+        <?php endif ?>
     </form>
     <?php if ($task == 'change') : ?>
-        <br/>
-        <a href="<?=html_escape(link_to('delete_expense', array('id' => $expense['id'])))?>#expense_form">Ausgabe l&ouml;schen</a>
-        <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
-    <?php elseif ($task == 'delete') : ?>
-        <br/>
-        <a href="<?=html_escape(link_to('finances'))?>#expense_form">Neue Ausgabe</a>
-    <?php endif ?>
-    <?php
-}/*}}}*/
-
-
+    <br/>
+    <a href="<?php echo html_escape(link_to('delete_expense', array('id' => $expense['id']))) ?>#expense_form">Ausgabe l&ouml;schen</a>
+    <a href="<?php echo html_escape(link_to('finances')) ?>#expense_form">Neue Ausgabe</a>
+<?php elseif ($task == 'delete') : ?>
+    <br/>
+    <a href="<?php echo html_escape(link_to('finances')) ?>#expense_form">Neue Ausgabe</a>
+<?php
+endif;
+}