<?php
/**********************************************************************
***********************************************************************/

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


function upload_item_material_groups_import_file(&$Mode, $import_value)
{
    if ($Mode == 'ADD_ITEM' && (!isset($_FILES['filename']) || $_FILES['filename']['size'] == 0)){
		display_error(_("Select attachment file."));
        return;
    }
    
    switch ($import_value){
        case 'ITEM_MATERIAL_GROUPS':
            $unique_name = "Import_Item_Material_Groups";
            break;
        default:
            break;
    }

     //$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
    $tmpname = $_FILES['filename']['tmp_name'];

    $dir =  company_path()."/import_files";
    if (!file_exists($dir))
    {
        mkdir ($dir,0777);
        $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
        $fp = fopen($dir."/index.php", "w");
        fwrite($fp, $index_file);
        fclose($fp);
    }

    $dir =  company_path()."/import_files/item_material_groups";
    if (!file_exists($dir))
    {
        mkdir ($dir,0777);
        $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
        $fp = fopen($dir."/index.php", "w");
        fwrite($fp, $index_file);
        fclose($fp);
    }

    $filename = basename($_FILES['filename']['name']);
    $filesize = $_FILES['filename']['size'];
    $filetype = $_FILES['filename']['type'];

		// file name compatible with POSIX
		// protect against directory traversal
        

    if ($filename && file_exists($dir."/".$unique_name)){
        unlink($dir."/".$unique_name);
    }

    move_uploaded_file($tmpname, $dir."/".$unique_name);

    $Mode = 'RESET';
}


function display_item_material_groups_import_file_data($import_value, $validate = false, $import = false)
{
    if($validate || $import){
        clear_session_variable('import_item_material_groups');
    }
    
    if(list_updated('import_values')){
        clear_session_variable('import_item_material_groups');
    }
  
    $file_data = get_session_variable('import_item_material_groups');
    if($file_data ==''){
        $file_data = read_item_material_groups_import_file($import_value);
    }
    
    switch ($import_value){
        case 'ITEM_MATERIAL_GROUPS':
            display_item_material_groups_import_file_data_array($file_data, $validate, $import);
            break;
        default:
            break;
    }    
    
    end_table();

    set_session_variable('import_item_material_groups', $file_data);
    
}
    

function read_item_material_groups_import_file($import_value)
{
        $file_data = array();
        
        switch ($import_value){
            case 'ITEM_MATERIAL_GROUPS':
                $file_name = "Import_Item_Material_Groups";
                break;
            default:
                break;
        }
        
        $file_path_name = company_path()."/import_files/item_material_groups/".$file_name;
        
        if(file_exists($file_path_name)){
            $file_handle = fopen($file_path_name, "r");
        }
        else{
            return array();
        }
        
        $header_line = true;
        if ($file_handle) {            
            while (!feof($file_handle)) 
            {
                $line = fgetcsv($file_handle, 1024);
                if($header_line){
                    $header_line =  false;
                }
                else{
                    switch ($import_value){
                        case 'ITEM_MATERIAL_GROUPS':
                            read_item_material_groups_import_file_data($file_data, $line);
                            break;
                        default:
                            break;
                    }
                    
                }
            } 
            
            fclose($file_handle);
            
            return $file_data;
        } 
        else {

        }
}

//function read_employees_import_file_data(&$file_data, $line)
//{
//
//        $l = $line;
//
//        $code = str_replace('"', '', $l[0]);
//        $description = str_replace('"', '', $l[1]);
//        $type_code = str_replace('"', '', $l[2]);                    
//        $display_order = str_replace('"', '', $l[3]);
//        $unit_name = str_replace('"', '', $l[4]);
//        $boq_item_group_code = str_replace('"', '', $l[5]);
//        $billing_type_name = str_replace('"', '', $l[6]);        
//        $rate = str_replace('"', '', $l[7]);
//        $quantity = str_replace('"', '', $l[8]);
//        $tax_group_name = str_replace('"', '', $l[9]);
//        $long_description = str_replace('"', '', $l[10]);
//        $print_long_description = str_replace('"', '', $l[11]);
//
//        add_to_file_data_line($file_data, $code, $description, $type_code, $display_order, 
//                                $unit_name, $boq_item_group_code, $billing_type_name, $rate,
//                                $quantity, $tax_group_name, $long_description, $print_long_description);
//
//}

//function add_to_file_data_line(&$file_data, $code, $description, $type_code, $display_order, 
//                                $unit_name, $boq_item_group_code, $billing_type_name, $rate,
//                                $quantity, $tax_group_name, $long_description, $print_long_description)
//{
//    $file_data_line = array("code" => $code, 
//                        "description" => $description, 
//                        "type_code" => $type_code, 
//                        "display_order" => $display_order, 
//                        "unit_name" => $unit_name,
//                        "boq_item_group_code" => $boq_item_group_code, 
//                        "billing_type_name" => $billing_type_name, 
//                        "rate" => $rate,
//                        "quantity" => $quantity, 
//                        "tax_group_name" => $tax_group_name,
//                        "long_description" => $long_description, 
//                        "print_long_description" => $print_long_description, 
//                        "message" => "");
//    
//    $file_data[] = $file_data_line;
//}


?>
