+function generate_accruals($amount, $date, $freq, $periods)
+{
+ $per = $periods - 1;
+ $date_ = $date;
+
+ if ($freq == 3 || $freq == 4) {
+ $date_ = begin_month($date_); // avoid skip on shorter months
+ $date = end_month($date_); // avoid skip on shorter months
+ }
+
+ $lastdate = ($freq == 1 ? add_days($date_, 7*$per) :
+ ($freq == 2 ? add_days($date_, 14*$per) :
+ ($freq == 3 ? end_month(add_months($date_, $per)) :
+ end_month(add_months($date_, 3*$per)))));
+ if (!is_date_in_fiscalyears($lastdate, false))
+ {
+ display_error(_("Some of the period dates are outside the fiscal year or are closed for further data entry. Create a new fiscal year first!"));
+ return null;
+ }
+ $amount = input_num('amount');
+ $am = round2($amount / $periods, user_price_dec());
+ if ($am * $periods != $amount)
+ $am0 = $am + $amount - $am * $periods;
+ else
+ $am0 = $am;
+
+ $amounts = array();
+ for ($i = 0; $i < $periods; $i++)
+ {
+ $amounts[$date] = $am0;
+ switch($freq)
+ {
+ case 1:
+ $date = $date_ = add_days($date_, 7);
+ break;
+ case 2:
+ $date = $date_ = add_days($date_, 14);
+ break;
+ case 3:
+ $date_ = add_months($date_, 1);
+ $date = end_month($date_);
+ break;
+ case 4:
+ $date_ = add_months($date_, 3);
+ $date = end_month($date_);
+ break;
+ }
+ $am0 = $am;
+ }
+ return $amounts;
+}
+