<?php

function get_fi_i_owe_you_expense_batches($cutoff_date, $batch_size)
{
//    $sql = "SELECT ioub.batch_no,
//                ioub.tran_date,
//                ioub.amount,
//                ioub.float_id,               
//                ioub.project_id,               
//                pcf.account_code float_account,
//                pm.type_id project_type_id 
//            FROM ".TB_PREF."i_owe_you_batches ioub  
//                JOIN ".TB_PREF."petty_cash_floats pcf 
//                    ON pcf.id = ioub.float_id 
//                    AND pcf.reimbursement_method = 0  
//                JOIN ".TB_PREF."projects_master pm  
//                    ON pm.project_id = ioub.project_id  
//            WHERE ioub.tran_date <= ".db_escape($cutoff_date)."  
//                    AND ioub.approval_status IN (".db_escape(AS_APPROVED).", ".db_escape(AS_COMPLETED).")
//                    AND ioub.amount > 0 
//                    AND ioub.fi_batch_no = 0 
//            ORDER BY ioub.batch_no 
//            LIMIT ".$batch_size;
    
    
    $sql = "SELECT ioub.batch_no,
                ioub.tran_date,
                ioub.amount,
                ioub.float_id,               
                ioub.project_id,               
                pcf.account_code float_account,
                pcf.credit_account float_employee_advances_account,
                pm.type_id project_type_id, 
                pm.employee_advances_account  project_employee_advances_account,
                ioub.batch_no trans_no 
            FROM ".TB_PREF."i_owe_you_batches ioub  
                JOIN ".TB_PREF."petty_cash_floats pcf 
                    ON pcf.id = ioub.float_id   
                JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = ioub.project_id  
            WHERE ioub.tran_date <= ".db_escape($cutoff_date)."  
                    AND ioub.approval_status IN (".db_escape(AS_APPROVED).", ".db_escape(AS_COMPLETED).")
                    AND ioub.amount > 0 
                    AND ioub.fi_batch_no = 0 
            ORDER BY ioub.batch_no 
            LIMIT ".$batch_size;
    
    return db_query($sql, "get_fi_i_owe_you_expense_batches");
}

function get_fi_i_owe_you_batch_expense_details($batch_no)
{
    $sql = "SELECT ex.approved_amount, 
                ea.type_id,
                ea.debit_account,               
                ea.credit_account,
                ea.coc_account 
            FROM ".TB_PREF."i_owe_you_expenses ex   
                LEFT JOIN ".TB_PREF."petty_cash_expense_accounts  ea  
                    ON ex.expense_type_id = ea.type_id  
            WHERE ex.iou_batch_no = ".db_escape($batch_no);
    
    return db_query($sql, "get_fi_i_owe_you_batch_expense_details");
}