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