Cleanup: removed all closing tags in php files.
[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 (user_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 out of fiscal year or is closed for further data entry."));
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                 if ($freq == 3 || $freq == 4) {
61                         $date_ = begin_month($date_); // avoid skip on shorter months
62                         $date  = end_month($date_); // avoid skip on shorter months
63                 }
64                 $lastdate = ($freq == 1 ? add_days($date_, 7*$per)
65                                 : ($freq == 2 ? add_days($date_, 14*$per)
66                                 : ($freq == 3 ? add_months($date_, $per)
67                                 : add_months($date_, 3*$per))));
68
69                 if (!is_date_in_fiscalyears($lastdate, false))
70                 {
71                         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!"));
72                         set_focus('date_');
73                         $input_error = 1;
74                 }
75                 if ($input_error == 0)
76                 {
77                         $amount = input_num('amount');
78                         $am = round2($amount / $periods, user_price_dec());
79                         if ($am * $periods != $amount)
80                                 $am0 = $am + $amount - $am * $periods;
81                         else
82                                 $am0 = $am;
83                         if (get_post('memo_') != "")
84                                 $memo = $_POST['memo_'];
85                         else
86                                 $memo = sprintf(_("Accruals for %s"), $amount);
87                         if (isset($_POST['go']))
88                                 begin_transaction();
89                         else
90                         {
91                                 start_table(TABLESTYLE);
92                                 $dim = get_company_pref('use_dimension');
93
94                                 $first_cols = array(_("Date"), _("Account"));
95                                 if ($dim == 2)
96                                         $dim_cols = array(_("Dimension"). " 1", _("Dimension"). " 2");
97                                 elseif ($dim == 1)
98                                         $dim_cols = array(_("Dimension"));
99                                 else
100                                         $dim_cols = array();
101
102                                 $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"));
103
104                                 $th = array_merge($first_cols, $dim_cols, $remaining_cols);
105                                 table_header($th);
106                                 $k = 0;
107                         }
108                         for ($i = 0; $i < $periods; $i++)
109                         {
110                                 if ($i > 0)
111                                 {
112                                         switch($freq)
113                                         {
114                                                 case 1:
115                                                         $date = add_days($date_, $i*7);
116                                                         break;
117                                                 case 2:
118                                                         $date = add_days($date_, $i*14);
119                                                         break;
120                                                 case 3:
121                                                         $date = add_months($date_, $i*1);
122                                                         break;
123                                                 case 4:
124                                                         $date = add_months($date_, $i*3);
125                                                         break;
126                                         }
127                                         $am0 = $am;
128                                 }
129                                 if (isset($_POST['go']))
130                                 {
131                                         $id = get_next_trans_no(ST_JOURNAL);
132                                         $ref = $Refs->get_next(ST_JOURNAL);
133                                         add_gl_trans(ST_JOURNAL, $id, $date, get_post('acc_act'), 0,
134                                                 0, $ref, $am0 * -1);
135                                         add_gl_trans(ST_JOURNAL, $id, $date, get_post('res_act'), get_post('dimension_id'),
136                                                 get_post('dimension2_id'), $ref, $am0);
137                                         add_audit_trail(ST_JOURNAL, $id, $date);
138                                         add_comments(ST_JOURNAL, $id, $date, $memo);
139                                         $Refs->save(ST_JOURNAL, $id, $ref);
140                                 }
141                                 else
142                                 {
143                                         alt_table_row_color($k);
144                                         label_cell($date);
145                                         label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
146                                         if ($dim > 0)
147                                                 label_cell("");
148                                         if ($dim > 1)
149                                                 label_cell("");
150                                         display_debit_or_credit_cells($am0 * -1);
151                                         label_cell($memo);
152                                         alt_table_row_color($k);
153                                         label_cell($date);
154                                         label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
155                                         if ($dim > 0)
156                                                 label_cell(get_dimension_string($_POST['dimension_id'], true));
157                                         if ($dim > 1)
158                                                 label_cell(get_dimension_string($_POST['dimension2_id'], true));
159                                         display_debit_or_credit_cells($am0);
160                                         label_cell($memo);
161                                 }
162                         }
163                         if (isset($_POST['go']))
164                         {
165                                 commit_transaction();
166                                 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
167                                 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
168                         }
169                         else
170                         {
171                                 end_table(1);
172                                 display_notification_centered(_("Showing GL Transactions."));
173                         }
174                 }
175         }
176 }
177
178 function frequency_list_row($label, $name, $selected=null)
179 {
180         echo "<tr>\n";
181         label_cell($label, "class='label'");
182         echo "<td>\n";
183         $freq = array(
184                 '1'=> _("Weekly"),
185                 '2'=> _("Bi-weekly"),
186                 '3' => _("Monthly"),
187                 '4' => _("Quarterly"),
188         );
189         echo array_selector($name, $selected, $freq);
190         echo "</td>\n";
191         echo "</tr\n";
192 }
193
194 $dim = get_company_pref('use_dimension');
195
196 start_form(false, false, "", "accrual");
197 start_table(TABLESTYLE2);
198
199 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
200 start_row();
201 label_cell(_("Accrued Balance Account"), "class='label'");
202 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
203 end_row();
204 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
205
206 if ($dim >= 1)
207         dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
208 if ($dim > 1)
209         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
210
211 $url = "gl/view/accrual_trans.php?act=".get_post('acc_act')."&date=".get_post('date_');
212 amount_row(_("Amount"), 'amount', null, null, viewer_link(_("Search Amount"), $url, "", "", ICON_VIEW));
213
214 frequency_list_row(_("Frequency"), 'freq', null);
215
216 text_row(_("Periods"), 'periods', null, 3, 3);
217 textarea_row(_("Memo"), 'memo_', null, 35, 3);
218
219 end_table(1);
220 submit_center_first('show', _("Show GL Rows"));//,true,false,'process',ICON_SUBMIT);
221 submit_center_last('go', _("Process Accruals"));//,true,false,'process',ICON_SUBMIT);
222 submit_js_confirm('go', _("Are you sure you want to post accruals?"));
223
224 end_form();
225
226 end_page();
227