Merged changes from main branch up to 2.1.3.
[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 = 3;
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
25 $js = '';
26 if ($use_popup_windows)
27         $js .= get_js_open_window(800, 500);
28 if ($use_date_picker)
29         $js .= get_js_date_picker();
30
31 if (isset($_GET['ModifyGL'])) {
32         $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."), 
33                 $_GET['trans_no']);
34         $help_page_title =_("Modifying Journal Transaction");
35 } else
36         $_SESSION['page_title'] = _("Journal Entry");
37
38 page($_SESSION['page_title'], false, false,'', $js);
39 //--------------------------------------------------------------------------------------------------
40
41 function line_start_focus() {
42   global        $Ajax;
43
44   $Ajax->activate('items_table');
45   set_focus('_code_id_edit');
46 }
47 //-----------------------------------------------------------------------------------------------
48
49 if (isset($_GET['AddedID'])) 
50 {
51         $trans_no = $_GET['AddedID'];
52         $trans_type = systypes::journal_entry();
53
54         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
55
56     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
57
58         reset_focus();
59         hyperlink_params($_SERVER['PHP_SELF'], _("Entry &New Journal Entry"), "NewJournal=Yes");
60
61         display_footer_exit();
62 } elseif (isset($_GET['UpdatedID'])) 
63 {
64         $trans_no = $_GET['UpdatedID'];
65         $trans_type = systypes::journal_entry();
66
67         display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
68
69     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
70
71         hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
72
73         display_footer_exit();
74 }
75 //--------------------------------------------------------------------------------------------------
76
77 if (isset($_GET['NewJournal']))
78 {
79         create_cart(0,0);
80
81 elseif (isset($_GET['ModifyGL']))
82 {
83         if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
84                 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
85                 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
86                 display_footer_exit();
87         }
88         create_cart($_GET['trans_type'], $_GET['trans_no']);
89 }
90
91 function create_cart($type=0, $trans_no=0)
92 {
93         if (isset($_SESSION['journal_items']))
94         {
95                 unset ($_SESSION['journal_items']);
96         }
97
98         $cart = new items_cart($type);
99     $cart->order_id = $trans_no;
100
101         if ($trans_no) {
102                 $result = get_gl_trans($type, $trans_no);
103
104                 if ($result) {
105                         while ($row = db_fetch($result)) {
106                                 if ($row['amount'] == 0) continue;
107                                 $date = $row['tran_date'];
108                                 $cart->add_gl_item($row['account'], $row['dimension_id'], 
109                                         $row['dimension2_id'], $row['amount'], $row['memo_']);
110                         }
111                 }
112                 $cart->memo_ = get_comments_string($type, $trans_no);
113                 $cart->tran_date = sql2date($date);
114                 $cart->reference = references::get($type, $trans_no);
115         } else {
116                 $cart->reference = references::get_next(0);
117                 $cart->tran_date = new_doc_date();
118         }
119         if (!is_date_in_fiscalyear($cart->tran_date))
120                 $cart->tran_date = end_fiscalyear();
121
122         $_POST['memo_'] = $cart->memo_;
123         $_POST['ref'] = $cart->reference;       
124         $_POST['date_'] = $cart->tran_date;
125
126         $_SESSION['journal_items'] = &$cart;
127 }
128
129 //-----------------------------------------------------------------------------------------------
130
131 if (isset($_POST['Process']))
132 {
133
134         $input_error = 0;
135
136         if ($_SESSION['journal_items']->count_gl_items() < 1) {
137                 display_error(_("You must enter at least one journal line."));
138                 set_focus('code_id');
139                 $input_error = 1;
140         }
141         if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
142         {
143                 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
144                 set_focus('code_id');
145                 $input_error = 1;
146         }
147
148         if (!is_date($_POST['date_'])) 
149         {
150                 display_error(_("The entered date is invalid."));
151                 set_focus('date_');
152                 $input_error = 1;
153         } 
154         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
155         {
156                 display_error(_("The entered date is not in fiscal year."));
157                 set_focus('date_');
158                 $input_error = 1;
159         } 
160         if ($_SESSION['journal_items']->order_id == 0) {
161                 if (!references::is_valid($_POST['ref'])) 
162                 {
163                         display_error( _("You must enter a reference."));
164                         set_focus('ref');
165                         $input_error = 1;
166                 } 
167                 elseif (references::exists(systypes::journal_entry(), $_POST['ref'])) 
168                 {
169                         display_error( _("The entered reference is already in use."));
170                         set_focus('ref');
171                         $input_error = 1;
172                 }
173         }
174         if ($input_error == 1)
175                 unset($_POST['Process']);
176 }
177
178 if (isset($_POST['Process']))
179 {
180         $cart = &$_SESSION['journal_items'];
181         $new = $cart->order_id == 0;
182
183         if ($new) 
184                 $cart->reference = $_POST['ref'];
185         $cart->memo_ = $_POST['memo_'];
186         $cart->tran_date = $_POST['date_'];
187
188         $trans_no = write_journal_entries($cart, check_value('Reverse'));
189
190         $cart->clear_items();
191         new_doc_date($_POST['date_']);
192         unset($_SESSION['journal_items']);
193         if($new)
194                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
195         else
196                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
197 }
198
199 //-----------------------------------------------------------------------------------------------
200
201 function check_item_data()
202 {
203         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
204         {
205                 display_error(_("Dimension is closed."));
206                 set_focus('dimension_id');
207                 return false;
208         }
209
210         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
211         {
212                 display_error(_("Dimension is closed."));
213                 set_focus('dimension2_id');
214                 return false;
215         }
216
217         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
218         {
219                 display_error(_("You must enter either a debit amount or a credit amount."));
220                 set_focus('AmountDebit');
221                 return false;
222         }
223
224         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
225         {
226                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
227                 set_focus('AmountDebit');
228                 return false;
229         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
230         {
231                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
232                 set_focus('AmountCredit');
233                 return false;
234         }
235         
236         if (!is_tax_gl_unique(get_post('code_id'))) {
237                 display_error(_("Cannot post to GL account used by more than one tax type."));
238                 set_focus('code_id');
239                 return false;
240         }
241
242         if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
243         {
244                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
245                 set_focus('code_id');
246                 return false;
247         }
248
249         return true;
250 }
251
252 //-----------------------------------------------------------------------------------------------
253
254 function handle_update_item()
255 {
256     if($_POST['UpdateItem'] != "" && check_item_data())
257     {
258         if (input_num('AmountDebit') > 0)
259                 $amount = input_num('AmountDebit');
260         else
261                 $amount = -input_num('AmountCredit');
262
263         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
264                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
265     }
266         line_start_focus();
267 }
268
269 //-----------------------------------------------------------------------------------------------
270
271 function handle_delete_item($id)
272 {
273         $_SESSION['journal_items']->remove_gl_item($id);
274         line_start_focus();
275 }
276
277 //-----------------------------------------------------------------------------------------------
278
279 function handle_new_item()
280 {
281         if (!check_item_data())
282                 return;
283
284         if (input_num('AmountDebit') > 0)
285                 $amount = input_num('AmountDebit');
286         else
287                 $amount = -input_num('AmountCredit');
288         
289         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
290                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
291         line_start_focus();
292 }
293
294 //-----------------------------------------------------------------------------------------------
295 $id = find_submit('Delete');
296 if ($id != -1)
297         handle_delete_item($id);
298
299 if (isset($_POST['AddItem'])) 
300         handle_new_item();
301
302 if (isset($_POST['UpdateItem'])) 
303         handle_update_item();
304         
305 if (isset($_POST['CancelItemChanges']))
306         line_start_focus();
307
308 if (isset($_POST['go']))
309 {
310         display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
311         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
312         line_start_focus();
313 }       
314 //-----------------------------------------------------------------------------------------------
315
316 start_form();
317
318 display_order_header($_SESSION['journal_items']);
319
320 start_table("$table_style2 width=90%", 10);
321 start_row();
322 echo "<td>";
323 display_gl_items(_("Rows"), $_SESSION['journal_items']);
324 gl_options_controls();
325 echo "</td>";
326 end_row();
327 end_table(1);
328
329 submit_center('Process', _("Process Journal Entry"), true , 
330         _('Process journal entry only if debits equal to credits'), 'default');
331
332 end_form();
333 //------------------------------------------------------------------------------------------------
334
335 end_page();
336
337 ?>