<?php

function display_boq_item_status_view($project_id){
    global $Ajax;
    
    $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"), _("Completed"), _("BOQ Item Qty"), _("Completed Qty"), 
                        _("Percentage <br> Completed"));   
            table_header($th);

            $project_completed_percentage = 0;
            display_boq_item_status_details($project_id, $project_completed_percentage); 
            
            display_no_boq_item_status_details($project_id, $project_completed_percentage);
            
            start_row("style=background-color:#f5865b");
                label_cell(text_bold("Project Completed Quantity Percentage"),'colspan=8 align=right');
                label_cell(text_bold(number_format($project_completed_percentage, 2)." %"), 'align=right'); 
            end_row();

        end_table();
    div_end();
}

function display_boq_item_status_details($project_id, &$project_completed_percentage) {
    $has_data = false;
    $k = $previous_boq_item_id = $boq_item_total = $boq_group_total = $previous_boq_group_id = $boq_group_items_count = $project_boq_groups_count = 0;
    $boq_groups_completed_percentage = 0;
    $previous_boq_group_name = $previous_boq_item_name = $boq_item_name = $previous_task_name = $task_name = "";
    
    $result = get_boq_groups_and_item_details($project_id);
        while ($row = db_fetch($result)) {
            $depth = spaces(($row["depth"] - 1) * 5);

            $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"]; 

            $boq_item_code = $row["boq_item_code"];  
            $boq_item_description = $row["boq_item_description"];

            if(isset($boq_item_code) && strlen($boq_item_code) > 0){
                $boq_item_name = "[".$boq_item_code."] ".$boq_item_description;  
            }else{
                $boq_item_name = "No BOQ Items founds.";
            }        

            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){
                    start_row();
                        label_cell("BOQ Item Group ".text_bold($previous_boq_group_name)." Total", 'colspan=8 align=right');   
                        $boq_group_completed_percentage = $boq_group_total / $boq_group_items_count;
                        label_cell(text_bold(number_format($boq_group_completed_percentage ,2)." %"), 'align=right'); 
                        $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++;
                    end_row();
                    
                    start_row();
                        vertical_space("colspan=9 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=9');      
                    $previous_boq_item_name = $boq_item_name;
                end_row();
            }

                alt_table_row_color($k);

                    label_cell($depth.spaces(5).$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('');
                    }
                    
                    $style = $row['completed'] == 1 ? "style=background-color:#f5e507" : "";
                    label_cell(get_yes_no_name($row['completed']), "align=center $style");
                    $max_quantity = $row['max_quantity'];
                    amount_cell($max_quantity);  
                    $completed_qty = $row['completed_qty'];
                    if ($completed_qty > 0) {
                        label_cell(get_project_trans_view_str(DASH_VW_ST_PROJECT_BOQ_ITEM_WORK_DONE, $row["boq_item_id"], $completed_qty), "align=right");
                    } else {
                        amount_cell($completed_qty);  
                    }
                    
                    $completed_percentage = ($completed_qty / $max_quantity) * 100;
                    label_cell(text_bold(number_format($completed_percentage,2)." %"), 'align=right');
                    
                    $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;
                    $previous_task_name = $task_name;
                    
            $has_data = true;
        }
    
        if ($has_data === true) {
            start_row();
                label_cell("BOQ Item Group ".text_bold($previous_boq_group_name)." Total", 'colspan=8 align=right');  
                $boq_group_completed_percentage = $boq_group_total / $boq_group_items_count;
                label_cell(text_bold(number_format($boq_group_completed_percentage,2)." %"), 'align=right'); 
                $previous_boq_item_name = $boq_item_name;
                $boq_groups_completed_percentage += $boq_group_completed_percentage;
                $project_boq_groups_count++;
            end_row();

            $boq_groups_percentage_percentage = $boq_groups_completed_percentage / $project_boq_groups_count;
            
            start_row("style=background-color:#e5f56e");
                label_cell(text_bold("Project BOQ Items Groups Completed Quantity Percentage"),'colspan=8 align=right');
                label_cell(text_bold(number_format($boq_groups_percentage_percentage, 2)." %"), 'align=right'); 
            end_row();
            
            start_row();
                vertical_space("colspan=9 height=18px");
            end_row();
            
            $project_completed_percentage = $boq_groups_percentage_percentage;
        }
}

function display_no_boq_item_status_details($project_id, &$project_completed_percentage) {
    $has_data = false;
    $k = $no_boq_group_items_count = $no_boq_group_total = 0;

    $result = get_no_boq_groups_and_item_details($project_id);
        while ($row = db_fetch($result)) {
            $task_code = $row["code"];
            $task_description = $row["description"];
            $task_name = "[".$task_code."] ".$task_description;  

            if ($has_data === false) { 
                start_row("style=background-color:#0ec9ba");
                    label_cell(text_bold("No BOQ Item Group"), 'colspan=9');      
                end_row();
            }

                alt_table_row_color($k);

                    label_cell(spaces(5).$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('');
                    }
                    
                    $style = $row['completed'] == 1 ? "style=background-color:#f5e507" : "";
                    label_cell(get_yes_no_name($row['completed']), "align=center $style");
                    $max_quantity = $row['max_quantity'];
                    amount_cell($max_quantity);  
                    $completed_qty = $row['completed_qty'];
                    if ($completed_qty > 0) {
                        label_cell(get_project_trans_view_str(DASH_VW_ST_PROJECT_BOQ_ITEM_WORK_DONE, $row["boq_item_id"], $completed_qty), "align=right");
                    } else {
                        amount_cell($completed_qty);  
                    }
                    
                    $completed_percentage = ($completed_qty / $max_quantity) * 100;
                    label_cell(text_bold(number_format($completed_percentage,2)." %"), 'align=right');
                    
                    $no_boq_group_total += $completed_percentage;
                    $no_boq_group_items_count++;

                end_row();
                    
            $has_data = true;
        }
    
        if ($has_data === true) {
            start_row("style=background-color:#e5f56e");
                label_cell(text_bold("Project No BOQ Item Group Completed Quantity Percentage"),'colspan=8 align=right');
                $no_boq_group_completed_percentage = $no_boq_group_total / $no_boq_group_items_count;
                label_cell(text_bold(number_format($no_boq_group_completed_percentage,2)." %"), 'align=right'); 
            end_row();
            
            start_row();
                vertical_space("colspan=9 height=18px");
            end_row();
            
            $project_completed_percentage += $no_boq_group_completed_percentage;
        }
}