<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_I_OWE_YOU_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "..";
include_once($path_to_root . "/includes/db_pager.inc");
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/petty_cash_adjustments_inquiry_db.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.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 = "I Owe You Inquiry"), false, false, "", $js);

//----------------------------------------------------------------------------------------
simple_page_mode(true);



if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}

function edit_link($row) 
{
    if($row['approval_status'] != AS_NEW){
        return null;    
    }
    
    switch ($row['type']) {
        case ST_FLOAT_PAYMENT:
            return pager_link( _("Edit"),"/petty_cash/float_payment.php?type=".$row['type']."&trans_no=".$row['trans_no'], ICON_EDIT);       
            
        case ST_FLOAT_RECEIPT:
            return pager_link( _("Edit"),"/petty_cash/float_receipt.php?type=".$row['type']."&trans_no=".$row['trans_no'], ICON_EDIT);         
            
        case ST_PETTY_CASH_FLOAT_ADJ:
            return pager_link( _("Edit"),"/petty_cash/petty_cash_float_adjustments.php?type=".$row['type']."&trans_no=".$row['trans_no'], ICON_EDIT);           

        default:
            break;
    }
     
}

function _type_id($row)
{
    global $systypes_array;
    
    return $systypes_array[$row['type']];

}

function _trans_no($row)
{
    if($row['trans_no'] > 0){
        return get_petty_cash_trans_view_str($row['type'], $row['trans_no']);
    }else{
        return "";    
    }
}

function _reference($row)
{
    return $row['reference'];
    
}

function _float_name($row)
{
    return $row['float_name'];
    
}

function _is_return($row)
{
    return get_yes_no_name($row['is_return']);
    
}


function _tran_date($row)
{
   return sql2date($row['tran_date']);
}



function _amount($row)
{
   return number_format($row['amount'],2);
}

function _comment($row)
{
   return $row['comment'];
}

function _approval_status($row)
{
    global $approval_statuses;
    
   return $approval_statuses[$row['approval_status']];
}


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

if ($Mode == 'Delete')
{
        $recs = delete_i_owe_u($selected_id, $_POST['employee_code']);
        
        if($recs == 1){
                display_notification(_("Salary advance has been deleted."));
        }
        else{
                display_error(_("Salary advance could not be deleted."));
        }

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

global $systypes_array;

start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(text_bold("Transaction No:"), 'trans_no', '',null, '', true);
    end_row();
end_table();

$values_array = array();
$values_array[ST_FLOAT_RECEIPT] = $systypes_array[ST_FLOAT_RECEIPT];
$values_array[ST_FLOAT_PAYMENT] = $systypes_array[ST_FLOAT_PAYMENT];

start_table(TABLESTYLE_NOBORDER);
    start_row();
        date_cells(_("From:"), 'from_date', '', null, -14);
        date_cells(_("To:"), 'to_date', '', null, +7);
        custom_values_list_cells("Type:", 'type_id', $values_array, null, '');
        petty_cash_floats_list_cells(_("Float:"), 'float_id', null, _('All floats'));
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
    end_row();
end_table();

    end_row();

end_table(1);

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

$from_date = "";
if(isset($_POST['from_date'])){
    $from_date = $_POST['from_date'];
}
$to_date = "";
if(isset($_POST['to_date'])){
    $to_date = $_POST['to_date'];
}

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

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

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


    $sql = get_sql_for_petty_cash_adjustment_inquiry($from_date, $to_date, $type_id, $trans_no, $float_id);   
    
    $cols = array(
                _("Type")=> array('fun'=>'_type_id','align'=>'center'),
                _("#")=> array('fun'=>'_trans_no','align'=>'center'),
                _("Reference")=> array('fun'=>'_reference','align'=>'left'), 
                _("Date") => array('fun' => '_tran_date', 'align'=>'center'), 
                _("Float")=> array('fun'=>'_float_name','align'=>'left'), 
                _("Is Return")=> array('fun'=>'_is_return','align'=>'left'),  
                _("Amount") => array('fun' => '_amount','align'=>'right'), 
                _("Comment")=>array('fun'=>'_comment','align'=>'center'),
                _("Status")=>array('fun'=>'_approval_status','align'=>'center') 
    );
        array_append($cols, array( 
            array('insert'=>true, 'fun'=>'edit_link')));
//-----------------------------------------------------------------------------------------
    
   
    
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------

$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = "70%";

display_db_pager($table);

br();

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

?>
