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