<?php

function build_calendar_days_array(&$dates, $from_date, $to_date)
{
    $res = get_caledar_date_records($from_date,$to_date);
    while ($date = db_fetch($res)) {
        if($date["work_time_code"] == "F"){
            $d = 1;
        }elseif ($date["work_time_code"] == "M" || $date["work_time_code"] == "A"){
            $d = 0.5;
        }else{
            continue;
        }
        
        $dates[$date["date"]] = array("time"=>$d);
    }
    
//    $dates[] = $line;
}


function display_project_employees_daily_view($project_id, $from_date_, $to_date_)
{
    $day_values = array();
    $pt = array();
    $dates = array();
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    build_calendar_days_array($day_values, $from_date, $to_date);
    
    $th_dates = array();
    $date_cols = 0;
    foreach ($day_values as $key => $value) {
        $date_text = substr($key, 5, 5);
        $th_dates[] = $date_text;
        $dates[] = $key;
        $date_cols++;
        
        $pt[$key] = 0;
    }
            
    start_table(TABLESTYLE, "width=98%");
    
    $th = array(_("BOQ Item"));      
    
    $th = array_merge($th, $th_dates);

    table_header($th);
    
    $res_proj_emps = get_project_employees($project_id, $from_date, $to_date);
    
    while ($project_employee = db_fetch($res_proj_emps)) {
        $employee_code = $project_employee["employee_code"];
        $employee_name = get_employee_formatted_long_name($employee_code);
        
        start_row();
        label_cell(text_bold($employee_name), 'colspan='.($date_cols+1).' style=background-color:#0ec9ba');
        end_row();
        
        display_project_employee_boq_items($day_values, $project_id, $employee_code, $from_date, $to_date, $date_cols, $dates, $pt);        
    }
    
    display_project_employee_boq_item_project_total($pt);
    
}

function display_project_employee_boq_items($day_values, $project_id, $employee_code, $from_date, $to_date, $date_cols, $dates, &$pt)
{
    $et = array();
    $hour_factor = 8;
    
    foreach ($day_values as $date => $value) {            
        $et[$date] = 0;
    }  
        
        
    $res = get_project_employee_boq_items($project_id, $employee_code, $from_date, $to_date);
    
    $planned_start_date = $planned_end_date = "";
    while ($employee_boq_item = db_fetch($res)) {
        $planned_start_date = $employee_boq_item["start_date"];
        $planned_end_date = $employee_boq_item["end_date"];
        $boq_item_rec_id = $employee_boq_item["id"];
        
        $boq_item_name = $employee_boq_item['boq_item_code']." - ".$employee_boq_item['boq_item_name'];
        
        start_row();
        label_cell($boq_item_name, 'colspan='.($date_cols+1));
        end_row();
        
        start_row();
        label_cell("");
        foreach ($day_values as $date => $value) {
            $hours = 0;
            $day_rec = get_project_employee_boq_item_day_value($boq_item_rec_id, $date);
            if(isset($day_rec["id"]) && $day_rec["id"] > 0){
                $day_factor = $day_rec["factor"];
                $hours = $hour_factor * $day_factor;
//                label_cell(number_format($hours,2));
//                qty_cell($hours, false, 2);
                $txt = gmdate('i:s', floor($hours * 60));
                
                if ($hours > 0) {
                    label_cell(get_project_trans_view_str(DASH_VW_EMPLOYEE_PROJECT_BOQ_ITEM_WORK_DONE, $boq_item_rec_id, $txt), "align=center");
                } else {
                    label_cell('');
                }
            }else{
                label_cell('');
            }
            
            $et[$date] += $hours;
        }      
                
        end_row();
    } 
    
    display_project_employee_boq_item_employee_total($et, $pt);
}

function display_project_employee_boq_item_employee_total($et, &$pt)
{

    start_row();
    label_cell("Employee Total");
    
    foreach ($et as $date => $value) { 
        if($value > 0){ 
//            qty_cell($value, true, 2);            
            $txt = gmdate('i:s', floor($value * 60));
            if($value > 4){ 
                label_cell(text_bold($txt), 'align=center style=background-color:Tomato');
            }else{
                label_cell(text_bold($txt), 'align=center');
            }
            $pt[$date] += $value;
        }else{
            label_cell("");
        }
    }     
    
    end_row();
    
}

function display_project_employee_boq_item_project_total($et)
{

    start_row();
    label_cell("Project Total");
    
    foreach ($et as $date => $value) { 
        if($value > 0){ 
//            qty_cell($value, true, 2);
            $txt = gmdate('i:s', floor($value * 60));
            label_cell(text_bold($txt), 'align=center');
        }else{
            label_cell("");
        }
    }     
    
    end_row();
    
}