Undefined 'no_zeroes' in rep115 line 235. Fixed.
[fa-stable.git] / gl / accruals.php
1 <?php
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 /**********************************************
13 Author: Joe Hunt
14 Name: Revenue / Cost Accruals v2.2
15 Free software under GNU GPL
16 ***********************************************/
17 $page_security = 'SA_ACCRUALS';
18 $path_to_root="..";
19
20 include_once($path_to_root . "/includes/session.inc");
21 include_once($path_to_root . "/includes/ui/items_cart.inc");
22
23 $js = get_js_open_window(800, 500);
24 if (user_use_date_picker())
25         $js .= get_js_date_picker();
26
27 // Begin the UI
28 include_once($path_to_root . "/includes/ui.inc");
29
30 $_SESSION['page_title'] = _($help_context = "Revenue / Cost Accruals");
31 page($_SESSION['page_title'], false, false,'', $js);
32
33 function generate_accruals($amount, $date, $freq, $periods)
34 {
35         $per = $periods - 1;
36         $date_ = $date;
37
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
41         }
42
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))
48         {
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!"));
50                 return null;
51         }
52         $amount = input_num('amount');
53         $am = round2($amount / $periods, user_price_dec());
54         if ($am * $periods != $amount)
55                 $am0 = $am + $amount - $am * $periods;
56         else
57                 $am0 = $am;
58
59         $amounts = array();
60         for ($i = 0; $i < $periods; $i++)
61         {
62                 $amounts[$date] = $am0;
63                         switch($freq)
64                         {
65                                 case 1:
66                                         $date = $date_ = add_days($date_, 7);
67                                         break;
68                                 case 2:
69                                         $date = $date_ = add_days($date_, 14);
70                                         break;
71                                 case 3:
72                                         $date_ = add_months($date_, 1);
73                                         $date = end_month($date_);
74                                         break;
75                                 case 4:
76                                         $date_ = add_months($date_, 3);
77                                         $date = end_month($date_);
78                                         break;
79                         }
80                 $am0 = $am;
81         }
82         return $amounts;
83 }
84
85 //--------------------------------------------------------------------------------------------------
86 if (!isset($_POST['freq']))
87         $_POST['freq'] = 3;
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']))
90 {
91         $input_error = 0;
92         if (!is_date($_POST['date_']))
93         {
94                 display_error(_("The entered date is invalid."));
95                 set_focus('date_');
96                 $input_error = 1;
97         }
98         elseif (!is_date_in_fiscalyear($_POST['date_']))
99         {
100                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
101                 set_focus('date_');
102                 $input_error = 1;
103         }
104         elseif (input_num('amount', 0) == 0.0)
105         {
106                 display_error(_("The amount can not be 0."));
107                 set_focus('periods');
108                 $input_error = 1;
109         }
110         elseif (input_num('periods', 0) < 1)
111         {
112                 display_error(_("The periods must be greater than 0."));
113                 set_focus('periods');
114                 $input_error = 1;
115         }
116         if ($input_error == 0)
117         {
118                 $accruals = generate_accruals(input_num('amount'), get_post('date_'), get_post('freq'), get_post('periods')); 
119
120                 if ($accruals)
121                 {
122                         if (get_post('memo_') != "")
123                                 $memo = $_POST['memo_'];
124                         else
125                                 $memo = sprintf(_("Accruals for %s"), input_num('amount'));
126
127                         if (isset($_POST['go'])) {
128
129                                 begin_transaction();
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();
141
142                                 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
143                                 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
144                                 }
145                         } else
146                         {
147                                 start_table(TABLESTYLE);
148                                 $dim = get_company_pref('use_dimension');
149
150                                 $first_cols = array(_("Date"), _("Account"));
151                                 if ($dim == 2)
152                                         $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
153                                 elseif ($dim == 1)
154                                         $dim_cols = array(_("Dimension"));
155                                 else
156                                         $dim_cols = array();
157
158                                 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
159
160                                 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
161                                 table_header($th);
162                                 $k = 0;
163                                 foreach ($accruals as $date => $amount)
164                                 {
165                                         alt_table_row_color($k);
166                                         label_cell($date);
167                                         label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
168                                         if ($dim > 0)
169                                                 label_cell("");
170                                         if ($dim > 1)
171                                                 label_cell("");
172                                         display_debit_or_credit_cells($amount * -1);
173                                         label_cell($memo);
174                                         alt_table_row_color($k);
175                                         label_cell($date);
176                                         label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
177                                         if ($dim > 0)
178                                                 label_cell(get_dimension_string($_POST['dimension_id'], true));
179                                         if ($dim > 1)
180                                                 label_cell(get_dimension_string($_POST['dimension2_id'], true));
181                                         display_debit_or_credit_cells($amount);
182                                         label_cell($memo);
183                                 }
184                                 end_table(1);
185                                 display_notification_centered(_("Showing GL Transactions."));
186                         }
187                 }
188         }
189 }
190
191 function frequency_list_row($label, $name, $selected=null)
192 {
193         echo "<tr>\n";
194         label_cell($label, "class='label'");
195         echo "<td>\n";
196         $freq = array(
197                 '1'=> _("Weekly"),
198                 '2'=> _("Bi-weekly"),
199                 '3' => _("Monthly"),
200                 '4' => _("Quarterly"),
201         );
202         echo array_selector($name, $selected, $freq);
203         echo "</td>\n";
204         echo "</tr\n";
205 }
206
207 $dim = get_company_pref('use_dimension');
208
209 start_form(false, false, "", "accrual");
210 start_table(TABLESTYLE2);
211
212 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
213 start_row();
214 label_cell(_("Accrued Balance Account"), "class='label'");
215 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
216 end_row();
217 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
218
219 if ($dim >= 1)
220         dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
221 if ($dim > 1)
222         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
223
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));
226
227 frequency_list_row(_("Frequency"), 'freq', null);
228
229 text_row(_("Periods"), 'periods', null, 3, 3);
230 textarea_row(_("Memo"), 'memo_', null, 35, 3);
231
232 end_table(1);
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?"));
236
237 end_form();
238
239 end_page();