<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_PAYMENTS_ENTRY';
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");
include_once($path_to_root . "/includes/db/group_companies_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 Payments Entry / Complete Batch"), false, false, "", $js);

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

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

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

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


function show_batch_status($row)
{
    if($row['batch_complete'] == 1){
        return "Completed";
    }
    else{
        return "";
    }
}


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

function complete_link($row) 
{
    if($row['type_code'] == "I_OWE_YOU"){
        if(isset($row['batch_approval_status']) && $row['batch_approval_status'] == AS_APPROVED && $row['batch_complete'] == 0){
            $modify = "ModifyMRNumber";
            return pager_link(_('Complete'), "/petty_cash/i_owe_you_payments_entry.php?$modify=" . $row["id"]);
        }
        else{
            return null;
        }
    }
    elseif($row['type_code'] == "PETTY_CASH"){
        if(isset($row['batch_approval_status']) && $row['batch_approval_status'] == AS_APPROVED && $row['batch_complete'] == 0){
            $modify = "ModifyMRNumber";
            return pager_link(_('Complete'), "/petty_cash/petty_cash_payments_entry.php?$modify=" . $row["id"]);
        }
        else{
            return null;
        }
    }
    else{
        
    }

}

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['type_code'] == "I_OWE_YOU"){
        if($row['iou_approval_status'] == AS_APPROVED && $row['closed_for_payments'] == 0){
            $modify = "ModifyMRNumber";
            return pager_link( _("Pay"),"/petty_cash/i_owe_you_payments_entry.php?$modify=". $row['id']);  
        }
        elseif($row['iou_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;
        }
    }
    elseif($row['type_code'] == "PETTY_CASH"){
        if($row['iou_approval_status'] == AS_APPROVED){
            $modify = "ModifyMRNumber";
            return pager_link( _("Pay"),"/petty_cash/petty_cash_payments_entry.php?$modify=". $row['id']);  
        }
        else{
            return null;
        }
    }
    else{
        return null;
    }
}

function view_iou_pc_link($row)
{
    if($row['type_code'] == "I_OWE_YOU"){
        return get_petty_cash_trans_view_str(ST_I_OWE_YOU, $row['id']);
    }
    elseif($row['type_code'] == "PETTY_CASH"){
        return get_petty_cash_trans_view_str(ST_PETTY_CASH, $row['id']);        
    }
    else{
        return null;
    }
}


function view_batch_link($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');
//--------------------------------------------------------------------------------------------------

start_form();

start_table();
    start_row();
        ref_cells(text_bold("IOU Id"), 'iou_id', '',null, '', true);
        ref_cells(text_bold("Batch No"), 'batch_no', '',null, '', true);
        petty_cash_floats_list_cells(_("Float:"), 'float_id', null, _('All floats'));
        value_list_cells("I Owe You Type:", "type_id", "PETTY_CASH_TYPES", null, '');
        date_cells(_("from:"), 'from_date', '', null, -14);
        date_cells(_("to:"), 'to_date');
        
    end_row();
end_table();

start_table();
    start_row();
        project_list_cells(_("Project:"), 'project_id', null, _('All projects'));      
        group_companies_list_cells("Company:", 'group_company_code', null, false, true);
        group_company_employees_simple_list_cells(_("Select an Employee:"), 'employee_code', get_post('group_company_code'), null, "All employees", false);    
    
//        emoplyees_list_cells(_("Select an Employee:"), 'employee_code', null, "All employees");
        
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');

end_row();
end_table(1);

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

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

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

    $sql = get_sql_for_i_owe_you_payments($iou_id, $batch_no, $from_date, $to_date, $employee_code, $type_id, $project_id, $float_id);    
    
    $cols = array(
                _("Type"), 
                _("Id")=> array('fun'=>'view_iou_pc_link','align'=>'center'),
                _("Batch No")=> array('fun'=>'view_batch_link','align'=>'center'),
                _("Employee")=> array('fun'=>'show_employee_name','align'=>'left'), 
                _("Project") ,
                _("Float") , 
                
                _("Amount") => array('align'=>'right'),
                _("Date") => array('fun' => 'convert_date', 'align'=>'center'), 
                _("Comment") , 
                
//                _("Expenses")=>array('fun'=>'expenses_link'),
//                _("Confirm")=>array('fun'=>'confirm_link'),
                _("Pay")=>array('fun'=>'pay_link','align'=>'center'),
                _("Complete")=>array('fun'=>'complete_link','align'=>'center') 

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

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

$table->width = "95%";

display_db_pager($table);

br();

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

?>
