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