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