<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_REBUILD_PROJECT_BUDGETS_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$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/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 . "/projects/includes/db/rebuild_project_budget_costs_inquiry_db.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_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();
}

page($help_context = "Rebuild Project Budget Costs Inquiry", false, false, "", $js);
//---------------------------------------------------------------------------------------------
simple_page_mode(true);

//if (isset($_GET['order_no']))
//{
//    $_POST['order_no'] = $_GET['order_no'];
//}

function id($row)
{
    return $row['id'];
}

function boq_group_description($row)
{
    return $row['boq_group_description'];
}

function task_description($row)
{
    return $row['task_description'];
}

function tran_type($row)
{
//    return $row['tran_type'];
    global $systypes_array;
    
    $s = $systypes_array[$row["tran_type"]];
    return $s;
}

function tran_no($row)
{
//    return $row['tran_no'];
    return get_trans_view_str($row['tran_type'], $row['tran_no']);
}

function amount($row)
{
    return number_format($row['amount'], 2);
}

function total_cost($row)
{
    return number_format($row['total_cost'], 2);
}

function added_on($row)
{
    return $row['added_on'];
}


function edit_link($row) 
{
    if (@$_GET['popup'])
            return '';
    global $trans_type;
    $modify = "IOUNo";
   
}


    

//function update_project_boq_group_task_costs($project_id, $boq_group_id)
//{
//    $boq_group_task_groups_result = get_boq_group_task_groups($project_id, $boq_group_id);
//    while($boq_group_task_group = db_fetch($boq_group_task_groups_result))
//    { 
//        $task_group_tasks_result = get_task_group_tasks($project_id, $boq_group_task_group['id']);
//        while($task = db_fetch($task_group_tasks_result))
//        {                         
//            if($task['task_id'] > 0){
//                $msg = update_new_costs($task['task_id']);   
//                if($msg['success'] !== true){
//                    return $msg;
//                }
//            }
//        }
//    }
//    return array("success" => true, "message" => "");
//
//}
    
//function update_new_costs($task_id)   
//{
//    $task = get_task_record($task_id);
//    $task_row_state = $task['row_state'];
//    $r = get_last_task_cost_recalculation_record($task_id);
//    $d = substr($r['end_time'],0,4);
//    if($d == '0000'){
//        $m = "Budget ".$task['task_id']." - ".$task['task_code']." - ".$task['description']." recalculation has not been completed successfully. Not possible to proceed with this cost update.";
//        return array("success" => false, "message" => $m);
//    }
//    
//    if(!($r['task_row_state'] == $task_row_state)){
//        $m = "Budget ".$task['task_id']." - ".$task['task_code']." - ".$task['description']." has been updated after cost recalculation. Costs recalculated are now outdated. Not possible to proceed with this cost update.";
//        return array("success" => false, "message" => $m);        
//    }
//    
//    begin_transaction();
//    
//    delete_task_all_cost_details($task_id);
//    delete_task_all_allocation_details($task_id);
//    
//    update_task_costs_from_rebuilt_values($task_id); 
//    update_task_allocations_from_rebuilt_values($task_id); 
//    
//    $total_cost = get_rebuilt_task_total($task_id, 0);
//    $total_allocation = get_rebuilt_task_total($task_id, 1);
//    
//    $n = update_task_cost_totals($task_id, $total_allocation, $total_cost, $task_row_state);
//    if($n !== 1){
//        cancel_transaction();
//        $m = "Budget ".$task['task_id']." - ".$task['task_code']." - ".$task['description']." has been updated after cost recalculation. Could not update the budget with recalculated values.";
//        return array("success" => false, "message" => $m);
//    }
//    
//    commit_transaction();
//    return array("success" => true, "message" => "");
//
//}
//if(isset($_POST['UpdateNewCosts'])){
//    $task_id = $_POST['task_id'];
//    
//    if(!($_POST['task_id'] > 0)){
//        
//        $filtering_group_id = 0;
//        if($_POST['filtering_group_id'] > 0){
//            $filtering_group_id = $_POST['filtering_group_id'];
//        }
//        update_project_boq_group_task_costs($_POST['project_id'], $filtering_group_id);
//    }
//    else{
//        update_new_costs($task_id); 
//    }
//}
//
//if(isset($_POST['UpdateForEntireProject']))
//{
//   $project_id = $_POST['project_id'];
//   update_entire_project_task_costs($project_id);
//   
//}
//
//
//if(isset($_POST['UpdateForTask'])){
//    $task_id = $_POST['task_id'];
//    if($_POST['task_id'] > 0){
//        $msg = update_new_costs($task_id);     
//        if($msg['success'] !== true){
//            display_error($msg['message']);
//        }
//        else{
//            display_notification("Costs updated for selected budget.");
//        }
//    }
//    else{
//        display_error("Please select a budget");
//    }
//}
//
//
//if(isset($_POST['UpdateForBOQItemGroup'])){
//
//    $filtering_group_id = 0;
//    if($_POST['filtering_group_id'] > 0){
//        $filtering_group_id = $_POST['filtering_group_id'];
//    }
//            
//    $msg = update_project_boq_group_task_costs($_POST['project_id'], $filtering_group_id);
//    if($msg['success'] !== true){
//        display_error($msg['message']);
//    }
//    else{
//        display_notification("Costs updated for selected BOQ item group.");
//    }
//}
//    
//if(isset($_POST['UpdateForEntireProject']))
//{
//    $project_id = $_POST['project_id'];
//
//    if($project_id > 0){
//        $msg = update_entire_project_task_costs($project_id);
//        if($msg['success'] !== true){
//            display_error($msg['message']);
//        }
//        else{
//            display_notification("Costs updated for selected BOQ item group.");
//        }
//        display_notification("Costs updated for the selected project.");
//    }
//    else{
//        display_error("Please a project");
//    }
//   
//}
//
//if(isset($_POST['UpdateNewCosts']))
//{
//  $task_id = $_POST['task_id'];
//
//    update_new_costs($task_id); 
//  
//}


if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();
start_table();
start_row();
    project_list_cells(_("Project:"), 'project_id', null, _('New project'), true, check_value('show_inactive'));
end_row();
$project_id = get_post('project_id');

start_row();
    project_boq_item_groups_list_cells("BOQ Item Group:", 'filtering_group_id', $project_id, null, '', true);
end_row();

if(get_post('filtering_group_id') > 0){
    $filetering_group_id = get_post('filtering_group_id');
}
else{
    $filetering_group_id = 0;
}

start_row();
    group_tasks_list_cells("Budget :", 'task_id', $_POST['project_id'], $_POST['filtering_group_id'], null, _('Select a budget'), true);  
end_row();

//start_row();
//yesno_list_cells("Type :", 'type', null, "Allocation", "Costs", true);
//end_row();


end_table(1);

//submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
submit_center('SearchOrders', _("Search"), true, 'Search',  'default');

br(2);

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

function display_rebuilt_project_task_allocation_details($task_id)
{
    global $systypes_array;
    
    display_heading2("Allocations");
    
    $th = array(("Id"),_("Tran Type"), _("Tran No"),
            _("Date"),_("Amount"));
    
    start_table(TABLESTYLE2, "width=60%");

    table_header($th);
    
    start_row();

    $total = 0;
    $result = get_rebuilt_project_task_cost_details($task_id, true);
    while ($myrow = db_fetch($result)) {
        start_row();
        label_cell($myrow['id'], "align=center");
        label_cell($systypes_array[$myrow['tran_type']]);
        label_cell(get_trans_view_str($myrow['tran_type'], $myrow['tran_no']), "align=center");
        label_cell($myrow['added_on'], "align=center");
        amount_cell($myrow['amount']);
//        amount_cell($myrow['total_cost']);
        end_row();
        
        $total += $myrow['amount']; 

        
    }
    
    label_cell("<b>Total Allocations</b>", "colspan=4 align=right");
    amount_cell($total, true);
    
    end_table();
}


function display_rebuilt_project_task_cost_details($task_id)
{
    global $systypes_array;
    
    display_heading2("Costs");
    
    $th = array(("Id"),_("Tran Type"), _("Tran No"),
            _("Date"),_("Amount"), "Total Cost");
    
    start_table(TABLESTYLE2, "width=90%");

    table_header($th);
    
    start_row();
    $total = 0;
    $result = get_rebuilt_project_task_cost_details($task_id, false);
    while ($myrow = db_fetch($result)) {
        start_row();
        label_cell($myrow['id'], "align=center");
        label_cell($systypes_array[$myrow['tran_type']]);
        label_cell(get_trans_view_str($myrow['tran_type'], $myrow['tran_no']), "align=center");
        label_cell($myrow['added_on'], "align=center");
        amount_cell($myrow['amount']);
        amount_cell($myrow['total_cost']);
        end_row();

        $total += $myrow['total_cost']; 
    }
    
//    label_cell("<b>Total Cost</b>", "colspan=5 align=right");
//    amount_cell($total, true);
    
    end_table();
}


$project_id = 0;
if(isset($_POST['project_id'])){
  $project_id = $_POST['project_id'];
}

$filtering_group_id = 0;
if(isset($_POST['filtering_group_id'])){
  $filtering_group_id = $_POST['filtering_group_id'];
}

$task_id = 0;
if(isset($_POST['task_id'])){
  $task_id = $_POST['task_id'];
}

$task_id = $_POST['task_id'];

display_rebuilt_project_task_allocation_details($task_id);

br(2);

display_rebuilt_project_task_cost_details($task_id);

//if(isset($_POST['SearchOrders'])){
  
 
   

//------------------------------------------------------------------------------------------------      
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------



br(2);


//start_row();
    



//
//if(isset($_POST['task_id']) && $_POST['task_id'] > 0){
//    submit_center('UpdateForTask', _("Update for Budget"), true, '',  'default');
//}    
//
//
//br(1);
//
//$filtering_group_id = 0;
//if(isset($_POST['filtering_group_id'])){
//  $filtering_group_id = $_POST['filtering_group_id'];
//}
//
//if(isset($_POST['project_id']) && $_POST['project_id'] > 0){
//    submit_center('UpdateForBOQItemGroup', _("Update For BOQ Item Group"), true, '',  'default');
//}
//
//br(1);
//
//if(isset($_POST['project_id']) && $_POST['project_id'] > 0){
//  submit_center('UpdateForEntireProject', _("Update For Entire Project"), true, '',  'default');
//}


br(2);


//end_row();

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



?>
