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