<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MONTHS';
$path_to_root="..";
include($path_to_root . "/includes/session.inc");


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "Month"));

include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/admin/db/months_db.inc");
include_once($path_to_root . "/includes/date_functions.inc");

simple_page_mode(true);

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

function can_process_month($selected_id)
{         
    if($_POST['month_code'] == -1)
    {
        $message = "Please select a month.";
        $set_focus = 'month_code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    } 
    
    if(!($_POST['year_code'] > 0))
    {
        $message = "Please select a year..";
        $set_focus = 'year_code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    } 
    
    if(!($_POST['display_order'] > 0))
    {
        $message = "Display order must be greater than zero.";
        $set_focus = 'display_order';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);       
    } 
      	
    return array("success" => true, "message" => "");    
}


function save_month($selected_id)
{
    $month_number = substr($_POST['month_code'], 0,2);
//    $date = date_parse($month_code);
    $first_day_this_month = date($_POST['year_code'].'-'.$month_number.'-01');
    $num_days = cal_days_in_month(CAL_GREGORIAN,$month_number,$_POST['year_code']);
    $last_day_this_month = date($_POST['year_code'].'-'.$month_number.'-'.$num_days);
    
    if($selected_id != -1){
        update_month_description($selected_id, $_POST['description'], 
                                $_POST['year_code'], $_POST['month_code'],
                                input_num('display_order'), $_POST['active_id'], $_POST['short_name'],
                                $first_day_this_month, $last_day_this_month); 
                             
        $note = _('Selected month details have been updated');
    }else{
        
        add_month_description($_POST['description'], $_POST['year_code'], $_POST['month_code'],                            
                                input_num('display_order'), $_POST['active_id'], $_POST['short_name'],
                                $first_day_this_month, $last_day_this_month); 
        $note = _('New month details have been added');
   
    }

    display_notification($note);
    $Mode = 'RESET';
  
}



if($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM'){
    $msg = can_process_month($selected_id);
    if($msg['success'] !== true){
        display_error($msg['message']);
        
    }else{
        save_month($selected_id);
    }
   
}


if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

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

$result = get_months();

start_form();
start_table(TABLESTYLE, "width=70%");
$th = array(_("Id"), _("Name"), "Short Name", _("Year"), _("Month"),           
            _("Month Start Date"), _("Month End Date"), _("Display Order"), ("Active"),"", "");          
        
//inactive_control_column($th);
table_header($th);

$k = 0; //row colour counter
while ($myrow = db_fetch($result)) 
{
    alt_table_row_color($k);
   
    label_cell($myrow["id"]);
    label_cell($myrow["description"]);
    label_cell($myrow["short_name"]);
    label_cell($myrow["year_name"]);
    label_cell($myrow["month_name"]);
    label_cell(sql2date($myrow["month_begin_date"]),"align=center");
    label_cell(sql2date($myrow["month_end_date"]),"align=center");
    label_cell($myrow["display_order"], "align=right");
          
    if(($myrow["active"]) == 1){
        $active = "Yes";
    }else{
        $active = "No";
    }
    
    label_cell($active,"align=center");
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));  

    end_row();

}

end_table(1);

function get_month_start_date_and_end_date($year, $month)
{
    return $date;
}

if($Mode == "Delete"){
    delete_month_description($selected_id);
}
//-------------------------------------------------------------------------------------------------
//if (list_updated('type')) {
//	$Ajax->activate('edits');
//}


div_start('edits');

start_table(TABLESTYLE2);

if ($selected_id != -1) 
{
	if ($Mode == 'Edit') {
		//editing an existing payment terms
		$myrow = get_month($selected_id);

		$_POST['description']  = $myrow["description"];
                $_POST['short_name']  = $myrow["short_name"];
		$_POST['year_code'] = $myrow["year_code"];
		$_POST['month_code'] = $myrow["month_code"];               
		$_POST['display_order'] = $myrow["display_order"];
                $_POST['active_id']  = $myrow["active"];
		
	}
	hidden('selected_id', $selected_id);
}


text_row(_("Name:"), 'description', null, 50, 200);
text_row(_("Short Name:"), 'short_name', null, 40, 50);
value_list_code_row("Year:", 'year_code', 'YEAR', null, 'Select a year', false);
value_list_code_row("Month:", 'month_code', 'MONTHS', null, 'Select a Month', false);
amount_row("Display Order:", 'display_order');
yesno_list_row("Active:", 'active_id', null, $name_yes="Yes", $name_no="No", false);

end_table(1);
div_end();

submit_add_or_update_center($selected_id == -1, '', 'both');
//submit_center_first('Save_Month', _("Save"), '', 'default');

end_form();

end_page();

?>

























