<?php

function get_fi_customer_credit_notes($trans_type, $cutoff_date, $batch_size)
{
    $sql = "SELECT cn.type,
                    cn.trans_no,
                    cn.tran_date,
                    cn.due_date,
                    cn.ov_amount,
                    cn.ov_gst,
                    cn.ov_freight,
                    cn.ov_freight_tax,
                    cn.ov_discount,
                    cn.debtor_no,
                    cn.project_id,
                    
                    del.type del_type,
                    del.trans_no del_no,
                    
                    inv.type inv_type,
                    inv.trans_no inv_no,
                    
                    cb.sales_account,
                    cb.sales_discount_account,
                    cb.receivables_account,
                    cb.payment_discount_account,
                    dm.curr_code,
                    
                    so.tax_included,
                    so.order_no 
                    
            FROM ".TB_PREF."debtor_trans cn
                JOIN ".TB_PREF."debtor_trans del 
                    ON cn.order_ = del.trans_no   
                    AND del.type = ".db_escape(ST_CUSTDELIVERY)."     
                JOIN ".TB_PREF."debtor_trans inv 
                    ON del.order_ = inv.trans_no   
                    AND inv.type = ".db_escape(ST_SALESINVOICE)."     
                JOIN ".TB_PREF."sales_orders so 
                    ON inv.order_ = so.order_no 
                    AND so.trans_type = ".db_escape(ST_SALESORDER)."      
                JOIN ".TB_PREF."debtors_master dm ON 
                    inv.debtor_no = dm.debtor_no  
                JOIN ".TB_PREF."cust_branch cb ON 
                    inv.branch_code = cb.branch_code    
            WHERE cn.type = ".db_escape($trans_type)."     
                    AND cn.tran_date <= ".db_escape($cutoff_date)."    
                    AND cn.fi_batch_no = 0 
            ORDER BY cn.trans_no LIMIT ".$batch_size;

    return db_query($sql);
}

function get_fi_customer_credit_note_details($trans_type, $trans_no)
{
    $sql = "SELECT d.id detail_line_id,
                    d.quantity,
                    d.standard_cost,
                    sm.mb_flag, 
                    sm.inventory_account, 
                    sm.cogs_account,
                    sm.adjustment_account, 
                    sm.sales_account, 
                    sm.assembly_account,
                    sm.stock_id,
                    sm.description 
            FROM ".TB_PREF."debtor_trans_details d   
                    JOIN ".TB_PREF."stock_master sm ON 
                    d.stock_id = sm.stock_id 
            WHERE d.debtor_trans_type = ".db_escape($trans_type)." 
                    AND d.debtor_trans_no = ".db_escape($trans_no);
                    

    return db_query($sql);
}


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

    return db_query($sql);
}



?>