Excel-Export
[memberdb.git] / include / fees.php
index 4c1f6e2..c168a66 100644 (file)
@@ -77,7 +77,7 @@ function fees_get_list_for_member($member_id, $end_date) {
 
     static $cache = array();
 
-    $end_date = db_date2unixtime(db_unixtime2date($end_date)); // remove hours, minutes, seconds
+    $end_date = mktime(0, 0, 0, date('m', $end_date) + 1, 0, date('Y', $end_date)); // last day of given month
 
     if (isset($cache[$member_id][$end_date])) return $cache[$member_id][$end_date];
     if (isset($cache[$member_id])) {
@@ -122,6 +122,13 @@ function fees_for_member_at_date($member_id, $end_date) {
     return NULL;
 }
 
+function fees_info_for_member($member_id, $end_date) {
+    $membership_info = fees_get_list_for_member($member_id, $end_date);
+    
+    $this_year = array_pop($membership_info);
+    return array_pop($this_year);
+}
+
 function fees_sum_by_month($end_date) {
     $members = db_get_members();
     $fees = array();
@@ -153,19 +160,20 @@ function fees_get_list_for_month($year, $month) {
 
 function fee_next_directdebit_for_member($member_id, $max_date = NULL) {
 
-       $member = db_get_member_with_id($member_id);
-       if (!$member['directdebit']) return;
+    $member = db_get_member_with_id($member_id);
+    if (!$member['directdebit']) return NULL;
 
     $sum_old_fees   = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
     $sum_new_paid   = finance_get_paid_fees_for_member($member_id);
     $year   = date('Y', DIRECTDEBIT_DATE);
     $month  = date('n', DIRECTDEBIT_DATE);
+    $day    = 1;
    
 
 
     while (true) {
-        $start_date = mktime(0, 0, 0, $month, 1, $year);
-        if (isset($max_date) && $start_date > $max_date) return;
+        $start_date = mktime(0, 0, 0, $month, $day, $year);
+        if (isset($max_date) && $start_date > $max_date) return NULL;
 
         // check if fee is zero at the moment and skip to next event
         // quit searching if theres no event in future
@@ -174,6 +182,7 @@ function fee_next_directdebit_for_member($member_id, $max_date = NULL) {
             $events = db_get_events_for_member($member_id, $start_date + 86400);
             if (empty($events)) return NULL;
             $start_date = db_date2unixtime($events[0]['event_date']);
+            $day   = date('j', $start_date);
             $month = date('n', $start_date);
             $year  = date('Y', $start_date);
             continue;
@@ -205,6 +214,7 @@ function fee_next_directdebit_for_member($member_id, $max_date = NULL) {
             $ret['info'] = dtaus_string(sprintf('CCCFFM %d, %s-%s', $member['number'], format_month($start_date), format_month($end_date)));
             return $ret;
         }
+        $day = 1;
         $month++;
         if ($month == 13) { $month = 1; $year++; }
     }
@@ -228,6 +238,7 @@ function action_fees() {/*{{{*/
     render_fees_by_member();
     render_accrued_fees_by_month();
     render_next_direct_debit();
+    render_future_fees();
 
 }/*}}}*/
 
@@ -246,16 +257,19 @@ function render_fees_by_member() {/*{{{*/
 <?php if (empty($members)) $members = array(); ?>
 <?php foreach ($members as $member) : ?>
 <?php
-       $total_fees = fees_sum_for_member($member['id'], time());
-       $current_fee = fees_for_member_at_date($member['id'], time());
-       $old_paid_fees = finance_get_paid_fees_for_member($member['id'], true);
-       $new_paid_fees = finance_get_paid_fees_for_member($member['id']);
-       $open_fees = bcsub(bcsub($total_fees, $old_paid_fees), $new_paid_fees);
+    $current_fee  = fees_for_member_at_date($member['id'], time());
+
+    $sum_fees     = fees_sum_for_member($member['id'], time());
+    $sum_old_fees = fees_sum_for_member($member['id'], DIRECTDEBIT_DATE - 86400);
+    $sum_old_paid = finance_get_paid_fees_for_member($member['id'], true);
+    $sum_new_paid = finance_get_paid_fees_for_member($member['id']);
+    $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
+    $open_fees    = bcadd(bcsub($sum_old_fees, $sum_old_paid), max(bcsub($sum_new_fees, $sum_new_paid), 0));
 ?>
     <tr>
         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
         <td><?=html_escape($member['nickname'])?></td>
-        <td style="text-align: right;"><?=format_money($total_fees)?></td>
+        <td style="text-align: right;"><?=format_money($sum_fees)?></td>
         <td style="text-align: right;"><?=isset($current_fee) ? format_money($current_fee) : '-' ?></td>
         <td style="text-align: right;"><?=$open_fees > 0 ? format_money($open_fees) : '-' ?></td>
     </tr>
@@ -264,6 +278,53 @@ function render_fees_by_member() {/*{{{*/
 <?php
 }/*}}}*/
 
+function render_future_fees() {/*{{{*/
+    $total_paid = finance_get_total_paid_fees();
+    $this_year = date('Y');
+    $this_month = date('m');
+    $fees = fees_sum_by_month(mktime(0, 0, 0, date('m') + 6,   date('d'),   date('Y')+1));
+    $total = 0;
+    foreach ($fees as $year => $months) {
+        foreach ($months as $month => $fee) {
+            $total = bcadd($total, $fee);
+            $fees[$year][$month] = array('total' => $total, 'fee' => $fee);
+        }
+    }
+    $fees = array_reverse($fees, true);
+?>
+<h2>Beitragsprognose nach Monat</h2>
+<table>
+    <tr>
+        <th>Monat</th>
+        <th style="text-align: right;">Mitgliedsbeitr&auml;ge</th>
+        <th style="text-align: right;">kummuliert</th>
+        <th style="text-align: right;">eingenommen</th>
+        <th style="text-align: right;"><strong>offen</strong></th>
+    </tr>
+<?php foreach ($fees as $year => $months) : ?>
+    <?php $months = array_reverse($months, true); ?>
+    <?php foreach ($months as $month => $data) : ?>
+    <tr<?php if ($year == $this_year && $month == $this_month) : ?> class="current"<?php endif?>>
+        <td><a href="<?=html_escape(link_to('fees', array('year' => $year, 'month'=> $month)))?>"><?=html_escape(format_month($year, $month))?></a></td>
+        <td style="text-align: right;"><?=html_escape(format_money($data['fee']))?></td>
+        <td style="text-align: right;"><?=html_escape(format_money($data['total']))?></td>
+        <td style="text-align: right;">
+        <?php if ($year == $this_year && $month == $this_month) : ?>
+            <?=html_escape(format_money($total_paid))?>
+        <?php endif ?>
+        </td>
+        <td style="text-align: right;">
+        <?php if ($year > $this_year || ($year >= $this_year && $month >= $this_month)) : ?>
+            <?=html_escape(format_money(bcsub($data['total'], $total_paid)))?>
+        <?php endif ?>
+        </td>
+    </tr>
+    <?php endforeach ?>
+<?php endforeach ?>
+</table>
+<?php
+}/*}}}*/
+
 function render_accrued_fees_by_month() {/*{{{*/
     $fees = fees_sum_by_month(time());
     $fees = array_reverse($fees, true);
@@ -340,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'));
@@ -348,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);
@@ -446,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 ?>