<?php

function display_employee_project_boq_items_gantt_chart($employee_code, $to_date){
    global $Ajax;
    
    $chart_start_date = date2sql(add_days($to_date, -30));
    $chart_end_date = date2sql($to_date);
        
    $total_qty_and_total_complated_qty = get_project_boq_item_total_qty_and_total_completed_qty($employee_code, null);
        $total_qty = $total_qty_and_total_complated_qty['total_qty'];
        
        $colspan = 1;
        $num_columns = 12;
        $colspan += $num_columns;
        
    $Ajax->activate('employee_project_boq_items_gantt');
    div_start('employee_project_boq_items_gantt');
        start_table(TABLESTYLE, "width=99%");

            $project_completed_percentage = 0;
            display_employee_project_boq_items_details($employee_code, $chart_start_date, $chart_end_date, $num_columns, $project_completed_percentage); 
            
//            display_employee_project_no_boq_items_details($employee_code, $chart_start_date, $chart_end_date, $total_qty, $num_columns, $project_completed_percentage);
            
            $project_completed_percentage_ = number_format($project_completed_percentage, 3);
            
//            start_row("style=background-color:#98fa6e");
//                label_cell(text_bold("Project Percentage Completed Quantity = $project_completed_percentage_ %"), "colspan=$colspan align=right");
//            end_row();

        end_table();
    div_end();
}

function display_employee_project_boq_items_details($employee_code, $chart_start_date, $chart_end_date, $num_columns, &$project_completed_percentage) {
    $has_data = false;
    $k = $previous_boq_item_id = $boq_group_total = $previous_project_id = $boq_group_items_count = $project_boq_groups_count = $boq_groups_completed_percentage = 0;
    $previous_project_name = $task_name = "";
    $total_qty = 0;
    
    $colspan = 1;
    $colspan += $num_columns;
    
    // projects
    $result = get_employee_projects_by_filters(0, $chart_start_date, $chart_end_date, $employee_code);
        while ($row = db_fetch($result)) {
            $project_id = $row["project_id"];
            $project_name = get_employee_formatted_long_name($row['project_id']);  
            
            start_row("style=background-color:#0ec9ba");
                label_cell(text_bold($project_name)." (Project)", 'colspan=5');
                label_cell('', "width='80%'");
            end_row();
            
            // boq items
            $result = get_employee_projects_by_filters(0, $chart_start_date, $chart_end_date, $employee_code, $project_id);
                while ($row = db_fetch($result)) {
                    $boq_item_id = $row["boq_item_id"];
                    $task_name = "[".$row["boq_item_code"]."] ".$row["boq_item_name"]; 
                    
                    alt_table_row_color($k);
                        label_cell($task_name);
                    end_row();
                    
                    // employee project boq items recs
                    $result = get_employee_projects_by_filters(0, $chart_start_date, $chart_end_date, $employee_code, $project_id, $boq_item_id);
                        while ($row = db_fetch($result)) {
                            label_cell('');
                            
                            $start_at = $end_at = $start_at_2 = $end_at_2 = 0;
                            get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $row['start_date'], $row['end_date'], $start_at, $end_at);
                            
                            $project_boq_items_days = 1;
                            if ($row['last_entry_date'] != null) {
                                $project_boq_items_days = date_diff2(sql2date($row['last_entry_date']), sql2date($row['actual_start_date']), 'd');
                                $project_boq_items_days == 0 ? 1 : $project_boq_items_days;
                            }
                            
                            $calendar_working_days = $row['calendar_working_days'];
                            $boq_qty_pct = round2(($calendar_working_days / $project_boq_items_days) * 100, 3);
                            
                            // employee project boq items work done recs
                            $result = get_employee_project_works_by_from_and_date_dates($chart_start_date, $chart_end_date, $employee_code, $row['id'], $project_id, 
                                                                                        $boq_item_id);
                                while ($row = db_fetch($result)) {
                                    label_cell('');
                                    
                                    $start_at = $end_at = $start_at_2 = $end_at_2 = 0;
                                    
                                    get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $row['start_date'], $row['end_date'], $start_at_2, 
                                                                                   $end_at_2);
                                    
                                    
                    $completed_qty = $row['completed_qty'];
                    
                    
                    $com_qty_pct = round2(($completed_qty / $calendar_working_days) * 100, 3);
                    
                    draw_gantt_chart_two_lines('100%', '25px', "$start_at", "$end_at", '#00a6ff', "$boq_qty_pct%", "$start_at_2", "$end_at_2", '#ff7700', "$com_qty_pct%", 
                                               $boq_item_id);
                                }
                        }
                }
            
            start_row();
                label_cell(text_bold("Project $previous_project_name Percentage Completed Quantity = 0 %"), "colspan=$colspan align=right");
            end_row();

            start_row();
                vertical_space("colspan=$colspan height=18px");
            end_row();
        }
            
            

            $task_name = "[".$row["code"]."] ".$row["description"];  

            $boq_item_id = $row["boq_item_id"]; 

            if(($project_id > 0 && $previous_project_id == 0) || // first pass
                            ($project_id != $previous_project_id && $previous_project_id > 0)){ // other
                if($previous_project_id > 0){
                        $boq_group_completed_percentage = $boq_group_total / $boq_group_items_count;
                        $previous_project_name = $project_name;
                        $boq_groups_completed_percentage += $boq_group_completed_percentage;
                        $boq_group_total = $boq_group_items_count = 0;
                        $project_boq_groups_count++;
                    
                    $boq_group_completed_percentage_ = number_format($boq_group_completed_percentage, 3);
                    start_row();
                        label_cell(text_bold("Project $previous_project_name Percentage Completed $boq_group_completed_percentage_ %"), "colspan=$colspan align=right");
                    end_row();

                    start_row();
                        vertical_space("colspan=$colspan height=18px");
                    end_row();
                }
            }

            if(($project_id > 0 && $previous_project_id == 0) || // first pass
                            ($project_id != $previous_project_id && $previous_project_id > 0)){ // other
                start_row("style=background-color:#0ec9ba");
                    label_cell(text_bold($project_name)." (Project)", 'colspan=5');
                    label_cell('', "width='80%'");
                end_row();
                start_row();
                    label_cell(text_bold(""), 'colspan=5');
                    draw_gantt_chart_header('100%', '20px', $num_columns, $chart_start_date, $chart_end_date);
                end_row();
            }

                alt_table_row_color($k);

                    label_cell($task_name);
                    label_cell(sql2date($row['start_date']), 'align=center');  
                    label_cell(sql2date($row['end_date']), 'align=center'); 
                    if ($row['actual_start_date'] != INFINITE_START_DATE) {
                        label_cell(sql2date($row['actual_start_date']), 'align=center'); 
                    } else {
                        label_cell('');
                    }
                    if ($row['completed'] == 1 && $row['actual_end_date'] != INFINITE_START_DATE) {
                        label_cell(sql2date($row['actual_end_date']), 'align=center'); 
                    } else {
                        label_cell('');
                    }
                    
                    $start_at = $end_at = $start_at_2 = $end_at_2 = 0;
                    get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $row['start_date'], $row['end_date'], $start_at, $end_at);
                    
                    $actual_start_date = $row['actual_start_date'];
                        if ($actual_start_date > INFINITE_START_DATE) {
                            $actual_end_date = $row['actual_end_date'];
                            if ($actual_end_date == INFINITE_START_DATE && date1_greater_date2(Today(), sql2date($actual_start_date))) {
                                $actual_end_date = date2sql(Today());
                            } else {
                                $last_row = get_boq_item_last_work_done_entry_date_from_details($boq_item_id);
                                    if (isset($last_row['id']) && $last_row['id'] > 0) {
                                        $actual_end_date = $last_row['tran_date'];
                                    } else {
                                        $actual_end_date = $actual_start_date;
                                    }
                            }
                            
                            get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $actual_start_date, $actual_end_date, $start_at_2, $end_at_2);
                        }
                        
                    $max_quantity = $row['max_quantity'];
                    $completed_qty = $row['completed_qty'];
                    
                    $boq_qty_pct = round2(($max_quantity / $total_qty) * 100, 3);
                    
                    $com_qty_pct = round2(($completed_qty / $max_quantity) * 100, 3);
                    
                    draw_gantt_chart_two_lines('100%', '25px', "$start_at", "$end_at", '#00a6ff', "$boq_qty_pct%", "$start_at_2", "$end_at_2", '#ff7700', "$com_qty_pct%", 
                                               $boq_item_id);
                    
                    
                    $completed_percentage = ($completed_qty / $max_quantity) * 100;
                    
                    $boq_group_total += $completed_percentage;
                    $boq_group_items_count++;

                end_row();

                    $previous_boq_item_id = $boq_item_id;
                    $previous_project_id = $project_id; 
                    $previous_project_name = $project_name;
                    
            $has_data = true;
//        } end while loop
    
        if ($has_data === true) {
            $boq_group_completed_percentage = $boq_group_total / $boq_group_items_count;
            $boq_groups_completed_percentage += $boq_group_completed_percentage;
            $project_boq_groups_count++;

            $boq_groups_percentage_percentage = $boq_groups_completed_percentage / $project_boq_groups_count;
            
            $boq_group_completed_percentage_ = number_format($boq_group_completed_percentage, 3);
            start_row();
                label_cell(text_bold("Project $previous_project_name Percentage Completed Quantity = $boq_group_completed_percentage_ %"), 
                           "colspan=$colspan align=right");
            end_row();
            
            start_row("style=background-color:#e5f56e");
                label_cell(text_bold("Project BOQ Items Groups Percentage Completed Quantity = ".number_format($boq_groups_percentage_percentage, 3)." %"), 
                           "colspan=$colspan align=right");
            end_row();

            start_row();
                vertical_space("colspan=$colspan height=18px");
            end_row();
            
            $project_completed_percentage = $boq_groups_percentage_percentage;
        }
}

function display_employee_project_no_boq_items_details($employee_code, $chart_start_date, $chart_end_date, $total_qty, $num_columns, &$project_completed_percentage) {
    $has_data = false;
    $k = $no_boq_group_items_count = $no_boq_group_total = 0;
    
        $colspan = 5;
        $colspan += $num_columns;
        
    $result = get_no_boq_groups_and_item_details($employee_code);
        while ($row = db_fetch($result)) {
            $boq_item_id = $row["boq_item_id"];
            $task_code = $row["code"];
            $task_description = $row["description"];
            $task_name = "[".$task_code."] ".$task_description;  
            $task_name = $task_description;  

            if ($has_data === false) { 
                start_row("style=background-color:#0ec9ba");
                    label_cell(text_bold("No Project"), 'colspan=5');
                    label_cell('', "width='80%'");
                end_row();
                start_row();
                    label_cell(text_bold(""), 'colspan=5');
                    draw_gantt_chart_header('100%', '20px', $num_columns, $chart_start_date, $chart_end_date);
                end_row();
            }

                alt_table_row_color($k);

                    label_cell($task_name);
                    label_cell(sql2date($row['start_date']), 'align=center');  
                    label_cell(sql2date($row['end_date']), 'align=center'); 
                    
                    if ($row['actual_start_date'] != INFINITE_START_DATE) {
                        label_cell(sql2date($row['actual_start_date']), 'align=center'); 
                    } else {
                        label_cell('');
                    }
                    if ($row['completed'] == 1 && $row['actual_end_date'] != INFINITE_START_DATE) {
                        label_cell(sql2date($row['actual_end_date']), 'align=center'); 
                    } else {
                        label_cell('');
                    }
                    
                    $start_at = $end_at = $start_at_2 = $end_at_2 = 0;
                    get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $row['start_date'], $row['end_date'], $start_at, $end_at);
                    
                    $actual_start_date = $row['actual_start_date'];
                        if ($actual_start_date > INFINITE_START_DATE) {
                            $actual_end_date = $row['actual_end_date'];
                            if ($actual_end_date == INFINITE_START_DATE && date1_greater_date2(Today(), sql2date($actual_start_date))) {
                                $actual_end_date = date2sql(Today());
                            } else {
                                $last_row = get_boq_item_last_work_done_entry_date_from_details($boq_item_id);
                                    if (isset($last_row['id']) && $last_row['id'] > 0) {
                                        $actual_end_date = $last_row['tran_date'];
                                    } else {
                                        $actual_end_date = $actual_start_date;
                                    }
                            }
                            
                            get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $actual_start_date, $actual_end_date, $start_at_2, $end_at_2);
                        }
                        
                    $max_quantity = $row['max_quantity'];
                    $completed_qty = $row['completed_qty'];
                    
                    $boq_qty_pct = round2(($max_quantity / $total_qty) * 100, 3);
                    
                    $com_qty_pct = round2(($completed_qty / $max_quantity) * 100, 3);
                    
                    draw_gantt_chart_two_lines('100%', '25px', "$start_at", "$end_at", '#00a6ff', "$boq_qty_pct%", "$start_at_2", "$end_at_2", '#ff7700', "$com_qty_pct%", 
                                               $boq_item_id);
                    
                end_row();
                    
                vertical_space("colspan=$colspan height=18px");
                
                $no_boq_group_total += $com_qty_pct;
                $no_boq_group_items_count++;
                    
                $has_data = true;
        }
    
        if ($has_data === true) {
            start_row("style=background-color:#e5f56e");
                $no_boq_group_completed_percentage = $no_boq_group_total / $no_boq_group_items_count;
                $no_boq_group_completed_percentage_ = number_format($no_boq_group_completed_percentage, 3);
                label_cell(text_bold("Project No Project Percentage Completed Quantity = $no_boq_group_completed_percentage_ %"), "colspan=$colspan align=right");
            end_row();
            
            start_row();
                vertical_space("colspan=$colspan height=18px");
            end_row();
            
            $project_completed_percentage += $no_boq_group_completed_percentage;
        }
}