Fehler bei der Berechnung der offenen Beiträge in der Liste der Beträge nach Mitglied
[memberdb.git] / include / fees.php
1 <?php
2
3
4 /* HELPER functions FOR FEES {{{ */
5
6 // build an empty structure for holding monthly information
7 function _fees_build_month_array($start_date, $end_date, $preset = array()) {
8     $start_info = getdate($start_date);
9     $end_info   = getdate($end_date);
10
11     if ($start_info['year'] > $end_info['year']) return $preset;
12     if ($start_info['year'] == $end_info['year'] && $start_info['mon'] > $end_info['mon']) return $preset;
13
14     $ret = $preset;
15     for ($year = $start_info['year']; $year <= $end_info['year']; $year++) {
16         if (!isset($ret[$year])) $ret[$year] = array();
17         for (
18             $month  = (($year == $start_info['year']) ? $start_info['mon'] :  1);
19             $month <= (($year == $end_info['year']  ) ? $end_info['mon']   : 12);
20             $month++
21         ) {
22             if (isset($ret[$year][$month])) continue;
23             $ret[$year][$month] = array(
24                 'is_member'         => NULL,
25                 'member_type'       => NULL,
26                 'fee'               => NULL,
27                 'payment_interval'  => NULL
28             );
29         }
30     }
31     return $ret;
32 }
33
34 function _fees_apply_event_information(&$info, $events) {
35     if (!empty($events)) {
36
37         foreach ($events as $event) {
38
39             $timestamp = db_date2unixtime($event['event_date']);
40             if ($timestamp < FOUNDING_DATE) $timestamp = FOUNDING_DATE;
41             $date_info = getdate($timestamp);
42
43             if (!isset($info[$date_info['year']])) continue;
44             if (!isset($info[$date_info['year']][$date_info['mon']])) continue;
45
46             if ($event['fee']              !== NULL)      $info[$date_info['year']][$date_info['mon']]['fee']              = $event['fee'];
47             if ($event['member_type']      !== NULL)      $info[$date_info['year']][$date_info['mon']]['member_type']      = $event['member_type'];
48             if ($event['event_type']       !== 'changed') $info[$date_info['year']][$date_info['mon']]['is_member']        = ($event['event_type'] == 'joined' ? 1 : 0);
49             if ($event['payment_interval'] !== NULL)      $info[$date_info['year']][$date_info['mon']]['payment_interval'] = $event['payment_interval'];
50         }
51     }
52     
53     $fee              = 0;
54     $member_type      = NULL;
55     $is_member        = false;
56     $payment_interval = NULL;
57
58     foreach (array_keys($info) as $year) {
59         foreach (array_keys($info[$year]) as $month) {
60             if (!isset($info[$year][$month]['fee'])) $info[$year][$month]['fee'] = $fee;
61             else $fee = $info[$year][$month]['fee'];
62
63             if (!isset($info[$year][$month]['member_type'])) $info[$year][$month]['member_type'] = $member_type;
64             else $member_type = $info[$year][$month]['member_type'];
65
66             if (!isset($info[$year][$month]['is_member'])) $info[$year][$month]['is_member'] = $is_member;
67             else $is_member = $info[$year][$month]['is_member'];
68
69             if (!isset($info[$year][$month]['payment_interval'])) $info[$year][$month]['payment_interval'] = $payment_interval;
70             else $payment_interval = $info[$year][$month]['payment_interval'];
71         }
72     }
73     return;
74 }
75
76 function fees_get_list_for_member($member_id, $end_date) {
77
78     static $cache = array();
79
80     $end_date = db_date2unixtime(db_unixtime2date($end_date)); // remove hours, minutes, seconds
81
82     if (isset($cache[$member_id][$end_date])) return $cache[$member_id][$end_date];
83     if (isset($cache[$member_id])) {
84         foreach (array_reverse(array_keys($cache[$member_id])) as $cache_date) {
85             if ($cache_date <= $end_date) {
86                 $ret = _fees_build_month_array($cache_date /* XXX einen Monat später wäre an dieser Stelle richtiger*/, $end_date, $cache[$member_id][$cache_date]);
87                 _fees_apply_event_information($ret, db_get_events_for_member($member_id, $cache_date, $end_date));
88                 $cache[$member_id][$end_date] = $ret;
89                 return $ret;
90             }
91         }
92     }
93
94     $ret = _fees_build_month_array(FOUNDING_DATE, $end_date);
95     if (empty($ret)) return;
96
97     _fees_apply_event_information($ret, db_get_events_for_member($member_id));
98
99     $cache[$member_id][$end_date] = $ret;
100     return $ret;
101 }
102
103 function fees_sum_for_member($member_id, $end_date) {
104     $membership_info = fees_get_list_for_member($member_id, $end_date);
105     
106     $total = '0';
107
108     foreach ($membership_info as $year => $months) {
109         foreach ($months as $month => $info) {
110             if ($info['is_member']) $total = bcadd($total, $info['fee']);
111         }
112     }
113     return $total;
114 }
115
116 function fees_for_member_at_date($member_id, $end_date) {
117     $membership_info = fees_get_list_for_member($member_id, $end_date);
118     
119     $this_year = array_pop($membership_info);
120     $this_month = array_pop($this_year);
121     if ($this_month['is_member']) return $this_month['fee'];
122     return NULL;
123 }
124
125 function fees_sum_by_month($end_date) {
126     $members = db_get_members();
127     $fees = array();
128     if (empty($members)) return array();
129     foreach ($members as $member) {
130         $membership_info = fees_get_list_for_member($member['id'], $end_date);
131         foreach ($membership_info as $year => $months) {
132             foreach ($months as $month => $info) {
133                 if (!isset($fees[$year][$month])) $fees[$year][$month] = '0';
134                 if ($info['is_member']) $fees[$year][$month] = bcadd($fees[$year][$month], $info['fee']);
135             }
136         }
137     }
138     return $fees;
139 }
140 function fees_get_list_for_month($year, $month) {
141     $members = db_get_members();
142     $fees = array();
143     foreach ($members as $member) {
144         $membership_info = fees_get_list_for_member($member['id'], mktime(0, 0, 0, $month, 1, $year));
145
146         if (empty($membership_info)) continue;
147         $member['fee']       = $membership_info[$year][$month]['fee'];
148         $member['is_member'] = $membership_info[$year][$month]['is_member'];
149         $fees[] = $member;
150     }
151     return $fees;
152 }
153
154 function fee_next_directdebit_for_member($member_id, $max_date = NULL) {
155
156     $member = db_get_member_with_id($member_id);
157     if (!$member['directdebit']) return;
158
159     $sum_old_fees   = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
160     $sum_new_paid   = finance_get_paid_fees_for_member($member_id);
161     $year   = date('Y', DIRECTDEBIT_DATE);
162     $month  = date('n', DIRECTDEBIT_DATE);
163    
164
165
166     while (true) {
167         $start_date = mktime(0, 0, 0, $month, 1, $year);
168         if (isset($max_date) && $start_date > $max_date) return;
169
170         // check if fee is zero at the moment and skip to next event
171         // quit searching if theres no event in future
172         $current_fee = fees_for_member_at_date($member_id, $start_date);
173         if (empty($current_fee)) {
174             $events = db_get_events_for_member($member_id, $start_date + 86400);
175             if (empty($events)) return NULL;
176             $start_date = db_date2unixtime($events[0]['event_date']);
177             $month = date('n', $start_date);
178             $year  = date('Y', $start_date);
179             continue;
180         }
181
182         $sum_fees       = fees_sum_for_member($member_id, $start_date);
183         $sum_new_fees   = bcsub($sum_fees, $sum_old_fees);
184         if (bccomp($sum_new_fees, $sum_new_paid) == 1) {
185             $info = fees_get_list_for_member($member_id, $start_date);
186             $months = 1;
187             $ret = array(
188                 'date'  => $start_date,
189                 'value' => bcsub($sum_new_fees, $sum_new_paid),
190                 'info'  => '',
191             );
192             switch ($info[$year][$month]['payment_interval']) {
193                 case 'monthly'   : $months = 1;  break;
194                 case 'quarterly' : $months = 3;  break;
195                 case 'halfyearly': $months = 6;  break;
196                 case 'yearly'    : $months = 12; break;
197             }
198             if ($months == 1) {
199                 $ret['info'] = dtaus_string(sprintf('CCCFFM %d, %s', $member['number'], format_month($start_date))); 
200                 return $ret;
201             }
202             $end_date = mktime(0, 0, 0, $month + $months - 1, 1, $year);
203             $sum_fee_end = fees_sum_for_member($member_id, $end_date);
204             $ret['value'] = bcadd($ret['value'], bcsub($sum_fee_end, $sum_fees));
205             $ret['info'] = dtaus_string(sprintf('CCCFFM %d, %s-%s', $member['number'], format_month($start_date), format_month($end_date)));
206             return $ret;
207         }
208         $month++;
209         if ($month == 13) { $month = 1; $year++; }
210     }
211     
212     
213 }
214 /* }}} */
215
216
217 function action_fees() {/*{{{*/
218
219     if (isset($_REQUEST['member_id'])) {
220         render_fees_for_member($_REQUEST['member_id']);
221         return;
222     }
223     if (isset($_REQUEST['year']) && isset($_REQUEST['month'])) {
224         render_accrued_fees_for_month($_REQUEST['year'], $_REQUEST['month']);
225         return;
226     }
227
228     render_fees_by_member();
229     render_accrued_fees_by_month();
230     render_next_direct_debit();
231
232 }/*}}}*/
233
234 function render_fees_by_member() {/*{{{*/
235     $members = db_get_members();
236 ?>
237 <h2>Mitgliedsbeitr&auml;ge nach Mitglied</h2>
238 <table>
239     <tr>
240         <th>Mitgliedsnummer</th>
241         <th>Nickname</th>
242         <th style="text-align: right;">Angefallene Beitr&auml;ge</th>
243         <th style="text-align: right;">Aktueller Beitrag</th>
244         <th style="text-align: right;">Offener Beitrag</th>
245     </tr>
246 <?php if (empty($members)) $members = array(); ?>
247 <?php foreach ($members as $member) : ?>
248 <?php
249     $current_fee  = fees_for_member_at_date($member['id'], time());
250
251     $sum_fees     = fees_sum_for_member($member['id'], time());
252     $sum_old_fees = fees_sum_for_member($member['id'], DIRECTDEBIT_DATE - 86400);
253     $sum_old_paid = finance_get_paid_fees_for_member($member['id'], true);
254     $sum_new_paid = finance_get_paid_fees_for_member($member['id']);
255     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
256     $open_fees    = bcadd(bcsub($sum_old_fees, $sum_old_paid), max(bcsub($sum_new_fees, $sum_new_paid), 0));
257 ?>
258     <tr>
259         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
260         <td><?=html_escape($member['nickname'])?></td>
261         <td style="text-align: right;"><?=format_money($sum_fees)?></td>
262         <td style="text-align: right;"><?=isset($current_fee) ? format_money($current_fee) : '-' ?></td>
263         <td style="text-align: right;"><?=$open_fees > 0 ? format_money($open_fees) : '-' ?></td>
264     </tr>
265 <?php endforeach ?>
266 </table>
267 <?php
268 }/*}}}*/
269
270 function render_accrued_fees_by_month() {/*{{{*/
271     $fees = fees_sum_by_month(time());
272     $fees = array_reverse($fees, true);
273 ?>
274 <h2>Angefallene Mitgliedsbeitr&auml;ge nach Monat</h2>
275 <table>
276     <tr>
277         <th>Monat</th>
278         <th style="text-align: right;">Mitgliedsbeitrag</th>
279     </tr>
280 <?php foreach ($fees as $year => $months) : ?>
281     <?php $months = array_reverse($months, true); ?>
282     <?php foreach ($months as $month => $fee) : ?>
283     <tr>
284         <td><a href="<?=html_escape(link_to('fees', array('year' => $year, 'month'=> $month)))?>"><?=html_escape(format_month($year, $month))?></a></td>
285         <td style="text-align: right;"><?=html_escape(format_money($fee))?></td>
286     </tr>
287     <?php endforeach ?>
288 <?php endforeach ?>
289 </table>
290 <?php
291 }/*}}}*/
292
293 function render_accrued_fees_for_month($year, $month) {/*{{{*/
294     $fees = fees_get_list_for_month($year, $month);
295 ?>
296 <h2>Angefallene Mitgliedsbeitr&auml;ge f&uuml;r <?=format_month($year, $month)?></h2>
297 <table>
298     <tr>
299         <th>Mitgliedsnummer</th>
300         <th>Nickname</th>
301         <th style="text-align: right;">Mitgliedsbeitrag</th>
302     </tr>
303 <?php foreach ($fees as $info) : ?>
304     <tr>
305         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $info['id'])))?>"><?=html_escape($info['number'])?></a></td>
306         <td><?=html_escape($info['nickname'])?></td>
307         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
308     </tr>
309 <?php endforeach ?>
310 </table>
311 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
312 <?php
313 }/*}}}*/
314
315 function render_next_direct_debit() {/*{{{*/
316     $members = db_get_members();
317 ?>
318 <h2>Nächste Abbuchungen nach Mitglied</h2>
319 <table>
320     <tr>
321         <th>Mitgliedsnummer</th>
322         <th>Nickname</th>
323         <th style="text-align: right;">Verwendungszweck</th>
324         <th style="text-align: right;">Betrag</th>
325     </tr>
326 <?php if (empty($members)) $members = array(); ?>
327 <?php foreach ($members as $member) : ?>
328     <?php $next_debit = fee_next_directdebit_for_member($member['id']); ?>
329     <tr>
330         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
331         <td><?=html_escape($member['nickname'])?></td>
332 <?php if (empty($next_debit)) : ?>
333         <td>-</td>
334         <td style="text-align: right;">-</td>
335 <?php else : ?>
336         <td><?=html_escape($next_debit['info'])?></td>
337         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
338 <?php endif ?>
339     </tr>
340 <?php endforeach ?>
341 </table>
342 <?php
343 }
344
345 function render_fees_for_member($member_id) {/*{{{*/
346     global $MEMBER_TYPES, $EARNING_TYPES;
347
348     $member      = db_get_member_with_id($member_id);
349     if (!isset($member)) redirect(link_to('fees'));
350
351     $membership_info = fees_get_list_for_member($member_id, time());
352     $membership_info = array_reverse($membership_info, true);
353
354     $paid_fees = array_reverse(finance_list_paid_fees_for_member($member_id, time(), true));
355
356     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
357     $sum_old_paid = finance_get_paid_fees_for_member($member_id, true);
358     $sum_old_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
359     $sum_fees     = fees_sum_for_member($member_id, time());
360     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
361
362     $state = '';
363     $new_open = 0;
364     $old_open = 0;
365     if (bccomp($sum_new_fees, $sum_new_paid) == 1) $new_open = 1;
366     if (bccomp($sum_old_fees, $sum_old_paid) == 1) $old_open = 1;
367
368     if ($new_open && $old_open) {
369         $state = sprintf('Es sind noch %1$s Mitgliedsbeitrag offen, davon %2$s für die Zeit vor dem %3$s und %4$s für danach.',
370             format_money(bcadd(bcsub($sum_old_fees, $sum_old_paid), bcsub($sum_new_fees, $sum_new_paid))),
371             format_money(bcsub($sum_old_fees, $sum_old_paid)),
372             format_date(DIRECTDEBIT_DATE),
373             format_money(bcsub($sum_new_fees, $sum_new_paid))
374         );
375     }
376     elseif($new_open) {
377         $state = sprintf('Es sind noch %1$s Mitgliedsbeitrag offen.', format_money(bcsub($sum_new_fees, $sum_new_paid)));
378     }
379     elseif($old_open) {
380         $state = sprintf('Für die Zeit vor dem %1$s sind noch %2$s Mitgliedsbeitrag offen.', format_date(DIRECTDEBIT_DATE), format_money(bcsub($sum_old_fees, $sum_old_paid)));
381     }
382
383     $next_debit = fee_next_directdebit_for_member($member_id);
384
385 ?>
386 <h2>Mitgliedsbeitr&auml;ge von <?=html_escape(!empty($member['nickname']) ? $member['nickname'] : sprintf('Mitglied Nr. %d', $member['number']))?></h2>
387 <h3>Mitgliedsdetails</h3>
388 <table>
389     <tr>
390         <th>Mitgliedsnummer</th>
391         <th>Nickname</th>
392         <th>Status</th>
393     </tr>
394     <tr>
395         <td><a href="<?=html_escape(link_to('view_member', array('id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
396         <td><?=html_escape($member['nickname'])?></strong></p></td>
397         <td>
398         <?php if (empty($state)) : ?>
399             Kein Beitragsrückstand
400         <?php else : ?>
401             <?=wordwrap(html_escape($state), 70, '<br/>')?>
402         <?php endif ?>
403 </td>
404     </tr>
405 </table>
406 <div style="float: left">
407 <h3>Angefallene Mitgliedsbeitr&auml;ge</h3>
408 <table>
409     <tr>
410         <th>Monat</th>
411         <th>Mitgliedsart</th>
412         <th style="text-align: right;">Mitgliedsbeitrag</th>
413     </tr>
414 <?php foreach ($membership_info as $year => $months) : ?>
415     <?php $months = array_reverse($months, true); ?>
416     <?php foreach ($months as $month => $info) : ?>
417     <tr>
418         <td><?=html_escape(format_month($year, $month))?></td>
419         <td><?=html_escape($info['is_member'] ? $MEMBER_TYPES[$info['member_type']] : 'Kein Mitglied')?></td>
420         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
421     </tr>
422     <?php endforeach ?>
423 <?php endforeach ?>
424 </table>
425 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
426 </div>
427 <div style="float: left; margin-left: 1em;">
428 <h3>Nächste Abbuchung</h3>
429 <table>
430     <tr>
431         <th>Verwendungszweck</th>
432         <th style="text-align: right;">Betrag</th>
433     </tr>
434 <?php if (empty($next_debit)) : ?>
435         <td>-</td>
436         <td style="text-align: right;">-</td>
437 <?php else : ?>
438         <td><?=html_escape($next_debit['info'])?></td>
439         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
440 <?php endif ?> 
441 </table>
442 <h3>Bezahlte Mitgliedsbeitr&auml;ge</h3>
443 <table>
444     <tr>
445         <th>Monat</th>
446         <th style="text-align: right;">Typ</th>
447         <th style="text-align: right;">Betrag</th>
448     </tr>
449 <?php foreach ($paid_fees as $payment) : ?>
450     <tr>
451         <td><?=html_escape(format_date(db_date2unixtime($payment['date'])))?></td>
452         <td><?=$EARNING_TYPES[$payment['type']]?></td>
453         <td style="text-align: right;"><?=format_money($payment['value'])?></td>
454     </tr>
455 <?php endforeach ?>
456 </table>
457 </div>
458 <br style="clear: left;"/>
459 <?php
460 }/*}}}*/
461
462