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