Rückbuchungen von Mitgliedsbeiträgen
[memberdb.git] / include / finances.php
1 <?php
2
3
4 function finance_get_total_paid_fees() {
5     $paid = db_select_single(sprintf('SELECT SUM(value) as paid_fees FROM %1$s WHERE type IN(\'old_fee\', \'fee\') AND status=\'paid\'',
6         DB_TABLE_EARNINGS
7     ));
8     $refunded = db_select_single(sprintf('SELECT SUM(value) as refunded_fees FROM %1$s WHERE type IN(\'member_fee\') AND status=\'paid\'',
9         DB_TABLE_EXPENSES
10     ));
11
12     return bcsub($paid['paid_fees'], $refunded['refunded_fees']);
13 }
14
15
16 function finance_get_paid_fees_for_member($member_id, $old_fees = false) {
17     // $old_fees: Beiträge die nicht durch Lastschrift eingezogen werden.
18     //            Falls zu viel bezahlt, müssen diese auf die per Lastschrift
19     //            eingezogenen Beiträge angerechnet werden.
20     $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\'',
21         DB_TABLE_EARNINGS,
22         db_escape($member_id),
23         $old_fees ? 'old_fee' : 'fee'
24     ));
25
26     $paid_fees = $row['paid_fees'];
27
28     if ($old_fees) return $paid_fees;
29
30     $invoiced_fees      = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
31     $paid_invoiced_fees = finance_get_paid_fees_for_member($member_id, true);
32     if (bccomp($paid_invoiced_fees, $invoiced_fees) == 1) $paid_fees = bcadd($paid_fees, bcsub($paid_invoiced_fees, $invoiced_fees));
33
34
35     $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\'',
36         DB_TABLE_EXPENSES,
37         db_escape($member_id)
38     ));
39     return bcsub($paid_fees, $refunded['refunded_fees']);
40 }
41
42 function finance_list_paid_fees_for_member($member_id) {
43     return db_select_multi(sprintf('
44                 (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
45                 (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\'))
46                 ORDER BY `date` DESC',
47         DB_TABLE_EARNINGS,
48         DB_TABLE_EXPENSES,
49         db_escape($member_id)
50     ));
51 }
52
53 function action_finances() {/*{{{*/
54 ?>
55     <h2>Finanzen</h2>
56 <?php
57     action_create_expense();
58     action_create_earning();
59     echo '<br style="clear: left;"/>';
60     render_open_payments();
61     render_finished_payments();
62 }/*}}}*/
63