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

$page_security = 'SA_MONTHLY_PROJECT_COSTS';
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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/approval_functions.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_db.inc");
include_once($path_to_root . "/gl/includes/ui/monthly_project_costs_ui.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
//include_once($path_to_root . "/includes/references.inc");


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

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

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

//------------------------------------------------------------------------------------------------------
simple_page_mode(true);

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


if (isset($_GET['NewOrder']))
{
    $_POST['MPC_No'] = -1;
    set_session_variable("ST_MONTHLY_PROJECT_COST_no", $_POST['MPC_No']);
} 

if (isset($_POST['MPC_No']) && $_POST['MPC_No'] > 0){
    set_session_variable("ST_MONTHLY_PROJECT_COST_no", $_POST['MPC_No']);
}

$mpc_no = get_session_variable('ST_MONTHLY_PROJECT_COST_no');
if(!($mpc_no>0)){
    $mpc_no = -1;
    $_POST['MPC_No'] = $mpc_no;
    set_session_variable("ST_MONTHLY_PROJECT_COST_no", $mpc_no);
}



    



if (isset($_GET['Processed_MPC_No'])) 
{
	//return;
        
    $trans_no = $_GET['Processed_MPC_No'];
    $trans_type = ST_MISCELLANEOUS_PROJECT_COST;
    $message = get_session_variable('ST_MONTHLY_PROJECT_COST_messages');
    clear_session_variable('ST_MONTHLY_PROJECT_COST_messages');
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this Miscellaneous Project Cost"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/projects/monthly_project_costs.php", _("Create another &Miscellaneous Project Cost"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}



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


if(isset($_POST['Update_Release_Monthly_PC']))
{
    $rmpca_msg = release_monthly_project_costs_for_approval($mpc_no);
    if($rmpca_msg['success'] !== true){
        display_error($rmpca_msg["message"]);
    }
}


if (isset($_POST['Back_To_Originator_Monthly_PC'])){

    $sbompc_msg = send_monthly_project_costs_back_to_originator($mpc_no); 
    if($sbompc_msg['success'] !== true){
        display_error($sbompc_msg["message"]);
    }
} 

if (isset($_POST['Reject_Monthly_PC'])){

    $rmpc_msg = reject_monthly_project_costs($mpc_no);       
    if($rmpc_msg['success'] !== true){
        display_error($rmpc_msg["message"]);
    }
}


if(isset($_POST['Save_Monthly_PC_header'])){
        
    if($mpc_no > 0){
        $mpch = get_monthly_project_costs_header_record($mpc_no);
        $row_state = $mpch['row_state'];
    }
    else{
        $row_state = 0;
    }
    $h_msg = can_process_monthly_project_costs_header($mpc_no);   
    
    if($h_msg["success"] !== true){          
        if(isset($h_msg["focus_to"])){
            set_focus($h_msg["focus_to"]);
        }
        display_error($h_msg['message']);
    }
    else{   
        begin_transaction();
        $msg = save_monthly_project_costs_header($mpc_no, $row_state);    
        if($msg['success'] !== true){
            cancel_transaction();
            display_error($msg['message']);
        }
        else{
            commit_transaction();
            display_notification($msg['message']);
        }

    }

}


if(isset($_POST['AddItem']) || isset($_POST['UpdateItem'])){
        
    $msg = save_monthly_project_costs($mpc_no, $selected_id);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['MPC_No'] = $msg['trans_no'];
        display_notification($msg['message']);
       // display_notification("Taxes have been recalculated and updated.");
        $Mode = 'RESET';
    }

}


if (isset($_POST['CancelItemChanges'])){

    $selected_id = -1;
}


if (isset($_POST['cancel'])){
    $Mode = 'RESET';
    $selected_id = -1;
    unset($_POST);
}  



if ($Mode == 'Delete')
{
    $dmpcd_msg = delete_monthly_project_costs_department_detais($mpc_no, $selected_id);
    if($dmpcd_msg["success"] !== true){          
        display_error($dmpcd_msg["message"]);
    }
    else{
        display_notification($dmpcd_msg["message"]);
        $Mode = 'RESET';
    }
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('billing_items');
//--------------------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
start_form();


display_monthly_project_costs_header($mpc_no);   

if($mpc_no > 0){
    submit_center_first('Save_Monthly_PC_header', _("Update"), '', 'default');
   
    br(2);
    
    display_monthly_pc_departments($mpc_no);
    
    br(2);
    submit_center_first('refresh', _("Refresh"), '', 'default');
    br(2);
    submit_center_first('Update_Release_Monthly_PC', "Release for Approval", '', 'default');
    submit('Back_To_Originator_Monthly_PC', 'Back To Originator', true, '', 'default');
    submit_center_last('Reject_Monthly_PC', "Reject", '', 'default');	
    
    

           
    
}
else{
    submit_center_first('Save_Monthly_PC_header', _("Add"), '', 'default');
}


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

br(2);

//------------------------------------------------------------------------------------------------------------
               
//display_monthly_pc_details($selected_id, $mpc_no);
  

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



?>
