-- Group: billingSelections
-- Name: detail
-- Notes: used by dspBillingSelections
--        Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
--        See www.xtuple.com/CPAL for the full text of the software license.

SELECT cobmisc_id, cohead_id,
       cohead_number, cust_number, cust_name,
       sum(round(coitem_price*cobill_qty,2)) AS subtotal,
       cobmisc_misc, cobmisc_freight, calcCobmiscTax(cobmisc_id) AS cobmisc_tax, cobmisc_payment,
       (sum(round(coitem_price * cobill_qty, 2)) +
                  cobmisc_misc + cobmisc_freight +
                  calcCobmiscTax(cobmisc_id)) AS total,
       'curr' AS subtotal_xtnumericrole,
       'curr' AS total_xtnumericrole,
       'curr' AS cobmisc_misc_xtnumericrole,
       'curr' AS cobmisc_freight_xtnumericrole,
       'curr' AS cobmisc_tax_xtnumericrole,
       'curr' AS cobmisc_payment_xtnumericrole
FROM cobmisc, cohead, custinfo, coitem, cobill
WHERE ((cobmisc_cohead_id=cohead_id)
   AND (cohead_cust_id=cust_id)
   AND (coitem_cohead_id=cohead_id)
   AND (cobill_coitem_id=coitem_id) AND (COALESCE(cobill_invcnum,0) = 0)
   AND (NOT cobmisc_posted))
GROUP BY cobmisc_id, cohead_id, cohead_number, cust_number,
         cust_name, cobmisc_misc, cobmisc_freight, cobmisc_tax, cobmisc_payment
ORDER BY cohead_number;
