<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_FLOAT_ADJUSTMENTS';
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/petty_cash_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 . "/reporting/includes/reporting.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 Float Entry"));
//----------------------------------------------------------------------------------------
simple_page_mode(true);

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

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

	hyperlink_no_params("$path_to_root/petty_cash/petty_cash_float_adjustments.php", _("Create another &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($selected_id)
{      
    if (strlen($_POST['reference']) == 0)
    {
        $message = "Reference is mandatory.";
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    if (!is_new_reference($_POST['reference'], ST_PETTY_CASH_FLOAT_ADJ)) 
    {
        $message = ( _("The entered reference is already in use."));
        $set_focus = ('ref');
        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($_POST['float_id'] == '-1'){            
        $message = "Please select a Petty cash float.";
        $set_focus = 'float_id';
        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);
    }        
    
    if (!is_date_in_fiscalyear($_POST['tran_date'])) {
        $message = (_("The entered invoice date is not in fiscal year."));
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    return array("success" => true, "message" => "");        
}

//function display_petty_cash_float_adjustments()
//{
//    echo "<br>";
//    div_start('payments_table');
//    display_heading('Petty Cash Float Adjustments');
//    start_table(TABLESTYLE2, "width=50%");  
// 
//   //$float_adjustment = get_petty_cash_float_adjustments($float_id);
//
//    //$th = array(("ID"), _("Reference"), _("Type"),_("Date"),_("Amount"),_("Comment"),  "", "");
//   // table_header($th);
//    
////    $j = 1;
////    $k=0; 
////    $payment_total = 0;
////    while ($row = db_fetch($float_adjustment))
////    {
////            alt_table_row_color($k);
////            
////            label_cell($row["id"]);
////            label_cell($row["reference"]);
////            label_cell($row["is_return"]);
////            label_cell(sql2date($row["tran_date"]),"align=center"); 
////            amount_cell($row["amount"]); 
////            $payment_total += $payment_row["amount"];
////            label_cell($row["comment"]);
////            
////           label_cell($payment_row["paid"] == 1 ? "Paid": "ssss");
//
//            end_row();
//            $j++;
//  
//    }
//   // end_table(1);
//    br();
//    div_end();

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

    $amount = input_num('amount');
    if($_POST['is_return'] > 0)
    {        
      $amount *= -1;
    }    
       
    begin_transaction();
    $rec_id = add_petty_cash_float_adjustment($_POST['reference'], $_POST['is_return'],$_POST['float_id'], $_POST['tran_date'], $amount, $_POST['comment'] );
    if($rec_id > 0){
        add_to_petty_cash_float(ST_PETTY_CASH_FLOAT_ADJ, $rec_id,$_POST['float_id'], $amount, $_POST['tran_date']);
        $Refs->save(ST_PETTY_CASH_FLOAT_ADJ, $rec_id, $_POST['reference']);
        commit_transaction();
        meta_forward($_SERVER['PHP_SELF'], "Added_ID=$rec_id");
        return true;
    }
    else{
        cancel_transaction();
        display_error("Petty Cash Float Adjustment could not be added.");
        return false;
    }
}
//--------------------------------------------------------------------------------------------------

if ($Mode == 'ADD_ITEM') 
{
    if(add_float_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_PETTY_CASH_FLOAT_ADJ));
    yesno_list_row("Type", 'is_return', $_POST['is_return'], $name_yes='Deduct from float', $name_no='Add to float');
    petty_cash_floats_list_row('Petty Cash Float:', 'float_id', null, 'Select Float', false);
    date_row("Date:", "tran_date");    
    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($selected_id == -1, '', '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);
}


?>
