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



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

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

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

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

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

        display_project_boq_item_material_import_data_array($line);   
    }
    
    end_table();
    
}
    
function save_import_project_boq_item_material(&$line)
{
    $boq_item_id = $line['boq_item_id'];
    $detail_type_code = $line['detail_type_code'];
    $stock_id = $line['item_code'];
    $unit = $line['unit'];    
    $rate = input_num2($line['rate']);
    $quantity = input_num2($line['quantity']);
    $comment = $line['comment'];       
    
//    $boq_item_material = get_boq_item_material($boq_item_id, $detail_type_code);
    

    $msg = add_boq_item_material($boq_item_id, $detail_type_code, $stock_id, 
                        $unit, $rate, $quantity, $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_material_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_MATERIAL){
                $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;
            }
        }
        
        $stock_item = get_stock_item_edit_info($line['item_code']);
        if(!(isset($stock_item['stock_id']) && strlen($stock_item['stock_id']) > 0)){
            $m = "Unknown itemcode.";
            $line['item_code'] = $line['item_code']." <font color='red'>".$m."</font>";
            $success = false;
        }
        
        $Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$stock_item['description']); 
        $temp_sid = preg_replace("/[^A-Za-z0-9]/", "", $Content);
        
//        $temp_sid = preg_replace("/[^A-Za-z0-9]/", "", $stock_item['description']);
        $temp_icd = preg_replace("/[^A-Za-z0-9]/", "", $line['item_description']);
        if(trim($temp_sid) !=  trim($temp_icd)){
            $m = "Item description does not match. - ".$stock_item['description'];
            $line['item_description'] = $line['item_description']." <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'];
            $alternate_unit = get_alternate_unit_info($line['item_code'], $line['unit']);
            if(!(isset($alternate_unit['id']) && $alternate_unit['id'] > 0)){
                $m = "Unit not defined for this item.";
                $line['unit_name'] = $line['unit_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;
        }
      
        
        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_material_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['item_code']);
    label_cell($row['item_description']);

    label_cell($row['unit_name']);
    
    $rate = $row['rate'];
    $quantity = $row['quantity'];

    amount_decimal_cell($rate);
    qty_cell($quantity, false, 3);
//    label_cell($row['quantity'], 'align=right');
    
    amount_decimal_cell($rate * $quantity);
    label_cell($row['comment']);

    label_cell($row['message']);

    end_row();

}



function read_project_boq_item_material_import_file_data(&$file_data, $line)
{

        $l = $line;

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


        add_to_import_project_boq_item_material_data_line($file_data, $boq_item_code, $detail_type_name,  
                                                            $item_code, $item_description, $unit_name, $rate, $quantity, $comment);

}

function add_to_import_project_boq_item_material_data_line(&$file_data, $boq_item_code, $detail_type_name,  
                                                            $item_code, $item_description, $unit_name, $rate, $quantity, $comment)
{
    $file_data_line = array("boq_item_code" => $boq_item_code, 
                        "detail_type_name" => $detail_type_name, 
                        "item_code" => $item_code, 
                        "item_description" => $item_description,
                        "unit_name" => $unit_name, 
                        "rate" => $rate, 
                        "quantity" => $quantity,
                        "comment" => $comment, 
                        "message" => "");
    
    $file_data[] = $file_data_line;
}


?>
