2 /**********************************************
4 Name: Revenue / Cost Accruals v2.2
5 Free software under GNU GPL
6 ***********************************************/
7 $page_security = 'SA_ACCRUALS';
10 include_once($path_to_root . "/includes/session.inc");
11 include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc");
13 $js = get_js_open_window(800, 500);
15 $js .= get_js_date_picker();
18 include_once($path_to_root . "/includes/ui.inc");
20 $_SESSION['page_title'] = _($help_context = _("Revenue / Cost Accruals"));
21 page($_SESSION['page_title'], false, false,'', $js);
23 //--------------------------------------------------------------------------------------------------
24 if (!isset($_POST['freq']))
26 // If the import button was selected, we'll process the form here. (If not, skip to actual content below.)
27 if (isset($_POST['go']) || isset($_POST['show']))
30 if (!is_date($_POST['date_']))
32 display_error(_("The entered date is invalid."));
36 elseif (!is_date_in_fiscalyear($_POST['date_']))
38 display_error(_("The entered date is not in fiscal year."));
42 elseif (input_num('amount', 0) == 0.0)
44 display_error(_("The amount can not be 0."));
48 elseif (input_num('periods', 0) < 1)
50 display_error(_("The periods must be greater than 0."));
54 if ($input_error == 0)
56 $periods = input_num('periods');
58 $date = $date_ = get_post('date_');
59 $freq = get_post('freq');
60 $lastdate = ($freq == 1 ? add_days($date_, 7*$per)
61 : ($freq == 2 ? add_days($date_, 14*$per)
62 : ($freq == 3 ? add_months($date_, $per)
63 : add_months($date_, 3*$per))));
65 if (!is_date_in_fiscalyears($lastdate, false))
67 display_error(_("Some of the period dates are outside the fiscal year. Create a new fiscal year first!"));
71 if ($input_error == 0)
73 $amount = input_num('amount');
74 $am = round2($amount / $periods, user_price_dec());
75 if ($am * $periods != $amount)
76 $am0 = $am + $amount - $am * $periods;
79 if (get_post('memo_') != "")
80 $memo = $_POST['memo_'];
82 $memo = sprintf(_("Accruals for %s"), $amount);
83 if (isset($_POST['go']))
87 start_table(TABLESTYLE);
88 $dim = get_company_pref('use_dimension');
90 $first_cols = array(_("Date"), _("Account"));
92 $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
94 $dim_cols = array(_("Dimension"));
98 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
100 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
104 for ($i = 0; $i < $periods; $i++)
111 $date = add_days($date_, $i*7);
114 $date = add_days($date_, $i*14);
117 $date = add_months($date_, $i*1);
120 $date = add_months($date_, $i*3);
125 if (isset($_POST['go']))
127 $id = get_next_trans_no(ST_JOURNAL);
128 $ref = $Refs->get_next(ST_JOURNAL);
129 add_gl_trans(ST_JOURNAL, $id, $date, get_post('acc_act'), 0,
131 add_gl_trans(ST_JOURNAL, $id, $date, get_post('res_act'), get_post('dimension_id'),
132 get_post('dimension2_id'), $ref, $am0);
133 add_comments(ST_JOURNAL, $id, $date, $memo);
134 $Refs->save(ST_JOURNAL, $id, $ref);
138 alt_table_row_color($k);
140 label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
145 display_debit_or_credit_cells($am0 * -1);
147 alt_table_row_color($k);
149 label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
151 label_cell(get_dimension_string($_POST['dimension_id'], true));
153 label_cell(get_dimension_string($_POST['dimension2_id'], true));
154 display_debit_or_credit_cells($am0);
158 if (isset($_POST['go']))
160 commit_transaction();
161 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
162 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
167 display_notification_centered(_("Showing GL Transactions."));
173 function frequency_list_row($label, $name, $selected=null)
176 label_cell($label, "class='label'");
180 '2'=> _("Bi-weekly"),
182 '4' => _("Quarterly"),
184 echo array_selector($name, $selected, $freq);
189 $dim = get_company_pref('use_dimension');
191 start_form(false, false, "", "accrual");
192 start_table(TABLESTYLE2);
194 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
196 label_cell(_("Accrued Balance Account"), "class='label'");
197 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
199 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
202 dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
204 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
206 $url = "gl/view/accrual_trans.php?act=".get_post('acc_act')."&date=".get_post('date_');
207 amount_row(_("Amount"), 'amount', null, null, viewer_link(_("Search Amount"), $url, "", "", ICON_VIEW));
209 frequency_list_row(_("Frequency"), 'freq', null);
211 text_row(_("Periods"), 'periods', null, 3, 3);
212 textarea_row(_("Memo"), 'memo_', null, 35, 3);
215 submit_center_first('show', _("Show GL Rows"));//,true,false,'process',ICON_SUBMIT);
216 submit_center_last('go', _("Process Accruals"));//,true,false,'process',ICON_SUBMIT);
217 submit_js_confirm('go', _("Are you sure you want to post accruals?"));