<?php

function get_fi_supplier_invoices($cutoff_date, $batch_size)
{
    $sql = "SELECT st.trans_no, st.type, 
                    st.supplier_id, st.tran_date, 
                    st.due_date, st.ov_amount, 
                    st.ov_discount, st.ov_gst, 
                    st.rate, st.tax_included, 
                    st.reference,  
                    sup.purchase_account, sup.payable_account, 
                    sup.payment_discount_account, sup.curr_code 
                    
            FROM ".TB_PREF."supp_trans st  
                JOIN ".TB_PREF."suppliers sup 
                    ON st.supplier_id = sup.supplier_id 
                    
            WHERE st.tran_date <= ".db_escape($cutoff_date)."  
                    AND st.type = ".db_escape(ST_SUPPINVOICE)."  
                    AND st.fi_batch_no = 0 
                    
            ORDER BY st.trans_no LIMIT ".$batch_size;

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


function get_fi_supplier_invoice_gl_codes($trans_type, $trans_no)
{
    $sql = "SELECT sii.id detail_line_id,
                    sii.supp_trans_no, 
                    sii.supp_trans_type,
                    sii.gl_code,  
                    sii.unit_price,
                    sii.unit_tax 
            FROM ".TB_PREF."supp_invoice_items sii  
            WHERE sii.supp_trans_type = ".db_escape($trans_type)."                   
                    AND sii.supp_trans_no = ".db_escape($trans_no)." 
                    AND sii.stock_id = '' ";

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


function get_fi_supplier_invoice_items($trans_type, $trans_no)
{
    $sql = "SELECT sii.id detail_line_id,
                    sii.grn_item_id,
                    sii.po_detail_item_id, 
                    sii.stock_id,
                    sii.po_unit_qty inv_tran_unit_qty, 
                    sii.po_unit_price inv_tran_unit_price,
                    sii.po_unit_conv_factor inv_tran_unit_conv_factor,
                    sii.po_unit_price_discount inv_tran_unit_price_discount,
                    sii.unit_tax, 
                    sii.quantity,
                    sii.unit_price,
                    pod.unit_price po_base_unit_price,
                    pod.order_unit_price,
                    pod.order_unit_conv_factor,
                    pod.order_unit_price_discount,
                    sm.mb_flag, 
                    sm.inventory_account, 
                    sm.cogs_account,
                    sm.adjustment_account, 
                    sm.sales_account, 
                    sm.assembly_account,
                    sm.description 
                                      
            FROM ".TB_PREF."supp_invoice_items sii  
            	JOIN ".TB_PREF."purch_order_details pod 
                	ON pod.po_detail_item = sii.po_detail_item_id 
                JOIN ".TB_PREF."stock_master sm 
                    ON sii.stock_id = sm.stock_id 
            WHERE sii.supp_trans_type = ".db_escape($trans_type)."                   
                    AND sii.supp_trans_no = ".db_escape($trans_no);                    

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


function get_fi_supplier_invoice_taxes($trans_type, $trans_no)
{
    $sql = "SELECT ttd.id,
                    ttd.trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    ttd.suspended                     
            FROM ".TB_PREF."trans_tax_details ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id  
                    
            WHERE ttd.trans_type = ".db_escape($trans_type)." 
                AND ttd.trans_no = ".db_escape($trans_no);
                    

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



?>