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

function display_employee_petty_cash_outstanding_filtering_criteria(){
    
//    Start Date') => 'DATEBEGIN',
//			_('End Date') => 'DATEENDM',
//			_('Customer') => 'CUSTOMERS_NO_FILTER',
//			_('Show Balance') => 'YES_NO',
//			_('Currency Filter') => 'CURRENCY',
//			_('Suppress Zeros') => 'YES_NO',
//			_('Comments') => 'TEXTBOX',
//			_('Orientation') => 'ORIENTATION',
//			_('Destination') => 'DESTINATION'));
                            
    start_table(TABLESTYLE_NOBORDER);
    start_row();

	group_companies_list_cells("Company:", 'company_code', null, false, true);
        group_company_employees_simple_list_cells("Employee:", 'employee_code', $_POST['company_code'], null, 'Please select an employee', true);
        
//        $expenses_filter_array = array();
//        $expenses_filter_array[1] = "All";
//        $expenses_filter_array[2] = "Released or Approved";
//        $expenses_filter_array[3] = "Checked or Approved";
//        
//        custom_values_list_cells("Expenses", 'expenses_filter', $expenses_filter_array, null, false, true);
        
        yesno_list_cells("Batch Completed:", 'batch_complete_filter');
        yesno_list_cells("Date Filter Type:", 'date_filter_type', null, 'IOU Date', 'Payment Date', true);
        

        date_cells(_("From:"), 'from_date', '', null, -14, 0, 0, null, true);
        date_cells(_("To:"), 'to_date', '', null, 0, 0, 0, null, true);

        submit_cells('RefreshInquiry', _("Display"),'',_('Refresh Inquiry'), 'default');
        
    end_row();
//    }
//    else{
//        $_POST['from_date'] = Today();
//        hidden('from_date', $_POST['from_date']);
        
//        $_POST['to_date'] = Today();
//        hidden('to_date', $_POST['to_date']);
//    }
    end_table();

    
}


function display_employee_petty_cash_outstanding($company_code, $employee_code, $batch_complete_filter, $from_date_, $to_date_, $date_filter_type=0)
{
    global $systypes_array, $approval_statuses;

//    if($no_zeros){
//        $from_date_ = sql2date(INFINITE_START_DATE);        
//        $to_date_ = sql2date(INFINITE_END_DATE);
//    }
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    


    start_table(TABLESTYLE, "width=99%");

    $th = array(_("Tran. Date"), _("IOU Date"), _("Type"),_("Pay. No"),_("IOU No"),_("IOU Status"),_("Batch No"),_("Batch Status"), _("Float"),
                _("Comments"), _("Payments"),_("Expenses"), _("Period<br>Outstaning"), _("Total<br>Outstaning"),_("Completed"));

    //inactive_control_column($th);
    table_header($th);
    
    $ob = array(0,0,0,0,0);
//    display_customer_opening_balance($ob, $customer_id, $from_date_, 0);
    
    $k = 0;
    $rate =1;
    $dec = 2;
    $payments = $expenses = $period_balance = $total_balance = 0;
    
    $opening_balance_date_ = add_days($from_date_, -1);
    $total_balance = get_employee_petty_cash_opening_balance($company_code, $employee_code, $batch_complete_filter, INFINITE_START_DATE, 
                                                             date2sql($opening_balance_date_), $date_filter_type);
//    $total_balance = 5000000;
    
    start_row();
        label_cell(text_bold("Opening Balance"), 'align=right colspan=12');
        amount_cell($total_balance, true);
    end_row();
    
    
    $result = get_employee_petty_cash_outstanding_transactions($company_code, $employee_code, $batch_complete_filter, $from_date, $to_date, 
                                                               $date_filter_type);
    
    while ($trans = db_fetch($result)) 
    {
	$debit = $credit = $allocated = $balance = 0;
                    
        
        alt_table_row_color($k);

        label_cell(sql2date($trans['trans_date']), 'align=center');
        if ($trans['trans_type'] == ST_I_OWE_YOU_BATCH) {
            label_cell('');
            label_cell(text_bold($systypes_array[$trans['trans_type']]));
        } else {
            label_cell(sql2date($trans['iou_date']), 'align=center');
            label_cell($systypes_array[$trans['trans_type']]);
        }
//        $link = get_trans_view_str($trans['trans_type'], $trans['trans_no']);
        if($trans['trans_no'] > 0){
            $pay_link = get_trans_view_str($trans['trans_type'], $trans['trans_no']);
        }else{
            $pay_link = '';
        }        
        label_cell($pay_link, 'align=center');        
        
        if($trans['iou_id'] > 0){
            $iou_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $trans['batch_no'], $trans['iou_id']);
        }else{
            $iou_link = '';
        }
        label_cell($iou_link, 'align=center'); 
        label_cell($approval_statuses[$trans['iou_status']]);
        
        if($trans['batch_no'] > 0){
            $batch_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $trans['batch_no']);
        }else{
            $batch_link = '';
        }
        label_cell($batch_link, 'align=center');
        
        if($trans['batch_status'] > 0){
            label_cell($approval_statuses[$trans['batch_status']]);
        }else{
            label_cell("");
        }
        
        label_cell($trans['float_name']);  
        
        label_cell($trans['comments']);      
        
//        $project_name = get_project_formatted_name($trans['project_id']);
//        label_cell($project_name);        

        $amount = $trans['amount'];
        if(in_array($trans['trans_type'], array(ST_I_OWE_YOU_PAYMENT, ST_PETTY_CASH_PAYMENT, ST_I_OWE_YOU_ADVANCES, ST_IOU_BANKPAYMENT, ST_IOU_BANKPAYMENT_RETURN))){
            amount_cell($amount);
            label_cell('');
            $payments += $amount;
        }else{
            label_cell('');
            amount_cell($amount);
            $expenses += $amount;
        }

        $period_balance += $amount; 
        $total_balance += $amount; 

        amount_cell($period_balance);
        amount_cell($total_balance);
        label_cell(get_yes_no_name($trans['batch_complete']), 'align=center');
        
        end_row();
        
  

    }
    
    start_row();
        label_cell(text_bold("Totals"), 'align=right colspan=10');
        amount_cell($payments, true);
        amount_cell($expenses, true);
        amount_cell($period_balance, true); 
        label_cell('');    
    end_row();
    
    start_row();
        label_cell(text_bold("Closing Balance"), 'align=right colspan=13');
        amount_cell($total_balance, true);

    end_row();

    end_table(1);
}

