Merge branch 'master' of ssh://git.ccc-ffm.de/memberdb
[memberdb.git] / include / finances.php
index 57ac176..5731292 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)
     ));
 }
@@ -49,5 +59,6 @@ function action_finances() {/*{{{*/
     echo '<br style="clear: left;"/>';
     render_open_payments();
     render_finished_payments();
+    echo '<a href="' . link_to('export') . '">.xls runterladen</a>';
 }/*}}}*/