Form submit/escape hotkeys added.
[fa-stable.git] / admin / fiscalyears.php
index d6cd8c0138b432467bfbd13bb0edafe188ed49dd..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");
@@ -12,40 +21,33 @@ if ($use_date_picker)
        $js .= get_js_date_picker();
 page(_("Fiscal Years"), false, false, "", $js);
 
+simple_page_mode(true);
 //---------------------------------------------------------------------------------------------
 
-if (isset($_GET['selected_id']))
-{
-       $selected_id = $_GET['selected_id'];
-}
-elseif (isset($_POST['selected_id']))
+function is_date_in_fiscalyears($date)
 {
-       $selected_id = $_POST['selected_id'];
-}
-else
-       $selected_id = null;
+       $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 (!isset($selected_id))
-               $from = $_POST['from_date'];
-       else
-               $from = $selected_id;
-       if (!is_date($from) || get_fiscalyear($from) !== false)
+       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');
                return false;
        }
-       if (date1_greater_date2($from, $_POST['to_date']))
+       if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
        {
                display_error( _("BEGIN date bigger than END date."));
                set_focus('from_date');
@@ -53,37 +55,35 @@ function check_data()
        }
        return true;
 }
-
 //---------------------------------------------------------------------------------------------
 
 function handle_submit()
 {
-       global $selected_id;
-
-       if (!check_data())
-               return false;
+       global $selected_id, $Mode;
 
-       if (isset($selected_id))
+       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'));
        }
-
-       return true;
+       $Mode = 'RESET';
 }
 
 //---------------------------------------------------------------------------------------------
 
-function check_can_delete($todate)
+function check_can_delete($selected_id)
 {
-       global $selected_id;
-
+       $myrow = get_fiscalyear($selected_id);
        // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
-       $from = date2sql($selected_id);
-       $to = date2sql($todate);
+       $from = $myrow['begin'];
+       $to = $myrow['end'];
        $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
        $result = db_query($sql, "could not query gl_trans master");
        $myrow = db_fetch_row($result);
@@ -98,17 +98,16 @@ function check_can_delete($todate)
 
 //---------------------------------------------------------------------------------------------
 
-function handle_delete($todate)
+function handle_delete()
 {
-       global $selected_id;
+       global $selected_id, $Mode;
 
-       if (!check_can_delete($todate))
-               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);
-
-       meta_forward($_SERVER['PHP_SELF']);
+               delete_fiscalyear($selected_id);
+               display_notification(_('Selected fiscal year has been deleted'));
+       }
+       $Mode = 'RESET';
 }
 
 //---------------------------------------------------------------------------------------------
@@ -120,7 +119,7 @@ function display_fiscalyears()
        $company_year = get_company_pref('f_year');
 
        $result = get_all_fiscalyears();
-
+       start_form();
        start_table($table_style);
 
        $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
@@ -149,13 +148,16 @@ function display_fiscalyears()
                label_cell($from);
                label_cell($to);
                label_cell($closed_text);
-               edit_link_cell("selected_id=" . urlencode($from));
+               edit_button_cell("Edit".$myrow['id'], _("Edit"));
                if ($myrow["id"] != $company_year)
-                       delete_link_cell("selected_id=" . urlencode($from) . "&to_date=" . urlencode($to) . "&delete=1");
+                       delete_button_cell("Delete".$myrow['id'], _("Delete"));
+               else
+                       label_cell('');
                end_row();
        }
 
-       end_table();;
+       end_table();
+       end_form();
        display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
 }
 
@@ -163,21 +165,23 @@ function display_fiscalyears()
 
 function display_fiscalyear_edit($selected_id)
 {
-       global $table_style2;
+       global $table_style2, $Mode;
 
        start_form();
        start_table($table_style2);
 
-       if ($selected_id)
+       if ($selected_id != -1)
        {
-               $myrow = get_fiscalyear($selected_id);
-
-               $_POST['from_date'] = sql2date($myrow["begin"]);
-               $_POST['to_date']  = sql2date($myrow["end"]);
-               $_POST['closed']  = $myrow["closed"];
-               hidden('selected_id', $selected_id);
-               hidden('from_date', $_POST['from_date']);
-               hidden('to_date', $_POST['to_date']);
+               if($Mode =='Edit')
+               {
+                       $myrow = get_fiscalyear($selected_id);
+
+                       $_POST['from_date'] = sql2date($myrow["begin"]);
+                       $_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']);
        }
@@ -186,38 +190,41 @@ function display_fiscalyear_edit($selected_id)
                date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
                date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
        }
+       hidden('selected_id', $selected_id);
 
        yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
 
        end_table(1);
 
-       submit_add_or_update_center(!isset($selected_id));
+       submit_add_or_update_center($selected_id == -1, '', 'default');
 
        end_form();
 }
 
 //---------------------------------------------------------------------------------------------
 
-if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
+if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 {
-       if (handle_submit())
-       {
-               meta_forward($_SERVER['PHP_SELF']);
-       }
+       handle_submit();
 }
 
 //---------------------------------------------------------------------------------------------
 
-if (isset($_GET['delete']))
+if ($Mode == 'Delete')
 {
-       handle_delete($_GET['to_date']);
+       global $selected_id;
+       handle_delete($selected_id);
 }
 
+if ($Mode == 'RESET')
+{
+       $selected_id = -1;
+}
 //---------------------------------------------------------------------------------------------
 
 display_fiscalyears();
 
-hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter a New Fiscal Year"));
+echo '<br>';
 
 display_fiscalyear_edit($selected_id);