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

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


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

    //$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/manual_project_invoice";
    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_manual_project_invoice_import_file_data($validate = false, $import = false)
{
    if($validate || $import){
        clear_session_variable('import_manual_project_invoice');
    }
    
    if(list_updated('import_values')){
        clear_session_variable('import_manual_project_invoice');
    }
  
    $file_data = get_session_variable('import_manual_project_invoice');
    if($file_data ==''){
        $file_data = read_manual_project_invoice_import_file();
    }
    

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

    set_session_variable('import_manual_project_invoice', $file_data);
    
}
    
//function save_import_project_boq_item(&$line, $project_id)
//{
//    $display_order = $line['display_order'];
//    $type_id = $line['type_id'];
//    $code = $line['code'];
//    $description = $line['description'];
//    $billing_type = $line['description'];
//    $unit = $line['unit'];    
//    $rate = $line['rate'];
//    $max_quantity = $line['quantity'];
//    $long_description = $line['long_description'];
//    $print_long_description = $line['print_long_description'];
//    $tax_group_id = $line['tax_group_id'];
//    $group_id = $line['group_id'];        
//    
//    $boq_item = get_project_boq_item_record_by_code($project_id, $code);
//    
//    $line['message'] = "<font color='red'>Could not update</font>";
//    
////    return;
//    
//    if(isset($boq_item['id']) && $boq_item['id'] > 0){
//        $selected_id = $boq_item['id'];
//        
//        $n = update_project_boq_item($selected_id, $display_order, $type_id, $code, $description, $billing_type, $unit, $rate, $max_quantity, 
//                                    $long_description, $print_long_description, $tax_group_id, $group_id);
//        if($n == 1){
//            $line['success'] = true;
//            $line['message'] = "Updated";
//        }
//        else{
//            $line['success'] = false;
//            $line['message'] = "<font color='red'>Could not update</font>";
//        }
//    }
//    else{
//        $n = add_project_boq_item($project_id, $type_id, $display_order, $code, $description, $billing_type, $unit, $rate,  $max_quantity, 
//                                    $long_description, $print_long_description, $tax_group_id,  $group_id)   ;
//        if($n > 0){
//            $line['success'] = true;
//            $line['message'] = "Added";
//        }
//        else{
//            $line['success'] = false;
//            $line['message'] = "<font color='red'>Could not add</font>";
//        }
//    }
//}


//function validate_project_boq_item_import_data(&$line, $project_id)
//{
//
//        $success = true;
//        
//        if(!($project_id > 0)){
//            $m = "No project selected";
//            $line['message'] = $m." ";
//            $success = false;
//        }
//        
//        if(!(strlen($line['code']) > 0)){
//            $m = "BOQ item code can not be empty";
//            $line['code'] = $line['code']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//
//        if(!(strlen($line['description']) > 0)){
//            $m = "BOQ item description can not be empty";
//            $line['description'] = $line['description']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        if(!(strlen($line['type_code']) > 0)){
//            $m = "BOQ item type can not be empty";
//            $line['type_code'] = $line['type_code']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//
//        $type_code = get_listvalue_by_code("PROJECT_TASK_TYPES", $line['type_code']);
//        if(!(isset($type_code['id']) && $type_code['id'] > 0)){
//            $m = "Unknown BOQ item type.";
//            $line['type_code'] = $line['type_code']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        else{
//            $line['type_id'] = $type_code['id'];
//        }
//        
//        if(!check_num2($line['display_order'], 0)){
//            $m = "Display Order should be numeric and can not be less than zero.";
//            $line['display_order'] = $line['display_order']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        $unit = get_unit_abbrivation($line['unit_name']);
//        if(!(isset($unit['abbr']) && strlen($unit['abbr']) > 0)){
//            $m = "Unknown unit.";
//            $line['unit_name'] = $line['unit_name']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        else{
//            $line['unit'] = $unit['abbr'];
//        }
//        
//        $n1 = get_existing_code_from_project_boq_item_groups($project_id, $line['boq_item_group_code'], -1);
//        if(!($n1 > 0)){
//            $m = "Unknown BOQ item group code.";
//            $line['boq_item_group_code'] = $line['boq_item_group_code']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        $boq_item_group = get_project_boq_item_group_record_by_code($project_id, $line['boq_item_group_code']);
//        $line['group_id'] = $boq_item_group['id'];
//        
//        global $project_billing_types;
//        $valid_billing_type = false;
//        foreach ($project_billing_types as $billing_type) {
//            if($billing_type == $line['billing_type_name']){
//                $valid_billing_type = true;
//            }
//        }
//        if(!$valid_billing_type){
//            $m = "Unknown project billing type.";
//            $line['billing_type_name'] = $line['billing_type_name']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        if(!check_num2($line['rate'], 0)){
//            $m = "Rate should be numeric and can not be less than zero.";
//            $line['rate'] = $line['rate']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        if(!check_num2($line['quantity'], 0.000001)){
//            $m = "Quantity should be numeric and greater than zero.";
//            $line['quantity'] = $line['quantity']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        
//        $tax_type = get_tax_group_by_name($line['tax_group_name']);
//        if(!(isset($tax_type['id']) && $tax_type['id']) > 0){
//            $m = "Unknown Tax Group.";
//            $line['tax_group_name'] = $line['tax_group_name']." <font color='red'>".$m."</font>";
//            $success = false;
//        }
//        else{
//            $line['tax_group_id'] = $tax_type['id'];
//        }
//        
//        if($line['print_long_description'] == 1 || strtoupper($line['print_long_description']) == "YES"){
//            $line['print_long_description'] = "Yes";
//        }
//        else{
//            $line['print_long_description'] = "No";
//        }
//        
//        if($success === true){
//            $line['success'] = true;
//            $line['message'] = "Can import";
//        }
//        else{
//            $line['success'] = false;
//            $line['message'] .= "<font color='red'>Can not import</font>";
//        }
//}

//function display_project_boq_item_import_data_array($row)
//{
//    $j = $k = 0;
//  
//    if($row == ''){
//        return;
//    }
//
//    label_cell($row['code']);
//    label_cell($row['description']);
//    label_cell($row['type_code']);
//
//    label_cell($row['display_order'], "align=center");
//    label_cell($row['unit_name']);
//
//    label_cell($row['boq_item_group_code']);
//    label_cell($row['billing_type_name']);
//    label_cell($row['rate']);
//    label_cell($row['quantity']);
//    label_cell($row['tax_group_name']);
//
//    label_cell($row['long_description']);
//    label_cell($row['print_long_description']);
//
//    label_cell($row['message']);
//
//    end_row();
//
//}


function read_manual_project_invoice_import_file()
{
        $file_data = array();
        
        $file_name = "Import_Manual_Project_Invoice";
        
        $file_path_name = company_path()."/import_files/manual_project_invoice/".$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_manual_project_invoice_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;
//}


?>
