<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_FLOATS';
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 Floats"));
//------------------------------------------------------------------------------------------
simple_page_mode(true);

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

function can_process_vr($rec_id = '-1')
{
    if ($_POST['name'] == '')
    {
        display_error(_("Please add petty cash float name."));
        set_focus('vehicle_type_id');
        return false;
    }
     
     if(input_num('amount') <= 0)
    {        
        display_error( _("The amount should be a positive."));
        set_focus('amount');
        return false;
    }
    if (!check_num('amount',0))
    {
        display_error( _("Amount entered is not numeric or negative."));
        set_focus('amount');
        return false;
        
    }

        
     return array("success" => true, "message" => "");    
}


function clear_data()
{
    unset($_POST['id']);
    unset($_POST['name']);
    unset($_POST['amount']);
    unset($_POST['account_code']);
}

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

if(isset($_POST['Add'])){
    
    $cd = can_process_vr(-1);
    
    if($cd["success"] !== true) 
    {          
        display_error($cd["message"]);
        return;
    }
    
    add_petty_cash_float($_POST['name'], input_num('amount'), $_POST['reimbursement_method'], get_post('account_code'), get_post('credit_account'));
    $Mode = 'RESET';
    display_notification("New petty cash float has been added");
    clear_data();
    

}    
 
if(isset($_POST['Update'])){
    update_petty_cash_float($selected_id, $_POST['name'], input_num('amount'), $_POST['reimbursement_method'], get_post('account_code'), get_post('credit_account'));
    $Mode = 'RESET';
    display_notification("Petty cash float has been updated");
    clear_data();
    
 }


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_float($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');
//}

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

//if (!@$_GET['popup'])	
    
$result = get_petty_cash_floats();   

start_form();
start_table(TABLESTYLE, "width=70%");
$th = array(_('Float'),_('Amount'), _("Reimbursement Method"), "Float Account","Employee Advances Account", "");

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

while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);
   
//    label_cell($myrow["id"], "align=left");
    
    label_cell($myrow["name"]);
    amount_decimal_cell($myrow["amount"]);
    
    if($myrow['reimbursement_method'] == 1){
        $reimbursement_method = "Payments";
        label_cell($reimbursement_method, "align=center"); 
    }else{
        $reimbursement_method = "Expenses";
        label_cell($reimbursement_method, "align=center");
    }

    
    $account = "";
    if(isset($myrow["account_code"]) && strlen($myrow["account_code"] > 0)){
        $account = $myrow["account_code"]." - ".$myrow["account_name"];
    }
    label_cell($account);
    
    $credit_account = "";
    if(isset($myrow["credit_account"]) && strlen($myrow["credit_account"] > 0)){
        $credit_account = $myrow["credit_account"]." - ".$myrow["credit_account_name"];
    }
    label_cell($credit_account);
    
    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_float_line($selected_id);
    
    $_POST['id'] = $myrow["id"];
    $_POST['name'] = $myrow["name"];
    $_POST['amount'] = $myrow["amount"];
    $_POST['account_code'] = $myrow["account_code"];
    $_POST['credit_account'] = $myrow["credit_account"];
    $_POST['reimbursement_method'] = $myrow["reimbursement_method"];

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

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

start_table(TABLESTYLE2);

    text_row("Float:", 'name', null, 35, 35);
    amount_row('Amount:', 'amount', null, null, null, 2); 
    gl_all_accounts_list_row("Float Account:", 'account_code', null, true, false,"Select an account");
    gl_all_accounts_list_row("Employee Advances Account:", 'credit_account', null, true, false,"Select an account");
    yesno_list_row("Reimbursement Method:", 'reimbursement_method', null, "Payments", "Expenses", 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 Float"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel"), 'cancel');
}


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


?>
