<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_FLOAT_USERS';
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/petty_cash_floats_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 = "Petty Cash Users"));
//------------------------------------------------------------------------------------------
simple_page_mode(true);

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

function can_process($rec_id)
{	

    if(!($_POST['float_id'] > 0))
    {
        $message = "Please select a Float.";
        $set_focus = 'float_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if(!($_POST['user_id'] > 0))
    {
        $message = "Please select a User.";
        $set_focus = 'user_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if(!($rec_id > 0))
    {
        $fu = get_petty_cash_float_user($_POST['float_id'], $_POST['user_id']);
        if(isset($fu['user_id'])){
            $message = "Selected user already attached to this float.";
            $set_focus = 'user_id';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    }
    
    return array("success" => true, "message" => "");    
}










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

if(isset($_POST['Add'])){
    
    $cd = can_process(-1);
    
    if($cd["success"] !== true) 
    {   
        display_error($cd["message"]);
        return;
    }
    
    add_petty_cash_user($_POST['float_id'],$_POST['user_id'],$_POST['blocked']);
        
    $Mode = 'RESET';
    display_notification("New petty cash user has been added");
    

}    
 
if(isset($_POST['Update'])){
    
    $cd = can_process($selected_id);    
    if($cd["success"] !== true) 
    {   
        display_error($cd["message"]);
        return;
    }
    
    update_petty_cash_user($selected_id,$_POST['float_id'],$_POST['user_id'],$_POST['blocked']);
    $Mode = 'RESET';
    display_notification("Petty cash user has been updated");
 }


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

//if (isset($_POST['Close_VR'])){
//    close_vr($selected_id);
//} 

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

if ($Mode == 'Delete')
{
        $recs = delete_petty_cash_user($selected_id);
        
        if($recs == 1){
                display_notification(_("Petty Cash Float has been deleted."));
        }
        else{
                display_error(_("Vehicle Request could not be deleted."));
        }
	$Mode = 'RESET';
}

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

//if (isset($_POST['Update']) )
//{
//	$selected_id = get_post('selected_id');
//}

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


    


start_form();

petty_cash_floats_list_row("Petty Cash Float:", 'float_id', null,'Select Float',true);

br(2);

start_table(TABLESTYLE, "width=50%");
$th = array(_('User'),_('Blocked'),_('Added On'), "", "");

table_header($th);
$k = 0; //row colour counter

$result = get_petty_cash_float_users($_POST['float_id']);   

while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);
   
//    label_cell($myrow["name"], "align=left");
    label_cell($myrow["real_name"], "align=left");
    $blocked = '';
    if($myrow["blocked"] == 0)
    {
        $blocked = "No";
    }
    else
    {
        $blocked = "Yes";
    }
    label_cell($blocked, "align=center");
    label_cell(sql2date($myrow["created_on"]), "align=center");
    edit_button_cell("Edit".$myrow["id"], _("Edit"));    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}
//inactive_control_row($th);
end_table(1);
//----------------------------------------------------------------------------------------

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

    

if ($Mode =='Edit')   
{
    $myrow = get_petty_cash_user($selected_id);
    $_POST['id'] = $myrow["id"];
    $_POST['float_id'] = $myrow["float_id"];
    $_POST['user_id'] = $myrow["user_id"];
    $_POST['blocked'] = $myrow["blocked"];
    
    hidden('selected_id', $selected_id);
}

br();
//hidden('selected_id', $selected_id);

start_table(TABLESTYLE2);


user_list_row("User:", 'user_id', null);
yesno_list_row("Blocked", 'blocked', null, 'Yes', 'No', false);

end_table(1);


if($selected_id > 0){
    submit_center_first('Update', _("Update"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');

}
else{
        
    submit_center_first('Add', _("Create Petty Cash User"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel"), 'cancel');
}


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

?>
