<?php

function get_fi_i_owe_you_advances($cutoff_date, $batch_size)
{
//    $sql = "SELECT pay.id,
//                pay.iou_id,
//                adv.tran_date payment_date,
//                pay.amount,
//                pcf.account_code,
//                pcf.credit_account,
//                pm.project_id,
//                pm.description project_description,
//                pm.cost_account 
//            FROM ".TB_PREF."i_owe_you_advances_details pay 
//                JOIN ".TB_PREF."i_owe_you_advances adv  
//                    ON adv.batch_no = pay.batch_no  
//                JOIN ".TB_PREF."i_owe_you iou 
//                    ON iou.id = pay.iou_id   
//                JOIN ".TB_PREF."petty_cash_floats pcf 
//                    ON pcf.id = pay.float_id  
//                JOIN ".TB_PREF."projects_master pm  
//                    ON pm.project_id = iou.project_id  
//            WHERE adv.tran_date <= ".db_escape($cutoff_date)."  
//                AND pay.fi_batch_no = 0 
//            ORDER BY pay.id 
//            LIMIT ".$batch_size;

    $sql = "SELECT pay.id,
                pay.iou_id,
                adv.tran_date payment_date,
                pay.amount,
                pcf.account_code,
                pcf.credit_account,
                pm.project_id,
                pm.description project_description,
                pm.cost_account, 
                pm.employee_advances_account,
                pay.id trans_no,
                adv.tran_date tran_date    
            FROM ".TB_PREF."i_owe_you_advances_details pay 
                JOIN ".TB_PREF."i_owe_you_advances adv  
                    ON adv.batch_no = pay.batch_no  
                JOIN ".TB_PREF."i_owe_you iou 
                    ON iou.id = pay.iou_id   
                JOIN ".TB_PREF."petty_cash_floats pcf 
                    ON pcf.id = pay.float_id 
                    AND pcf.reimbursement_method = 1 
                JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = iou.project_id  
            WHERE adv.tran_date <= ".db_escape($cutoff_date)."  
                AND pay.fi_batch_no = 0 
            ORDER BY pay.id 
            LIMIT ".$batch_size;

    return db_query($sql, 'get_fi_i_owe_you_advances');
}

function get_fi_i_owe_you_payments($cutoff_date, $batch_size)
{
//    $sql = "SELECT pay.id,
//                pay.iou_id,
//                pay.is_return,
//                pay.payment_date,
//                pay.amount,
//                pay.paid,
//                pay.expense_source,
//                pcf.account_code,
//                pcf.credit_account,
//                pm.project_id,
//                pm.description project_description,
//                pm.cost_account 
//            FROM ".TB_PREF."i_owe_you_payments pay 
//                JOIN ".TB_PREF."petty_cash_floats pcf 
//                    ON pcf.id = pay.float_id 
//                    AND pcf.reimbursement_method = 1 
//                JOIN ".TB_PREF."i_owe_you iou  
//                    ON iou.id = pay.iou_id 
//                JOIN ".TB_PREF."projects_master pm  
//                    ON pm.project_id = iou.project_id  
//            WHERE pay.expense_source IN (".ST_I_OWE_YOU.")
//                AND pay.payment_date <= ".db_escape($cutoff_date)."  
//                AND pay.paid = 1  
//                AND pay.fi_batch_no = 0 
//            ORDER BY pay.id 
//            LIMIT ".$batch_size;

    $sql = "SELECT pay.id,
                pay.iou_id,
                pay.is_return,
                pay.payment_date,
                pay.amount,
                pay.paid,
                pay.expense_source,
                pcf.account_code,
                pcf.credit_account,
                pm.project_id,
                pm.description project_description,
                pm.cost_account, 
                pm.employee_advances_account,
                pay.id trans_no,
                pay.payment_date tran_date 
            FROM ".TB_PREF."i_owe_you_payments pay 
                JOIN ".TB_PREF."petty_cash_floats pcf 
                    ON pcf.id = pay.float_id  
                JOIN ".TB_PREF."i_owe_you iou  
                    ON iou.id = pay.iou_id 
                JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = iou.project_id  
            WHERE pay.expense_source IN (".ST_I_OWE_YOU.")
                AND pay.payment_date <= ".db_escape($cutoff_date)."  
                AND pay.paid = 1  
                AND pay.fi_batch_no = 0 
            ORDER BY pay.id 
            LIMIT ".$batch_size;

    return db_query($sql, 'get_fi_i_owe_you_payments');
}

function get_fi_petty_cash_payments($cutoff_date, $batch_size)
{
//    $sql = "SELECT pay.id,
//                pay.iou_id,
//                pay.is_return,
//                pay.payment_date,
//                pay.amount,
//                pay.paid,
//                pay.expense_source,
//                pcf.account_code,
//                pcf.credit_account,
//                pm.project_id,
//                pm.description project_description,
//                pm.cost_account 
//            FROM ".TB_PREF."i_owe_you_payments pay 
//                JOIN ".TB_PREF."petty_cash_floats pcf 
//                    ON pcf.id = pay.float_id 
//                    AND pcf.reimbursement_method = 1 
//                JOIN ".TB_PREF."petty_cash_header iou  
//                    ON iou.id = pay.iou_id 
//                JOIN ".TB_PREF."projects_master pm  
//                    ON pm.project_id = iou.project_id  
//            WHERE pay.expense_source IN (".ST_PETTY_CASH.")
//                AND pay.payment_date <= ".db_escape($cutoff_date)."  
//                AND pay.paid = 1  
//                AND pay.fi_batch_no = 0 
//            ORDER BY pay.id 
//            LIMIT ".$batch_size;

    $sql = "SELECT pay.id,
                pay.iou_id,
                pay.is_return,
                pay.payment_date,
                pay.amount,
                pay.paid,
                pay.expense_source,
                pcf.account_code,
                pcf.credit_account,
                pm.project_id,
                pm.description project_description,
                pm.cost_account, 
                pm.employee_advances_account,
                pay.id trans_no,
                pay.payment_date tran_date    
            FROM ".TB_PREF."i_owe_you_payments pay 
                JOIN ".TB_PREF."petty_cash_floats pcf 
                    ON pcf.id = pay.float_id  
                JOIN ".TB_PREF."petty_cash_header iou  
                    ON iou.id = pay.iou_id 
                JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = iou.project_id  
            WHERE pay.expense_source IN (".ST_PETTY_CASH.")
                AND pay.payment_date <= ".db_escape($cutoff_date)."  
                AND pay.paid = 1  
                AND pay.fi_batch_no = 0 
            ORDER BY pay.id 
            LIMIT ".$batch_size;

    return db_query($sql, 'get_fi_petty_cash_payments');
}

