<?php
$page_security = 'SA_MONTHLY_PROJECT_COSTS_VIEW';
$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 . "/gl/includes/db/monthly_project_costs_db.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/projects_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/admin/db/months_db.inc");

include_once($path_to_root . "/dashboard/includes/db/summary_projects_db.inc");
include_once($path_to_root . "/dashboard/includes/db/summary_payrolls_db.inc");

include_once($path_to_root . "/includes/approval_functions.inc");

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

//include_once($path_to_root . "/projects/includes/project_boq_item_costs.inc");



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



global $selected_id, $Ajax;

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

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

$sep_pos = strpos($trans_no, "|");
$mpc_no = substr($trans_no, 0, $sep_pos);
$department_id = substr($trans_no, $sep_pos+1);





function display_monthly_project_cost_details($mpc_array, $mpch, $department_id, $distribute=0, $select_all=0, $deselect_all=0)
{

    $mpc_no = $mpch['id'];
            
    start_table(TABLESTYLE, "width=80%");

    $th = array(_("Project"), _("Cost"), _("Budget"), _("Comment"));

    table_header($th);
    $k = 0;
    
    $result = view_monthly_project_costs_details($mpc_no, $department_id);
    $total_value = 0;
    while ($row = db_fetch($result)) {
//        $suffix = $mpc_no."_".$line['project_id'];
        
        alt_table_row_color($k);
        $project_name = get_project_formatted_name($row['project_id']);
        label_cell($project_name);
//        label_cell($row['project_description']);
        label_cell($row['value'],'align=right');
        label_cell($row['task_description']);
        label_cell($row['comment']);
        end_row();
        
        $total_value += $row['value'];
    }
    
    start_row();
    label_cell("Totals", "colspan=1 align=right");
    label_cell("<b>".number_format($total_value, 2)."</b>", "align=right");
    label_cell("", "colspan=4");
    
    end_row();

    end_table();
    
    
    
}



if(isset($_POST['Save_MPC_Details'])){
    $smpc_msg = save_monthly_project_costs_details($mpc_no, $department_id, $_POST['base_total_type'], $_POST['distribution_type'], 
                                                input_num('calculated_value'), input_num('applying_total'));
    if($smpc_msg["success"] !== true){
        display_error($smpc_msg["message"]);
        set_focus($smpc_msg["set_focus"]);
    }
    else{
        display_notification("Monthly project costs details have been saved.");
    }
    
}

$distribute = 0;
if(isset($_POST['distribute_values'])){
    $distribute = 1;
}

$select_all = 0;
if(isset($_POST['Select_All'])){
    $select_all = 1;
}

$deselect_all = 0;
if(isset($_POST['DeSelect_All'])){
    $deselect_all = 1;
}


start_form();



hidden('trans_no', $trans_no);

global $monthly_project_costs_base_totals, $monthly_project_costs_distribution_types;

$mpch = view_monthly_project_costs_detail_header($trans_no);

$month = get_month($mpch['month_id']);
$department = get_listvalue_by_id($department_id);
$mpc_department = get_monthly_project_costs_department($mpc_no, $department_id);
if(!(isset($_POST['applying_total']) && $_POST['applying_total'] > 0)){
    $_POST['applying_total'] = $mpc_department['applied_value'];
}
start_outer_table(TABLESTYLE2, "width=50%"); // outer table
//start_table(TABLESTYLE, "width=00%");
//$th = array(_("Lot Reference"), _("Available Qty"),_("Qty"));
//table_header($th);

table_section(1, "0%");
table_section_title("Details");
label_row("Month: ", $month['description'], "class='tableheader2'");
label_row("Department: ", $department['value_name'], "class='tableheader2'");

table_section(2, "0%");
table_section_title("Calculation");
label_row("Total Of: ", $monthly_project_costs_base_totals[$mpch['base_total_type']], "class='tableheader2'");

label_row("Calculated Value: ", $mpch['calculated_value'], "class='tableheader2'");


table_section(3, "0%");
table_section_title("Distribution");
label_row("Distribute On: ", $monthly_project_costs_distribution_types[$mpch['distribution_type']], "class='tableheader2'");
hidden('distribution_type', $mpch['distribution_type']);

label_row("Distribution Value: ", $_POST['applying_total'], "class='tableheader2'");

end_outer_table(1); // outer table

br();
//        global $Ajax;
        
$Ajax->activate('mpc_dep_details');
div_start('mpc_dep_details');

$mpc_array = array();

display_monthly_project_cost_details($mpc_array, $mpch, $department_id, $distribute, $select_all, $deselect_all);
div_end();
        
//inactive_control_row($th);

br();

//submit_center_first('Save_MPC_Details', "Save", '', 'default', '');

br(2);

echo "<center>";
//submit("Submit", "submit");
echo "<input type='button' name='cancel' class='inputsubmit' onclick='goBack()' value='Close'/>";
echo "</center>";

br();

end_form();

//js_get_data_in_parent("guid","parent_guid");
//js_get_data_in_parent($parent,"parent_item_qty");
//js_get_data_in_parent($parent_unit,"parent_unit");
//show_quantity("parent_item_qty");
end_page();


























































