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


$page_security = 'SA_PROJECT_COST_ADJUST';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$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 = "Project Cost Adjustments"), @$_REQUEST['popup'], false, "", $js);

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 . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/projects/includes/db/project_cost_adjustments_db.inc");

function clear_data()
{
	//return;
        unset($_POST['no_of_points']);
        
}

//------------------------------------------------------------------------------------
if (isset($_GET['AddedID'])) 
{
	//return;
        
    $trans_no = $_GET['AddedID'];
	$trans_type = ST_PROJECT_COST_ADJUST;

	display_notification("Project Cost Adjustment ".$trans_no." has been added.");
	
//    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Material Requisition")));
//        
//    br();
//
//	display_note(print_document_link($trans_no, _("&Print this Material Requisition"), true, $trans_type), 0, 0);
//
//	hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &another Material Requisition"));
//
//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
        

	display_footer_exit();
}

function can_process()
{ 
	if ($_POST['project_id'] == -1)
    {
        display_error( _("Select a project."));
        set_focus('project_id');
        return false;
    }

    if ($_POST['task_id'] == -1)
    {
        display_error( _("Select a task."));
        set_focus('task_id');
        return false;
    }

    if (!check_num('amount', 0))
    {
        display_error( _("The amount entered must be numeric and can not be negative."));
        set_focus('amount');
        return false;
    }

    return true;
}



function display_edit_controls() 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;

    start_outer_table(TABLESTYLE2);
    table_section_title(_("Enter Cost Adjustment"));

    project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);
    tasks_list_row(_("Task:"), 'task_id', $_POST['project_id'], null, _('Select a task'), true);
    yesno_list_row(_("Deduction:"), 'deduction');
    amount_row(_('Amount:'), 'amount');
    date_row('Date :', 'tran_date');

    $exclude = array (ST_JOURNAL);
    systypes_list_row("Reference Transaction Type:", "ref_tran_type", null, false, $exclude, '');
    small_qty_cells("Reference Transaction No:", "ref_tran_no", null, null, null, 0);
    
    textarea_row('Comment', 'comment', '', 25, 2);
        
	end_outer_table();
        
        br();

	div_start('controls');

        submit_center_first('addupdate', _("Process"), '', 
                @$_REQUEST['popup'] ? true : 'default');

	div_end();
}

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

if (isset($_POST['addupdate'])){
    if(can_process()){
        $apca = add_project_cost_adjustment($_POST['project_id'], $_POST['task_id'], 
                                    input_num('amount'), $_POST['deduction'], 
                                    $_POST['ref_tran_type'], input_num('ref_tran_no'),
                                    date2sql($_POST['tran_date']), $_POST['comment']);
        
        if($apca['success'] !== true){
             display_error($apca['message']);
             return;
        }
        else{
            meta_forward($_SERVER['PHP_SELF'], "AddedID=".$apca['tran_no']);
        }
            
    }
    
}

$Ajax->activate('_page_body');
    
start_form(true);

div_start('details');

display_edit_controls();

//tabbed_content_end();

div_end();

hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);
?>
