<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_FI_BATCHES_INQ';
$path_to_root="../..";

include($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 . "/gl/includes/db/financial_integration_inquiry_db.inc");
include_once($path_to_root . "/gl/includes/fi/db/generate_fi_accounting_entries_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 = "Financial Integration Batches Inquiry"), false, false, "", $js);

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

if (get_post('Search'))
{
    $Ajax->activate('financial_integration_tbl');
}

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

if (!isset($_POST['filterType']))
	$_POST['filterType'] = -1;

start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();
    ref_cells(_(text_bold("Batch No:")), 'batch_no', '',null, _('Enter memo fragment or leave empty'));
end_row();

start_row();
    journal_types_list_cells(_("Type:"), "filterType");
    date_cells(_("From:"), 'FromDate', '', null, 0, -1, 0);
    date_cells(_("To:"), 'ToDate');
    three_value_list_cells('Status:', 'status', null, 'All', 'ALL', 'Not Posted', 'NOT_PST', 'Posted', AS_POSTED, true);
    submit_cells('Search', _("Search"), '', '', 'default'); 
end_row();

end_table();

function batch_no($row)
{
    return get_journal_trans_view_str(ST_FI_BATCH, $row['batch_no']);
}

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

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

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

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

function _cuttoff_date($row)
{
    return sql2date($row['cutoff_date']);
}

function _status($type)
{
    global $approval_statuses;
	
    return $approval_statuses[$type['status']];
}

function _batch_trans_type($type)
{
    global $systypes_array;
	
    if($type['batch_trans_type'] > 0){
        return $systypes_array[$type['batch_trans_type']];
    }
    else{
        return "Multiple Transaction Types";
    }
}

function _transaction_count($row) 
{
    $count = get_fi_batch_transaction_count($row["batch_no"]);
    return $count;
    
}

function _gl_batch_no($row) 
{
    if($row["gl_batch_no"] > 0){
        if($row["gl_batch_type"] > 0){
            $link = get_trans_view_str($row["gl_batch_type"], $row["gl_batch_no"]);
            return $link;
        }
        else{
            $link = get_trans_view_str(ST_GL_BATCH, $row["gl_batch_no"]);
            return $link;
        }
    }
    else{
        return '';
    }
}

function view_link($row) 
{
    return get_trans_view_str($row["type"], $row["type_no"]);
}

function _delete_link($row) 
{
    $modify = "batch_no";   
    return pager_link(_("Delete"), "/gl/delete_fi_batch.php?$modify=". $row['batch_no'], ICON_DELETE);  
        
//    if($row["gl_batch_no"] == 0 && $row["status"] == AS_NEW){
//        return pager_link(_("Delete"), "/gl/delete_fi_batch.php?$modify=". $row['batch_no'], ICON_DELETE);  
//    }
//    else{
//        return null;
//    }
}

$editors = array(
	ST_JOURNAL => "/gl/gl_journal.php?ModifyGL=Yes&trans_no=%d&trans_type=%d",
	ST_BANKPAYMENT => "/gl/gl_bank.php?ModifyPayment=Yes&trans_no=%d&trans_type=%d",
	ST_BANKDEPOSIT => "/gl/gl_bank.php?ModifyDeposit=Yes&trans_no=%d&trans_type=%d",
//	4=> Funds Transfer,
   ST_SALESINVOICE => "/sales/customer_invoice.php?ModifyInvoice=%d",
//   11=>
// free hand (debtors_trans.order_==0)
//	"/sales/credit_note_entry.php?ModifyCredit=%d"
// credit invoice
//	"/sales/customer_credit_invoice.php?ModifyCredit=%d"
//	 12=> Customer Payment,
   ST_CUSTDELIVERY => "/sales/customer_delivery.php?ModifyDelivery=%d",
//   16=> Location Transfer,
//   17=> Inventory Adjustment,
//   20=> Supplier Invoice,
//   21=> Supplier Credit Note,
//   22=> Supplier Payment,
//   25=> Purchase Order Delivery,
//   28=> Work Order Issue,
//   29=> Work Order Production",
//   35=> Cost Update,

//    ST_POINTSUPDATE => "/sales/customer_invoice.php?ModifyInvoice=%d",
);



$sql = get_sql_for_fi_batches_inquiry(get_post('filterType', -1), get_post('batch_no'), get_post('FromDate'), get_post('ToDate'), get_post('status'));

$cols = array(
 
        _("Batch No") => array('fun'=>'batch_no', 'align'=>'center'), 
        _("Cutoff Date") =>array('fun'=>'_cuttoff_date', 'align'=>'center'),
        _("Transaction Type") =>array('fun'=>'_batch_trans_type'),
        _("Transaction Count") =>array('fun'=>'_transaction_count', 'align'=>'center'),
    	_("Created By") => array('fun'=>'_created_by', 'align'=>'center'),  
    	_("Created On") => array('fun'=>'_created_on', 'align'=>'center'), 
        _("Status") => array('fun'=>'_status', 'align'=>'center'), 
        _("GL Batch") => array('fun'=>'_gl_batch_no', 'align'=>'center'), 
        _("Processed By") => array('fun'=>'_processed_by', 'align'=>'center'), 
        _("Processed On") => array('fun'=>'_processed_on', 'align'=>'center'), 
		        	
//        _("Account") => array('fun'=>'account'), 
//	_("Accounts Total") => array('fun'=>'amount','align'=>'right'),    

	_("Delete") => array('insert'=>true, 'fun'=>'_delete_link', 'align'=>'center'),

);



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

$table->width = "90%";

display_db_pager($table);

end_form();
end_page();



?>
