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 //--------------------------------------------------------------------------------------------------
34 if (!isset($_POST['freq']))
36 // If the import button was selected, we'll process the form here. (If not, skip to actual content below.)
37 if (isset($_POST['go']) || isset($_POST['show']))
40 if (!is_date($_POST['date_']))
42 display_error(_("The entered date is invalid."));
46 elseif (!is_date_in_fiscalyear($_POST['date_']))
48 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
52 elseif (input_num('amount', 0) == 0.0)
54 display_error(_("The amount can not be 0."));
58 elseif (input_num('periods', 0) < 1)
60 display_error(_("The periods must be greater than 0."));
64 if ($input_error == 0)
66 $periods = input_num('periods');
68 $date = $date_ = get_post('date_');
69 $freq = get_post('freq');
70 if ($freq == 3 || $freq == 4) {
71 $date_ = begin_month($date_); // avoid skip on shorter months
72 $date = end_month($date_); // avoid skip on shorter months
75 $lastdate = ($freq == 1 ? add_days($date_, 7*$per) :
76 ($freq == 2 ? add_days($date_, 14*$per) :
77 ($freq == 3 ? end_month(add_months($date_, $per)) :
78 end_month(add_months($date_, 3*$per)))));
79 if (!is_date_in_fiscalyears($lastdate, false))
81 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!"));
85 if ($input_error == 0)
87 $amount = input_num('amount');
88 $am = round2($amount / $periods, user_price_dec());
89 if ($am * $periods != $amount)
90 $am0 = $am + $amount - $am * $periods;
93 if (get_post('memo_') != "")
94 $memo = $_POST['memo_'];
96 $memo = sprintf(_("Accruals for %s"), $amount);
97 if (isset($_POST['go']))
101 start_table(TABLESTYLE);
102 $dim = get_company_pref('use_dimension');
104 $first_cols = array(_("Date"), _("Account"));
106 $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
108 $dim_cols = array(_("Dimension"));
112 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
114 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
118 for ($i = 0; $i < $periods; $i++)
125 $date = $date_ = add_days($date_, 7);
128 $date = $date_ = add_days($date_, 14);
131 $date_ = add_months($date_, 1);
132 $date = end_month($date_);
135 $date_ = add_months($date_, 3);
136 $date = end_month($date_);
141 if (isset($_POST['go']))
143 $cart = new items_cart(ST_JOURNAL);
144 $cart->memo_ = $memo;
145 $cart->reference = $Refs->get_next(ST_JOURNAL, null, $date);
146 $cart->tran_date = $cart->doc_date = $cart->event_date = $date;
147 $cart->add_gl_item(get_post('acc_act'), 0, 0, -$am0, $cart->reference);
148 $cart->add_gl_item(get_post('res_act'), get_post('dimension_id'),
149 get_post('dimension2_id'), $am0, $cart->reference);
150 write_journal_entries($cart);
151 $cart->clear_items();
155 alt_table_row_color($k);
157 label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
162 display_debit_or_credit_cells($am0 * -1);
164 alt_table_row_color($k);
166 label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
168 label_cell(get_dimension_string($_POST['dimension_id'], true));
170 label_cell(get_dimension_string($_POST['dimension2_id'], true));
171 display_debit_or_credit_cells($am0);
175 if (isset($_POST['go']))
177 commit_transaction();
178 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
179 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
184 display_notification_centered(_("Showing GL Transactions."));
190 function frequency_list_row($label, $name, $selected=null)
193 label_cell($label, "class='label'");
197 '2'=> _("Bi-weekly"),
199 '4' => _("Quarterly"),
201 echo array_selector($name, $selected, $freq);
206 $dim = get_company_pref('use_dimension');
208 start_form(false, false, "", "accrual");
209 start_table(TABLESTYLE2);
211 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
213 label_cell(_("Accrued Balance Account"), "class='label'");
214 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
216 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
219 dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
221 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
223 $url = "gl/view/accrual_trans.php?act=".get_post('acc_act')."&date=".get_post('date_');
224 amount_row(_("Amount"), 'amount', null, null, viewer_link(_("Search Amount"), $url, "", "", ICON_VIEW));
226 frequency_list_row(_("Frequency"), 'freq', null);
228 text_row(_("Periods"), 'periods', null, 3, 3);
229 textarea_row(_("Memo"), 'memo_', null, 35, 3);
232 submit_center_first('show', _("Show GL Rows"));//,true,false,'process',ICON_SUBMIT);
233 submit_center_last('go', _("Process Accruals"));//,true,false,'process',ICON_SUBMIT);
234 submit_js_confirm('go', _("Are you sure you want to post accruals?"));