<?php
$page_security = 'SA_PROJECT_BOQ_ITEMS';
$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 . "/estimation/includes/db/est_project_inquiry_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_tasks_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_costs_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_material_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_groups_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_labour_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_assets_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_transport_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_projects_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);

$boq_item_id = 0;

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


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

function display_est_boq_item_header($myrow)
{
    global $project_billing_types;
//    $myrow = view_task_group_header($task_group_id);
    
    display_heading("BOQ Item ".$myrow['code']." - ".$myrow['description']);
    
    br();
    
    

    start_table(TABLESTYLE2, "width=50%");

    start_row();

//    $iou_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU, $myrow['iou_id']);
//    label_cells(_("I Owe You No"), $iou_link, "class='tableheader2'");
//    end_row();
    label_cells(_("Type"), $myrow['type_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Code"), $myrow['code'], "class='tableheader2'");
    end_row();
    label_cells(_("Description"), $myrow['description'], "class='tableheader2'");
    end_row();
    

    label_cells(_("Display Order"), $myrow['display_order'], "class='tableheader2'");
    end_row();
    label_cells(_("Unit"), $myrow['unit'], "class='tableheader2'");
    end_row();
    if($myrow['group_id'] > 0){
        $s = get_estimation_trans_view_str(ST_EST_PROJECT_BOQ_ITEM_GROUP, $myrow['est_project_id']."|".$myrow['group_id'], $myrow['boq_group_code']." - ".$myrow['boq_group_description']);
    }
    else{
        $s = get_estimation_trans_view_str(ST_EST_PROJECT_BOQ_ITEM_GROUP, $myrow['est_project_id']."|0", "No BOQ item group");
    }
    label_cells(_("BOQ Item Group"), $s, "class='tableheader2'");
    end_row();    
    label_cells(_("Calculated Cost"), $myrow['calculated_cost'], "class='tableheader2'");
    end_row();
    label_cells(_("Billing Type"), $project_billing_types[$myrow['billing_type']], "class='tableheader2'");
    end_row();    
    label_cells(_("Rate"), $myrow['rate'], "class='tableheader2'");
    end_row();
    label_cells(_("Quantity"), $myrow['max_quantity'], "class='tableheader2'");
    end_row();    
    label_cells(_("Tax Group"), $myrow['tax_group_name'], "class='tableheader2'");
    end_row();
    
    label_cells(_("Long Description"), $myrow['long_description'], "class='tableheader2'");
    end_row();
    label_cells(_("Print Long Description"), get_yes_no_name($myrow['print_long_description']), "class='tableheader2'");
    end_row();
    
    
    end_row();
    
    end_table();
    
}

function display_est_boq_item_components($boq_item_id)
{
    global $project_boq_item_component_primary_types;
    
    br(2);
    
    display_heading("BOQ Item Cost Components");

    start_outer_table(TABLESTYLE_NOBORDER, "width=90%");
    $result = view_est_boq_item_components($boq_item_id);
    
    $k = 0;
    while ($myrow = db_fetch($result))
    {
        $sub_heading = "Detail Type : ".$myrow["detail_type_name"];
        if($myrow["primary_type"] > 0){
            $sub_heading .= spaces(10)."Primary Type : ".$project_boq_item_component_primary_types[$myrow["primary_type"]];
//            $sub_heading .= spaces(10)."Cost : ".$myrow["cost"];
        }
//        else{
//            $sub_heading .= spaces(10)."No components";
//        }
//        display_heading2($sub_heading);
        start_table(TABLESTYLE, "width=100%");
        start_row();
        label_cell('<b>'.$sub_heading.'</b>');
        end_row();
        if(!(isset($myrow["id"]) && $myrow["id"] > 0)){
            start_row();
            label_cell("No components");
            end_row();
        }
        end_table();
        
        if(isset($myrow["id"]) && $myrow["id"] > 0){
            display_est_boq_component_details($boq_item_id, $myrow["detail_type_code"], $myrow["primary_type"]);
        }
        else{
            br();
        }
//        else{
//            start_table(TABLESTYLE, "width=100%");
//            start_row();
//            label_cell("No components");
//            end_row();
//            end_table();
//            br();
//        }
        

    }
    

    end_outer_table();
    
    br(2);
}

function display_est_boq_component_details($boq_item_id, $detail_type_code, $primary_type)
{
    switch ($primary_type) {
        case BOQPT_COST:
            display_est_boq_item_costs($boq_item_id, $detail_type_code);
            break;
        case BOQPT_MATERIAL:
            display_est_boq_item_material($boq_item_id, $detail_type_code);
            break;
        case BOQPT_LABOUR:
            display_est_boq_item_labour($boq_item_id, $detail_type_code);
            break;
        case BOQPT_ASSET:
            display_est_boq_item_assets($boq_item_id, $detail_type_code);
            break;
        case BOQPT_TRANSPORT:
            display_est_boq_item_transport($boq_item_id, $detail_type_code);
            break;

        default:
            break;
    }
}

function display_est_boq_item_transport($boq_item_id, $detail_type_code)
{
    $result = get_est_boq_item_transport($boq_item_id, $detail_type_code);
    
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("ID"), _("Vehicle Type"),  
                    _("Rate"),_("Days"),"Cost", _("Comment"));
	table_header($th);         
        
	$k = 0;
        $total_cost = 0;
	while ($myrow = db_fetch($result))
	{
        alt_table_row_color($k);

        label_cell($myrow['id'], "align=right");
        label_cell($myrow["vehicle_type_name"]);    
        label_cell(number_format($myrow['rate'], 2), "align=right"); 
        label_cell(number_format($myrow['days'], 1), "align=right"); 
                 
        
        $cost = $myrow['rate'] * $myrow['days'];
        label_cell(number_format($cost, 2), "align=right");  
        label_cell($myrow["comment"]); 

        $total_cost += $cost; 

        end_row();

	} //END WHILE LIST LOOP
        
        start_row();
            label_cell('<b>'."Total :".'</b>', "colspan=4 align=right");
            amount_cell($total_cost, true);
        end_row();
        
        end_table(1);
}

function display_est_boq_item_assets($boq_item_id, $detail_type_code)
{
    $result = get_est_boq_item_assets($boq_item_id, $detail_type_code);
    
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("ID"), "Asset", _("Asset Group"), _("Current Rate"), 
                    _("Applied Rate"),_("Days"),"Cost", _("Comment"));
	table_header($th);         
        
	$k = 0;
        $total_cost = 0;
	while ($myrow = db_fetch($result))
	{
        alt_table_row_color($k);

        label_cell($myrow['id'], "align=right");
        $asset_name = $myrow['asset_code']." - ".$myrow['asset_description'];
        label_cell($asset_name);
        label_cell($myrow["rate_category_description"]);    
        label_cell(number_format($myrow['current_rate'], 2), "align=right"); 
        label_cell(number_format($myrow['rate'], 2), "align=right"); 
        label_cell(number_format($myrow['days'], 1), "align=right"); 
                 
        
        $cost = $myrow['rate'] * $myrow['days'];
        label_cell(number_format($cost, 2), "align=right");  
        label_cell($myrow["comment"]); 

        $total_cost += $cost; 

        end_row();

	} //END WHILE LIST LOOP
        
        start_row();
            label_cell('<b>'."Total :".'</b>', "colspan=5 align=right");
            amount_cell($total_cost, true);
        end_row();
        
         end_table(1);
}



function display_est_boq_item_labour($boq_item_id, $detail_type_code)
{
    $result = get_est_boq_item_labour($boq_item_id, $detail_type_code);
    
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("ID"), _("Rate Category"), _("Current Rate"), 
                    _("Applied Rate"),_("Days"),"Cost", _("Comment"));
	table_header($th);         
        
	$k = 0;
        $total_cost = 0;
	while ($myrow = db_fetch($result))
	{
        alt_table_row_color($k);

        label_cell($myrow['id'], "align=right");
        label_cell($myrow["rate_category_description"]);    
        label_cell(number_format($myrow['current_rate'], 2), "align=right"); 
        label_cell(number_format($myrow['rate'], 2), "align=right"); 
        label_cell(number_format($myrow['days'], 1), "align=right"); 
                 
        
        $cost = $myrow['rate'] * $myrow['days'];
        label_cell(number_format($cost, 2), "align=right");  
        label_cell($myrow["comment"]); 

        $total_cost += $cost; 

        end_row();

	} //END WHILE LIST LOOP
        
        start_row();
            label_cell('<b>'."Total :".'</b>', "colspan=5 align=right");
            amount_cell($total_cost, true);
        end_row();
        
         end_table(1);

}


function display_est_boq_item_material($boq_item_id, $detail_type_code)
{
    $result = get_est_boq_item_material($boq_item_id, $detail_type_code);
    

    $n = db_num_rows($result);    
    if($n == 0){
        display_note("No Materials found.");
        return;
    }
    

	start_table(TABLESTYLE, "width=100%");
	$th = array( _("ID"), _("Item"), _("Description"), 
                     _("Unit"), "Current Cost", _("Applied Cost"), _("Qty"), _("Line Cost"),_("Comment"));
	table_header($th);

	$k = 0;
        $j = "";
        $full_total = 0;
                  
	while ($myrow = db_fetch($result))
	{                       
            alt_table_row_color($k);

            label_cell($myrow["id"], "align=right");          
            
            if($myrow["item_type"] == 0){
                $stock_item = get_stock_item_edit_info($myrow["stock_id"]);
                $item_code = $stock_item['stock_id'];
                $item_description = $stock_item['description'];
                $item_unit_cost = $stock_item["standard_cost"];
            }
            else{
                $material_group = get_material_group_item($myrow["stock_id"]);
                $item_code = $material_group['code'];
                $item_description = $material_group['description'];
                $item_unit_cost = $material_group["cost"];
            }
            
            label_cell($item_code);
            label_cell($item_description);           
            label_cell($myrow["tran_unit_name"], "align=center");
            label_cell(number_format($item_unit_cost, 2), "align=right");
            label_cell(number_format($myrow["tran_unit_cost"], 2), "align=right");
                        
            label_cell($myrow["tran_qty"], "align=right");          

            $line_cost = $myrow["tran_unit_cost"] * $myrow["tran_qty"];

            label_cell(number_format($line_cost, 2), "align=right");
            label_cell($myrow["comment"]);

            end_row();          

            $j++;

            $full_total += $line_cost ;                
	} 
        
        start_row();
            label_cell('<b>'."Total :".'</b>', "colspan=7 align=right");
            amount_cell($full_total, true);
        end_row();
                                                 
    end_table(1);

}

function display_est_boq_item_costs($boq_item_id, $detail_type_code)
{
    global $Ajax;
    
    $result = get_est_boq_item_costs($boq_item_id, $detail_type_code);
//    div_start('bom');
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("ID"), _("Description"), _("Cost"), _("Comment"));
	table_header($th);        
        
	$k = 0;
        $total = 0;
	while ($myrow = db_fetch($result))
	{
        alt_table_row_color($k);

        label_cell($myrow['id'], "align=right");
        label_cell($myrow["description"]);
        label_cell(number_format($myrow["cost"], 2), "align=right");
        label_cell($myrow["comment"]);

        $total += $myrow["cost"];

        end_row();

	} //END WHILE LIST LOOP
        
        start_row();
            label_cell('<b>'."Total :".'</b>', "colspan=2 align=right");
            amount_cell($total, true);
        end_row();


    end_table(1);

}


start_form();

$boq_item = view_est_boq_item_header($boq_item_id);

$user_id = $_SESSION['wa_current_user']->user;
//$r = can_access_project($boq_item['est_project_id'], $user_id);
//if($r["success"] !== true){
//    display_error($r["message"]);
//    display_footer_exit();
//    exit;
//}

display_est_boq_item_header($boq_item);

display_est_boq_item_components($boq_item_id);

end_form();


end_page();















































