<?php

function get_due_payment_view_opening_balance($supplier_id, $due_date) {
    global $supplier_credit_types_array;
    
        $sql = "SELECT SUM(((st.ov_amount + st.ov_gst) - st.alloc)) balance 
                FROM ".TB_PREF."supp_trans st 
                    JOIN ".TB_PREF."utility_bills ub   
                        ON st.trans_no = ub.id 
                            AND st.type = ".db_escape(ST_UTILITY_BILLS)." 
                WHERE ub.due_date < ".db_escape($due_date)." 
                    AND st.type IN (".implode(',', $supplier_credit_types_array).")";

                if ($supplier_id > 0) {
                    $sql .= " AND st.supplier_id = ".db_escape($supplier_id);
                }

            $result = db_query($sql, "get_due_payment_view_opening_balance");
            return db_result_value($result, 'balance');
}

function get_due_payment_view_transactions($supplier_id, $due_date) {
    global $supplier_credit_types_array;
    
        $sql = "SELECT st.*, ((st.ov_amount + st.ov_gst) - st.alloc) balance, ub.due_date due_date_u
                FROM ".TB_PREF."supp_trans st 
                    JOIN ".TB_PREF."utility_bills ub   
                        ON st.trans_no = ub.id 
                            AND st.type = ".db_escape(ST_UTILITY_BILLS)." 
                WHERE ub.due_date >= ".db_escape($due_date)." 
                    AND st.type IN (".implode(',', $supplier_credit_types_array).")";

                if ($supplier_id > 0) {
                    $sql .= " AND st.supplier_id = ".db_escape($supplier_id);
                }
    
        $sql .= " AND ((st.ov_amount + st.ov_gst) - st.alloc) > 0 
                  ORDER BY ub.due_date ASC, st.type, st.trans_no";
    
        return db_query($sql, "get_supplier_transactions");
}

?>