Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 //--------------------------------------------------------------------------------------------------
34 if (!isset($_POST['freq']))
35         $_POST['freq'] = 3;
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']))
38 {
39         $input_error = 0;
40         if (!is_date($_POST['date_']))
41         {
42                 display_error(_("The entered date is invalid."));
43                 set_focus('date_');
44                 $input_error = 1;
45         }
46         elseif (!is_date_in_fiscalyear($_POST['date_']))
47         {
48                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
49                 set_focus('date_');
50                 $input_error = 1;
51         }
52         elseif (input_num('amount', 0) == 0.0)
53         {
54                 display_error(_("The amount can not be 0."));
55                 set_focus('amount');
56                 $input_error = 1;
57         }
58         elseif (input_num('periods', 0) < 1)
59         {
60                 display_error(_("The periods must be greater than 0."));
61                 set_focus('periods');
62                 $input_error = 1;
63         }
64         if ($input_error == 0)
65         {
66                 $periods = input_num('periods');
67                 $per = $periods - 1;
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
73                 }
74                 
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))
80                 {
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!"));
82                         set_focus('date_');
83                         $input_error = 1;
84                 }
85                 if ($input_error == 0)
86                 {
87                         $amount = input_num('amount');
88                         $am = round2($amount / $periods, user_price_dec());
89                         if ($am * $periods != $amount)
90                                 $am0 = $am + $amount - $am * $periods;
91                         else
92                                 $am0 = $am;
93                         if (get_post('memo_') != "")
94                                 $memo = $_POST['memo_'];
95                         else
96                                 $memo = sprintf(_("Accruals for %s"), $amount);
97                         if (isset($_POST['go']))
98                                 begin_transaction();
99                         else
100                         {
101                                 start_table(TABLESTYLE);
102                                 $dim = get_company_pref('use_dimension');
103
104                                 $first_cols = array(_("Date"), _("Account"));
105                                 if ($dim == 2)
106                                         $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
107                                 elseif ($dim == 1)
108                                         $dim_cols = array(_("Dimension"));
109                                 else
110                                         $dim_cols = array();
111
112                                 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
113
114                                 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
115                                 table_header($th);
116                                 $k = 0;
117                         }
118                         for ($i = 0; $i < $periods; $i++)
119                         {
120                                 if ($i > 0)
121                                 {
122                                         switch($freq)
123                                         {
124                                                 case 1:
125                                                         $date = $date_ = add_days($date_, 7);
126                                                         break;
127                                                 case 2:
128                                                         $date = $date_ = add_days($date_, 14);
129                                                         break;
130                                                 case 3:
131                                                         $date_ = add_months($date_, 1);
132                                                         $date = end_month($date_);
133                                                         break;
134                                                 case 4:
135                                                         $date_ = add_months($date_, 3);
136                                                         $date = end_month($date_);
137                                                         break;
138                                         }
139                                         $am0 = $am;
140                                 }
141                                 if (isset($_POST['go']))
142                                 {
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();
152                                 }
153                                 else
154                                 {
155                                         alt_table_row_color($k);
156                                         label_cell($date);
157                                         label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
158                                         if ($dim > 0)
159                                                 label_cell("");
160                                         if ($dim > 1)
161                                                 label_cell("");
162                                         display_debit_or_credit_cells($am0 * -1);
163                                         label_cell($memo);
164                                         alt_table_row_color($k);
165                                         label_cell($date);
166                                         label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
167                                         if ($dim > 0)
168                                                 label_cell(get_dimension_string($_POST['dimension_id'], true));
169                                         if ($dim > 1)
170                                                 label_cell(get_dimension_string($_POST['dimension2_id'], true));
171                                         display_debit_or_credit_cells($am0);
172                                         label_cell($memo);
173                                 }
174                         }
175                         if (isset($_POST['go']))
176                         {
177                                 commit_transaction();
178                                 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
179                                 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
180                         }
181                         else
182                         {
183                                 end_table(1);
184                                 display_notification_centered(_("Showing GL Transactions."));
185                         }
186                 }
187         }
188 }
189
190 function frequency_list_row($label, $name, $selected=null)
191 {
192         echo "<tr>\n";
193         label_cell($label, "class='label'");
194         echo "<td>\n";
195         $freq = array(
196                 '1'=> _("Weekly"),
197                 '2'=> _("Bi-weekly"),
198                 '3' => _("Monthly"),
199                 '4' => _("Quarterly"),
200         );
201         echo array_selector($name, $selected, $freq);
202         echo "</td>\n";
203         echo "</tr\n";
204 }
205
206 $dim = get_company_pref('use_dimension');
207
208 start_form(false, false, "", "accrual");
209 start_table(TABLESTYLE2);
210
211 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
212 start_row();
213 label_cell(_("Accrued Balance Account"), "class='label'");
214 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
215 end_row();
216 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
217
218 if ($dim >= 1)
219         dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
220 if ($dim > 1)
221         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
222
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));
225
226 frequency_list_row(_("Frequency"), 'freq', null);
227
228 text_row(_("Periods"), 'periods', null, 3, 3);
229 textarea_row(_("Memo"), 'memo_', null, 35, 3);
230
231 end_table(1);
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?"));
235
236 end_form();
237
238 end_page();