<?php

function get_fi_project_credit_notes($cutoff_date, $batch_size)
{
    $sql = "SELECT pi.order_no trans_no, 
                    pi.ord_date tran_date,
                    pi.debtor_no,
                    pi.rate_include_taxes,
                    pi.total,
                    pi.taxes,
                    pi.project_id,
                    cb.project_sales_account,
                    cb.sales_discount_account,
                    cb.receivables_account,
                    cb.payment_discount_account,
                    dm.curr_code,
                    pm.revenue_account 
            FROM ".TB_PREF."project_credit_notes pi 
                JOIN ".TB_PREF."debtors_master dm ON 
                    pi.debtor_no = dm.debtor_no  
                JOIN ".TB_PREF."cust_branch cb ON 
                    pi.branch_code = cb.branch_code   
                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_project_credit_note_details($trans_no)
{
    $sql = "SELECT d.id detail_line_id,
                    d.order_no trans_no,
                    d.amount,              
                    a.debit_account, 
                    a.credit_account 
            FROM ".TB_PREF."project_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_project_credit_note_taxes($trans_no)
{
    $sql = "SELECT tx.id,
                    tx.pcn_id trans_no,
                    tx.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tx.suspended 
            FROM ".TB_PREF."project_credit_note_item_taxes tx 
                JOIN ".TB_PREF."tax_types tt ON 
                    tx.tax_type_id = tt.id 
            WHERE tx.pcn_id = ".db_escape($trans_no);
                    

    return db_query($sql);
}



?>