<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_FLOAT_ENTRIES';
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 . "/petty_cash/includes/db/i_owe_you_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/approve_i_owe_you_enties_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/project_float_adjustments_db.inc");
include_once($path_to_root . "/petty_cash/includes/ui/petty_cash_ui.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/petty_cash/includes/db/project_floats_db.inc");

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

include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_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 = "Project Float Entry"));
//----------------------------------------------------------------------------------------
simple_page_mode(true);

if (isset($_GET['Added_ID'])) 
{
	//return;
        
    $trans_no = $_GET['Added_ID'];
	$trans_type = ST_PROJECT_FLOAT_ADJ;

    
	display_notification_centered("Project Float Adjustment $trans_no has been created.");
        
	display_note(print_document_link($trans_no, _("&Print this Project Float Adjustment "), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/petty_cash/project_float_adjustments.php", _("Create another &Project Float Adjustment"));

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

	display_footer_exit();
}


function check_data()
{      
    if (strlen($_POST['reference']) == 0)
    {
        $message = "Reference is mandatory.";
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    $rm = reference_exists($_POST['reference'], ST_PROJECT_FLOAT_ADJ);
    if($rm) 
    {
        $message = $rm;
        $set_focus = ('reference');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
        
    if(!($_POST['project_id'] > 0)){            
        $message = "Please select a project.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }  
    
    
    $project_float = get_project_float_by_project_id($_POST['project_id']);
    if(!($project_float && $project_float['inactive'] == 0)){
        $message = ("No active Project Float found for the selected project.");
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    
    if($_POST['employee_code'] == 0 || $_POST['employee_code'] == -1 || $_POST['employee_code'] == '')
    {
        $message = "Please select an employee.";
        $set_focus = 'employee_code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    
    if(!check_num('amount', 0.001)){            
        $message = "Amount entered should be greater than zero.";
        $set_focus = 'amount';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }  
    
    
   
    if (!is_date($_POST['tran_date']))
    {       
        $message = "Entered date is not a valid date.";
        $set_focus = 'tran_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }        
    
    
    return array("success" => true, "message" => "");        
}



function add_pf_adjustment($selected_id)
{   
    global $Refs;
    
    $cd = check_data($selected_id);
    if($cd["success"] !== true) 
    {
        display_error($cd["message"]);
        set_focus($cd["focus_to"]);
        return false;
    }

    $amount = input_num('amount');
    if($_POST['is_return'] > 0)
    {        
      $amount *= -1;
    }    
       
    begin_transaction();
    $rec_id = add_project_float_adjustment($_POST['reference'], $_POST['project_id'],$_POST['group_company_code'],       
                                            $_POST['employee_code'],$_POST['tran_date'], $_POST['is_return'], 
                                            $amount, $_POST['comment'] );

    if($rec_id > 0){
        add_to_project_float(ST_PROJECT_FLOAT_ADJ, $rec_id, $_POST['project_id'], 
                                $_POST['group_company_code'], $_POST['employee_code'], $amount, 
                                date2sql($_POST['tran_date']));
        
        $Refs->save(ST_PROJECT_FLOAT_ADJ, $rec_id, $_POST['reference']);
        commit_transaction();
        meta_forward($_SERVER['PHP_SELF'], "Added_ID=$rec_id");
        return true;
    }
    else{
        cancel_transaction();
        display_error("Project Float Adjustment could not be added.");
        return false;
    }
}
//--------------------------------------------------------------------------------------------------

if ($Mode == 'ADD_ITEM') 
{
    if(add_pf_adjustment($selected_id)){
        $Mode = 'RESET';
    }
}
//--------------------------------------------------------------------------------------------------

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();

//$i_owe_you = display_i_owe_you_header($iou_id);
br();

//display_petty_cash_float_adjustments();
//-----------------------------------------------------------------------------------------------

    $_POST['id'] = 0;
    $_POST['reference'] = "";
    $_POST['float_id'] = "";
    $_POST['is_return'] = ""; 
    $_POST['tran_date'] ="";
    $_POST['amount'] = "";
    $_POST['comment'] ="";  
        

    hidden('selected_id', $_POST['id']);   

    start_table(TABLESTYLE2);
    
    ref_row(_("Reference:"), 'reference', '', $Refs->get_next(ST_PROJECT_FLOAT_ADJ));
    project_list_row("Project :", 'project_id', null, "Select a project", true);
//    group_companies_list_row("Company:", 'group_company_code', null, false, true);    
    
    $group_company_name = $group_company_employee_name = '';
    
    $project_float = get_project_float_by_project_id($_POST['project_id']);
    if(isset($project_float['id']) && $project_float['inactive'] == 0){
        $group_company_name = get_group_company_name($project_float['company_code']);
        $group_company_employee_name = get_group_company_employee_formatted_long_name($project_float['company_code'], $project_float['employee_code']);
        
   
        hidden('group_company_code', $project_float['company_code']);
        hidden('employee_code', $project_float['employee_code']);
    }
    else{
        hidden('group_company_code', "");
        hidden('employee_code', "");
    }
    
    label_row("Company:", $group_company_name);
    label_row("Employee:", $group_company_employee_name);
    
    if(list_updated('group_company_code')){
        $_POST['employee_code'] = 0;
    }
//    group_company_employees_simple_list_row(_("Select an Employee:"), 'employee_code', get_post('group_company_code'), null, "Select an employee", false);    
    
    date_row("Date:", "tran_date");    
    yesno_list_row("Type", 'is_return', $_POST['is_return'], $name_yes='Deduct from float', $name_no='Add to float');
    amount_row('Amount:', 'amount', $_POST['amount'], null, null, 2);
    textarea_row("Comment:", "comment", null, 30,3);
    end_table(1);

    br();
    submit_add_or_update_center(true, '', 'both');
//    br();
//    submit_center_first('Add_Make_Payment', _("Save and Make Payment"), '', 'default');


br();

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


?>
