Merged changes from main trunk up to 2.2.7
[fa-stable.git] / gl / gl_journal.php
index b5a2c1cc287a05f0b400a9abb63df99ba6b0844e..035de8ac0c22fe4990911eb3496ccd09aac087a0 100644 (file)
@@ -1,7 +1,16 @@
 <?php
-
-$page_security = 3;
-$path_to_root="..";
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+$page_security = 'SA_JOURNALENTRY';
+$path_to_root = "..";
 include_once($path_to_root . "/includes/ui/items_cart.inc");
 
 include_once($path_to_root . "/includes/session.inc");
@@ -13,67 +22,112 @@ include_once($path_to_root . "/gl/includes/ui/gl_journal_ui.inc");
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 include_once($path_to_root . "/gl/includes/gl_ui.inc");
 
-$js = get_js_form_entry("CodeID2", "code_id", "AmountDebit");
+$js = '';
 if ($use_popup_windows)
        $js .= get_js_open_window(800, 500);
 if ($use_date_picker)
        $js .= get_js_date_picker();
-$js .= get_js_set_focus('CodeID2');
 
-page(_("Journal Entry"), false, false, "setFocus()", $js);
+if (isset($_GET['ModifyGL'])) {
+       $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."), 
+               $_GET['trans_no']);
+       $help_context = "Modifying Journal Entry";
+} else
+       $_SESSION['page_title'] = _($help_context = "Journal Entry");
+
+page($_SESSION['page_title'], false, false,'', $js);
+//--------------------------------------------------------------------------------------------------
+
+function line_start_focus() {
+  global       $Ajax;
 
+  $Ajax->activate('items_table');
+  set_focus('_code_id_edit');
+}
 //-----------------------------------------------------------------------------------------------
 
 if (isset($_GET['AddedID'])) 
 {
        $trans_no = $_GET['AddedID'];
-       $trans_type = systypes::journal_entry();
+       $trans_type = ST_JOURNAL;
 
        display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
 
-    display_note(get_gl_view_str($trans_type, $trans_no, _("View this Journal Entry")));
+    display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
 
-       hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Journal Entry"));
+       reset_focus();
+       hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
 
        display_footer_exit();
-}
+} elseif (isset($_GET['UpdatedID'])) 
+{
+       $trans_no = $_GET['UpdatedID'];
+       $trans_type = ST_JOURNAL;
 
-//--------------------------------------------------------------------------------------------------
+       display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
 
-function copy_to_je()
-{
-       $_SESSION['journal_items']->tran_date = $_POST['date_'];
-       $_SESSION['journal_items']->transfer_type = check_value('Reverse');
-       $_SESSION['journal_items']->memo_ = $_POST['memo_'];
-}
+    display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
 
+       hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
+
+       display_footer_exit();
+}
 //--------------------------------------------------------------------------------------------------
 
-function copy_from_je()
+if (isset($_GET['NewJournal']))
 {
-       $_POST['date_'] = $_SESSION['journal_items']->tran_date;
-       $_POST['Reverse'] = $_SESSION['journal_items']->transfer_type;
-       $_POST['memo_'] = $_SESSION['journal_items']->memo_;
+       create_cart(0,0);
+} 
+elseif (isset($_GET['ModifyGL']))
+{
+       if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
+               display_error(_("You can edit directly only journal entries created via Journal Entry page."));
+               hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
+               display_footer_exit();
+       }
+       create_cart($_GET['trans_type'], $_GET['trans_no']);
 }
 
-//----------------------------------------------------------------------------------------
-
-function handle_new_order()
+function create_cart($type=0, $trans_no=0)
 {
+       global $Refs;
+
        if (isset($_SESSION['journal_items']))
        {
-               $_SESSION['journal_items']->clear_items();
                unset ($_SESSION['journal_items']);
        }
 
-    session_register("journal_items");
+       $cart = new items_cart($type);
+    $cart->order_id = $trans_no;
+
+       if ($trans_no) {
+               $result = get_gl_trans($type, $trans_no);
+
+               if ($result) {
+                       while ($row = db_fetch($result)) {
+                               if ($row['amount'] == 0) continue;
+                               $date = $row['tran_date'];
+                               $cart->add_gl_item($row['account'], $row['dimension_id'], 
+                                       $row['dimension2_id'], $row['amount'], $row['memo_']);
+                       }
+               }
+               $cart->memo_ = get_comments_string($type, $trans_no);
+               $cart->tran_date = sql2date($date);
+               $cart->reference = $Refs->get($type, $trans_no);
+               $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
+       } else {
+               $cart->reference = $Refs->get_next(0);
+               $cart->tran_date = new_doc_date();
+               if (!is_date_in_fiscalyear($cart->tran_date))
+                       $cart->tran_date = end_fiscalyear();
+               $_POST['ref_original'] = -1;
+       }
 
-    $_SESSION['journal_items'] = new items_cart;
+       $_POST['memo_'] = $cart->memo_;
+       $_POST['ref'] = $cart->reference;
+       $_POST['date_'] = $cart->tran_date;
 
-       $_POST['date_'] = Today();
-       if (!is_date_in_fiscalyear($_POST['date_']))
-               $_POST['date_'] = end_fiscalyear();
-       $_SESSION['journal_items']->tran_date = $_POST['date_'];        
+       $_SESSION['journal_items'] = &$cart;
 }
 
 //-----------------------------------------------------------------------------------------------
@@ -83,42 +137,68 @@ if (isset($_POST['Process']))
 
        $input_error = 0;
 
+       if ($_SESSION['journal_items']->count_gl_items() < 1) {
+               display_error(_("You must enter at least one journal line."));
+               set_focus('code_id');
+               $input_error = 1;
+       }
+       if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
+       {
+               display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
+               set_focus('code_id');
+               $input_error = 1;
+       }
+
        if (!is_date($_POST['date_'])) 
        {
                display_error(_("The entered date is invalid."));
+               set_focus('date_');
                $input_error = 1;
        } 
        elseif (!is_date_in_fiscalyear($_POST['date_'])) 
        {
                display_error(_("The entered date is not in fiscal year."));
+               set_focus('date_');
                $input_error = 1;
        } 
-       elseif (!references::is_valid($_POST['ref'])) 
+       if (!$Refs->is_valid($_POST['ref'])) 
        {
                display_error( _("You must enter a reference."));
+               set_focus('ref');
                $input_error = 1;
        } 
-       elseif (references::exists(systypes::journal_entry(), $_POST['ref'])) 
+       elseif ($Refs->exists(ST_JOURNAL, $_POST['ref'])) 
        {
-               display_error( _("The entered reference is already in use."));
-               $input_error = 1;
+           // The reference can exist already so long as it's the same as the original (when modifying) 
+           if ($_POST['ref'] != $_POST['ref_original']) {
+               display_error( _("The entered reference is already in use."));
+               set_focus('ref');
+               $input_error = 1;
+           }
        }
-
        if ($input_error == 1)
                unset($_POST['Process']);
 }
 
 if (isset($_POST['Process']))
 {
+       $cart = &$_SESSION['journal_items'];
+       $new = $cart->order_id == 0;
 
-       $trans_no = add_journal_entries($_SESSION['journal_items']->gl_items,
-               $_POST['date_'], $_POST['ref'], check_value('Reverse'), $_POST['memo_']);
+       $cart->reference = $_POST['ref'];
+       $cart->memo_ = $_POST['memo_'];
+       $cart->tran_date = $_POST['date_'];
 
-       $_SESSION['journal_items']->clear_items();
-       unset($_SESSION['journal_items']);
+       $trans_no = write_journal_entries($cart, check_value('Reverse'));
 
-       meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
-} /*end of process credit note */
+       $cart->clear_items();
+       new_doc_date($_POST['date_']);
+       unset($_SESSION['journal_items']);
+       if($new)
+               meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
+       else
+               meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
+}
 
 //-----------------------------------------------------------------------------------------------
 
@@ -127,35 +207,46 @@ function check_item_data()
        if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
        {
                display_error(_("Dimension is closed."));
-                       return false;
+               set_focus('dimension_id');
+               return false;
        }
 
        if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
        {
                display_error(_("Dimension is closed."));
-                       return false;
+               set_focus('dimension2_id');
+               return false;
        }
 
-       if (!(!strlen($_POST['AmountDebit']) ^ !strlen($_POST['AmountCredit'])))
+       if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
        {
                display_error(_("You must enter either a debit amount or a credit amount."));
+               set_focus('AmountDebit');
                return false;
        }
 
        if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
        {
                display_error(_("The debit amount entered is not a valid number or is less than zero."));
+               set_focus('AmountDebit');
                return false;
        } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
        {
                display_error(_("The credit amount entered is not a valid number or is less than zero."));
+               set_focus('AmountCredit');
                return false;
        }
+       
+       if (!is_tax_gl_unique(get_post('code_id'))) {
+               display_error(_("Cannot post to GL account used by more than one tax type."));
+               set_focus('code_id');
+               return false;
+       }
 
-
-       if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
+       if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id'])) 
        {
                display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
+               set_focus('code_id');
                return false;
        }
 
@@ -173,16 +264,18 @@ function handle_update_item()
        else
                $amount = -input_num('AmountCredit');
 
-       $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
-               $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
+       $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
+           $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
     }
+       line_start_focus();
 }
 
 //-----------------------------------------------------------------------------------------------
 
-function handle_delete_item()
+function handle_delete_item($id)
 {
-       $_SESSION['journal_items']->remove_gl_item($_GET['Delete']);
+       $_SESSION['journal_items']->remove_gl_item($id);
+       line_start_focus();
 }
 
 //-----------------------------------------------------------------------------------------------
@@ -199,32 +292,29 @@ function handle_new_item()
        
        $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
                $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
+       line_start_focus();
 }
 
 //-----------------------------------------------------------------------------------------------
+$id = find_submit('Delete');
+if ($id != -1)
+       handle_delete_item($id);
 
-if (isset($_GET['Delete']) || isset($_GET['Edit']))
-       copy_from_je();
-
-if (isset($_GET['Delete']))
-       handle_delete_item();
-
-if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
-       copy_to_je();
-
-if (isset($_POST['AddItem']))
+if (isset($_POST['AddItem'])) 
        handle_new_item();
 
-if (isset($_POST['UpdateItem']))
+if (isset($_POST['UpdateItem'])) 
        handle_update_item();
+       
+if (isset($_POST['CancelItemChanges']))
+       line_start_focus();
 
-//-----------------------------------------------------------------------------------------------
-
-if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
+if (isset($_POST['go']))
 {
-       handle_new_order();
-}
-
+       display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
+       $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
+       line_start_focus();
+}      
 //-----------------------------------------------------------------------------------------------
 
 start_form();
@@ -240,18 +330,10 @@ echo "</td>";
 end_row();
 end_table(1);
 
-if ($_SESSION['journal_items']->count_gl_items() >= 1 &&
-       abs($_SESSION['journal_items']->gl_items_total()) < 0.0001)
-{
-    submit_center('Process', _("Process Journal Entry"));
-} 
-else 
-{
-       display_note(_("The journal must balance (debits equal to credits) before it can be processed."), 0, 1);
-}
+submit_center('Process', _("Process Journal Entry"), true , 
+       _('Process journal entry only if debits equal to credits'), 'default');
 
 end_form();
-
 //------------------------------------------------------------------------------------------------
 
 end_page();