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';
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_context = "Modifying Journal Entry";
36 $_SESSION['page_title'] = _($help_context = "Journal Entry");
38 page($_SESSION['page_title'], false, false,'', $js);
40 //--------------------------------------------------------------------------------------------------
42 function line_start_focus() {
45 unset($_POST['Index']);
46 $Ajax->activate('items_table');
47 set_focus('_code_id_edit');
49 //-----------------------------------------------------------------------------------------------
51 if (isset($_GET['AddedID']))
53 $trans_no = $_GET['AddedID'];
54 $trans_type = ST_JOURNAL;
56 display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
58 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
61 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
63 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
65 display_footer_exit();
66 } elseif (isset($_GET['UpdatedID']))
68 $trans_no = $_GET['UpdatedID'];
69 $trans_type = ST_JOURNAL;
71 display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
73 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
75 hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
77 display_footer_exit();
79 //--------------------------------------------------------------------------------------------------
81 if (isset($_GET['NewJournal']))
85 elseif (isset($_GET['ModifyGL']))
87 check_is_editable($_GET['trans_type'], $_GET['trans_no']);
89 if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
90 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
91 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
92 display_footer_exit();
95 create_cart($_GET['trans_type'], $_GET['trans_no']);
98 function create_cart($type=0, $trans_no=0)
102 if (isset($_SESSION['journal_items']))
104 unset ($_SESSION['journal_items']);
107 $cart = new items_cart($type);
108 $cart->order_id = $trans_no;
111 $result = get_gl_trans($type, $trans_no);
114 while ($row = db_fetch($result)) {
115 if ($row['amount'] == 0) continue;
116 $date = $row['tran_date'];
117 $cart->add_gl_item($row['account'], $row['dimension_id'],
118 $row['dimension2_id'], $row['amount'], $row['memo_'], '', $row['person_id']);
121 $cart->memo_ = get_comments_string($type, $trans_no);
122 $cart->tran_date = sql2date($date);
123 $cart->reference = $Refs->get($type, $trans_no);
124 $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
126 $cart->reference = $Refs->get_next(0);
127 $cart->tran_date = new_doc_date();
128 if (!is_date_in_fiscalyear($cart->tran_date))
129 $cart->tran_date = end_fiscalyear();
130 $_POST['ref_original'] = -1;
133 $_POST['memo_'] = $cart->memo_;
134 $_POST['ref'] = $cart->reference;
135 $_POST['date_'] = $cart->tran_date;
137 $_SESSION['journal_items'] = &$cart;
140 //-----------------------------------------------------------------------------------------------
142 if (isset($_POST['Process']))
147 if ($_SESSION['journal_items']->count_gl_items() < 1) {
148 display_error(_("You must enter at least one journal line."));
149 set_focus('code_id');
152 if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
154 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
155 set_focus('code_id');
159 if (!is_date($_POST['date_']))
161 display_error(_("The entered date is invalid."));
165 elseif (!is_date_in_fiscalyear($_POST['date_']))
167 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
171 if (!$Refs->is_valid($_POST['ref']))
173 display_error( _("You must enter a reference."));
177 elseif ($Refs->exists(ST_JOURNAL, $_POST['ref']))
179 // The reference can exist already so long as it's the same as the original (when modifying)
180 if ($_POST['ref'] != $_POST['ref_original']) {
181 display_error( _("The entered reference is already in use."));
186 if ($input_error == 1)
187 unset($_POST['Process']);
190 if (isset($_POST['Process']))
192 $cart = &$_SESSION['journal_items'];
193 $new = $cart->order_id == 0;
195 $cart->reference = $_POST['ref'];
196 $cart->memo_ = $_POST['memo_'];
197 $cart->tran_date = $_POST['date_'];
199 $trans_no = write_journal_entries($cart, check_value('Reverse'));
201 $cart->clear_items();
202 new_doc_date($_POST['date_']);
203 unset($_SESSION['journal_items']);
205 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
207 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
210 //-----------------------------------------------------------------------------------------------
212 function check_item_data()
214 if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id']))
216 display_error(_("Dimension is closed."));
217 set_focus('dimension_id');
221 if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id']))
223 display_error(_("Dimension is closed."));
224 set_focus('dimension2_id');
228 if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
230 display_error(_("You must enter either a debit amount or a credit amount."));
231 set_focus('AmountDebit');
235 if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0))
237 display_error(_("The debit amount entered is not a valid number or is less than zero."));
238 set_focus('AmountDebit');
240 } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
242 display_error(_("The credit amount entered is not a valid number or is less than zero."));
243 set_focus('AmountCredit');
247 if (!is_tax_gl_unique(get_post('code_id'))) {
248 display_error(_("Cannot post to GL account used by more than one tax type."));
249 set_focus('code_id');
253 if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id']))
255 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
256 set_focus('code_id');
263 //-----------------------------------------------------------------------------------------------
265 function handle_update_item()
267 if($_POST['UpdateItem'] != "" && check_item_data())
269 if (input_num('AmountDebit') > 0)
270 $amount = input_num('AmountDebit');
272 $amount = -input_num('AmountCredit');
274 $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'],
275 $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
280 //-----------------------------------------------------------------------------------------------
282 function handle_delete_item($id)
284 $_SESSION['journal_items']->remove_gl_item($id);
288 //-----------------------------------------------------------------------------------------------
290 function handle_new_item()
292 if (!check_item_data())
295 if (input_num('AmountDebit') > 0)
296 $amount = input_num('AmountDebit');
298 $amount = -input_num('AmountCredit');
300 $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
301 $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
305 //-----------------------------------------------------------------------------------------------
306 $id = find_submit('Delete');
308 handle_delete_item($id);
310 if (isset($_POST['AddItem']))
313 if (isset($_POST['UpdateItem']))
314 handle_update_item();
316 if (isset($_POST['CancelItemChanges']))
319 if (isset($_POST['go']))
321 display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
322 $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
325 //-----------------------------------------------------------------------------------------------
329 display_order_header($_SESSION['journal_items']);
331 start_table(TABLESTYLE2, "width=90%", 10);
334 display_gl_items(_("Rows"), $_SESSION['journal_items']);
335 gl_options_controls();
340 submit_center('Process', _("Process Journal Entry"), true ,
341 _('Process journal entry only if debits equal to credits'), 'default');
344 //------------------------------------------------------------------------------------------------