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



include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/projects/includes/db/work_done_entry_details_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_sites_db.inc");
include_once($path_to_root . "/projects/includes/ui/work_done_entry_ui.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");

        
$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}


simple_page_mode(true);

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


$sep_pos = strpos($trans_no, "|");
$project_id = substr($trans_no, 0, $sep_pos);
$boq_group_id = substr($trans_no, $sep_pos+1);

$site_id = 0;

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

function display_project_boq_group_work_done($boq_group_id, $site_id)
{
    if(!($boq_group_id >= 0)){
        $boq_group_id = 0;
    }
    
    if(!($site_id >= 0)){
        $site_id = 0;
    }
    
    $boq_group = get_project_boq_item_group_record($boq_group_id);    
    
    display_heading2("Workdone Details of BOQ Item Group [".$boq_group['code']."] - ".$boq_group['description']);
    
    br();
            
    $result_a = view_project_boq_group_work_done($boq_group_id);

    
    start_table(TABLESTYLE, "width=60%");

    $th = array(_("ID"),_("Code"), _("Description"), "Billing Type",
                _("Unit"), _("Quantity"), "Last Entry Date");
   
    table_header($th);

    while ($boq_item = db_fetch($result_a))
    {
        start_row();

//        label_cell(get_project_trans_view_str(ST_PROJECT_BOQ_ITEM, $boq_item["id"]), "align=center");
        label_cell($boq_item["id"]);
        label_cell($boq_item["code"]);
        label_cell($boq_item["description"]);

        if($boq_item["billing_type"] == 1){
            label_cell("Lump Sum");
        }else{
            label_cell("Measure and Pay");
        }
        label_cell($boq_item["unit"]);  
        
        $completed_qty = 0;
        if($boq_item['completed_qty'] > 0){
            $completed_qty =  $boq_item['completed_qty'];
        }        

        
        if($completed_qty > 0){
            $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_WORK_DONE, $boq_item["id"]."|".$site_id, $completed_qty);
        }
        else{
            $s = "";
        }
        label_cell($s, "align=right");
            
        
        if($boq_item["last_entry_date"] == "0000-00-00"){
            label_cell("");
        }else{
            label_cell(sql2date($boq_item['last_entry_date']), "align=center");
        }
       
    }
    
    end_table(1);

}

function display_project_no_group_work_done($project_id, $site_id)
{
    if(!($project_id >= 0)){
        return;
    }
    
    if(!($site_id >= 0)){
        $site_id = 0;
    }
    
    display_heading2("Workdone Details of BOQ Items with no BOQ Item Group");
    
    br();
    
    $result_a = view_project_no_group_work_done($project_id);

    
    start_table(TABLESTYLE, "width=60%");

    $th = array(_("ID"),_("Code"), _("Description"), "Billing Type",
                _("Unit"), _("Quantity"), "Last Entry Date");
   
    table_header($th);

    while ($boq_item = db_fetch($result_a))
    {
        start_row();

//        label_cell(get_project_trans_view_str(ST_PROJECT_BOQ_ITEM, $boq_item["id"]), "align=center");
        label_cell($boq_item["id"]);
        label_cell($boq_item["code"]);
        label_cell($boq_item["description"]);

        if($boq_item["billing_type"] == 1){
            label_cell("Lump Sum");
        }else{
            label_cell("Measure and Pay");
        }
        label_cell($boq_item["unit"]);  
        
        $completed_qty = 0;
        if($boq_item['completed_qty'] > 0){
            $completed_qty =  $boq_item['completed_qty'];
        }        

        
        if($completed_qty > 0){
            $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_WORK_DONE, $boq_item["id"]."|".$site_id, $completed_qty);
        }
        else{
            $s = "";
        }
        label_cell($s, "align=right");
            
        
        if($boq_item["last_entry_date"] == "0000-00-00"){
            label_cell("");
        }else{
            label_cell(sql2date($boq_item['last_entry_date']), "align=center");
        }
       
    }
    
    end_table(1);

}

start_form();

$site_id = 0;

if($boq_group_id > 0){
    display_project_boq_group_work_done($boq_group_id, $site_id);
}
else{
    display_project_no_group_work_done($project_id, $site_id);
}

//submit_center_first('Recalculate_Poject_PA', _("Recalculate Payment Application Summaries"), '', 'default');

end_form();


end_page();
