Cleanup, $SysPrefs.
[fa-stable.git] / gl / gl_journal.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_JOURNALENTRY';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/gl/includes/ui/gl_journal_ui.inc");
22 include_once($path_to_root . "/gl/includes/gl_db.inc");
23 include_once($path_to_root . "/gl/includes/gl_ui.inc");
24 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
25
26 $js = '';
27 if ($use_popup_windows)
28         $js .= get_js_open_window(800, 500);
29 if (user_use_date_picker())
30         $js .= get_js_date_picker();
31
32 if (isset($_GET['ModifyGL'])) {
33         $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."), 
34                 $_GET['trans_no']);
35         $help_context = "Modifying Journal Entry";
36 } else
37         $_SESSION['page_title'] = _($help_context = "Journal Entry");
38
39 page($_SESSION['page_title'], false, false,'', $js);
40
41 //--------------------------------------------------------------------------------------------------
42
43 function line_start_focus() {
44   global        $Ajax;
45
46   unset($_POST['Index']);
47   $Ajax->activate('items_table');
48   set_focus('_code_id_edit');
49 }
50 //-----------------------------------------------------------------------------------------------
51
52 if (isset($_GET['AddedID'])) 
53 {
54         $trans_no = $_GET['AddedID'];
55         $trans_type = ST_JOURNAL;
56
57         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
58
59     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
60
61         reset_focus();
62         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
63
64         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
65
66         display_footer_exit();
67 } elseif (isset($_GET['UpdatedID'])) 
68 {
69         $trans_no = $_GET['UpdatedID'];
70         $trans_type = ST_JOURNAL;
71
72         display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
73
74     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
75
76         hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
77
78         display_footer_exit();
79 }
80 //--------------------------------------------------------------------------------------------------
81
82 if (isset($_GET['NewJournal']))
83 {
84         create_cart(0,0);
85 }
86 elseif (isset($_GET['ModifyGL']))
87 {
88         check_is_editable($_GET['trans_type'], $_GET['trans_no']);
89
90         if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
91                 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
92                 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
93                 display_footer_exit();
94         }
95
96         create_cart($_GET['trans_type'], $_GET['trans_no']);
97 }
98
99 function create_cart($type=0, $trans_no=0)
100 {
101         global $Refs;
102
103         if (isset($_SESSION['journal_items']))
104         {
105                 unset ($_SESSION['journal_items']);
106         }
107
108         $cart = new items_cart($type);
109     $cart->order_id = $trans_no;
110
111         if ($trans_no) {
112                 $result = get_gl_trans($type, $trans_no);
113
114                 if ($result) {
115                         while ($row = db_fetch($result)) {
116                                 if ($row['amount'] == 0) continue;
117                                 $date = $row['tran_date'];
118                                 $cart->add_gl_item($row['account'], $row['dimension_id'], 
119                                         $row['dimension2_id'], $row['amount'], $row['memo_'], '', $row['person_id']);
120                         }
121                 }
122                 $cart->memo_ = get_comments_string($type, $trans_no);
123                 $cart->tran_date = sql2date($date);
124                 $cart->reference = $Refs->get($type, $trans_no);
125                 $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
126         } else {
127                 $cart->reference = $Refs->get_next(0);
128                 $cart->tran_date = new_doc_date();
129                 if (!is_date_in_fiscalyear($cart->tran_date))
130                         $cart->tran_date = end_fiscalyear();
131                 $_POST['ref_original'] = -1;
132         }
133
134         $_POST['memo_'] = $cart->memo_;
135         $_POST['ref'] = $cart->reference;
136         $_POST['date_'] = $cart->tran_date;
137
138         $_SESSION['journal_items'] = &$cart;
139 }
140
141 //-----------------------------------------------------------------------------------------------
142
143 if (isset($_POST['Process']))
144 {
145
146         $input_error = 0;
147
148         if ($_SESSION['journal_items']->count_gl_items() < 1) {
149                 display_error(_("You must enter at least one journal line."));
150                 set_focus('code_id');
151                 $input_error = 1;
152         }
153         if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
154         {
155                 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
156                 set_focus('code_id');
157                 $input_error = 1;
158         }
159
160         if (!is_date($_POST['date_'])) 
161         {
162                 display_error(_("The entered date is invalid."));
163                 set_focus('date_');
164                 $input_error = 1;
165         } 
166         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
167         {
168                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
169                 set_focus('date_');
170                 $input_error = 1;
171         } 
172         if (!$Refs->is_valid($_POST['ref'])) 
173         {
174                 display_error( _("You must enter a reference."));
175                 set_focus('ref');
176                 $input_error = 1;
177         } 
178         elseif ($Refs->exists(ST_JOURNAL, $_POST['ref'])) 
179         {
180             // The reference can exist already so long as it's the same as the original (when modifying) 
181             if ($_POST['ref'] != $_POST['ref_original']) {
182                 display_error( _("The entered reference is already in use."));
183                 set_focus('ref');
184                 $input_error = 1;
185             }
186         }
187         if ($input_error == 1)
188                 unset($_POST['Process']);
189 }
190
191 if (isset($_POST['Process']))
192 {
193         $cart = &$_SESSION['journal_items'];
194         $new = $cart->order_id == 0;
195
196         $cart->reference = $_POST['ref'];
197         $cart->memo_ = $_POST['memo_'];
198         $cart->tran_date = $_POST['date_'];
199
200         $trans_no = write_journal_entries($cart, check_value('Reverse'));
201
202         $cart->clear_items();
203         new_doc_date($_POST['date_']);
204         unset($_SESSION['journal_items']);
205         if($new)
206                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
207         else
208                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
209 }
210
211 //-----------------------------------------------------------------------------------------------
212
213 function check_item_data()
214 {
215         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
216         {
217                 display_error(_("Dimension is closed."));
218                 set_focus('dimension_id');
219                 return false;
220         }
221
222         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
223         {
224                 display_error(_("Dimension is closed."));
225                 set_focus('dimension2_id');
226                 return false;
227         }
228
229         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
230         {
231                 display_error(_("You must enter either a debit amount or a credit amount."));
232                 set_focus('AmountDebit');
233                 return false;
234         }
235
236         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
237         {
238                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
239                 set_focus('AmountDebit');
240                 return false;
241         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
242         {
243                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
244                 set_focus('AmountCredit');
245                 return false;
246         }
247         
248         if (!is_tax_gl_unique(get_post('code_id'))) {
249                 display_error(_("Cannot post to GL account used by more than one tax type."));
250                 set_focus('code_id');
251                 return false;
252         }
253
254         if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id'])) 
255         {
256                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
257                 set_focus('code_id');
258                 return false;
259         }
260
261         return true;
262 }
263
264 //-----------------------------------------------------------------------------------------------
265
266 function handle_update_item()
267 {
268     if($_POST['UpdateItem'] != "" && check_item_data())
269     {
270         if (input_num('AmountDebit') > 0)
271                 $amount = input_num('AmountDebit');
272         else
273                 $amount = -input_num('AmountCredit');
274
275         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
276             $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
277     }
278         line_start_focus();
279 }
280
281 //-----------------------------------------------------------------------------------------------
282
283 function handle_delete_item($id)
284 {
285         $_SESSION['journal_items']->remove_gl_item($id);
286         line_start_focus();
287 }
288
289 //-----------------------------------------------------------------------------------------------
290
291 function handle_new_item()
292 {
293         if (!check_item_data())
294                 return;
295
296         if (input_num('AmountDebit') > 0)
297                 $amount = input_num('AmountDebit');
298         else
299                 $amount = -input_num('AmountCredit');
300         
301         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
302                 $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
303         line_start_focus();
304 }
305
306 //-----------------------------------------------------------------------------------------------
307 $id = find_submit('Delete');
308 if ($id != -1)
309         handle_delete_item($id);
310
311 if (isset($_POST['AddItem'])) 
312         handle_new_item();
313
314 if (isset($_POST['UpdateItem'])) 
315         handle_update_item();
316         
317 if (isset($_POST['CancelItemChanges']))
318         line_start_focus();
319
320 if (isset($_POST['go']))
321 {
322         display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
323         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
324         line_start_focus();
325 }       
326 //-----------------------------------------------------------------------------------------------
327
328 start_form();
329
330 display_order_header($_SESSION['journal_items']);
331
332 start_table(TABLESTYLE2, "width=90%", 10);
333 start_row();
334 echo "<td>";
335 display_gl_items(_("Rows"), $_SESSION['journal_items']);
336 gl_options_controls();
337 echo "</td>";
338 end_row();
339 end_table(1);
340
341 submit_center('Process', _("Process Journal Entry"), true , 
342         _('Process journal entry only if debits equal to credits'), 'default');
343
344 end_form();
345 //------------------------------------------------------------------------------------------------
346
347 end_page();
348
349 ?>