Merge branch 'master' of ssh://git.ccc-ffm.de/memberdb
[memberdb.git] / include / finances.php
index 179f206..5731292 100644 (file)
@@ -1,6 +1,18 @@
 <?php
 
 
+function finance_get_total_paid_fees() {
+    $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
+    ));
+
+    return bcsub($paid['paid_fees'], $refunded['refunded_fees']);
+}
+
+
 function finance_get_paid_fees_for_member($member_id, $old_fees = false) {
     // $old_fees: Beiträge die nicht durch Lastschrift eingezogen werden.
     //            Falls zu viel bezahlt, müssen diese auf die per Lastschrift
@@ -19,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)
     ));
 }
@@ -35,8 +56,9 @@ function action_finances() {/*{{{*/
 <?php
     action_create_expense();
     action_create_earning();
-       echo '<br style="clear: left;"/>';
+    echo '<br style="clear: left;"/>';
     render_open_payments();
     render_finished_payments();
+    echo '<a href="' . link_to('export') . '">.xls runterladen</a>';
 }/*}}}*/