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");
24 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
27 if ($SysPrefs->use_popup_windows)
28 $js .= get_js_open_window(800, 500);
29 if (user_use_date_picker())
30 $js .= get_js_date_picker();
32 if (isset($_GET['ModifyGL'])) {
33 $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."),
35 $help_context = "Modifying Journal Entry";
37 $_SESSION['page_title'] = _($help_context = "Journal Entry");
39 page($_SESSION['page_title'], false, false,'', $js);
41 //--------------------------------------------------------------------------------------------------
43 function line_start_focus() {
46 unset($_POST['Index']);
47 $Ajax->activate('items_table');
48 set_focus('_code_id_edit');
50 //-----------------------------------------------------------------------------------------------
52 if (isset($_GET['AddedID']))
54 $trans_no = $_GET['AddedID'];
55 $trans_type = ST_JOURNAL;
57 display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
59 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
62 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
64 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
66 display_footer_exit();
67 } elseif (isset($_GET['UpdatedID']))
69 $trans_no = $_GET['UpdatedID'];
70 $trans_type = ST_JOURNAL;
72 display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
74 display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
76 hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
78 display_footer_exit();
80 //--------------------------------------------------------------------------------------------------
82 if (isset($_GET['NewJournal']))
86 elseif (isset($_GET['ModifyGL']))
88 check_is_editable($_GET['trans_type'], $_GET['trans_no']);
90 if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
91 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
92 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
93 display_footer_exit();
96 create_cart($_GET['trans_type'], $_GET['trans_no']);
99 function create_cart($type=0, $trans_no=0)
103 if (isset($_SESSION['journal_items']))
105 unset ($_SESSION['journal_items']);
108 $cart = new items_cart($type);
109 $cart->order_id = $trans_no;
112 $result = get_gl_trans($type, $trans_no);
115 while ($row = db_fetch($result)) {
116 if ($row['amount'] == 0) continue;
117 $date = $row['tran_date'];
118 $cart->add_gl_item($row['account'], $row['dimension_id'],
119 $row['dimension2_id'], $row['amount'], $row['memo_'], '', $row['person_id']);
122 $cart->memo_ = get_comments_string($type, $trans_no);
123 $cart->tran_date = sql2date($date);
124 $cart->reference = $Refs->get($type, $trans_no);
125 $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
127 $cart->reference = $Refs->get_next(0);
128 $cart->tran_date = new_doc_date();
129 if (!is_date_in_fiscalyear($cart->tran_date))
130 $cart->tran_date = end_fiscalyear();
131 $_POST['ref_original'] = -1;
134 $_POST['memo_'] = $cart->memo_;
135 $_POST['ref'] = $cart->reference;
136 $_POST['date_'] = $cart->tran_date;
138 $_SESSION['journal_items'] = &$cart;
141 //-----------------------------------------------------------------------------------------------
143 if (isset($_POST['Process']))
148 if ($_SESSION['journal_items']->count_gl_items() < 1) {
149 display_error(_("You must enter at least one journal line."));
150 set_focus('code_id');
153 if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
155 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
156 set_focus('code_id');
160 if (!is_date($_POST['date_']))
162 display_error(_("The entered date is invalid."));
166 elseif (!is_date_in_fiscalyear($_POST['date_']))
168 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
172 if (!$Refs->is_valid($_POST['ref']))
174 display_error( _("You must enter a reference."));
178 elseif ($Refs->exists(ST_JOURNAL, $_POST['ref']))
180 // The reference can exist already so long as it's the same as the original (when modifying)
181 if ($_POST['ref'] != $_POST['ref_original']) {
182 display_error( _("The entered reference is already in use."));
187 if ($input_error == 1)
188 unset($_POST['Process']);
191 if (isset($_POST['Process']))
193 $cart = &$_SESSION['journal_items'];
194 $new = $cart->order_id == 0;
196 $cart->reference = $_POST['ref'];
197 $cart->memo_ = $_POST['memo_'];
198 $cart->tran_date = $_POST['date_'];
200 $trans_no = write_journal_entries($cart, check_value('Reverse'));
202 $cart->clear_items();
203 new_doc_date($_POST['date_']);
204 unset($_SESSION['journal_items']);
206 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
208 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
211 //-----------------------------------------------------------------------------------------------
213 function check_item_data()
215 if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id']))
217 display_error(_("Dimension is closed."));
218 set_focus('dimension_id');
222 if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id']))
224 display_error(_("Dimension is closed."));
225 set_focus('dimension2_id');
229 if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
231 display_error(_("You must enter either a debit amount or a credit amount."));
232 set_focus('AmountDebit');
236 if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0))
238 display_error(_("The debit amount entered is not a valid number or is less than zero."));
239 set_focus('AmountDebit');
241 } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
243 display_error(_("The credit amount entered is not a valid number or is less than zero."));
244 set_focus('AmountCredit');
248 if (!is_tax_gl_unique(get_post('code_id'))) {
249 display_error(_("Cannot post to GL account used by more than one tax type."));
250 set_focus('code_id');
254 if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id']))
256 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
257 set_focus('code_id');
264 //-----------------------------------------------------------------------------------------------
266 function handle_update_item()
268 if($_POST['UpdateItem'] != "" && check_item_data())
270 if (input_num('AmountDebit') > 0)
271 $amount = input_num('AmountDebit');
273 $amount = -input_num('AmountCredit');
275 $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'],
276 $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
281 //-----------------------------------------------------------------------------------------------
283 function handle_delete_item($id)
285 $_SESSION['journal_items']->remove_gl_item($id);
289 //-----------------------------------------------------------------------------------------------
291 function handle_new_item()
293 if (!check_item_data())
296 if (input_num('AmountDebit') > 0)
297 $amount = input_num('AmountDebit');
299 $amount = -input_num('AmountCredit');
301 $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
302 $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
306 //-----------------------------------------------------------------------------------------------
307 $id = find_submit('Delete');
309 handle_delete_item($id);
311 if (isset($_POST['AddItem']))
314 if (isset($_POST['UpdateItem']))
315 handle_update_item();
317 if (isset($_POST['CancelItemChanges']))
320 if (isset($_POST['go']))
322 display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
323 $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
326 //-----------------------------------------------------------------------------------------------
330 display_order_header($_SESSION['journal_items']);
332 start_table(TABLESTYLE2, "width=90%", 10);
335 display_gl_items(_("Rows"), $_SESSION['journal_items']);
336 gl_options_controls();
341 submit_center('Process', _("Process Journal Entry"), true ,
342 _('Process journal entry only if debits equal to credits'), 'default');
345 //------------------------------------------------------------------------------------------------