Rückbuchungen von Mitgliedsbeiträgen
authorPascal Hofmann <pascal@bigmac.local>
Mon, 31 Oct 2011 12:13:17 +0000 (13:13 +0100)
committerPascal Hofmann <pascal@bigmac.local>
Mon, 31 Oct 2011 12:13:17 +0000 (13:13 +0100)
include/expenses.php
include/fees.php
include/finances.php
include/payments.php

index dcbd851..e014a93 100644 (file)
@@ -4,7 +4,7 @@ $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\', \'bankcharges\', \'account_transfer\') NULL,
+      `type` enum(\'invoice\', \'gains_tax\', \'bankcharges\', \'account_transfer\', \'member_fee\') NULL,
       `status` enum(\'open\',\'paid\') NOT NULL,
       `account` enum(\'bank\',\'cash\') NOT NULL,
       `value` DECIMAL(10,2) NULL,
@@ -22,7 +22,8 @@ $EXPENSE_TYPES = array(
     'invoice'   => 'Rechnung',
     'gains_tax' => 'Kapitalertragssteuer',
     'bankcharges' => 'Bankgebühren',
-    'account_transfer'  => 'Umbuchung'
+    'account_transfer'  => 'Umbuchung',
+    'member_fee' => 'Rückbuchung Mitgliedsbeitrag'
 );
 
 
index 581686b..c168a66 100644 (file)
@@ -401,7 +401,7 @@ function render_next_direct_debit() {/*{{{*/
 }
 
 function render_fees_for_member($member_id) {/*{{{*/
-    global $MEMBER_TYPES, $EARNING_TYPES;
+    global $MEMBER_TYPES, $EARNING_TYPES, $EXPENSE_TYPES;
 
     $member      = db_get_member_with_id($member_id);
     if (!isset($member)) redirect(link_to('fees'));
@@ -409,7 +409,7 @@ function render_fees_for_member($member_id) {/*{{{*/
     $membership_info = fees_get_list_for_member($member_id, time());
     $membership_info = array_reverse($membership_info, true);
 
-    $paid_fees = array_reverse(finance_list_paid_fees_for_member($member_id, time(), true));
+    $paid_fees = finance_list_paid_fees_for_member($member_id, time(), true);
 
     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
     $sum_old_paid = finance_get_paid_fees_for_member($member_id, true);
@@ -507,7 +507,7 @@ function render_fees_for_member($member_id) {/*{{{*/
 <?php foreach ($paid_fees as $payment) : ?>
     <tr>
         <td><?=html_escape(format_date(db_date2unixtime($payment['date'])))?></td>
-        <td><?=$EARNING_TYPES[$payment['type']]?></td>
+        <td><?=($payment['value'] < 0) ? $EXPENSE_TYPES[$payment['type']]: $EARNING_TYPES[$payment['type']]?></td>
         <td style="text-align: right;"><?=format_money($payment['value'])?></td>
     </tr>
 <?php endforeach ?>
index 57ac176..4d5e4a9 100644 (file)
@@ -2,13 +2,14 @@
 
 
 function finance_get_total_paid_fees() {
-    $row = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE type IN(\'old_fee\', \'fee\') AND status=\'paid\'',
+    $paid = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE type IN(\'old_fee\', \'fee\') AND status=\'paid\'',
         DB_TABLE_EARNINGS
     ));
+    $refunded = db_select_single(sprintf('SELECT SUM(value) as refunded_fees FROM %1$s WHERE type IN(\'member_fee\') AND status=\'paid\'',
+        DB_TABLE_EXPENSES
+    ));
 
-    $paid_fees = $row['paid_fees'];
-
-    return $paid_fees;
+    return bcsub($paid['paid_fees'], $refunded['refunded_fees']);
 }
 
 
@@ -30,12 +31,21 @@ function finance_get_paid_fees_for_member($member_id, $old_fees = false) {
     $paid_invoiced_fees = finance_get_paid_fees_for_member($member_id, true);
     if (bccomp($paid_invoiced_fees, $invoiced_fees) == 1) $paid_fees = bcadd($paid_fees, bcsub($paid_invoiced_fees, $invoiced_fees));
 
-    return $paid_fees;
+
+    $refunded = db_select_single(sprintf('SELECT SUM(value) as refunded_fees FROM %1$s WHERE member_id=\'%2$d\' AND type IN(\'member_fee\') AND status=\'paid\'',
+        DB_TABLE_EXPENSES,
+        db_escape($member_id)
+    ));
+    return bcsub($paid_fees, $refunded['refunded_fees']);
 }
 
 function finance_list_paid_fees_for_member($member_id) {
-    return db_select_multi(sprintf('SELECT * FROM %1$s WHERE member_id=\'%2$d\' ORDER BY `date` ASC',
+    return db_select_multi(sprintf('
+               (SELECT id, date, type, status, account, value, member_id, description, created_at, modified_at FROM %1$s WHERE member_id=\'%3$d\' AND type IN(\'old_fee\', \'fee\')) UNION
+               (SELECT id, date, type, status, account, value*-1, member_id, description, created_at, modified_at FROM %2$s WHERE member_id=\'%3$d\' AND type IN(\'member_fee\'))
+               ORDER BY `date` DESC',
         DB_TABLE_EARNINGS,
+        DB_TABLE_EXPENSES,
         db_escape($member_id)
     ));
 }
index 3ca6c2c..1c8d0dd 100644 (file)
@@ -24,7 +24,7 @@ function db_get_finished_payments($offset, $count) {
     $cash  = 0;
     $total = 0;
     if ($count != 0) { // XXX TODO hä? - An dieser Stelle gäbe es sinnigere Bedinungen
-        $limit = sprintf(' LIMIT %d,18446744073709551615', (int)($count + $offset));
+        $limit = sprintf(' LIMIT %d,18446744073709551615', (int)($count + $offset)); // SELECT mit Offset aber ohne Limit
         $sql = 'SELECT SUM(IF(`account`=\'bank\', `value`, 0)) as `bank`, SUM(IF(`account`=\'cash\', `value`, 0)) as `cash`, SUM(`value`) AS `total` 
                 FROM (
                     SELECT `value`, `account`