Formatting
[memberdb.git] / include / finances.php
index 5731292..77496ef 100644 (file)
@@ -1,64 +1,67 @@
 <?php
 
+use MemberDB\Config\Config;
 
-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
-    ));
+function finance_get_total_paid_fees()
+{
+    $paid = db_select_single("SELECT SUM(value) AS paid_fees FROM `earnings` WHERE type IN('old_fee', 'fee') AND `status`='paid'");
+    $refunded = db_select_single("SELECT SUM(value) AS refunded_fees FROM `expenses` WHERE type IN('member_fee') AND `status`='paid'");
 
     return bcsub($paid['paid_fees'], $refunded['refunded_fees']);
 }
 
-
-function finance_get_paid_fees_for_member($member_id, $old_fees = false) {
+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
     //            eingezogenen Beiträge angerechnet werden.
-    $row = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE member_id=\'%2$d\' AND type=\'%3$s\' AND status=\'paid\'',
-        DB_TABLE_EARNINGS,
-        db_escape($member_id),
-        $old_fees ? 'old_fee' : 'fee'
-    ));
+    $row = db_select_single(
+        'SELECT SUM(value) AS paid_fees FROM `earnings` WHERE member_id=? AND type=? AND `status`=\'paid\'',
+        [$member_id, $old_fees ? 'old_fee' : 'fee']
+    );
 
     $paid_fees = $row['paid_fees'];
 
-    if ($old_fees) return $paid_fees;
+    if ($old_fees) {
+        return $paid_fees;
+    }
 
-    $invoiced_fees      = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
+    /** @var DateTime $direct_debit */
+    $direct_debit = Config::getInstance()->get('direct_debit')['date'];
+    $invoiced_fees = fees_sum_for_member($member_id, $direct_debit->getTimestamp() - 86400);
     $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));
+    if (bccomp($paid_invoiced_fees, $invoiced_fees) == 1) {
+        $paid_fees = bcadd($paid_fees, bcsub($paid_invoiced_fees, $invoiced_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)
-    ));
+    $refunded = db_select_single(
+        "SELECT SUM(value) AS refunded_fees FROM `expenses` WHERE member_id=? AND type IN('member_fee') AND `status`='paid'",
+        [$member_id]
+    );
     return bcsub($paid_fees, $refunded['refunded_fees']);
 }
 
-function finance_list_paid_fees_for_member($member_id) {
-    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)
-    ));
+function finance_list_paid_fees_for_member($member_id)
+{
+    return db_select_multi("
+               (SELECT id, date, type, status, account, value, member_id, description, created_at, modified_at FROM `earnings` WHERE member_id=? AND type IN('old_fee', 'fee'))
+               UNION
+               (SELECT id, date, type, status, account, value*-1, member_id, description, created_at, modified_at FROM `expenses` WHERE member_id=? AND type IN('member_fee'))
+               ORDER BY `date` DESC",
+        [$member_id, $member_id]
+    );
 }
 
-function action_finances() {/*{{{*/
-?>
+function action_finances()
+{
+    ?>
     <h2>Finanzen</h2>
-<?php
+    <?php
     action_create_expense();
     action_create_earning();
     echo '<br style="clear: left;"/>';
     render_open_payments();
     render_finished_payments();
     echo '<a href="' . link_to('export') . '">.xls runterladen</a>';
-}/*}}}*/
-
+}