7551a91ec92cfbc59fed5971e5aa61192565bfcf
[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
235 }/*}}}*/
236
237 function render_fees_by_member() {/*{{{*/
238     $members = db_get_members();
239 ?>
240 <h2>Mitgliedsbeitr&auml;ge nach Mitglied</h2>
241 <table>
242     <tr>
243         <th>Mitgliedsnummer</th>
244         <th>Nickname</th>
245         <th style="text-align: right;">Angefallene Beitr&auml;ge</th>
246         <th style="text-align: right;">Aktueller Beitrag</th>
247         <th style="text-align: right;">Offener Beitrag</th>
248     </tr>
249 <?php if (empty($members)) $members = array(); ?>
250 <?php foreach ($members as $member) : ?>
251 <?php
252     $current_fee  = fees_for_member_at_date($member['id'], time());
253
254     $sum_fees     = fees_sum_for_member($member['id'], time());
255     $sum_old_fees = fees_sum_for_member($member['id'], DIRECTDEBIT_DATE - 86400);
256     $sum_old_paid = finance_get_paid_fees_for_member($member['id'], true);
257     $sum_new_paid = finance_get_paid_fees_for_member($member['id']);
258     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
259     $open_fees    = bcadd(bcsub($sum_old_fees, $sum_old_paid), max(bcsub($sum_new_fees, $sum_new_paid), 0));
260 ?>
261     <tr>
262         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
263         <td><?=html_escape($member['nickname'])?></td>
264         <td style="text-align: right;"><?=format_money($sum_fees)?></td>
265         <td style="text-align: right;"><?=isset($current_fee) ? format_money($current_fee) : '-' ?></td>
266         <td style="text-align: right;"><?=$open_fees > 0 ? format_money($open_fees) : '-' ?></td>
267     </tr>
268 <?php endforeach ?>
269 </table>
270 <?php
271 }/*}}}*/
272
273 function render_accrued_fees_by_month() {/*{{{*/
274     $fees = fees_sum_by_month(time());
275     $fees = array_reverse($fees, true);
276 ?>
277 <h2>Angefallene Mitgliedsbeitr&auml;ge nach Monat</h2>
278 <table>
279     <tr>
280         <th>Monat</th>
281         <th style="text-align: right;">Mitgliedsbeitrag</th>
282     </tr>
283 <?php foreach ($fees as $year => $months) : ?>
284     <?php $months = array_reverse($months, true); ?>
285     <?php foreach ($months as $month => $fee) : ?>
286     <tr>
287         <td><a href="<?=html_escape(link_to('fees', array('year' => $year, 'month'=> $month)))?>"><?=html_escape(format_month($year, $month))?></a></td>
288         <td style="text-align: right;"><?=html_escape(format_money($fee))?></td>
289     </tr>
290     <?php endforeach ?>
291 <?php endforeach ?>
292 </table>
293 <?php
294 }/*}}}*/
295
296 function render_accrued_fees_for_month($year, $month) {/*{{{*/
297     $fees = fees_get_list_for_month($year, $month);
298 ?>
299 <h2>Angefallene Mitgliedsbeitr&auml;ge f&uuml;r <?=format_month($year, $month)?></h2>
300 <table>
301     <tr>
302         <th>Mitgliedsnummer</th>
303         <th>Nickname</th>
304         <th style="text-align: right;">Mitgliedsbeitrag</th>
305     </tr>
306 <?php foreach ($fees as $info) : ?>
307     <tr>
308         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $info['id'])))?>"><?=html_escape($info['number'])?></a></td>
309         <td><?=html_escape($info['nickname'])?></td>
310         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
311     </tr>
312 <?php endforeach ?>
313 </table>
314 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
315 <?php
316 }/*}}}*/
317
318 function render_next_direct_debit() {/*{{{*/
319     $members = db_get_members();
320 ?>
321 <h2>Nächste Abbuchungen nach Mitglied</h2>
322 <table>
323     <tr>
324         <th>Mitgliedsnummer</th>
325         <th>Nickname</th>
326         <th style="text-align: right;">Verwendungszweck</th>
327         <th style="text-align: right;">Betrag</th>
328     </tr>
329 <?php if (empty($members)) $members = array(); ?>
330 <?php foreach ($members as $member) : ?>
331     <?php $next_debit = fee_next_directdebit_for_member($member['id']); ?>
332     <tr>
333         <td><a href="<?=html_escape(link_to('fees', array('member_id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
334         <td><?=html_escape($member['nickname'])?></td>
335 <?php if (empty($next_debit)) : ?>
336         <td>-</td>
337         <td style="text-align: right;">-</td>
338 <?php else : ?>
339         <td><?=html_escape($next_debit['info'])?></td>
340         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
341 <?php endif ?>
342     </tr>
343 <?php endforeach ?>
344 </table>
345 <?php
346 }
347
348 function render_fees_for_member($member_id) {/*{{{*/
349     global $MEMBER_TYPES, $EARNING_TYPES;
350
351     $member      = db_get_member_with_id($member_id);
352     if (!isset($member)) redirect(link_to('fees'));
353
354     $membership_info = fees_get_list_for_member($member_id, time());
355     $membership_info = array_reverse($membership_info, true);
356
357     $paid_fees = array_reverse(finance_list_paid_fees_for_member($member_id, time(), true));
358
359     $sum_new_paid = finance_get_paid_fees_for_member($member_id);
360     $sum_old_paid = finance_get_paid_fees_for_member($member_id, true);
361     $sum_old_fees = fees_sum_for_member($member_id, DIRECTDEBIT_DATE - 86400);
362     $sum_fees     = fees_sum_for_member($member_id, time());
363     $sum_new_fees = bcsub($sum_fees, $sum_old_fees);
364
365     $state = '';
366     $new_open = 0;
367     $old_open = 0;
368     if (bccomp($sum_new_fees, $sum_new_paid) == 1) $new_open = 1;
369     if (bccomp($sum_old_fees, $sum_old_paid) == 1) $old_open = 1;
370
371     if ($new_open && $old_open) {
372         $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.',
373             format_money(bcadd(bcsub($sum_old_fees, $sum_old_paid), bcsub($sum_new_fees, $sum_new_paid))),
374             format_money(bcsub($sum_old_fees, $sum_old_paid)),
375             format_date(DIRECTDEBIT_DATE),
376             format_money(bcsub($sum_new_fees, $sum_new_paid))
377         );
378     }
379     elseif($new_open) {
380         $state = sprintf('Es sind noch %1$s Mitgliedsbeitrag offen.', format_money(bcsub($sum_new_fees, $sum_new_paid)));
381     }
382     elseif($old_open) {
383         $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)));
384     }
385
386     $next_debit = fee_next_directdebit_for_member($member_id);
387
388 ?>
389 <h2>Mitgliedsbeitr&auml;ge von <?=html_escape(!empty($member['nickname']) ? $member['nickname'] : sprintf('Mitglied Nr. %d', $member['number']))?></h2>
390 <h3>Mitgliedsdetails</h3>
391 <table>
392     <tr>
393         <th>Mitgliedsnummer</th>
394         <th>Nickname</th>
395         <th>Status</th>
396     </tr>
397     <tr>
398         <td><a href="<?=html_escape(link_to('view_member', array('id'=> $member['id'])))?>"><?=html_escape($member['number'])?></a></td>
399         <td><?=html_escape($member['nickname'])?></strong></p></td>
400         <td>
401         <?php if (empty($state)) : ?>
402             Kein Beitragsrückstand
403         <?php else : ?>
404             <?=wordwrap(html_escape($state), 70, '<br/>')?>
405         <?php endif ?>
406 </td>
407     </tr>
408 </table>
409 <div style="float: left">
410 <h3>Angefallene Mitgliedsbeitr&auml;ge</h3>
411 <table>
412     <tr>
413         <th>Monat</th>
414         <th>Mitgliedsart</th>
415         <th style="text-align: right;">Mitgliedsbeitrag</th>
416     </tr>
417 <?php foreach ($membership_info as $year => $months) : ?>
418     <?php $months = array_reverse($months, true); ?>
419     <?php foreach ($months as $month => $info) : ?>
420     <tr>
421         <td><?=html_escape(format_month($year, $month))?></td>
422         <td><?=html_escape($info['is_member'] ? $MEMBER_TYPES[$info['member_type']] : 'Kein Mitglied')?></td>
423         <td style="text-align: right;"><?=html_escape($info['is_member'] ? format_money($info['fee']) : '-')?></td>
424     </tr>
425     <?php endforeach ?>
426 <?php endforeach ?>
427 </table>
428 <p><a href="<?=html_escape(link_to('fees'))?>">Alle angefallenen Mitgliedsbeitr&auml;ge</a></p>
429 </div>
430 <div style="float: left; margin-left: 1em;">
431 <h3>Nächste Abbuchung</h3>
432 <table>
433     <tr>
434         <th>Verwendungszweck</th>
435         <th style="text-align: right;">Betrag</th>
436     </tr>
437 <?php if (empty($next_debit)) : ?>
438         <td>-</td>
439         <td style="text-align: right;">-</td>
440 <?php else : ?>
441         <td><?=html_escape($next_debit['info'])?></td>
442         <td style="text-align: right;"><?=format_money($next_debit['value'])?></td>
443 <?php endif ?> 
444 </table>
445 <h3>Bezahlte Mitgliedsbeitr&auml;ge</h3>
446 <table>
447     <tr>
448         <th>Monat</th>
449         <th style="text-align: right;">Typ</th>
450         <th style="text-align: right;">Betrag</th>
451     </tr>
452 <?php foreach ($paid_fees as $payment) : ?>
453     <tr>
454         <td><?=html_escape(format_date(db_date2unixtime($payment['date'])))?></td>
455         <td><?=$EARNING_TYPES[$payment['type']]?></td>
456         <td style="text-align: right;"><?=format_money($payment['value'])?></td>
457     </tr>
458 <?php endforeach ?>
459 </table>
460 </div>
461 <br style="clear: left;"/>
462 <?php
463 }/*}}}*/
464
465