<?php


function get_fi_subcontractor_credit_notes($cutoff_date, $batch_size)
{
    $sql = "SELECT pi.order_no trans_no, 
                    pi.ord_date tran_date,
                    pi.supplier_id,
                    pi.rate_include_taxes,
                    pi.total,
                    pi.taxes,
                    pi.project_id,
                    supp.purchase_account,
                    supp.payable_account,
                    supp.payment_discount_account,
                    supp.curr_code,
                    pm.cost_account  
                    
            FROM ".TB_PREF."subcontractor_credit_notes pi 
                JOIN ".TB_PREF."suppliers supp ON 
                    pi.supplier_id = supp.supplier_id             
                  
                JOIN ".TB_PREF."projects_master pm ON 
                    pi.project_id = pm.project_id  
                    
            WHERE pi.ord_date <= ".db_escape($cutoff_date)." 
                    AND pi.approval_status = ".db_escape(AS_APPROVED)." 
                    AND pi.fi_batch_no = 0 
            ORDER BY pi.order_no LIMIT ".$batch_size;

    return db_query($sql);
}


function get_fi_subcontractor_credit_note_details($trans_no)
{
    $sql = "SELECT d.id,
                    d.order_no trans_no,
                    d.amount,              
                    a.debit_account, 
                    a.credit_account 
            FROM ".TB_PREF."subcontractor_credit_note_details d   
                    JOIN ".TB_PREF."project_invoice_line_categories a ON 
                        d.category_id = a.category_id 
            WHERE d.order_no = ".db_escape($trans_no);
                    

    return db_query($sql);
}


function get_fi_subcontractor_credit_note_taxes($trans_no)
{
    $sql = "SELECT tx.id,
                    tx.order_no trans_no,
                    tx.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.suspended  
                    
            FROM ".TB_PREF."subcontractor_credit_note_taxes tx 
                JOIN ".TB_PREF."tax_types tt ON 
                    tx.tax_type_id = tt.id 
                    
            WHERE tx.order_no = ".db_escape($trans_no);
                    

    return db_query($sql);
}



?>