Inserted Copyright Notice and fixed graphic items
[fa-stable.git] / gl / gl_journal.php
index b5a2c1cc287a05f0b400a9abb63df99ba6b0844e..fe4b56c2669dfed0cc54dd9e63f8b87da604b2ed 100644 (file)
@@ -1,5 +1,14 @@
 <?php
-
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU Affero General Public License,
+       AGPL, 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/agpl-3.0.html>.
+***********************************************************************/
 $page_security = 3;
 $path_to_root="..";
 include_once($path_to_root . "/includes/ui/items_cart.inc");
@@ -13,15 +22,21 @@ 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);
+page(_("Journal Entry"), false, false,'', $js);
+
+//--------------------------------------------------------------------------------------------------
+function line_start_focus() {
+  global       $Ajax;
 
+  $Ajax->activate('items_table');
+  set_focus('_code_id_edit');
+}
 //-----------------------------------------------------------------------------------------------
 
 if (isset($_GET['AddedID'])) 
@@ -31,33 +46,14 @@ if (isset($_GET['AddedID']))
 
        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"));
+       hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Journal Entry"));
 
        display_footer_exit();
 }
-
-//--------------------------------------------------------------------------------------------------
-
-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_'];
-}
-
 //--------------------------------------------------------------------------------------------------
 
-function copy_from_je()
-{
-       $_POST['date_'] = $_SESSION['journal_items']->tran_date;
-       $_POST['Reverse'] = $_SESSION['journal_items']->transfer_type;
-       $_POST['memo_'] = $_SESSION['journal_items']->memo_;
-}
-
-//----------------------------------------------------------------------------------------
-
 function handle_new_order()
 {
        if (isset($_SESSION['journal_items']))
@@ -68,7 +64,7 @@ function handle_new_order()
 
     session_register("journal_items");
 
-    $_SESSION['journal_items'] = new items_cart;
+    $_SESSION['journal_items'] = new items_cart(systypes::journal_entry());
 
        $_POST['date_'] = Today();
        if (!is_date_in_fiscalyear($_POST['date_']))
@@ -83,24 +79,41 @@ 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 (!references::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'])) 
        {
                display_error( _("The entered reference is already in use."));
+               set_focus('ref');
                $input_error = 1;
        }
 
@@ -127,28 +140,33 @@ 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;
        }
 
@@ -156,6 +174,7 @@ function check_item_data()
        if ($_SESSION["wa_current_user"]->access != 2 && 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;
        }
 
@@ -176,13 +195,15 @@ function handle_update_item()
        $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_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,25 +220,80 @@ 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();
 }
 
-//-----------------------------------------------------------------------------------------------
-
-if (isset($_GET['Delete']) || isset($_GET['Edit']))
-       copy_from_je();
-
-if (isset($_GET['Delete']))
-       handle_delete_item();
+function display_quick_entries(&$cart)
+{
+       if (!get_post('person_id'))
+       {
+               display_error( _("No Quick Entries are defined."));
+               set_focus('totamount');
+       }
+       else
+       {
+               $rate = 0;
+               $totamount = input_num('totamount');
+               //$totamount = ($cart->trans_type==systypes::bank_payment() ? 1:-1) * $totamount;
+               $qe = get_quick_entry($_POST['person_id']);
+               $qe_lines = get_quick_entry_lines($_POST['person_id']);
+               while ($qe_line = db_fetch($qe_lines))
+               {
+                       if ($qe_line['tax_acc'])
+                       {
+                               $account = get_gl_account($qe_line['account']);
+                               $tax_group = $account['tax_code'];
+                               $items = get_tax_group_items($tax_group);
+                               while ($item = db_fetch($items))
+                                       $rate += $item['rate'];
+                               if ($rate != 0)
+                                       $totamount = $totamount * 100 / ($rate + 100);
+                               $cart->clear_items();
+
+                               $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
+                                       $totamount, $qe['description']);
+                               $items = get_tax_group_items($tax_group);
+                               while ($item = db_fetch($items))
+                               {
+                                       if ($item['rate'] != 0)
+                                       {
+                                               $amount = $totamount * $item['rate'] / 100;
+                                               $code = ($amount < 0 ? $item['purchasing_gl_code'] : 
+                                                       $item['sales_gl_code']);
+                                               $cart->add_gl_item($code, 0, 0, $amount, $qe['description']);
+                                       }
+                               }
+                       }
+                       else
+                       {
+                               if ($qe_line['pct'])
+                                       $amount = $totamount * $qe_line['amount'] / 100;
+                               else
+                                       $amount = $qe_line['amount'];
+                               $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
+                                       $amount, $qe['description']);
+                       }               
+               }
+               line_start_focus();
+       }       
+}
 
-if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
-       copy_to_je();
+//-----------------------------------------------------------------------------------------------
+$id = find_submit('Delete');
+if ($id != -1)
+       handle_delete_item($id);
 
-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($_POST['go']))
+       display_quick_entries($_SESSION['journal_items']);
 //-----------------------------------------------------------------------------------------------
 
 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
@@ -240,18 +316,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'), true);
 
 end_form();
-
 //------------------------------------------------------------------------------------------------
 
 end_page();