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

$js = "";
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

page(_($help_context = "Assets"), false, false, "", $js);

include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/transport/includes/db/vehicles_db.inc");

simple_page_mode(true);

function clear_data()
{
    unset($_POST['serial']);
    unset($_POST['description']);
    unset($_POST['long_description']);  
    unset($_POST['group_id']);
    unset($_POST['asset_category_id']);
    unset($_POST['cost']);
    unset($_POST['current_value']);
    unset($_POST['make']);
    unset($_POST['model']);
    unset($_POST['po_number']);
    unset($_POST['useful_life']);
    unset($_POST['extended_life']);         
    unset($_POST['rate_category_id']);
}

function can_process($selected_id)
{ 
    $input_error = 0;
    
    if (strlen($_POST['asset_code']) == 0){
        $input_error = 1;
        display_error( _("Asset code can not be empty."));
        set_focus('asset_code');
        return;
    } 
    
    if (strlen($_POST['description']) == 0) 
    {
        $input_error = 1;
        display_error(_('Description can not be empty.'));
        set_focus('description');
        return;
    } 

    if(!(strlen($_POST['type']) > 0))
    {
        $input_error = 1;
        display_error(_("Please select an Asset Type."));
        set_focus('type');
        return;
    } 
    
    if(!($_POST['asset_category_id'] > 0)){
        $input_error = 1;
        display_error( _("Please select an Asset Category."));
        set_focus('asset_category_id');
        return;
    } 
   
    if($_POST['type'] == AT_TOOL){
        if(!($_POST['rate_category_id'] > 0)){
            $input_error = 1;
            display_error( _("Please select a Rate Category."));
            set_focus('rate_category_id');
            return;
        } 
    }
   
    $count = get_existing_asset_code($selected_id, $_POST['asset_code']);
    if($count > 0) 
    { 
        $input_error = 1;
        display_error("Entered Asset code already exists.");
        set_focus('asset_code');
        return;                    
    }        
    return true;    
}

if(isset($_POST['AddAsset']))
{    
    if(can_process($selected_id) == true){
        if($_POST['owner_project_id'] < 0){
            $_POST['owner_project_id'] = 0;
        }
        add_asset($_POST['asset_code'] , $_POST['description'], $_POST['long_description'], $_POST['type'], $_POST['multiple_items'],
                    $_POST['group_id'], $_POST['is_hired'], $_POST['asset_category_id'], $_POST['subgroup_id'], $_POST['serial'], 
                    $_POST['make'], $_POST['model'], $_POST['po_number'],
                    input_num('cost'), input_num('current_value'), input_num('useful_life'),
                    input_num('extended_life'), $_POST['rate_category_id'], $_POST['inactive'], input_num('owner_project_id')); 

        $Mode = 'RESET';            

        display_notification(_('New Asset type has been added'));             
    }               
}

if(isset($_POST['Update'])) 
{    
    $asset_id = get_post('asset_id');
    if(can_process($asset_id) == true){        

        if($_POST['owner_project_id'] < 0){
            $_POST['owner_project_id'] = 0;
        }
        update_asset($asset_id, $_POST['asset_code'], $_POST['description'], $_POST['long_description'], $_POST['multiple_items'], 
                        $_POST['group_id'], $_POST['is_hired'], $_POST['asset_category_id'],
                        $_POST['subgroup_id'], $_POST['serial'], $_POST['make'], $_POST['model'], $_POST['po_number'], 
                        input_num('cost'), input_num('current_value'), input_num('useful_life'),
                        input_num('extended_life'), $_POST['rate_category_id'], $_POST['inactive'], input_num('owner_project_id'));   
        
        $vhc_row = get_vehicle($asset_id);
            if (isset($vhc_row['id']) && $vhc_row['id'] > 0) {
                update_vehicle_description($asset_id, $_POST['description']);
            }
        
        $Mode = 'RESET';
    
        display_notification(_('Asset has been Updated'));                     
    }
}
//---------------------------------------------------------------------------------------------------------------
if (isset($_POST['delete']))
{
//	$asset_id = get_post('asset_id');
//		delete_asset($asset_id);
//		display_notification(_('Selected asset has been deleted'));
	
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
//	unset($_POST);
	$_POST['show_inactive'] = $sav;
}
if (get_post('cancel')) {
	
    clear_data();
	set_focus('serial');
	$Ajax->activate('_page_body');
}

if (list_updated('asset_id')) {
	$Ajax->activate('asset_id');
    $Ajax->activate('asset_details');
    
}

if (list_updated('group_id')) {
	$Ajax->activate('subgroup_id');
}
//---------------------------------------------------------------------------------------------

function asset_settings($asset_id)
{      
    global $asset_types, $Ajax;
    
    $Ajax->activate('asset_details');
    
    div_start('asset_details');
   
    
    if (!(isset($asset_id) &&  $asset_id > 0))        
    {   
        if(list_updated('asset_id')){
            $_POST['owner_project_id'] = -1;
            $_POST['_owner_project_id_edit'] = -1;
        }
        
        start_outer_table(TABLESTYLE2);
            start_table(TABLESTYLE_NOBORDER);  
                table_section_title(_("Asset"));
                    text_row("Code: ", 'asset_code', null, 35, 50);        
                    text_row(_("Description :"), 'description', null, 35, 250);
                    textarea_row(_('Long Description:'), 'long_description', null, 25, 3);

                    asset_types_list_row(_("Type"), 'type', null, 'Select an asset type', true);   
                    yesno_list_row("Multiple Items", 'multiple_items', null, 'yes', 'no');

                    asset_categoris_list_row(_("Category"), 'asset_category_id', null, 'Select an asset category', null);   
                    asset_groups_list_row(_("Group:"), 'group_id', null, 'Select an asset group', true);
                    asset_subgroups_list_row("Subgroup:", 'subgroup_id', $_POST['group_id'], null, 'Select an asset subgroup', false);


                    yesno_list_row("Hired", 'is_hired', null, 'yes', 'no');

                    if($_POST['type'] == AT_TOOL){
                        tool_rate_category_list_row(_("Rate Category:"), 'rate_category_id', null, 'Select an asset rate'); 
                    }
                    else{
        //                unset($_POST['rate_category_id']);
                        hidden('rate_category_id', 0);
                    }

                    text_row(_("Serial/Plate Number :"), 'serial', null, 15, 50);            
                    text_row(_("Make :"), 'make', null, 25, 50);
                    text_row(_("Model :"), 'model', null, 25, 50);

                    project_list_row("Owner Project :", 'owner_project_id', null, "Select a project");

                    amount_row(_("Useful Life (Years) :"), 'useful_life', null, null, null, 1);
                    amount_row(_("Extended Life (Years) :"), 'extended_life', null, null, null, 1);

                    yesno_list_row("Inactive", 'inactive', null, 'yes', 'no');

                table_section_title(_("Value"));
                    text_row(_("PO Number :"), 'po_number', null, 25, 50);
                    amount_row(_("Purchased Value :"), 'cost', null, null, null, 2);
                    amount_row(_("Current Value :"), 'current_value', null, null, null, 2);


//            end_table();
        end_outer_table(1);
        
        start_table();
            submit_center_first('AddAsset', _("Insert New Asset"), '',  @$_REQUEST['popup'] ? true : 'default');
            submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
        end_table();
    }
    else 
    {
//        if($asset_id != ''){
//        $myrow = get_asset($asset_id);
//        }

        $myrow = get_asset($asset_id);
        
        $_POST['asset_code'] = $myrow["asset_code"];
        $_POST['description'] = $myrow["description"];
        $_POST['long_description'] = $myrow["long_description"];
        
        $_POST['type'] = $myrow['type'];
        $_POST['multiple_items'] = $myrow["multiple_items"];

        if(list_updated('asset_id')){
            $_POST['group_id'] = $myrow["group_id"];
        }
        !isset($_POST['group_id']) ? $myrow["group_id"]: $_POST['group_id'];            

        $_POST['is_hired'] = $myrow["is_hired"];
        $_POST['asset_category_id'] = $myrow["asset_category_id"];
        $_POST['subgroup_id'] = $myrow['subgroup_id'];
        $_POST['serial'] = $myrow["asset_serial"];            
        $_POST['make'] =  $myrow['make'];
        $_POST['model'] = $myrow['model'];
        $_POST['po_number'] = $myrow['po_number'];
        $_POST['cost'] = $myrow['purchase_value'];
        $_POST['current_value'] = $myrow['current_value'];
        
        if(list_updated('asset_id')){
            $_POST['owner_project_id'] = $myrow['owner_project_id'];
            $_POST['_owner_project_id_edit'] = $_POST['owner_project_id'];
        }
 
        if(!(isset($_POST['owner_project_id']) && $_POST['owner_project_id'] > 0)){
            $_POST['owner_project_id'] = $myrow['owner_project_id'];
        }
        
//        if(!(isset($_POST['_owner_project_id_edit']) && $_POST['_owner_project_id_edit'] > 0)){
//            $_POST['_owner_project_id_edit'] = $myrow['owner_project_id'];
//        }
        if(!(isset($_POST['owner_project_id']) && $_POST['owner_project_id'] > 0)){
            $_POST['_owner_project_id_edit'] = $myrow['owner_project_id'];
        }
        
        $_POST['useful_life'] = $myrow['useful_life'];
        $_POST['extended_life'] = $myrow['extended_life'];                
        $_POST['rate_category_id'] = $myrow['rate_category_id'];
        $_POST['inactive'] = $myrow["inactive"];

        start_outer_table(TABLESTYLE2);
            start_table(TABLESTYLE_NOBORDER);
                table_section_title(_("Asset"));    
                    text_row("Asset Code: ", 'asset_code', null, 35, 50); 
                    text_row(_("Description :"), 'description', $_POST['description'], 35, 250);
                    textarea_row(_('Long Description:'), 'long_description', $_POST['long_description'], 25, 3);

                    label_row("Type", $asset_types[$_POST['type']]) ;
                    hidden('type', $_POST['type']);

                    yesno_list_row("Multiple Items", 'multiple_items', null, 'yes', 'no');

                    asset_categoris_list_row("Asset Category", 'asset_category_id', null, false, false);
                    asset_groups_list_row(_("Group:"), 'group_id', get_post('group_id'), 'Select an asset group', true); 
                    asset_subgroups_list_row("Subgroup:", 'subgroup_id', $_POST['group_id'], $_POST['subgroup_id'], 'Select an asset subgroup', false);

                    yesno_list_row("Hired", 'is_hired', $_POST['is_hired'], 'yes', 'no');

        //            if($_POST['type'] == AT_TOOL){
                        tool_rate_category_list_row(_("Rate Category:"), 'rate_category_id', $_POST['rate_category_id'], 'Select an asset rate'); 
        //            }
        //            else{
        //                hidden('rate_category_id', 0);
        //            }


                    text_row(_("Serial Number :"), 'serial', $_POST['serial'], 15, 50);           
                    text_row(_("Make :"), 'make', $_POST['make'], 25, 50);
                    text_row(_("Model :"), 'model', $_POST['model'], 25, 50);

                    project_list_row("Owner Project :", 'owner_project_id', $_POST['owner_project_id'], "Select a project");

                    amount_row(_("Useful Life (Years) :"), 'useful_life', null, null, null, 1);
                    amount_row(_("Extended Life (Years) :"), 'extended_life', null, null, null, 1);

                    yesno_list_row("Inactive", 'inactive', null, 'yes', 'no');

                table_section_title(_("Value"));
                    text_row(_("PO Number :"), 'po_number', $_POST['po_number'], 25, 50);
                    amount_row(_("Purchased Value :"), 'cost', $_POST['cost'], null, null, 2); 
                    amount_row(_("Current Value :"), 'current_value', $_POST['current_value'], null, null, 2);


        //            hidden('asset_id', $_POST['asset_id']);

//            end_table();
        end_outer_table(1);
        
        start_table();
            submit_center_first('Update', _("Update"), '',  @$_REQUEST['popup'] ? true : 'default');
//            submit('delete', _("Delete This Asset"), true, '', true);
            submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
        end_table();
    }
    
    div_end();
}    

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            asset_list_cells(_("Select an asset:"), 'asset_id', null,_('New asset'), true, check_value('show_inactive'));

            $new_item = get_post('asset_id')=='';

            if(get_post('asset_id') == -1 || get_post('asset_id') == ''){
                $new_item = TRUE;
            }

        end_row();
    end_table();

    div_start('asset_details');

    $asset_id = get_post('asset_id');

    tabbed_content_start('tabs', array(
                            'settings' => array(_('&General settings'), $asset_id),        
//                            'asset_material' => array(_('&Material'), $asset_id),
//                            'asset_departments' => array(_('&Departments'), $asset_id), 
                        ));
    
    switch (get_post('_tabs_sel')) 
    {
            default:
            case 'settings':
                asset_settings($asset_id); 
                break;
            case 'asset_material':
                $_GET['asset_id'] = $asset_id;                           
                $_GET['popup'] = 1;
                include_once($path_to_root."/asset_register/asset_material.php");
                break;
            case 'asset_departments':
                $_GET['asset_id'] = $asset_id;                           
                $_GET['popup'] = 1;
                include_once($path_to_root."/asset_register/asset_departments.php");
                break;                  
    };

    br();
    tabbed_content_end();
div_end();
hidden('popup', @$_REQUEST['popup']);
end_form();
//---------------------------------------------------------------------------------------
end_page(@$_REQUEST['popup']);

?>

