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



function display_project_boq_item_cost_import_file_data($project_id, &$file_data, $validate = false, $import = false)
{

    start_table(TABLESTYLE, "width=90%");    
    $th = array("BOQ Item Code", "Detail Type", "Description", "Unit Name", "Rate", "Quantity", "Cost", "Comment", "Result");

    table_header($th);
    
    foreach ($file_data as &$line) {    

        if($validate || $import){
            validate_project_boq_item_cost_import_data($line, $project_id);
        }

        if($import && $line['success'] === true){
            save_import_project_boq_item_cost($line, $project_id);
        }

        display_project_boq_item_cost_import_data_array($line);   
    }
    
    end_table();
    
}
    
function save_import_project_boq_item_cost(&$line)
{
    $boq_item_id = $line['boq_item_id'];
    $detail_type_code = $line['detail_type_code'];
    $description = $line['description'];
    $unit_name = $line['unit_name'];
    $rate = input_num2($line['rate']);    
    $quantity = input_num2($line['quantity']);  
    $cost = $rate * $quantity;
    $comment = $line['comment'];       
    
//    $boq_item_cost = get_boq_item_cost($boq_item_id, $detail_type_code);
    
//return;
//    $msg = add_boq_item_cost($boq_item_id, $detail_type_code, $description, $cost, $comment);
    $msg = add_boq_item_cost($boq_item_id, $detail_type_code, $description, $unit_name, $rate, $quantity, $cost, $comment);
    
    if($msg['success'] === true){
        $line['success'] = true;
        $line['message'] = "Added";
    }
    else{
        $line['success'] = false;
        $line['message'] = "<font color='red'>Could not add</font>";
    }
    
}


function validate_project_boq_item_cost_import_data(&$line, $project_id)
{
        $success = true;
        
        if(!($project_id > 0)){
            $m = "No project selected";
            $line['message'] = $m." ";
            $success = false;
        }
        
        if(!(strlen($line['boq_item_code']) > 0)){
            $m = "BOQ item code can not be empty";
            $line['boq_item_code'] = $line['boq_item_code']." <font color='red'>".$m."</font>";
            $line['boq_item_id'] = 0;
            $success = false;
        }
        else{
            $boq_item = get_project_boq_item_record_by_code($project_id, $line['boq_item_code']);
            if(!(isset($boq_item['id']) && strlen($boq_item['id']) > 0)){
                $m = "BOQ item not found.";
                $line['boq_item_code'] = $line['boq_item_code']." <font color='red'>".$m."</font>";
                $line['boq_item_id'] = 0;
                $success = false;
            }
            else{
                $line['boq_item_id'] = $boq_item['id'];
            }
        }

        $detail_type = get_listvalue_by_name("PROJECT_TASK_DETAIL_TYPES", $line['detail_type_name']);
        if(!(isset($detail_type['id']) && $detail_type['id'] > 0)){
            $m = "Unknown Detail Type.";
            $line['detail_type_name'] = $line['detail_type_name']." <font color='red'>".$m."</font>";
            $line['detail_type_code'] = "";
            $success = false;
        }
        else{
            $line['detail_type_code'] = $detail_type['value_code'];
        }
        
//        global $project_boq_item_component_primary_types;
//        $component = get_boq_item_component_record($line['boq_item_id'], $line['detail_type_code']);
//        if(isset($component['id']) && $component['id'] > 0){
//            if($component['primary_type'] != BOQPT_COST){
//                $m = ("There are already created primary type ".$project_boq_item_component_primary_types[$component['primary_type']]." entries.");
//                $line['detail_type_name'] = $line['detail_type_name']." <font color='red'>".$m."</font>";
//                $success = false;
//            }
//        }
        
        if(!(strlen($line['description']) > 0)){
            $m = "Description can not be empty";
            $line['description'] = $line['description']." <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.00001)){
            $m = "Quantity should be positive and greater than zero.";
            $line['quantity'] = $line['quantity']." <font color='red'>".$m."</font>";
            $success = false;
        }
        
        if(!check_num2($line['cost'], 0)){
            $m = "Cost should be numeric and can not be less than zero.";
            $line['cost'] = $line['cost']." <font color='red'>".$m."</font>";
            $success = false;
        }
        
        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_cost_import_data_array($row)
{
    $j = $k = 0;
  
    if($row == ''){
        return;
    }

    label_cell($row['boq_item_code']);
    label_cell($row['detail_type_name']);

    label_cell($row['description']);
    label_cell($row['unit_name']);

    label_cell($row['rate'], 'align=right');
    label_cell($row['quantity'], 'align=right');
    label_cell($row['cost'], 'align=right');

    label_cell($row['comment']);

    label_cell($row['message']);

    end_row();

}



function read_project_boq_item_cost_import_file_data(&$file_data, $line)
{

        $l = $line;

        $boq_item_code = str_replace('"', '', $l[0]);
        $detail_type_name = str_replace('"', '', $l[1]);                  
        $description = str_replace('"', '', $l[2]);
        $unit_name = str_replace('"', '', $l[3]);
        $rate = input_num2(str_replace('"', '', $l[4]));
        $quantity = input_num2(str_replace('"', '', $l[5]));
        $comment = str_replace('"', '', $l[6]);
        
        $cost = $rate * $quantity;

        add_to_import_project_boq_item_cost_data_line($file_data, $boq_item_code, $detail_type_name,  
                                                            $description, $unit_name, $rate, $quantity, $cost, $comment);

}

function add_to_import_project_boq_item_cost_data_line(&$file_data, $boq_item_code, $detail_type_name,  
                                                            $description, $unit_name, $rate, $quantity, $cost, $comment)
{
    $file_data_line = array("boq_item_code" => $boq_item_code, 
                        "detail_type_name" => $detail_type_name, 
                        "description" => $description, 
                        "unit_name" => $unit_name, 
                        "rate" => number_format($rate,2),
                        "quantity" => number_format($quantity,3),
                        "cost" => number_format($cost,2),
                        "comment" => $comment, 
                        "message" => "");
    
    $file_data[] = $file_data_line;
}


?>
