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

page(_($help_context = "Vehicle Packages"));

include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/transport/includes/db/vehicle_packages_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        if ($_POST['vehicle_type'] == '-1')
	{
		$input_error = 1;
		display_error(_("Vehicle Type can not be empty."));
		set_focus('code');
                return;              
	}
	if (strlen($_POST['code']) == 0)
	{
		$input_error = 1;
		display_error(_("Code can not be empty."));
		set_focus('code');
                return;              
	}
        if (strlen($_POST['description']) == 0){
            
                $input_error = 1;
		display_error(_("Description can not be empty."));
		set_focus('description');
                return;
        }
        if (strlen($_POST['unit']) == 0){
            
                $input_error = 1;
		display_error(_("Unit can not be empty."));
		set_focus('unit');
                return;
        }
	if (!check_num('unit_rate', 0))
        {
            $input_error = 1;
            display_error( _("Unit Rate must be numeric and can not be negative."));
            set_focus('unit_rate');
        }        
        if (!check_num('hourly_rate', 0))
        {
            $input_error = 1;
            display_error( _("Hourly Rate must be numeric and can not be negative."));
            set_focus('hourly_rate');
        }
        if (!check_num('max_hours_per_day', 0))
        {
            $input_error = 1;
            display_error( _("Max hours per day must be numeric and can not be negative."));
            set_focus('max_hours_per_day');
        }
        

	if ($input_error !=1)
        {
            write_vehicle_packages($selected_id,  $_POST['vehicle_type'],$_POST['code'], $_POST['description'], $_POST['unit'], 
                                input_num('unit_rate'), input_num('hourly_rate'), $_POST['max_hours_per_day'],
                                input_num('cost'));
            
            if($selected_id != '')
            {
                display_notification(_('Selected Vehicle type has been updated'));
            }
            else
            {
                display_notification(_('New Vehicle type has been inserted'));
            }             
            $Mode = 'RESET';
	}
}
//----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
    delete_vehicle_package($selected_id);
    display_notification(_('Selected Vehicle type has been deleted'));
    $Mode = 'RESET';
}

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

$result = get_all_vehicle_packages();

start_form();
start_table(TABLESTYLE, "width=60%");
$th = array(_('Vehicle Type'),_('Code'),_('Description'),_('Unit'),_('Unit Rate'),_('Hourly Rate'),_('Max Hours per day'), "Cost", "", "");
//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["vehicle_type_name"]);
    label_cell($myrow["code"]);
    label_cell($myrow["description"]);
    label_cell($myrow["unit"]);
    label_cell($myrow["unit_rate"]);
    label_cell($myrow["hourly_rate"]);
    label_cell($myrow["max_hours_per_day"]);
    amount_decimal_cell($myrow["cost"]);
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}
//inactive_control_row($th);
end_table(1);
//----------------------------------------------------------------------------------

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
    if ($Mode == 'Edit') {
        //editing an existing item category

        $myrow = get_vehicle_packages_for_editing($selected_id);
        
        $_POST['id'] = $myrow["id"];
        $_POST['vehicle_type'] = $myrow["vehicle_type_id"];
        $_POST['code'] = $myrow["code"];
        $_POST['description']  = $myrow["description"];
        $_POST['unit'] = $myrow["unit"];
        $_POST['unit_rate'] = $myrow["unit_rate"];
        $_POST['hourly_rate'] = $myrow["hourly_rate"];
        $_POST['max_hours_per_day'] = $myrow["max_hours_per_day"];
        $_POST['cost'] = $myrow["cost"];
    }
    hidden('selected_id', $myrow["id"]);
}
value_list_row("Vehicle Type:", 'vehicle_type', 'VEHICLE_TYPES', null, 'Select a vehicle type');
text_row(_("Code:"), 'code', null, 20, 20);
text_row(_("Description:"), 'description', null, 20, 20);
text_row(_("Unit:"), 'unit', null, 20, 20);
text_row(_("Unit Rate:"), 'unit_rate', null, 20, 20);
text_row(_("Hourly Rate:"), 'hourly_rate', null, 20, 20);
text_row(_("Max hours per day:"), 'max_hours_per_day', null, 20, 20);
amount_row("Cost", "cost");
end_table(1);

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

end_form();

end_page();

?>
