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

$page_security = 'SA_MONTHLY_PROJECT_COSTS_VIEW';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.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 . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/db/approval_functions_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
include_once($path_to_root . "/gl/includes/db/monthly_project_costs_inquiry_db.inc");
include_once($path_to_root . "/includes/references.inc");
include_once($path_to_root . "/includes/main.inc");

      
$js = '';
if ($SysPrefs->use_popup_windows){
	$js .= get_js_open_window(1200, 1000);
}

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

page(_($help_context = "View Monthly Project Costs"), true, false, "", $js);

//------------------------------------------------------------------------------------------------------



simple_page_mode(true);   

if (isset($_GET['trans_no']))
{
    $_POST['trans_no'] = $_GET['trans_no'];
    set_session_variable("ST_MONTHLY_PROJECT_COSTS_no", $_POST['trans_no']);
}


if (!@$_GET['popup'])
start_form();


function display_monthly_project_costs_header($mpc_no)
{
    global $approval_statuses, $Refs, $monthly_project_costs_base_totals, $monthly_project_costs_distribution_types;
    
    if($mpc_no > 0){
        $mpc_header = view_monthly_project_costs_header($mpc_no);
        
        $project_id = $mpc_header['project_id'];
        $user_id = $_SESSION['wa_current_user']->user;

        $_POST['project_id'] = $mpc_header['project_id'];
    
        if(!isset($_POST['reference'])){
            $_POST['reference'] = $mpc_header['reference'];
        }

        if(!isset($_POST['date'])){
            $_POST['date'] = sql2date($mpc_header['date']);
        }
        
        if(!isset($_POST['project_description'])){
            $_POST['project_description'] = $mpc_header['project_description'];
        }
        
        if(!isset($_POST['approval_status'])){
            $_POST['approval_status'] = $mpc_header['approval_status'];
        }        
               
        if(!isset($_POST['header_comment'])){
            $_POST['header_comment'] = $mpc_header['comment'];
        }
        
        if(!isset($_POST['base_total_type'])){
            $_POST['base_total_type'] = $mpc_header['base_total_type'];
        }
        
        if(!isset($_POST['distribution_type'])){
            $_POST['distribution_type'] = $mpc_header['distribution_type'];
        }
        
        if(!isset($_POST['month_name'])){
            $_POST['month_name'] = $mpc_header['month_name'];
        }
        
        if(!isset($_POST['scheduled_total'])){
            $_POST['scheduled_total'] = $mpc_header['scheduled_total'];
        }
    
    }
    else{
        $_POST['reference'] = $Refs->get_next(ST_MONTHLY_PROJECT_COSTS);
        $_POST['approval_status'] = $_POST['base_total_type'] = $_POST['distribution_type'] = 0;
        $_POST['total'] = 0;
    }
        
    start_outer_table(TABLESTYLE2, "width=70%"); // outer table
    start_table(TABLESTYLE2);  
        table_section(1, "50%");

            label_row("Reference:", $_POST['reference'], "class='tableheader2'");  

            label_row("Month", $_POST['month_name'], "class='tableheader2'");


            label_row("Project:", $_POST['project_description'], "class='tableheader2'");   

            label_row("Date", $_POST['date'], "class='tableheader2'");              

        table_section(2, "30%");
            label_row("Comment:", $_POST['header_comment'], "class='tableheader2'");

            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $mpc_no."&source_type_id=".ST_MONTHLY_PROJECT_COSTS, "Approval Status");
            label_row($s.':', $approval_statuses[$_POST['approval_status']], "class='tableheader2'");      
            
            
        table_section(3, "30%");

            label_row("Total Of:", $monthly_project_costs_base_totals[$_POST['base_total_type']], "class='tableheader2'");
            label_row("Distribute On:", $monthly_project_costs_distribution_types[$_POST['distribution_type']], "class='tableheader2'");
            label_row("Scheduled Total:", $_POST['scheduled_total'], "class='tableheader2'");
            
        
    end_table(0);

    end_outer_table(1); // outer table
    hidden('MPC_No', $mpc_no);
 
}

function display_mpc_departments($mpc_no)
{      
    global $Ajax; 
    global $monthly_project_costs_base_totals, $monthly_project_costs_distribution_types;
       
    $result = view_monthly_project_costs_departments($mpc_no);    

         
    start_table(TABLESTYLE, "width=80%");
    $th = array(_("Department"), "Total Of", _("Base Value"), "Distributed On",  _('Distributed Total'), "Department Total Cost", "");              
  
    div_start('sc_purchase_orders');
        $Ajax->activate('sc_purchase_orders');
    div_end();
    
    table_header($th);
    $k = 0; //row colour counter
      
    $total_calculated_value = $total_applied_value = $total_cost = 0;
    while ($myrow = db_fetch($result))
    {

            alt_table_row_color($k);

            label_cell($myrow['department_name']);
            
            if(isset($myrow['id']) && $myrow['id'] > 0){
                label_cell($monthly_project_costs_base_totals[$myrow['base_total_type']]);
                label_cell(number_format($myrow['calculated_value'], 2), "align=right");
                label_cell($monthly_project_costs_distribution_types[$myrow['distribution_type']]);
                label_cell(number_format($myrow['applied_value'], 2), "align=right");
                label_cell(number_format($myrow['total'], 2), "align=right");
                
            }
            else{
                label_cell('');
                label_cell('');
                label_cell('');
                label_cell('');
                label_cell('');
            }    
            $edit_link = get_journal_trans_view_str(ST_MONTHLY_PROJECT_COSTS_DETAIL, $mpc_no."|".$myrow["lv_department_id"],"Details");
            label_cell($edit_link, "align=center");   

            $total_calculated_value += $myrow['calculated_value'];
            $total_applied_value += $myrow['applied_value'];
            $total_cost += $myrow['total'];
            end_row();         
        }
    
        
      

    
    start_row();
    label_cell("Totals", "colspan=2 align=right");
    label_cell("<b>".number_format($total_calculated_value, 2)."</b>", "align=right");
    label_cell("<b>".number_format($total_applied_value, 2)."</b>", "colspan=2 align=right");
    label_cell("<b>".number_format($total_cost, 2)."</b>", "align=right");
    label_cell("", "colspan=2");
    
    end_row();
    
    end_table();
  
}

br(2);

//------------------------------------------------------------------------------------------------------------
  

display_monthly_project_costs_header($_POST['trans_no']); 
    
display_mpc_departments($_POST['trans_no']); 

//display_monthly_pc_details($selected_id, $mpc_no);
  

 
if (!@$_GET['popup'])
{      
    end_form();       
    end_page(@$_GET['popup'], false, false);    
   
}



?>




































































































