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