Form submit/escape hotkeys added.
[fa-stable.git] / admin / fiscalyears.php
index da533d828c4c3521ac33f3b82093afc895e65ae8..b3de0f89dd6184795e633112eeeaa6cae8c71ca3 100644 (file)
@@ -1,5 +1,14 @@
 <?php
-
+/**********************************************************************
+    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 = 9;
 $path_to_root="..";
 include_once($path_to_root . "/includes/session.inc");
@@ -15,15 +24,24 @@ page(_("Fiscal Years"), false, false, "", $js);
 simple_page_mode(true);
 //---------------------------------------------------------------------------------------------
 
+function is_date_in_fiscalyears($date)
+{
+       $date = date2sql($date);
+       $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
+
+       $result = db_query($sql, "could not get all fiscal years");
+       return db_fetch($result) !== false;
+}
+
 function check_data()
 {
-       if (!is_date($_POST['from_date']))
+       if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
        {
                display_error( _("Invalid BEGIN date in fiscal year."));
                set_focus('from_date');
                return false;
        }
-       if (!is_date($_POST['to_date']))
+       if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
        {
                display_error( _("Invalid END date in fiscal year."));
                set_focus('to_date');
@@ -37,23 +55,21 @@ function check_data()
        }
        return true;
 }
-
 //---------------------------------------------------------------------------------------------
 
 function handle_submit()
 {
        global $selected_id, $Mode;
 
-       if (!check_data())
-               return false;
-
        if ($selected_id != -1)
        {
-               update_fiscalyear($_POST['from_date'], $_POST['closed']);
+               update_fiscalyear($selected_id, $_POST['closed']);
                display_notification(_('Selected fiscal year has been updated'));
        }
        else
        {
+               if (!check_data())
+                       return false;
                add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
                display_notification(_('New fiscal year has been added'));
        }
@@ -86,12 +102,11 @@ function handle_delete()
 {
        global $selected_id, $Mode;
 
-       if (!check_can_delete($selected_id))
-               return;
+       if (check_can_delete($selected_id)) {
        //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
-
-       delete_fiscalyear($selected_id);
-       display_notification(_('Selected fiscal year has been deleted'));
+               delete_fiscalyear($selected_id);
+               display_notification(_('Selected fiscal year has been deleted'));
+       }
        $Mode = 'RESET';
 }
 
@@ -135,7 +150,9 @@ function display_fiscalyears()
                label_cell($closed_text);
                edit_button_cell("Edit".$myrow['id'], _("Edit"));
                if ($myrow["id"] != $company_year)
-                       edit_button_cell("Delete".$myrow['id'], _("Delete"));
+                       delete_button_cell("Delete".$myrow['id'], _("Delete"));
+               else
+                       label_cell('');
                end_row();
        }
 
@@ -163,10 +180,10 @@ function display_fiscalyear_edit($selected_id)
                        $_POST['to_date']  = sql2date($myrow["end"]);
                        $_POST['closed']  = $myrow["closed"];
                }
-                       hidden('from_date');
-                       hidden('to_date');
-                       label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
-                       label_row(_("Fiscal Year End:"), $_POST['to_date']);
+               hidden('from_date');
+               hidden('to_date');
+               label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
+               label_row(_("Fiscal Year End:"), $_POST['to_date']);
        }
        else
        {
@@ -179,14 +196,14 @@ function display_fiscalyear_edit($selected_id)
 
        end_table(1);
 
-       submit_add_or_update_center($selected_id == -1, '', true);
+       submit_add_or_update_center($selected_id == -1, '', 'default');
 
        end_form();
 }
 
 //---------------------------------------------------------------------------------------------
 
-if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
+if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 {
        handle_submit();
 }