<?php
$page_security = 'SA_PROJECT_WORK_DONE';
$path_to_root = "../..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_sites_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");

$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

simple_page_mode(true);

$trans_no = 0;
if (isset($_GET["trans_no"])) {
    $_POST["trans_no"] = $_GET["trans_no"];
}

if (isset($_POST["trans_no"])) {
    $trans_no = $_POST["trans_no"];
}

$row_id = $trans_no;

page(_($help_context = "Project BOQ Item Workdone Details"), true, false, false, $js);

function display_project_boq_item_work_done_details($row_id, $max_quantity) {
    start_table(TABLESTYLE, "width=70%");

        $th = array(_("ID"), _("Project Site Name"), _("Last Tran Date"), _("Unit"), _("Completed Qty"), _("Percentage Completed"), _("Comment"));
        table_header($th);
        
        $total_quantity = $total_percentage_completed = $k = 0;
        $result_a = get_boq_item_site_with_work_done_entry_details($row_id);
            while ($row = db_fetch($result_a)) {
                alt_table_row_color($k);
                    label_cell($row["id"], "align=center");
                    $site_name = ! is_null($row["site_name"]) && strlen($row["site_name"]) > 0 ? $row["site_name"] : 'No site';
                    label_cell($site_name);   
                    label_cell(sql2date($row["last_entry_date"]), "align=center");
                    label_cell($row["unit"], "align=center");   
                    amount_decimal_cell($row["completed_qty"]);
                    
                    $percentage_completed = ($row['completed_qty'] / $max_quantity) * 100;
                    label_cell(number_format($percentage_completed, 3)." %", "align=right");
                    label_cell($row["comment"]);
                end_row();
                
                $total_quantity += $row["completed_qty"];
                $total_percentage_completed += $percentage_completed;
            }
            
            start_row();
                label_cell(text_bold(number_format($total_quantity, 3)), "align=right colspan=5");
                label_cell(text_bold(number_format($total_percentage_completed, 3)." %"), "align=right");
                label_cell('');
            end_row();
    
    end_table(1);
}

start_form();

    br();
    $row = get_project_boq_item_record($row_id);

    display_heading2("Workdone Details of BOQ Item : "."[".$row['code']."] - ".$row['description']." - Quantity : ".$row["max_quantity"]);    br(); 

    display_project_boq_item_work_done_details($row_id, $row["max_quantity"]);

end_form();

end_page();