<?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 . "/includes/common_functions.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_batch_expenses_inquiry_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_expenses_entry_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 = "I Owe You Batches Inquiry"), false, false, "", $js);

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

simple_page_mode(true);


function edit_link($row) 
{
    
//    if (in_array($row['approval_status'], array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER))){
//        $modify = "BatchNo";
//        return pager_link(_("Check and approve"),"/petty_cash/approve_i_owe_you_expenses.php?$modify=".$row['batch_no'], ICON_OK); 
//    }
    
    if(in_array($row['approval_status'], array(AS_NEW, AS_BACK_TO_ORIGINATOR, AS_REOPENED, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER))){
        $modify = "ModifyMRNumber";
        
        //return pager_link(_("Edit"),"/petty_cash/i_owe_you.php?$modify=".$row['batch_no'], ICON_EDIT);  
        $pceh = get_petty_cash_expenses_header_by_batch_no($row['batch_no']);
        if($pceh){
            $modify = "PCENo";
            return pager_link(_("Edit"),"/petty_cash/petty_cash_expenses_entry.php?$modify=".$pceh['id'], ICON_EDIT); 
        }
        else{
            $modify = "IouBatchNo";
            return pager_link(_("Edit"),"/petty_cash/i_owe_you_expenses_entry2.php?$modify=".$row['batch_no'], ICON_EDIT); 
        }
        
    }
    else{
        return null;
    }
}
function approve_link($row) 
{
    
    if (in_array($row['approval_status'], array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER))){
        $modify = "BatchNo";
        return pager_link(_("Check and approve"),"/petty_cash/approve_i_owe_you_expenses.php?$modify=".$row['batch_no'], ICON_OK); 
    }
    
//    if(in_array($row['approval_status'], array(AS_NEW, AS_BACK_TO_ORIGINATOR, AS_REOPENED))){
//        $modify = "ModifyMRNumber";
//        
//        //return pager_link(_("Edit"),"/petty_cash/i_owe_you.php?$modify=".$row['batch_no'], ICON_EDIT);  
//        $pceh = get_petty_cash_expenses_header_by_batch_no($row['batch_no']);
//        if($pceh){
//            $modify = "PCENo";
//            return pager_link(_("Edit"),"/petty_cash/petty_cash_expenses_entry.php?$modify=".$pceh['id'], ICON_EDIT); 
//        }
//        else{
//            $modify = "IouBatchNo";
//            return pager_link(_("Edit"),"/petty_cash/i_owe_you_expenses_entry2.php?$modify=".$row['batch_no'], ICON_EDIT); 
//        }
//        
//    }
    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 "";    
    }
}


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

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

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

function completed($row)
{
    if($row['complete']	== 1){
        $completed = "Yes";
    }else{
        $completed = "No";
    }
    
    return $completed;    
}

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

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

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


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("Batch No:"), 'batch_no', '',null, '', true);
        date_cells(_("From Date:"), 'from_date', '', null, null);
        date_cells(_("To Date:"), 'to_date', '', null, null);
    end_row();
end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        project_list_cells(_("Project:"), 'project_id', null, _('All projects'));
        petty_cash_floats_list_cells(_("Float:"), 'float_id', null, _('All floats'));
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
    end_row();
end_table(1);

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


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

$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'];
}

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

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


    $sql = get_sql_for_i_owe_you_batch_expenses_inquiry($batch_no, $project_id, $float_id, $from_date, $to_date);    
    
    $cols = array(
                _("Batch No")=> array('fun'=>'batch_no','align'=>'center'),             
                _("Project")=> array('fun'=>'project_description'),  
                _("Float")=> array('fun'=>'float_name'),  
                _("Date") => array('fun'=>'tran_date','align'=>'center'),
                _("Amount") =>  array('fun'=>'amount','align'=>'right'),  
                _("Batch Status") => array('fun' => 'show_batch_status','align'=>'center'),
                _("Completed") => array('fun'=>'completed','align'=>'center'),              
                
                _("Edit")=>array('fun'=>'edit_link','align'=>'center'),
                _("Approve")=>array('fun'=>'approve_link','align'=>'center')

);
    
//-----------------------------------------------------------------------------------------
     

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

$table->width = "90%";

display_db_pager($table);

br();

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


?>
