<?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>.
***********************************************************************/


function save_accounting_period(&$Mode, &$selected_id)
{
	
	if (!can_commit_accounting_period()){
            return false;
        }	
        
        $fiscal_year_id = $_POST['fiscal_year_id'];
        $end_date = date2sql($_POST['end_date']);
        $period_name = $_POST['period_name'];
	$closed = $_POST['closed'];
                
	if ($selected_id > 0) 
	{
            update_accounting_period($selected_id, $end_date, $period_name, $closed);

            display_notification(_('Selected accounting period has been updated'));
	} 
	else 
	{
            add_accounting_period($fiscal_year_id, $end_date, $period_name, $closed);

            display_notification(_('New accounting period has been added'));
	}
        
        
	$Mode = 'RESET';
        $selected_id = -1;

}




function display_accounting_period_edit_controls($selected_id)
{
	global $selected_id, $Ajax;
        

	start_table(TABLESTYLE2);

	if ($selected_id > 0)
	{
            $myrow = get_accounting_period($selected_id);

            $_POST['end_date'] = sql2date($myrow["end_date"]);
            $_POST['period_name'] = $myrow["period_name"];
            $_POST['closed'] = $myrow["closed"];		

	}
	else
	{

                
	}
       
        text_row("Period Name", 'period_name', null, 20, 50);
        date_row(_("End Date"), 'end_date');

        yesno_list_row("Closed", 'closed', null);
        
        hidden('selected_id', $selected_id);

	end_table(1);

	submit_add_or_update_center($selected_id == '', '', 'both');


}



function can_commit_accounting_period()
{
	if (!isset_numeric_post('fiscal_year_id')) 
	{
		display_error( _("Please select a fiscal year."));
		set_focus('fiscal_year_id');
		return false;
	} 
	if (!is_date($_POST['end_date'])) 
	{
		display_error( _("End date is not a valid date."));
		set_focus('end_date');
		return false;		
	} 
	if (strlen($_POST['period_name']) == 0) 
	{
		display_error( _("Period name can not be empty."));
		set_focus('period_name');
		return false;		
	} 

        $fiscalyear = get_fiscalyear($_POST['fiscal_year_id']);
        if (!date1_greater_date2($_POST['end_date'], sql2date($fiscalyear['begin']))) 
	{
            display_error( _("End date should be after the begin date of the fiscal year."));
            set_focus('end_date');
            return false;	
        }
        
        if (date1_greater_date2($_POST['end_date'], sql2date($fiscalyear['end']))) 
	{
            display_error( _("End date can not be after the end date of the fiscal year."));
            set_focus('end_date');
            return false;	
        }
	
	return true;
}



function display_accounting_periods()
{
    $fiscal_year_id = $_POST['fiscal_year_id'];
	
    $result = get_accounting_periods($fiscal_year_id);
	
    start_table(TABLESTYLE, "width=40%");
    $th = array(_("Period Name"), _("End Date"), _("Closed"), "", "");
    table_header($th);	
    
    $k = 0; //row colour counter
    
    while ($myrow = db_fetch($result)) 
    {
    	
    	if ($myrow['closed'] == false) 
    	{
    		start_row("class='settledbg'");
    	} 
    	else{
    		alt_table_row_color($k);
        }
    		
        
        label_cell($myrow["period_name"]);
        label_cell($myrow["end_date"], 'align=center');
        label_cell(get_yes_no_name($myrow["closed"]), 'align=center');
        
        edit_button_cell("Edit".$myrow["id"], _("Edit"));
        delete_button_cell("Delete".$myrow["id"], _("Delete"));

        end_row();
		
    } 

    end_table();

}



function check_can_delete_accounting_period($curr)
{

	
	
	return true;
}

//---------------------------------------------------------------------------------------------

function handle_delete_accounting_period(&$Mode, &$selected_id)
{
//	global $selected_id, $Mode;
	if (check_can_delete_accounting_period($selected_id)) {
	//only delete if used in neither customer or supplier, comp prefs, bank trans accounts
		delete_accounting_period($selected_id);
		display_notification(_('Selected accounting period has been deleted'));
	}
	$Mode = 'RESET';
        $selected_id = -1;
}
