<?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/approve_i_owe_you_enties_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['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}


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

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

function show_iou_status($row)
{
    global  $approval_statuses;
    
    if($row['batch_complete'] == 1){
        return "Completed";
    }
    
    return $approval_statuses[$row['approval_status']];
}


function show_batch_status($row)
{
    global $approval_statuses;
    
    if($row['complete'] == 1){
        return "Completed";
    }
    elseif(isset($row['batch_approval_status']) && $row['batch_approval_status'] >= 0){
        return $approval_statuses[$row['batch_approval_status']];
    }
    else{
        return '';
    }
}


function show_employee_name($row)
{
    return get_group_company_employee_formatted_long_name($row['company_code'], $row['employee_code']);
}

function edit_link($row) 
{

    if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_AWAITING_APPROVAL || $row['approval_status'] == AS_WITH_THE_APPROVER || $row['approval_status'] == AS_REOPENED){
        $modify = "ModifyMRNumber";
        return pager_link( _("Edit"),"/petty_cash/i_owe_you.php?$modify=".$row['id'], ICON_EDIT);        
    }
    else{
        return null;
    }
}

function _remove_iou_link($row) {
    $bt_ap_st = $row['batch_approval_status'];
    
    if ($row['approval_status'] == AS_APPROVED && $row['batch_no'] > 0 && ! in_array($bt_ap_st, array(AS_APPROVED, AS_REJECTED, AS_COMPLETED))) {
        return pager_link( _("Edit"),"/petty_cash/remove_i_owe_you.php?iou_no=".$row['id'], ICON_DOC);        
    } else {
        return null;
    }
}

function _close_iou_link($row) {
    if ($row['approval_status'] == AS_APPROVED && $row['batch_no'] == 0) {
        return pager_link( _("Edit"),"/petty_cash/close_i_owe_you.php?iou_no=".$row['id'], ICON_DOC);        
    } elseif ($row['batch_no'] > 0) {
        if ($row['approval_status'] == AS_CLOSED) {
            return null;
        } elseif (in_array($row['batch_approval_status'], array(AS_CLOSED, AS_REJECTED))) {
            return pager_link( _("Edit"),"/petty_cash/close_i_owe_you.php?iou_no=".$row['id'], ICON_DOC);        
        } else {
            return null;
        }
    } else {
        return null;
    }
}

function expense_link($row) 
{
    if($row['batch_complete'] == 1){
        return "";
    }
    if($row['batch_approval_status'] == AS_APPROVED){
        return "";
    }
    if($row['approval_status'] == AS_APPROVED && isset($row['batch_no']) && isset($row['batch_no']) > 0){
        $modify = "IouBatchNo";
        return pager_link( _("Edit"),"/petty_cash/i_owe_you_expenses_entry2.php?$modify=".$row['batch_no'], ICON_DOC);        
    }
    else{
        return null;
    }
}

//function expenses_link($row) 
//{
//    if($row['approval_status'] == AS_APPROVED && $row['batch_complete'] == 0){
//        $modify = "ModifyMRNumber";
//        return pager_link( _("Edit"),"/petty_cash/i_owe_you_expenses_entry_select.php?$modify=". $row['id'], ICON_EDIT);  
//    }
//    else{
//        return null;
//    }
//}

function confirm_link($row) 
{
    if($row['approval_status'] == AS_APPROVED && $row['batch_complete'] == 0){
        $modify = "ModifyMRNumber";
        return pager_link( _("Confirm"),"/petty_cash/approve_i_owe_you_entries.php?$modify=". $row['id'], ICON_EDIT);  
    }
    else{
        return null;
    }
}

function pay_link($row) 
{
    if($row['batch_complete'] == 1){
        return null;
    }
    
    if($row['approval_status'] == AS_APPROVED && $row['closed_for_payments'] == 0){
        $modify = "ModifyMRNumber";
        return pager_link( _("Confirm"),"/petty_cash/i_owe_you_payments_entry.php?$modify=". $row['id'], ICON_DOC);  
    }
    elseif($row['approval_status'] == AS_APPROVED && $row['closed_for_payments'] == 1){
        $modify = "ModifyMRNumber";
        return pager_link(_('---'), "/petty_cash/i_owe_you_payments_entry.php?$modify=" . $row["id"]);
    }
    else{
        return null;
    }
}

function view_link($row)
{
    
    return get_petty_cash_trans_view_str(ST_I_OWE_YOU, $row['id']);
}


function batch_no($row)
{
    if($row['batch_no'] > 0){
        return get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $row['batch_no']);
    }else{
        return "";    
    }
}


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

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();
start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(text_bold("IOU No:"), 'iou_id', '',null, '', true);
        ref_cells(text_bold("Batch No:"), 'batch_no', '',null, '', true);
        date_cells(_("From:"), 'from_date', '', null, -14);
        date_cells(_("To:"), 'to_date', '', null, +7);
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        value_list_cells("I Owe You Type:", "type_id", "PETTY_CASH_TYPES", null, '');
        project_list_cells(_("Project:"), 'project_id', null, _('All projects'));
        petty_cash_floats_list_cells(_("Float:"), 'float_id', null, _('All floats'));
    end_row();
end_table();
    
start_table(TABLESTYLE_NOBORDER);
    start_row();    
    group_companies_list_cells("Company:", 'company_code', null, 'Please select a company', true);
    group_company_employees_simple_list_cells("Employee:", 'employee_code', $_POST['company_code'], null, 'Please select an employee', false);
    
        
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
    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'];
}
$employee_code = "";
if(isset($_POST['employee_code'])){
    $employee_code = $_POST['employee_code'];
}
$type_id = 0;
if(isset($_POST['type_id'])){
    $type_id = $_POST['type_id'];
}

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

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

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

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

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


    $sql = get_sql_for_i_owe_you_inquiry($from_date, $to_date, $type_id, $project_id, $float_id, $company_code, $employee_code, $iou_id, $batch_no);    
    
    $cols = array(
                _("IOU")=> array('fun'=>'view_link','align'=>'center'),
                _("Batch")=> array('fun'=>'batch_no','align'=>'center'),
                _("Employee")=> array('fun'=>'show_employee_name','align'=>'left'), 
                _("Project") ,
                _("Float") , 
                _("Type"), 
                _("Amount") => array('align'=>'right'),
                _("Date") => array('fun' => 'convert_date', 'align'=>'center'), 
                _("Comment") , 
                _("IOU Status") => array('fun' => 'show_iou_status','align'=>'center'),
                _("Batch Status") => array('fun' => 'show_batch_status','align'=>'center'),
                _("Edit")=>array('fun'=>'edit_link','align'=>'center'), 
                _("Remove")=>array('fun'=>'_remove_iou_link','align'=>'center'),
                _("Close")=>array('fun'=>'_close_iou_link','align'=>'center')
//                _("Expenses")=>array('fun'=>'expense_link','align'=>'center')
//                _("Expenses")=>array('fun'=>'expenses_link'),
//                _("Confirm")=>array('fun'=>'confirm_link'),
//                _("Pay")=>array('fun'=>'pay_link','align'=>'center')

);
//-----------------------------------------------------------------------------------------
    
   
    
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------

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

$table->width = "98%";

display_db_pager($table);

br();

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

?>
