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 ***********************************************************************/
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
16 include_once($path_to_root . "/includes/session.inc");
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
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");
26 if ($use_popup_windows)
27 $js .= get_js_open_window(800, 500);
29 $js .= get_js_date_picker();
31 if (isset($_GET['ModifyGL'])) {
32 $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."),
34 $help_page_title =_("Modifying Journal Transaction");
36 $_SESSION['page_title'] = _("Journal Entry");
38 page($_SESSION['page_title'], false, false,'', $js);
39 //--------------------------------------------------------------------------------------------------
41 function line_start_focus() {
44 $Ajax->activate('items_table');
45 set_focus('_code_id_edit');
47 //-----------------------------------------------------------------------------------------------
49 if (isset($_GET['AddedID']))
51 $trans_no = $_GET['AddedID'];
52 $trans_type = systypes::journal_entry();
54 display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
56 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
59 hyperlink_params($_SERVER['PHP_SELF'], _("Entry &New Journal Entry"), "NewJournal=Yes");
61 display_footer_exit();
62 } elseif (isset($_GET['UpdatedID']))
64 $trans_no = $_GET['UpdatedID'];
65 $trans_type = systypes::journal_entry();
67 display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
69 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
71 hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
73 display_footer_exit();
75 //--------------------------------------------------------------------------------------------------
77 if (isset($_GET['NewJournal']))
81 elseif (isset($_GET['ModifyGL']))
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();
88 create_cart($_GET['trans_type'], $_GET['trans_no']);
91 function create_cart($type=0, $trans_no=0)
93 if (isset($_SESSION['journal_items']))
95 unset ($_SESSION['journal_items']);
98 $cart = new items_cart($type);
99 $cart->order_id = $trans_no;
102 $result = get_gl_trans($type, $trans_no);
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_']);
112 $cart->memo_ = get_comments_string($type, $trans_no);
113 $cart->tran_date = sql2date($date);
114 $cart->reference = references::get($type, $trans_no);
116 $cart->reference = references::get_next(0);
117 $cart->tran_date = new_doc_date();
119 if (!is_date_in_fiscalyear($cart->tran_date))
120 $cart->tran_date = end_fiscalyear();
122 $_POST['memo_'] = $cart->memo_;
123 $_POST['ref'] = $cart->reference;
124 $_POST['date_'] = $cart->tran_date;
126 $_SESSION['journal_items'] = &$cart;
129 //-----------------------------------------------------------------------------------------------
131 if (isset($_POST['Process']))
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');
141 if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
143 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
144 set_focus('code_id');
148 if (!is_date($_POST['date_']))
150 display_error(_("The entered date is invalid."));
154 elseif (!is_date_in_fiscalyear($_POST['date_']))
156 display_error(_("The entered date is not in fiscal year."));
160 if ($_SESSION['journal_items']->order_id == 0) {
161 if (!references::is_valid($_POST['ref']))
163 display_error( _("You must enter a reference."));
167 elseif (references::exists(systypes::journal_entry(), $_POST['ref']))
169 display_error( _("The entered reference is already in use."));
174 if ($input_error == 1)
175 unset($_POST['Process']);
178 if (isset($_POST['Process']))
180 $cart = &$_SESSION['journal_items'];
181 $new = $cart->order_id == 0;
184 $cart->reference = $_POST['ref'];
185 $cart->memo_ = $_POST['memo_'];
186 $cart->tran_date = $_POST['date_'];
188 $trans_no = write_journal_entries($cart, check_value('Reverse'));
190 $cart->clear_items();
191 new_doc_date($_POST['date_']);
192 unset($_SESSION['journal_items']);
194 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
196 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
199 //-----------------------------------------------------------------------------------------------
201 function check_item_data()
203 if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id']))
205 display_error(_("Dimension is closed."));
206 set_focus('dimension_id');
210 if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id']))
212 display_error(_("Dimension is closed."));
213 set_focus('dimension2_id');
217 if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
219 display_error(_("You must enter either a debit amount or a credit amount."));
220 set_focus('AmountDebit');
224 if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0))
226 display_error(_("The debit amount entered is not a valid number or is less than zero."));
227 set_focus('AmountDebit');
229 } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
231 display_error(_("The credit amount entered is not a valid number or is less than zero."));
232 set_focus('AmountCredit');
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');
242 if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id']))
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');
252 //-----------------------------------------------------------------------------------------------
254 function handle_update_item()
256 if($_POST['UpdateItem'] != "" && check_item_data())
258 if (input_num('AmountDebit') > 0)
259 $amount = input_num('AmountDebit');
261 $amount = -input_num('AmountCredit');
263 $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
264 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
269 //-----------------------------------------------------------------------------------------------
271 function handle_delete_item($id)
273 $_SESSION['journal_items']->remove_gl_item($id);
277 //-----------------------------------------------------------------------------------------------
279 function handle_new_item()
281 if (!check_item_data())
284 if (input_num('AmountDebit') > 0)
285 $amount = input_num('AmountDebit');
287 $amount = -input_num('AmountCredit');
289 $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
290 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
294 //-----------------------------------------------------------------------------------------------
295 $id = find_submit('Delete');
297 handle_delete_item($id);
299 if (isset($_POST['AddItem']))
302 if (isset($_POST['UpdateItem']))
303 handle_update_item();
305 if (isset($_POST['CancelItemChanges']))
308 if (isset($_POST['go']))
310 display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
311 $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
314 //-----------------------------------------------------------------------------------------------
318 display_order_header($_SESSION['journal_items']);
320 start_table("$table_style2 width=90%", 10);
323 display_gl_items(_("Rows"), $_SESSION['journal_items']);
324 gl_options_controls();
329 submit_center('Process', _("Process Journal Entry"), true ,
330 _('Process journal entry only if debits equal to credits'), 'default');
333 //------------------------------------------------------------------------------------------------