2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 /**********************************************
14 Name: Revenue / Cost Accruals v2.2
15 Free software under GNU GPL
16 ***********************************************/
17 $page_security = 'SA_ACCRUALS';
20 include_once($path_to_root . "/includes/session.inc");
21 include_once($path_to_root . "/includes/ui/items_cart.inc");
23 $js = get_js_open_window(800, 500);
24 if (user_use_date_picker())
25 $js .= get_js_date_picker();
28 include_once($path_to_root . "/includes/ui.inc");
30 $_SESSION['page_title'] = _($help_context = "Revenue / Cost Accruals");
31 page($_SESSION['page_title'], false, false,'', $js);
33 function generate_accruals($amount, $date, $freq, $periods)
38 if ($freq == 3 || $freq == 4) {
39 $date_ = begin_month($date_); // avoid skip on shorter months
40 $date = end_month($date_); // avoid skip on shorter months
43 $lastdate = ($freq == 1 ? add_days($date_, 7*$per) :
44 ($freq == 2 ? add_days($date_, 14*$per) :
45 ($freq == 3 ? end_month(add_months($date_, $per)) :
46 end_month(add_months($date_, 3*$per)))));
47 if (!is_date_in_fiscalyears($lastdate, false))
49 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!"));
52 $amount = input_num('amount');
53 $am = round2($amount / $periods, user_price_dec());
54 if ($am * $periods != $amount)
55 $am0 = $am + $amount - $am * $periods;
60 for ($i = 0; $i < $periods; $i++)
62 $amounts[$date] = $am0;
66 $date = $date_ = add_days($date_, 7);
69 $date = $date_ = add_days($date_, 14);
72 $date_ = add_months($date_, 1);
73 $date = end_month($date_);
76 $date_ = add_months($date_, 3);
77 $date = end_month($date_);
85 //--------------------------------------------------------------------------------------------------
86 if (!isset($_POST['freq']))
88 // If the import button was selected, we'll process the form here. (If not, skip to actual content below.)
89 if (isset($_POST['go']) || isset($_POST['show']))
92 if (!is_date($_POST['date_']))
94 display_error(_("The entered date is invalid."));
98 elseif (!is_date_in_fiscalyear($_POST['date_']))
100 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
104 elseif (input_num('amount', 0) == 0.0)
106 display_error(_("The amount can not be 0."));
107 set_focus('periods');
110 elseif (input_num('periods', 0) < 1)
112 display_error(_("The periods must be greater than 0."));
113 set_focus('periods');
116 if ($input_error == 0)
118 $accruals = generate_accruals(input_num('amount'), get_post('date_'), get_post('freq'), get_post('periods'));
122 if (get_post('memo_') != "")
123 $memo = $_POST['memo_'];
125 $memo = sprintf(_("Accruals for %s"), input_num('amount'));
127 if (isset($_POST['go'])) {
130 foreach($accruals as $date => $amount) {
131 $cart = new items_cart(ST_JOURNAL);
132 $cart->memo_ = $memo;
133 $cart->reference = $Refs->get_next(ST_JOURNAL, null, $date);
134 $cart->tran_date = $cart->doc_date = $cart->event_date = $date;
135 $cart->add_gl_item(get_post('acc_act'), 0, 0, -$amount, $cart->reference);
136 $cart->add_gl_item(get_post('res_act'), get_post('dimension_id'),
137 get_post('dimension2_id'), $amount, $cart->reference);
138 write_journal_entries($cart);
139 $cart->clear_items();
140 commit_transaction();
142 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
143 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
147 start_table(TABLESTYLE);
148 $dim = get_company_pref('use_dimension');
150 $first_cols = array(_("Date"), _("Account"));
152 $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
154 $dim_cols = array(_("Dimension"));
158 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
160 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
163 foreach ($accruals as $date => $amount)
165 alt_table_row_color($k);
167 label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
172 display_debit_or_credit_cells($amount * -1);
174 alt_table_row_color($k);
176 label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
178 label_cell(get_dimension_string($_POST['dimension_id'], true));
180 label_cell(get_dimension_string($_POST['dimension2_id'], true));
181 display_debit_or_credit_cells($amount);
185 display_notification_centered(_("Showing GL Transactions."));
191 function frequency_list_row($label, $name, $selected=null)
194 label_cell($label, "class='label'");
198 '2'=> _("Bi-weekly"),
200 '4' => _("Quarterly"),
202 echo array_selector($name, $selected, $freq);
207 $dim = get_company_pref('use_dimension');
209 start_form(false, false, "", "accrual");
210 start_table(TABLESTYLE2);
212 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
214 label_cell(_("Accrued Balance Account"), "class='label'");
215 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
217 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
220 dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
222 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
224 $url = "gl/view/accrual_trans.php?act=".get_post('acc_act')."&date=".get_post('date_');
225 amount_row(_("Amount"), 'amount', null, null, viewer_link(_("Search Amount"), $url, "", "", ICON_VIEW));
227 frequency_list_row(_("Frequency"), 'freq', null);
229 text_row(_("Periods"), 'periods', null, 3, 3);
230 textarea_row(_("Memo"), 'memo_', null, 35, 3);
233 submit_center_first('show', _("Show GL Rows"));//,true,false,'process',ICON_SUBMIT);
234 submit_center_last('go', _("Process Accruals"));//,true,false,'process',ICON_SUBMIT);
235 submit_js_confirm('go', _("Are you sure you want to post accruals?"));