<?php

function get_fi_petty_cash_item_receipts($cutoff_date, $batch_size)
{
    $sql = "SELECT d.id detail_line_id,
                    d.trans_no, 
                    d.tran_date,
                    d.project_id,
                    d.stock_id,
                    d.qty quantity,
                    d.tran_unit_cost,
                    d.conversion_factor,
                    d.source_detail_id,
                    pm.description project_description,
                    pm.cost_account,
                    pm.type_id project_type_id,
                    moves.standard_cost,  
                    moves.trans_id,                               
                    sm.mb_flag, 
                    sm.inventory_account, 
                    sm.description 
            FROM ".TB_PREF."petty_cash_stock_item_receipt_details d     
                JOIN ".TB_PREF."projects_master pm ON 
                    d.project_id = pm.project_id  
                JOIN ".TB_PREF."stock_master sm ON 
                    d.stock_id = sm.stock_id 
                LEFT JOIN ".TB_PREF."stock_moves moves 
                    ON moves.type = d.type  
                    AND d.trans_no = moves.trans_no     
            WHERE d.type = ".db_escape(ST_PETTY_CASH_ITEM_RECEIPT)."  
                AND d.source_type = ".db_escape(ST_I_OWE_YOU_BATCH)."  
                AND d.tran_date <= ".db_escape($cutoff_date)."  
                AND d.fi_batch_no = 0 
            ORDER BY d.trans_no LIMIT ".$batch_size;

    return db_query($sql, "get_fi_petty_cash_item_receipts");
}

function get_fi_petty_cash_item_receipts_iou_batch_expense_detail($expense_line_id)
{
    $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.id = ".db_escape($expense_line_id);
    
    $result = db_query($sql, "get_fi_petty_cash_item_receipts_iou_batch_expense_detail");
    
    return db_fetch($result);
    
}