Merged changes up to version 2.3.4 into unstable.
[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 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                 $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 or are closed for further data entry. 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_audit_trail(ST_JOURNAL, $id, $date);
134                                         add_comments(ST_JOURNAL, $id, $date, $memo);
135                                         $Refs->save(ST_JOURNAL, $id, $ref);
136                                 }
137                                 else
138                                 {
139                                         alt_table_row_color($k);
140                                         label_cell($date);
141                                         label_cell($_POST['acc_act'] . " " . get_gl_account_name($_POST['acc_act']));
142                                         if ($dim > 0)
143                                                 label_cell("");
144                                         if ($dim > 1)
145                                                 label_cell("");
146                                         display_debit_or_credit_cells($am0 * -1);
147                                         label_cell($memo);
148                                         alt_table_row_color($k);
149                                         label_cell($date);
150                                         label_cell($_POST['res_act'] . " " . get_gl_account_name($_POST['res_act']));
151                                         if ($dim > 0)
152                                                 label_cell(get_dimension_string($_POST['dimension_id'], true));
153                                         if ($dim > 1)
154                                                 label_cell(get_dimension_string($_POST['dimension2_id'], true));
155                                         display_debit_or_credit_cells($am0);
156                                         label_cell($memo);
157                                 }
158                         }
159                         if (isset($_POST['go']))
160                         {
161                                 commit_transaction();
162                                 display_notification_centered(_("Revenue / Cost Accruals have been processed."));
163                                 $_POST['date_'] = $_POST['amount'] = $_POST['periods'] = "";
164                         }
165                         else
166                         {
167                                 end_table(1);
168                                 display_notification_centered(_("Showing GL Transactions."));
169                         }
170                 }
171         }
172 }
173
174 function frequency_list_row($label, $name, $selected=null)
175 {
176         echo "<tr>\n";
177         label_cell($label, "class='label'");
178         echo "<td>\n";
179         $freq = array(
180                 '1'=> _("Weekly"),
181                 '2'=> _("Bi-weekly"),
182                 '3' => _("Monthly"),
183                 '4' => _("Quarterly"),
184         );
185         echo array_selector($name, $selected, $freq);
186         echo "</td>\n";
187         echo "</tr\n";
188 }
189
190 $dim = get_company_pref('use_dimension');
191
192 start_form(false, false, "", "accrual");
193 start_table(TABLESTYLE2);
194
195 date_row(_("Date"), 'date_', _('First date of Accruals'), true, 0, 0, 0, null, true);
196 start_row();
197 label_cell(_("Accrued Balance Account"), "class='label'");
198 gl_all_accounts_list_cells(null, 'acc_act', null, true, false, false, true);
199 end_row();
200 gl_all_accounts_list_row(_("Revenue / Cost Account"), 'res_act', null, true);
201
202 if ($dim >= 1)
203         dimensions_list_row(_("Dimension"), 'dimension_id', null, true, " ", false, 1);
204 if ($dim > 1)
205         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
206
207 $url = "gl/view/accrual_trans.php?act=".get_post('acc_act')."&date=".get_post('date_');
208 amount_row(_("Amount"), 'amount', null, null, viewer_link(_("Search Amount"), $url, "", "", ICON_VIEW));
209
210 frequency_list_row(_("Frequency"), 'freq', null);
211
212 text_row(_("Periods"), 'periods', null, 3, 3);
213 textarea_row(_("Memo"), 'memo_', null, 35, 3);
214
215 end_table(1);
216 submit_center_first('show', _("Show GL Rows"));//,true,false,'process',ICON_SUBMIT);
217 submit_center_last('go', _("Process Accruals"));//,true,false,'process',ICON_SUBMIT);
218 submit_js_confirm('go', _("Are you sure you want to post accruals?"));
219
220 end_form();
221
222 end_page();
223
224 ?>