<?php
/**********************************************************************
***********************************************************************/

function display_est_project_details($project_id) 
{       
    if(!($project_id > 0)){
        return;
    }
    
    display_heading2("Project Details");
    br(1);
    start_outer_table(TABLESTYLE2);

    table_section(1);
    table_section_title(_("Project"));
        
	//------------------------------------------------------------------------------------            

            $myrow = get_approve_est_project_details($project_id);
    
            label_row("No:", $myrow['est_project_id']);
            label_row("Code:", $myrow['code']);
            label_row("Description:", $myrow['description']);
            label_row("Long Description:", $myrow['long_description']);
            label_row("Department:", $myrow['department_name']);
            label_row("Bid Opening Date:", sql2date($myrow["bid_date"]));
            label_row("Start Date:", sql2date($myrow["start_date"]));
            label_row("End Date:", sql2date($myrow["end_date"]));
            label_row("Project Manager Name:", $myrow['project_manager_name']);

            label_row("Contact Person:", $myrow['contact_person']);

            

            
            table_section(2);
            table_section_title(_("Values"));
            label_row("Project Value:", number_format($myrow['project_value'], 2));
//            label_row("Estimated Project Cost:", number_format($_POST['estimated_cost'], 2));            
            
            //label_row("Project Approved Budgets Total:", number_format($myrow['approved_budgets_total'], 2));
            
            //==================Department approved values=====================
            start_row();
            label_cell(text_bold("Current BOQ Values"), "colspan=2 align=center");
            end_row();
            
            
            $cost_components_total = get_est_project_boq_items_cost_components_total($myrow["est_project_id"]);
            $overhead_components_total = get_est_project_boq_items_overhead_components_total($myrow["est_project_id"]);
            $boq_approved_all_components_total = $cost_components_total + $overhead_components_total;
            
            //----cost components----
            $boq_approved_cost_components_percentage = 0;
            if($boq_approved_all_components_total > 0 && $myrow['project_value'] > 0){
                $boq_approved_cost_components_percentage = $cost_components_total/$myrow['project_value']*100;  
            }
            $approved_cost_components_text = number_format($cost_components_total,2)." (".number_format($boq_approved_cost_components_percentage,2)."%)";
            label_row("Cost Components Total:", $approved_cost_components_text);     

            //----overhead components----            
            $boq_approved_overhead_components_percentage = 0;
            if($boq_approved_all_components_total > 0){
                $boq_approved_overhead_components_percentage = $overhead_components_total/$cost_components_total*100;  
            }
            $approved_overhead_components_text = number_format($overhead_components_total,2)." (".number_format($boq_approved_overhead_components_percentage,2)."%)";
            label_row("Overhead Components Total:", $approved_overhead_components_text); 
            label_row("Total Cost:", number_format($boq_approved_all_components_total,2)); 

            
            if($boq_approved_all_components_total > 0 && $myrow['project_value'] > 0){
                $boq_expected_profit = $myrow['project_value'] - $boq_approved_all_components_total; 
                $boq_expected_profit_percentage = $boq_expected_profit/$myrow['project_value']*100;
                $boq_expected_profit_text = number_format($boq_expected_profit,2)." (".number_format($boq_expected_profit_percentage,2)."%)";
            }
            else{
                $boq_expected_profit_text = "BOQ approval pending";
            }
            label_row("Expected Profit:", text_bold($boq_expected_profit_text));     
            

            //==================Project approved values=====================
            start_row();
            label_cell(text_bold("Project Approved Values"), "colspan=2 align=center");
            end_row();
            
            $proj_approved_all_components_total =  $myrow['approved_cost_components_total'] +  $myrow['approved_overhead_components_total'];
            
            //----cost components----
            $proj_approved_cost_components_percentage = 0;
            if($proj_approved_all_components_total > 0){
                $proj_approved_cost_components_percentage = $myrow['approved_cost_components_total']/$myrow['project_value']*100;  
            }
            $approved_components_text = number_format($myrow['approved_cost_components_total'],2)." (".number_format($proj_approved_cost_components_percentage,2)."%)";
            label_row("Cost Components Total:", $approved_components_text);      
            
            
            //----overhead components----            
            $proj_approved_overhead_components_percentage = 0;
            if($proj_approved_all_components_total > 0){
                $proj_approved_overhead_components_percentage = $myrow['approved_overhead_components_total']/$myrow['approved_cost_components_total']*100;  
            }
            $approved_components_text = number_format($myrow['approved_overhead_components_total'],2)." (".number_format($proj_approved_overhead_components_percentage,2)."%)";
            label_row("Overhead Components Total:", $approved_components_text); 
            label_row("Total Cost:", number_format($proj_approved_all_components_total,2)); 
            
            if($proj_approved_all_components_total > 0){
                $proj_expected_profit = $myrow['project_value'] - $proj_approved_all_components_total; 
                $proj_expected_profit_percentage = $proj_expected_profit/$myrow['project_value']*100;
                $proj_expected_profit_text = number_format($proj_expected_profit,2)." (".number_format($proj_expected_profit_percentage,2)."%)";
            }
            else{
                global $document_approval_statuses;
                $proj_expected_profit_text = $document_approval_statuses[$myrow['approval_status']];
            }
            label_row("Expected Profit:", text_bold($proj_expected_profit_text));                 
            
            table_section_title(_("Approval Status"));
            
            global $approval_statuses;
            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $project_id."&source_type_id=".ST_APPROVE_EST_PROJECT, "Approval Status");
            label_row($s,$approval_statuses[$myrow['approval_status']]);
          
        end_outer_table(1);
        
//        hidden('project_id',$project_id);
                

//        submit('update_app_project', _("Approve Project"), true, '', 'default');
        
        return $boq_approved_all_components_total;
              
}



function display_est_component_category_summary($Project_id, $dep_approved_all_budgets_total)
{
    display_heading2("BOQ Item Component Category Summary");
    br(1);
    
    start_table(TABLESTYLE, "width=40%");

    $th = array(_("Component Category"), _("Total"), "%");
      
    table_header($th);

//    $current_budgets_total = $bdget_totals['current_budgets_total'];

    $result = get_all_list_values('PROJECT_TASK_DETAIL_TYPE_CATEGORIES');    
    while($row = db_fetch($result))
    {
        start_row();
        label_cell($row['value_name']);
        
        $total_cost = get_est_project_boq_items_components_category_total($Project_id, $row['id']);
        amount_decimal_cell($total_cost);
        if($dep_approved_all_budgets_total>0){
            label_cell(number_format($total_cost/$dep_approved_all_budgets_total*100,2), "align=right");
        }
        else{
            label_cell(number_format(0,2), "align=right");
        }
        
        
        end_row();

    }
    
    end_table();
}
        


function display_competitors_inquiry($est_project_id)
{
    


    div_start('price_table');

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

    $th = array(_("ID"), "Type", "Competitor / Group Company", "Value", "Bid Winner", "Comments");

    table_header($th);

    $k = 0; //row colour counter

    $site_list = get_competitors($est_project_id);
    
    while ($myrow = db_fetch($site_list))
    {
        alt_table_row_color($k);

        label_cell($myrow["id"],'align=center');
        
        if($myrow["competitor_type"] == true){
            $competitor_type_name = "Group Company";            
        }
        else{
            $competitor_type_name = "Competitor";
        }
        label_cell($competitor_type_name);
        
        if($myrow["competitor_type"] == true){
            label_cell(get_group_company_name($myrow["company_code"]));            
        }
        else{
            label_cell($myrow["competitor"]);
        }
        
        
        
        amount_decimal_cell($myrow["value"]);
        label_cell(get_yes_no_name($myrow["bid_winner"]),'align=center');
        label_cell($myrow["comment"]);

        end_row();
    }

    end_table();

    div_end();
}


?>