[0003780] Sales Invoice to partial delivery was improperly issued as final prepayment...
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index 54e4374a8fb3b9a755c5ba0bbb2357f572a68750..d4fda212743ccecb616148e1a1b4526f81e1fbde 100644 (file)
@@ -284,22 +284,28 @@ function get_cust_prepayment_invoice_factor($trans_no)
        return $row[0];
 }
 
+/*
+       Return yet not invoiced amount of prepayment invoice (or 0 if normal invoice)
+*/
 function prepaid_invoice_remainder($order)
 {
        $sql = "SELECT so.total - IFNULL(SUM(inv.prep_amount),0) FROM "
                .TB_PREF."sales_orders so,
-               ".TB_PREF."debtor_trans inv
-               LEFT JOIN ".TB_PREF."payment_terms pt ON inv.payment_terms=pt.terms_indicator
+               ".TB_PREF."debtor_trans inv,
+               ".TB_PREF."payment_terms pt
                WHERE  so.order_no=".db_escape($order)
                        ." AND so.trans_type=".ST_SALESORDER
                        ." AND inv.type=".ST_SALESINVOICE
                        ." AND inv.order_=so.order_no"
-                       ." AND pt.days_before_due<0";
+                       ." AND so.payment_terms=pt.terms_indicator"
+                       ." AND inv.payment_terms=pt.terms_indicator"
+                       ." AND pt.days_before_due = -1";
 
        $result = db_fetch(db_query($sql, "cannot find final invoice value"));
-       return $result[0];
+       return $result[0] ? $result[0] : 0;
 }
 
+
 /*
        Find oldest delivery date for sales invoice
 */