<?php

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

    return db_query($sql);
}

function get_fi_sales_invoice_details($trans_type, $trans_no)
{
    $sql = "SELECT d.id detail_line_id,
                    d.tran_qty,
                    d.tran_unit_price,
                    d.discount_percent,
                    d.unit_tax, 
                    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_sales_invoice_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);
}



?>