<?php

function display_boq_item_status_gantt_chart($project_id){
    global $Ajax;
    
    $least_and_greatest_dates_array = get_project_boq_items_least_and_greatest_dates_array($project_id, null);
        $chart_start_date = $least_and_greatest_dates_array['least_date'];
        $chart_end_date = $least_and_greatest_dates_array['greatest_date'];
        
        if ($chart_start_date === null) {
            $chart_start_date = $chart_end_date;
        }
        
    $total_qty_and_total_complated_qty = get_project_boq_item_total_qty_and_total_completed_qty($project_id, null);
        $total_qty = $total_qty_and_total_complated_qty['total_qty'];
        
        $colspan = 5;
        $num_columns = 12;
        $colspan += $num_columns;
        
    $Ajax->activate('boq_item_status_view');
    div_start('boq_item_status_view');
        start_table(TABLESTYLE, "width=99%");

            $th = array(_("BOQ Item"), _("Start Date"), _("End Date"), _("Actual Start Date"), _("Actual End Date"), _("Project Work Status"));   
            table_header($th);

            $project_completed_percentage = 0;
            display_boq_item_status_details($project_id, $chart_start_date, $chart_end_date, $total_qty, $num_columns, $project_completed_percentage); 
            
            display_no_boq_item_status_details($project_id, $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_boq_item_status_details($project_id, $chart_start_date, $chart_end_date, $total_qty, $num_columns, &$project_completed_percentage) {
    $has_data = false;
    $k = $previous_boq_item_id = $boq_group_total = $previous_boq_group_id = $boq_group_items_count = $project_boq_groups_count = $boq_groups_completed_percentage = 0;
    $previous_boq_group_name = $task_name = "";
    
        $colspan = 5;
        $colspan += $num_columns;
    
    $result = get_boq_groups_and_item_details($project_id);
        while ($row = db_fetch($result)) {
            $boq_group_id = $row["boq_group_id"];
            
            $boq_group_code = $row["boq_group_code"];
            $boq_group_description = $row["boq_group_description"];

            $boq_group_name = "[".$boq_group_code."] ".$boq_group_description;  

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

            $boq_item_id = $row["boq_item_id"]; 

            if(($boq_group_id > 0 && $previous_boq_group_id == 0) || // first pass
                            ($boq_group_id != $previous_boq_group_id && $previous_boq_group_id > 0)){ // other
                if($previous_boq_group_id > 0){
                        $boq_group_completed_percentage = $boq_group_total / $boq_group_items_count;
                        $previous_boq_group_name = $boq_group_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("BOQ Item Group $previous_boq_group_name Percentage Completed Quantity = $boq_group_completed_percentage_ %"), 
                                   "colspan=$colspan align=right");
                    end_row();

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

            if(($boq_group_id > 0 && $previous_boq_group_id == 0) || // first pass
                            ($boq_group_id != $previous_boq_group_id && $previous_boq_group_id > 0)){ // other
                start_row("style=background-color:#0ec9ba");
                    label_cell(text_bold($boq_group_name)." (BOQ Item Group)", 'colspan=6');
                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 = $chart_end_date;
                            } 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 = $com_qty_pct = $completed_percentage = 0;
                    if ($total_qty != 0) {
                        $boq_qty_pct = round2(($max_quantity / $total_qty) * 100, 3);
                    }
                    
                    if ($max_quantity != 0) {
                        $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);
                    
                    
                    if ($max_quantity != 0) {
                        $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_boq_group_id = $boq_group_id; 
                    $previous_boq_group_name = $boq_group_name;
                    
            $has_data = true;
        }
    
        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("BOQ Item Group $previous_boq_group_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_no_boq_item_status_details($project_id, $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($project_id);
        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 BOQ Item Group"), '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 = $com_qty_pct = 0;
                    if ($total_qty != 0) {
                        $boq_qty_pct = round2(($max_quantity / $total_qty) * 100, 3);
                    }
                    
                    if ($max_quantity != 0) {
                        $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 BOQ Item Group 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;
        }
}

function draw_gantt_chart_line($width='100%', $height='18px', $start_at='0%', $end_at='100%', $color='red') {
    
    label_cell('', "style=\"width:$width; height:$height; 
                                background: linear-gradient(
                                            to right,
                                            transparent 0 $start_at,
                                            $color $start_at $end_at,
                                            transparent $end_at 100%
                                          );\"");
}

function draw_gantt_chart_two_lines($width='100%', $height='18px', $start_at='0', $end_at='100', $color='red', $value='', $start_at_2='0', $end_at_2='100', $color_2='orange', 
                                    $value_2='', $boq_item_id=0) {
    
    $value_position = round(($start_at + $end_at) / 2);
    $value_position_2 = round(($start_at_2 + $end_at_2) / 2);
    
    $value_2_link = get_project_trans_view_str(DASH_VW_ST_PROJECT_BOQ_ITEM_WORK_DONE, $boq_item_id, $value_2);
    
    $chart_lines = "<span style=\"display: inline-block; width:$width; height:$height; 
                                position: relative;
                                background: linear-gradient(
                                            to right,
                                            transparent 0 $start_at%,
                                            $color $start_at% $end_at%,
                                            transparent $end_at% 100%
                                          ), 
                                          linear-gradient(
                                            to right,
                                            transparent 0 $start_at_2%,
                                            $color_2 $start_at_2% $end_at_2%,
                                            transparent $end_at_2% 100%
                                          );
                                
                                background-repeat: no-repeat;
                                background-size: 100% 13px; /* height of each line */
                                background-position: 0px 0px, 0px 12px; /* x-offset, y-offset */
                                /*background-position: top, bottom; /* first line on top, second on bottom */
                                \"> 
                               
                        <!-- Label for first line -->
                        <span style='
                            position: absolute;
                            left:$value_position%; 
                            top:-3px; 
                            transform:translateX(-50%);
                            font-size: 12px;
                            color: black;
                        '>$value</span>

                        <!-- Label for second line -->
                        <span style='
                            position: absolute;
                            left:$value_position_2%; 
                            bottom:-4px; 
                            transform:translateX(-50%);
                            font-size: 10px;
                            color: black;
                        '>$value_2_link</span> 

                    </span>";
    
        label_cell($chart_lines);
}

function draw_gantt_chart_header($width='100%', $height='18px', $num_columns=12, $chart_start_date=null, $chart_end_date=null) {
    
    $chart_start_date_ = new DateTime($chart_start_date);
    $chart_end_date_ = new DateTime($chart_end_date);

    $interval = $chart_start_date_->diff($chart_end_date_);
    $chart_total_days = $interval->days; 
        
        $column_days_size = round($chart_total_days / $num_columns);
    
        $str = "<div style='display:flex; width:$width; height:$height;'>";

        for ($i = 0; $i < $num_columns; $i++) {
            // just alternate colors for demo
            $color = ($i % 2 == 0) ? "lightblue" : "lightgreen";

            $next_date = add_days(sql2date($chart_start_date), $column_days_size * $i);

            $str .= "<div style=\"
                flex:1;
                height:20px;
                background:$color;
                text-align:center;
                font-size:10px;
                font-weight: bold;
                line-height:20px;
                border:1px solid #ccc;
            \">$next_date</div>";
        }

        $str .= "</div>";

        echo "<td>$str</td>";
}

function get_gantt_chart_line_start_and_end_percentages($chart_start_date, $chart_end_date, $start_date, $end_date, &$start_at, &$end_at) {
    $chart_start_date_ = new DateTime($chart_start_date);
    $chart_end_date_ = new DateTime($chart_end_date);

        $total_interval = $chart_start_date_->diff($chart_end_date_);
        $total_days_on_graph = $total_interval->days;
        
        $start_at = $end_at = 0;
        if ($total_days_on_graph != 0) {
            $start_date_ = new DateTime($start_date);
            $start_interval = $chart_start_date_->diff($start_date_);
            $start_days = $start_interval->days;

            $end_date_ = new DateTime($end_date);
            $end_interval = $chart_start_date_->diff($end_date_);
            $end_days = $end_interval->days;
            
            $start_at = ($start_days / $total_days_on_graph) * 100;
            $end_at = ($end_days / $total_days_on_graph) * 100;
        }
}