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

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


function upload_gl_bank_transactions_import_file(&$Mode)
{
	if ($Mode == 'ADD_ITEM' && (!isset($_FILES['filename']) || $_FILES['filename']['size'] == 0)){
		display_error(_("Select attachment file."));
        return;
    }
    
    $unique_name = "Import_GL_Bank_Transactions";

    //$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/gl_bank_transactions";
    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_gl_bank_transactions_import_file($validate = false, $import = false)
{
    if($validate || $import){
        clear_session_variable('import_gl_bank_transactions');
    }
    
    if(list_updated('import_values')){
        clear_session_variable('import_gl_bank_transactions');
    }
    
    clear_session_variable('import_gl_bank_transactions');;
  
    $file_data = get_session_variable('import_gl_bank_transactions');
    if($file_data ==''){
        $file_data = read_gl_bank_transactions_import_file();
    }
    

    display_gl_bank_transactions_import_file_data($file_data, $validate, $import);

    set_session_variable('import_gl_bank_transactions', $file_data);
    
}
    


function read_gl_bank_transactions_import_file()
{
        $file_data = array();
        
        
        $file_name = "Import_GL_Bank_Transactions";
        
        $file_path_name = company_path()."/import_files/gl_bank_transactions/".$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{
                        read_gl_bank_transaction_details_import_file_data($file_data, $line);
                }
            } 
            
            fclose($file_handle);
            
            return $file_data;
        } 
        else {

        }
}

//function read_project_boq_item_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;
//}


?>
