Fixed error during new category addition.
[fa-stable.git] / admin / fiscalyears.php
index 9bbf4040579ac14c45f0a948c0675593b8251e55..48deeb972da8b019164266b2422b52b7161280ed 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");
@@ -24,9 +33,32 @@ function is_date_in_fiscalyears($date)
        return db_fetch($result) !== false;
 }
 
+function is_bad_begin_date($date)
+{
+       $bdate = date2sql($date);
+       $sql = "SELECT MAX(end) FROM ".TB_PREF."fiscal_year WHERE begin < '$bdate'";
+
+       $result = db_query($sql, "could not retrieve last fiscal years");
+       $row = db_fetch_row($result);
+       if ($row[0] === null)
+               return false;
+       $max = add_days(sql2date($row[0]), 1);
+       return ($max !== $date);
+}
+
+function check_open_before($date)
+{
+       $date = date2sql($date);
+       $sql = "SELECT COUNT(*) FROM ".TB_PREF."fiscal_year WHERE begin < '$date' AND closed=0";
+
+       $result = db_query($sql, "could not check open fiscal years");
+       $row = db_fetch_row($result);
+       return ($row[0] > 0);
+}
+
 function check_data()
 {
-       if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
+       if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']) || is_bad_begin_date($_POST['from_date']))
        {
                display_error( _("Invalid BEGIN date in fiscal year."));
                set_focus('from_date');
@@ -47,21 +79,74 @@ function check_data()
        return true;
 }
 //---------------------------------------------------------------------------------------------
+function close_year($year)
+{
+       $myrow = get_fiscalyear($year);
+       $to = $myrow['end'];
+       // retrieve total balances from balance sheet accounts
+    $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
+       INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid 
+               WHERE balance_sheet=1 AND tran_date <= '$to'";
+       $result = db_query($sql, "The total balance could not be calculated");
+
+       $row = db_fetch_row($result);
+       $balance = round2($row[0], user_price_dec());
+
+       begin_transaction();
+       $to = sql2date($to);
+
+       if ($balance != 0.0)
+       {
+               $co = get_company_prefs();
+
+               $trans_type = systypes::journal_entry();
+               $trans_id = get_next_trans_no($trans_type);
+
+               add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
+                       0, 0, _("Closing Year"), -$balance);
+               add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
+                       0, 0, _("Closing Year"), $balance);
+
+       }       
+               close_transactions($to);
+               commit_transaction();
+}
+
+function open_year($year)
+{
+       $myrow = get_fiscalyear($year);
+       $from = sql2date($myrow['begin']);
+
+       begin_transaction();
+       open_transactions($from);
+       commit_transaction();
+}
 
 function handle_submit()
 {
        global $selected_id, $Mode;
 
-       if (!check_data())
-               return false;
-
        if ($selected_id != -1)
        {
-               update_fiscalyear($_POST['from_date'], $_POST['closed']);
+               if ($_POST['closed'] == 1)
+               {
+                       if (check_open_before($_POST['from_date']))
+                       {
+                               display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
+                               set_focus('closed');
+                               return false;
+                       }       
+                       close_year($selected_id);
+               }       
+               else
+                       open_year($selected_id);
+               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'));
        }
@@ -142,7 +227,7 @@ 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();
@@ -188,7 +273,7 @@ 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, '', 'both');
 
        end_form();
 }