58011ce5f3e196162ff5f540321cf6fe657c8a4c
[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 NULL;
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     $day    = 1;
164    
165
166
167     while (true) {
168         $start_date = mktime(0, 0, 0, $month, $day, $year);
169         if (isset($max_date) && $start_date > $max_date) return NULL;
170
171         // check if fee is zero at the moment and skip to next event
172         // quit searching if theres no event in future
173         $current_fee = fees_for_member_at_date($member_id, $start_date);
174         if (empty($current_fee)) {
175             $events = db_get_events_for_member($member_id, $start_date + 86400);
176             if (empty($events)) return NULL;
177             $start_date = db_date2unixtime($events[0]['event_date']);
178             $day   = date('j', $start_date);
179             $month = date('n', $start_date);
180             $year  = date('Y', $start_date);
181             continue;
182         }
183
184         $sum_fees       = fees_sum_for_member($member_id, $start_date);
185         $sum_new_fees   = bcsub($sum_fees, $sum_old_fees);
186         if (bccomp($sum_new_fees, $sum_new_paid) == 1) {
187             $info = fees_get_list_for_member($member_id, $start_date);
188             $months = 1;
189             $ret = array(
190                 'date'  => $start_date,
191                 'value' => bcsub($sum_new_fees, $sum_new_paid),
192                 'info'  => '',
193             );
194             switch ($info[$year][$month]['payment_interval']) {
195                 case 'monthly'   : $months = 1;  break;
196                 case 'quarterly' : $months = 3;  break;
197                 case 'halfyearly': $months = 6;  break;
198                 case 'yearly'    : $months = 12; break;
199             }
200             if ($months == 1) {
201                 $ret['info'] = dtaus_string(sprintf('CCCFFM %d, %s', $member['number'], format_month($start_date))); 
202                 return $ret;
203             }
204             $end_date = mktime(0, 0, 0, $month + $months - 1, 1, $year);
205             $sum_fee_end = fees_sum_for_member($member_id, $end_date);
206             $ret['value'] = bcadd($ret['value'], bcsub($sum_fee_end, $sum_fees));
207             $ret['info'] = dtaus_string(sprintf('CCCFFM %d, %s-%s', $member['number'], format_month($start_date), format_month($end_date)));
208             return $ret;
209         }
210         $day = 1;
211         $month++;
212         if ($month == 13) { $month = 1; $year++; }
213     }
214     
215     
216 }
217 /* }}} */
218
219
220 function action_fees() {/*{{{*/
221
222     if (isset($_REQUEST['member_id'])) {
223         render_fees_for_member($_REQUEST['member_id']);
224         return;
225     }
226     if (isset($_REQUEST['year']) && isset($_REQUEST['month'])) {
227         render_accrued_fees_for_month($_REQUEST['year'], $_REQUEST['month']);
228         return;
229     }
230
231     render_fees_by_member();
232     render_accrued_fees_by_month();
233     render_next_direct_debit();
234     render_future_fees();
235
236 }/*}}}*/
237
238 function render_fees_by_member() {/*{{{*/
239     $members = db_get_members();
240 ?>
241 <h2>Mitgliedsbeitr&auml;ge nach Mitglied</h2>
242 <table>
243     <tr>
244         <th>Mitgliedsnummer</th>
245         <th>Nickname</th>
246         <th style="text-align: right;">Angefallene Beitr&auml;ge</th>
247         <th style="text-align: right;">Aktueller Beitrag</th>
248         <th style="text-align: right;">Offener Beitrag</th>
249     </tr>
250 <?php if (empty($members)) $members = array(); ?>
251 <?php foreach ($members as $member) : ?>
252 <?php
253     $current_fee  = fees_for_member_at_date($member['id'], time());
254
255     $sum_fees     = fees_sum_for_member($member['id'], time());
256     $sum_old_fees = fees_sum_for_member($member['id'], DIRECTDEBIT_DATE - 86400);
257     $sum_old_paid = finance_get_paid_fees_for_member($member['id'], true);
258     $sum_new_paid = finance_get_paid_fees_for_member($member['id']);
259     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
260     $open_fees    = bcadd(bcsub($sum_old_fees, $sum_old_paid), max(bcsub($sum_new_fees, $sum_new_paid), 0));
261 ?>
262     <tr>
263         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
264         <td><?=html_escape($member['nickname'])?></td>
265         <td style="text-align: right;"><?=format_money($sum_fees)?></td>
266         <td style="text-align: right;"><?=isset($current_fee) ? format_money($current_fee) : '-' ?></td>
267         <td style="text-align: right;"><?=$open_fees > 0 ? format_money($open_fees) : '-' ?></td>
268     </tr>
269 <?php endforeach ?>
270 </table>
271 <?php
272 }/*}}}*/
273
274 function render_future_fees() {/*{{{*/
275     $total_paid = finance_get_total_paid_fees();
276     $this_year = date('Y');
277     $this_month = date('m');
278     $fees = fees_sum_by_month(mktime(0, 0, 0, date('m') + 6,   date('d'),   date('Y')+1));
279     $total = 0;
280     foreach ($fees as $year => $months) {
281         foreach ($months as $month => $fee) {
282             $total = bcadd($total, $fee);
283             $fees[$year][$month] = array('total' => $total, 'fee' => $fee);
284         }
285     }
286     $fees = array_reverse($fees, true);
287 ?>
288 <h2>Beitragsprognose nach Monat</h2>
289 <table>
290     <tr>
291         <th>Monat</th>
292         <th style="text-align: right;">Mitgliedsbeitr&auml;ge</th>
293         <th style="text-align: right;">kummuliert</th>
294         <th style="text-align: right;">eingenommen</th>
295         <th style="text-align: right;"><strong>offen</strong></th>
296     </tr>
297 <?php foreach ($fees as $year => $months) : ?>
298     <?php $months = array_reverse($months, true); ?>
299     <?php foreach ($months as $month => $data) : ?>
300     <tr<?php if ($year == $this_year && $month == $this_month) : ?> class="current"<?php endif?>>
301         <td><a href="<?=html_escape(link_to('fees', array('year' => $year, 'month'=> $month)))?>"><?=html_escape(format_month($year, $month))?></a></td>
302         <td style="text-align: right;"><?=html_escape(format_money($data['fee']))?></td>
303         <td style="text-align: right;"><?=html_escape(format_money($data['total']))?></td>
304         <td style="text-align: right;">
305         <?php if ($year == $this_year && $month == $this_month) : ?>
306             <?=html_escape(format_money($total_paid))?>
307         <?php endif ?>
308         </td>
309         <td style="text-align: right;">
310         <?php if ($year > $this_year || ($year >= $this_year && $month >= $this_month)) : ?>
311             <?=html_escape(format_money(bcsub($data['total'], $total_paid)))?>
312         <?php endif ?>
313         </td>
314     </tr>
315     <?php endforeach ?>
316 <?php endforeach ?>
317 </table>
318 <?php
319 }/*}}}*/
320
321 function render_accrued_fees_by_month() {/*{{{*/
322     $fees = fees_sum_by_month(time());
323     $fees = array_reverse($fees, true);
324 ?>
325 <h2>Angefallene Mitgliedsbeitr&auml;ge nach Monat</h2>
326 <table>
327     <tr>
328         <th>Monat</th>
329         <th style="text-align: right;">Mitgliedsbeitrag</th>
330     </tr>
331 <?php foreach ($fees as $year => $months) : ?>
332     <?php $months = array_reverse($months, true); ?>
333     <?php foreach ($months as $month => $fee) : ?>
334     <tr>
335         <td><a href="<?=html_escape(link_to('fees', array('year' => $year, 'month'=> $month)))?>"><?=html_escape(format_month($year, $month))?></a></td>
336         <td style="text-align: right;"><?=html_escape(format_money($fee))?></td>
337     </tr>
338     <?php endforeach ?>
339 <?php endforeach ?>
340 </table>
341 <?php
342 }/*}}}*/
343
344 function render_accrued_fees_for_month($year, $month) {/*{{{*/
345     $fees = fees_get_list_for_month($year, $month);
346 ?>
347 <h2>Angefallene Mitgliedsbeitr&auml;ge f&uuml;r <?=format_month($year, $month)?></h2>
348 <table>
349     <tr>
350         <th>Mitgliedsnummer</th>
351         <th>Nickname</th>
352         <th style="text-align: right;">Mitgliedsbeitrag</th>
353     </tr>
354 <?php foreach ($fees as $info) : ?>
355     <tr>
356         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $info['id'])))?>"><?=html_escape($info['number'])?></a></td>
357         <td><?=html_escape($info['nickname'])?></td>
358         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
359     </tr>
360 <?php endforeach ?>
361 </table>
362 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
363 <?php
364 }/*}}}*/
365
366 function render_next_direct_debit() {/*{{{*/
367     $members = db_get_members();
368 ?>
369 <h2>Nächste Abbuchungen nach Mitglied</h2>
370 <table>
371     <tr>
372         <th>Mitgliedsnummer</th>
373         <th>Nickname</th>
374         <th style="text-align: right;">Verwendungszweck</th>
375         <th style="text-align: right;">Betrag</th>
376     </tr>
377 <?php if (empty($members)) $members = array(); ?>
378 <?php foreach ($members as $member) : ?>
379     <?php $next_debit = fee_next_directdebit_for_member($member['id']); ?>
380     <tr>
381         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
382         <td><?=html_escape($member['nickname'])?></td>
383 <?php if (empty($next_debit)) : ?>
384         <td>-</td>
385         <td style="text-align: right;">-</td>
386 <?php else : ?>
387         <td><?=html_escape($next_debit['info'])?></td>
388         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
389 <?php endif ?>
390     </tr>
391 <?php endforeach ?>
392 </table>
393 <?php
394 }
395
396 function render_fees_for_member($member_id) {/*{{{*/
397     global $MEMBER_TYPES, $EARNING_TYPES;
398
399     $member      = db_get_member_with_id($member_id);
400     if (!isset($member)) redirect(link_to('fees'));
401
402     $membership_info = fees_get_list_for_member($member_id, time());
403     $membership_info = array_reverse($membership_info, true);
404
405     $paid_fees = array_reverse(finance_list_paid_fees_for_member($member_id, time(), true));
406
407     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
408     $sum_old_paid = finance_get_paid_fees_for_member($member_id, true);
409     $sum_old_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
410     $sum_fees     = fees_sum_for_member($member_id, time());
411     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
412
413     $state = '';
414     $new_open = 0;
415     $old_open = 0;
416     if (bccomp($sum_new_fees, $sum_new_paid) == 1) $new_open = 1;
417     if (bccomp($sum_old_fees, $sum_old_paid) == 1) $old_open = 1;
418
419     if ($new_open && $old_open) {
420         $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.',
421             format_money(bcadd(bcsub($sum_old_fees, $sum_old_paid), bcsub($sum_new_fees, $sum_new_paid))),
422             format_money(bcsub($sum_old_fees, $sum_old_paid)),
423             format_date(DIRECTDEBIT_DATE),
424             format_money(bcsub($sum_new_fees, $sum_new_paid))
425         );
426     }
427     elseif($new_open) {
428         $state = sprintf('Es sind noch %1$s Mitgliedsbeitrag offen.', format_money(bcsub($sum_new_fees, $sum_new_paid)));
429     }
430     elseif($old_open) {
431         $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)));
432     }
433
434     $next_debit = fee_next_directdebit_for_member($member_id);
435
436 ?>
437 <h2>Mitgliedsbeitr&auml;ge von <?=html_escape(!empty($member['nickname']) ? $member['nickname'] : sprintf('Mitglied Nr. %d', $member['number']))?></h2>
438 <h3>Mitgliedsdetails</h3>
439 <table>
440     <tr>
441         <th>Mitgliedsnummer</th>
442         <th>Nickname</th>
443         <th>Status</th>
444     </tr>
445     <tr>
446         <td><a href="<?=html_escape(link_to('view_member', array('id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
447         <td><?=html_escape($member['nickname'])?></strong></p></td>
448         <td>
449         <?php if (empty($state)) : ?>
450             Kein Beitragsrückstand
451         <?php else : ?>
452             <?=wordwrap(html_escape($state), 70, '<br/>')?>
453         <?php endif ?>
454 </td>
455     </tr>
456 </table>
457 <div style="float: left">
458 <h3>Angefallene Mitgliedsbeitr&auml;ge</h3>
459 <table>
460     <tr>
461         <th>Monat</th>
462         <th>Mitgliedsart</th>
463         <th style="text-align: right;">Mitgliedsbeitrag</th>
464     </tr>
465 <?php foreach ($membership_info as $year => $months) : ?>
466     <?php $months = array_reverse($months, true); ?>
467     <?php foreach ($months as $month => $info) : ?>
468     <tr>
469         <td><?=html_escape(format_month($year, $month))?></td>
470         <td><?=html_escape($info['is_member'] ? $MEMBER_TYPES[$info['member_type']] : 'Kein Mitglied')?></td>
471         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
472     </tr>
473     <?php endforeach ?>
474 <?php endforeach ?>
475 </table>
476 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
477 </div>
478 <div style="float: left; margin-left: 1em;">
479 <h3>Nächste Abbuchung</h3>
480 <table>
481     <tr>
482         <th>Verwendungszweck</th>
483         <th style="text-align: right;">Betrag</th>
484     </tr>
485 <?php if (empty($next_debit)) : ?>
486         <td>-</td>
487         <td style="text-align: right;">-</td>
488 <?php else : ?>
489         <td><?=html_escape($next_debit['info'])?></td>
490         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
491 <?php endif ?> 
492 </table>
493 <h3>Bezahlte Mitgliedsbeitr&auml;ge</h3>
494 <table>
495     <tr>
496         <th>Monat</th>
497         <th style="text-align: right;">Typ</th>
498         <th style="text-align: right;">Betrag</th>
499     </tr>
500 <?php foreach ($paid_fees as $payment) : ?>
501     <tr>
502         <td><?=html_escape(format_date(db_date2unixtime($payment['date'])))?></td>
503         <td><?=$EARNING_TYPES[$payment['type']]?></td>
504         <td style="text-align: right;"><?=format_money($payment['value'])?></td>
505     </tr>
506 <?php endforeach ?>
507 </table>
508 </div>
509 <br style="clear: left;"/>
510 <?php
511 }/*}}}*/
512
513