*** empty log message ***
[fa-stable.git] / gl / gl_journal.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/ui/items_cart.inc");
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/gl/includes/ui/gl_journal_ui.inc");
13 include_once($path_to_root . "/gl/includes/gl_db.inc");
14 include_once($path_to_root . "/gl/includes/gl_ui.inc");
15
16 $js = get_js_form_entry("CodeID2", "code_id", "AmountDebit");
17 if ($use_popup_windows)
18         $js .= get_js_open_window(800, 500);
19 $js .= get_js_set_focus('CodeID2');
20
21 page(_("Journal Entry"), false, false, "setFocus()", $js);
22
23 //-----------------------------------------------------------------------------------------------
24
25 if (isset($_GET['AddedID'])) 
26 {
27         $trans_no = $_GET['AddedID'];
28         $trans_type = systypes::journal_entry();
29
30         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
31
32     display_note(get_gl_view_str($trans_type, $trans_no, _("View this Journal Entry")));
33
34         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Journal Entry"));
35
36         display_footer_exit();
37 }
38
39 //--------------------------------------------------------------------------------------------------
40
41 function copy_to_je()
42 {
43         $_SESSION['journal_items']->tran_date = $_POST['date_'];
44         $_SESSION['journal_items']->transfer_type = check_value('Reverse');
45         $_SESSION['journal_items']->memo_ = $_POST['memo_'];
46 }
47
48 //--------------------------------------------------------------------------------------------------
49
50 function copy_from_je()
51 {
52         $_POST['date_'] = $_SESSION['journal_items']->tran_date;
53         $_POST['Reverse'] = $_SESSION['journal_items']->transfer_type;
54         $_POST['memo_'] = $_SESSION['journal_items']->memo_;
55 }
56
57 //----------------------------------------------------------------------------------------
58
59 function handle_new_order()
60 {
61         if (isset($_SESSION['journal_items']))
62         {
63                 $_SESSION['journal_items']->clear_items();
64                 unset ($_SESSION['journal_items']);
65         }
66
67     session_register("journal_items");
68
69     $_SESSION['journal_items'] = new items_cart;
70
71         $_POST['date_'] = Today();
72         if (!is_date_in_fiscalyear($_POST['date_']))
73                 $_POST['date_'] = end_fiscalyear();
74         $_SESSION['journal_items']->tran_date = $_POST['date_'];        
75 }
76
77 //-----------------------------------------------------------------------------------------------
78
79 if (isset($_POST['Process']))
80 {
81
82         $input_error = 0;
83
84         if (!is_date($_POST['date_'])) 
85         {
86                 display_error(_("The entered date is invalid."));
87                 $input_error = 1;
88         } 
89         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
90         {
91                 display_error(_("The entered date is not in fiscal year."));
92                 $input_error = 1;
93         } 
94         elseif (!references::is_valid($_POST['ref'])) 
95         {
96                 display_error( _("You must enter a reference."));
97                 $input_error = 1;
98         } 
99         elseif (references::exists(systypes::journal_entry(), $_POST['ref'])) 
100         {
101                 display_error( _("The entered reference is already in use."));
102                 $input_error = 1;
103         }
104
105         if ($input_error == 1)
106                 unset($_POST['Process']);
107 }
108
109 if (isset($_POST['Process']))
110 {
111
112         $trans_no = add_journal_entries($_SESSION['journal_items']->gl_items,
113                 $_POST['date_'], $_POST['ref'], check_value('Reverse'), $_POST['memo_']);
114
115         $_SESSION['journal_items']->clear_items();
116         unset($_SESSION['journal_items']);
117
118         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
119 } /*end of process credit note */
120
121 //-----------------------------------------------------------------------------------------------
122
123 function check_item_data()
124 {
125         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
126         {
127                 display_error(_("Dimension is closed."));
128                         return false;
129         }
130
131         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
132         {
133                 display_error(_("Dimension is closed."));
134                         return false;
135         }
136
137         if (strlen($_POST['AmountDebit']) && strlen($_POST['AmountCredit'])) 
138         {
139                 display_error(_("You cannot enter both a debit amount and a credit amount."));
140                 return false;
141         }
142
143         if ((!isset($_POST['AmountDebit']) && !isset($_POST['AmountCredit']))
144                         || ($_POST['AmountDebit'] == "" && $_POST['AmountCredit'] == "")) 
145         {
146                 display_error(_("You must enter either a debit amount or a credit amount."));
147                 return false;
148         }
149
150         if (isset($_POST['AmountDebit']) && $_POST['AmountDebit'] != "") 
151         {
152
153         if (!is_numeric($_POST['AmountDebit']))
154         {
155                 display_error(_("The debit amount entered is not a valid number."));
156                 return false;
157         }
158
159         if ($_POST['AmountDebit'] <= 0)
160         {
161                 display_error(_("The debit amount entered cannot be zero or negative."));
162                 return false;
163         }
164         }
165
166         if (isset($_POST['AmountCredit']) && $_POST['AmountCredit'] != "") 
167         {
168
169         if (!is_numeric($_POST['AmountCredit']))
170         {
171                 display_error(_("The credit amount entered is not a valid number."));
172                 return false;
173         }
174
175         if ($_POST['AmountCredit'] <= 0)
176         {
177                 display_error(_("The credit amount entered cannot be zero or negative."));
178                 return false;
179         }
180         }
181
182         if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
183         {
184                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
185                 return false;
186         }
187
188         return true;
189 }
190
191 //-----------------------------------------------------------------------------------------------
192
193 function handle_update_item()
194 {
195     if($_POST['UpdateItem'] != "" && check_item_data())
196     {
197         if ($_POST['AmountDebit'] > 0)
198                 $amount = $_POST['AmountDebit'];
199         else
200                 $amount = -$_POST['AmountCredit'];
201
202         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
203                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
204     }
205 }
206
207 //-----------------------------------------------------------------------------------------------
208
209 function handle_delete_item()
210 {
211         $_SESSION['journal_items']->remove_gl_item($_GET['Delete']);
212 }
213
214 //-----------------------------------------------------------------------------------------------
215
216 function handle_new_item()
217 {
218         if (!check_item_data())
219                 return;
220
221         if ($_POST['AmountDebit'] > 0)
222                 $amount = $_POST['AmountDebit'];
223         else
224                 $amount = -$_POST['AmountCredit'];
225         
226         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
227                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
228 }
229
230 //-----------------------------------------------------------------------------------------------
231
232 if (isset($_GET['Delete']) || isset($_GET['Edit']))
233         copy_from_je();
234
235 if (isset($_GET['Delete']))
236         handle_delete_item();
237
238 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
239         copy_to_je();
240
241 if (isset($_POST['AddItem']))
242         handle_new_item();
243
244 if (isset($_POST['UpdateItem']))
245         handle_update_item();
246
247 //-----------------------------------------------------------------------------------------------
248
249 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
250 {
251         handle_new_order();
252 }
253
254 //-----------------------------------------------------------------------------------------------
255
256 start_form();
257
258 display_order_header($_SESSION['journal_items']);
259
260 start_table("$table_style2 width=90%", 10);
261 start_row();
262 echo "<td>";
263 display_gl_items(_("Rows"), $_SESSION['journal_items']);
264 gl_options_controls();
265 echo "</td>";
266 end_row();
267 end_table(1);
268
269 if (!isset($_POST['Process']))
270 {
271         if ($_SESSION['journal_items']->count_gl_items() >= 1 &&
272                 abs($_SESSION['journal_items']->gl_items_total()) < 0.0001)
273         {
274             submit_center('Process', _("Process Journal Entry"));
275         } 
276         else 
277         {
278                 display_note(_("The journal must balance (debits equal to credits) before it can be processed."), 0, 1);
279         }
280 }
281
282 end_form();
283
284 //------------------------------------------------------------------------------------------------
285
286 end_page();
287
288 ?>