-- Group: purchaseOrderItems
-- Name: detail
-- Notes: used by purchaseOrderItem
--        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 pohead_number, pohead_taxzone_id,
       pohead_curr_id, pohead_orderdate,
       COALESCE(pohead_cohead_id, -1) AS pohead_cohead_id,
       poitem_linenumber, poitem_vend_item_number,
       poitem_vend_item_descrip, poitem_vend_uom,
       poitem_invvenduomratio, poitem_status,
       poitem_duedate, poitem_qty_ordered,
       poitem_qty_received, poitem_unitprice,
       poitem_freight, poitem_taxtype_id,
       poitem_tax_recoverable, poitem_comments,
       poitem_prj_id, poitem_bom_rev_id,
       poitem_boo_rev_id, poitem_manuf_name,
       poitem_manuf_item_number, poitem_manuf_item_descrip,
       COALESCE(poitem_itemsite_id, -1) AS poitem_itemsite_id,
       COALESCE(poitem_itemsrc_id, -1) AS poitem_itemsrc_id,
       COALESCE(poitem_expcat_id, -1) AS poitem_expcat_id,
       COALESCE(poitem_order_id, -1) AS poitem_order_id,
       CASE WHEN (poitem_order_type='S') THEN TEXT(<? value("sonum") ?>)
            WHEN (poitem_order_type='W') THEN TEXT(<? value("wonum") ?>)
            ELSE ''
       END AS demand_type,
       CASE WHEN (poitem_order_type='S') THEN formatSoNumber(coitem_id)
            WHEN (poitem_order_type='W') THEN formatWoNumber(womatl_wo_id)
            ELSE ''
       END AS order_number,
       poitem_unitprice * poitem_qty_ordered AS extended_price,
       COALESCE(coitem_prcost, 0.0) AS override_cost,
       contrct_number,
       itemsrc.*
  FROM poitem JOIN pohead ON (pohead_id=poitem_pohead_id)
              LEFT OUTER JOIN itemsrc ON (itemsrc_id=poitem_itemsrc_id)
              LEFT OUTER JOIN coitem  ON (poitem_order_type='S' AND coitem_id=poitem_order_id)
              LEFT OUTER JOIN womatl  ON (poitem_order_type='W' AND womatl_id=poitem_order_id)
              LEFT OUTER JOIN contrct ON (itemsrc_contrct_id=contrct_id)
 WHERE ( (TRUE)
<? if exists("poitem_id") ?>
   AND (poitem_id=<? value("poitem_id") ?>)
<? endif ?>
);
