Formatting
[memberdb.git] / include / earnings.php
index e01627f..d792760 100644 (file)
@@ -1,76 +1,66 @@
 <?php
 
-$SQL_CREATE_EARNINGS = sprintf('
-    CREATE TABLE IF NOT EXISTS `%1$s` (
-      `id` int(10) unsigned NOT NULL auto_increment,
-      `date` date NOT NULL,
-      `type` enum(\'donation\', \'fee\', \'old_fee\', \'interest\', \'account_transfer\') 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_EARNINGS, DB_CHARSET);
+use MemberDB\Config\Config;
 
 $EARNING_TYPES = array(
-    'fee'       => 'Mitgliedsbeitrag',
-    'donation'  => 'Spende',
-    'interest'  => 'Zinsen',
-    'account_transfer'  => 'Umbuchung',
-    'old_fee'   => 'MitgliedsbeitrÃĪge vor dem ' . format_date(DIRECTDEBIT_DATE)
+    'fee'              => 'Mitgliedsbeitrag',
+    'donation'         => 'Spende',
+    'interest'         => 'Zinsen',
+    'room'             => 'Raumeinnahme',
+    'account_transfer' => 'Umbuchung',
+    'old_fee'          => 'MitgliedsbeitrÃĪge vor dem ' . format_date(Config::getInstance()->get('direct_debit')['date']->getTimestamp()),
+    'catering'         => 'Raum - Getraenke & Snacks'
 );
 
-
-/* DB functions {{{ */
-
-function db_get_earning_with_id($earning_id) {
-    $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
-    return db_select_single(sprintf($sql, DB_TABLE_EARNINGS, db_escape($earning_id)));
+// DB functions
+function db_get_earning_with_id($earning_id)
+{
+    return db_select_single('SELECT * FROM `earnings` WHERE id=? LIMIT 1', [$earning_id]);
 }
 
-function db_create_earning($earning) {
+function db_create_earning($earning)
+{
     unset($earning['id']);
-    $earning['date']        = db_unixtime2datetime(ui_date2unixtime($earning['date']));
-    $earning['value']       = ui_money2float($earning['value']);
-    $earning['created_at']  = db_unixtime2datetime(time());
+    $earning['date'] = db_unixtime2datetime(ui_date2unixtime($earning['date']));
+    $earning['value'] = ui_money2float($earning['value']);
+    $earning['created_at'] = db_unixtime2datetime(time());
     $earning['modified_at'] = db_unixtime2datetime(time());
-    return (db_insert_single(DB_TABLE_EARNINGS, $earning));
+    return (db_insert('earnings', $earning));
 }
 
-function db_change_earning($earning) {
+function db_change_earning($earning)
+{
     $id = $earning['id'];
     unset($earning['id']);
-    $earning['date']        = db_unixtime2datetime(ui_date2unixtime($earning['date']));
-    $earning['value']       = ui_money2float($earning['value']);
+    $earning['date'] = db_unixtime2datetime(ui_date2unixtime($earning['date']));
+    $earning['value'] = ui_money2float($earning['value']);
     $earning['modified_at'] = db_unixtime2datetime(time());
-    return db_update_single(DB_TABLE_EARNINGS, $earning, sprintf('id=\'%1$d\'', db_escape($id)));
+    return db_update('earnings', $earning, sprintf("id='%d'", (int)$id));
 }
 
-/* }}} */
-
+function db_delete_earning($earning)
+{
+    return db_delete('earnings', 'id=?', [$earning['id']]);
+}
 
-function validate_earning($userdata, &$dbdata, &$validation) {/*{{{*/
+function validate_earning($userdata, &$dbdata, &$validation)
+{
     global $EARNING_TYPES, $ACCOUNT_TYPES;
     $fields = array(
-        'id'              => 'number',
-        'date'            => 'date',
-        'type'            => $EARNING_TYPES,
-        'account'         => $ACCOUNT_TYPES,
-        'member_id'       => 'member',
-        'value'           => 'money',
-        'description'     => 'text',
-//      'status'          -  See below...
+        'id'          => 'number',
+        'date'        => 'date',
+        'type'        => $EARNING_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];
     }
 
@@ -82,13 +72,13 @@ function validate_earning($userdata, &$dbdata, &$validation) {/*{{{*/
     }
 
     return (count($validation) == 0);
-}/*}}}*/
-
+}
 
-function action_create_earning() {/*{{{*/
+function action_create_earning()
+{
     $earning = array();
-    $earning['date']      = format_date(time());
-    $earning['status']    = 'paid';
+    $earning['date'] = format_date(time());
+    $earning['status'] = 'paid';
     $log_messages = array();
     if (isset($_POST['submit'])) {
         unset($_POST['id']);
@@ -100,15 +90,19 @@ function action_create_earning() {/*{{{*/
         }
     }
     form_earning($earning, 'create', $log_messages);
-}/*}}}*/
-
+}
 
-function action_change_earning() {/*{{{*/
-    if (!isset($_REQUEST['id'])) redirect(link_to('finances'));
+function action_change_earning()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to('finances'));
+    }
     $earning = db_get_earning_with_id($_REQUEST['id']);
+    if (!isset($earning)) {
+        redirect(link_to('finances'));
+    }
     $earning['date'] = format_date(db_date2unixtime($earning['date']));
-    $earning['value']= format_money($earning['value']);
-    if (!isset($earning)) redirect(link_to('finances'));
+    $earning['value'] = format_money($earning['value']);
     $log_messages = array();
     if (isset($_POST['submit'])) {
         if (validate_earning($_POST, $earning, $log_messages)) {
@@ -120,57 +114,82 @@ function action_change_earning() {/*{{{*/
     }
 
     form_earning($earning, 'change', $log_messages);
-}/*}}}*/
+}
 
+function action_delete_earning()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to('finances'));
+    }
+    $earning = db_get_earning_with_id($_REQUEST['id']);
+    if (!isset($earning)) {
+        redirect(link_to('finances'));
+    }
+    $earning['date'] = format_date(db_date2unixtime($earning['date']));
+    $earning['value'] = format_money($earning['value']);
+    $log_messages = array();
+    if (isset($_POST['submit'])) {
+        if (db_delete_earning($earning)) {
+            redirect(link_to('finances'));
+        }
+        echo db_error();
+    }
+    form_earning($earning, 'delete', $log_messages);
+}
 
-function form_earning($earning = array(), $task, $log_messages) {/*{{{*/
+function form_earning($earning = array(), $task, $log_messages)
+{
     global $EARNING_TYPES, $ACCOUNT_TYPES;
 
     $readonly = ($task == 'delete');
 
     ?>
-    <form action="<?=link_to($task . '_earning')?>" method="post" class="clearfix" id="earning_form" style="<?php if ($_REQUEST['action'] == 'finances') : ?>float: left<?php else :?>margin-top: 2em<?php endif ?>">
-        <?php if (isset($earning['id'])) : ?><?=html_hidden_field('id', $earning)?><?php endif ?>
+    <form action="<?php echo link_to($task . '_earning') ?>" method="post" class="clearfix" id="earning_form"
+          style="<?php if ($_REQUEST['action'] == 'finances') : ?>float: left<?php else : ?>margin-top: 2em<?php endif ?>">
+        <?php if (isset($earning['id'])) : ?><?php echo html_hidden_field('id', $earning) ?><?php endif ?>
         <fieldset>
-        <?php if     ($task == 'create') : ?>
-            <legend>Neue Einnahme</legend>
-        <?php elseif ($task == 'delete') : ?>
-            <legend>Einnahme l&ouml;schen</legend>
-        <?php elseif ($task == 'change') : ?>
-            <legend>Einnahme &auml;ndern</legend>
-        <?php endif ?>
+            <?php if ($task == 'create') : ?>
+                <legend>Neue Einnahme</legend>
+            <?php elseif ($task == 'delete') : ?>
+                <legend>Einnahme l&ouml;schen</legend>
+            <?php elseif ($task == 'change') : ?>
+                <legend>Einnahme &auml;ndern</legend>
+            <?php endif ?>
             <?php log_messages($log_messages); ?>
-            <?=html_text_field('Datum', 'date', $earning, $readonly)?>
-            <?=html_list_box('Typ', 'type', $EARNING_TYPES, $earning, $readonly)?>
-            <?=html_member_list_box('Mitglied', 'member_id', $earning, $readonly)?>
-            <?=html_money_field('Betrag', 'value', $earning, $readonly)?>
-            <?=html_text_field('Beschreibung', 'description', $earning, $readonly)?>
-            <?=html_list_box('Ziel', 'account', $ACCOUNT_TYPES, $earning, $readonly)?>
-            <?=html_checkbox('Bezahlt', 'status', $earning['status'] == 'paid', 1, $readonly)?>
+            <?php echo html_text_field('Datum', 'date', $earning, $readonly) ?>
+            <?php echo html_list_box('Typ', 'type', $EARNING_TYPES, $earning, $readonly) ?>
+            <?php echo html_member_list_box('Mitglied', 'member_id', $earning, $readonly) ?>
+            <?php echo html_money_field('Betrag', 'value', $earning, $readonly) ?>
+            <?php echo html_text_field('Beschreibung', 'description', $earning, $readonly) ?>
+            <?php echo html_list_box('Ziel', 'account', $ACCOUNT_TYPES, $earning, $readonly) ?>
+            <?php echo html_checkbox('Bezahlt', 'status', $earning['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_earning', array('id' => $earning['id'])))?>">Einnahme l&ouml;schen</a>
-        <a href="<?=html_escape(link_to('finances'))?>">Neue Einnahme</a>
-    <?php elseif ($task == 'delete') : ?>
-        <br/>
-        <a href="<?=html_escape(link_to('finances'))?>">Neue Einnahme</a>
-    <?php endif ?>
+    <br/>
+    <a href="<?php echo html_escape(link_to('delete_earning', array('id' => $earning['id']))) ?>">Einnahme l&ouml;schen</a>
+    <a href="<?php echo html_escape(link_to('finances')) ?>">Neue Einnahme</a>
+<?php elseif ($task == 'delete') : ?>
+    <br/>
+    <a href="<?php echo html_escape(link_to('finances')) ?>">Neue Einnahme</a>
+<?php endif ?>
     <?php
-}/*}}}*/
-
+}
 
-function action_create_earnings() {/*{{{*/
-    if (empty($_POST['earnings']) || !is_array($_POST['earnings'])) redirect(link_to('finances'));
+function action_create_earnings()
+{
+    if (empty($_POST['earnings']) || !is_array($_POST['earnings'])) {
+        redirect(link_to('finances'));
+    }
     db_begin();
     foreach ($_POST['earnings'] as $post_data) {
         $log_messages = array();
+
         unset($post_data['id']);
         $earning = array(
             'account' => 'bank'
@@ -181,8 +200,7 @@ function action_create_earnings() {/*{{{*/
                 db_rollback();
                 return;
             }
-        }
-        else {
+        } else {
             db_rollback();
             echo '<h2>Verbuchen von Einnahmen</h2><p>Beim Verbuchen der Einnahmen ist ein Fehler aufgetreten:</p>';
             log_messages($log_messages);
@@ -190,6 +208,17 @@ function action_create_earnings() {/*{{{*/
         }
     }
     db_commit();
-    redirect(link_to('finances'));
-}/*}}}*/
 
+    // SET directdebit from FRST to RCUR
+    foreach ($_POST['earnings'] as $post_data) {
+        $member['modified_at'] = db_unixtime2datetime(time());
+        $member['directdebit'] = 3; // RCUR
+        db_update(
+            'members',
+            $member,
+            sprintf("directdebit=2 AND id='%d'", (int)$post_data['member_id'])
+        );
+    }
+
+    redirect(link_to('finances'));
+}