-- Group: packingListBatchByShipVia
-- Name: print
-- Notes: used by printPackingListBatchByShipVia
-- 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 DISTINCT pack_head_id, pack_head_type, pack_shiphead_id, getSoStatus(cohead_id) AS orderhead_status,
       COALESCE(shipform_report_name, findCustomerForm(cohead_cust_id, 'P')) AS packform,
       findCustomerForm(cohead_cust_id, 'L') AS pickform
FROM pack JOIN cohead ON (cohead_id=pack_head_id)
          JOIN coitem ON (coitem_cohead_id=cohead_id)
          JOIN itemsite ON (itemsite_id=coitem_itemsite_id)
          LEFT OUTER JOIN shipform ON (shipform_id=cohead_shipform_id)
WHERE ( (NOT pack_printed)
  AND   (pack_head_type='SO')
  AND   (getSoStatus(cohead_id) <> 'C') 
<? if exists("shipvia") ?>
  AND   (cohead_shipvia=<? value("shipvia") ?>)
<? endif ?>
  AND   (checkSOSitePrivs(cohead_id))
  AND	(coitem_scheddate BETWEEN <? value("startDate") ?> AND <? value("endDate") ?>)
<? if exists("warehous_id") ?>
  AND   (itemsite_warehous_id = <? value("warehous_id") ?>)
<? endif ?>
      )

<? if exists("MultiWhs") ?>
UNION ALL
SELECT DISTINCT pack_head_id, pack_head_type, pack_shiphead_id, tohead_status AS orderhead_status,
       COALESCE(shipform_report_name, findTOForm(tohead_id, 'P')) AS packform,
       findTOForm(tohead_id, 'L') AS pickform
FROM pack JOIN tohead ON (tohead_id=pack_head_id)
          JOIN toitem ON (toitem_tohead_id = tohead_id) 
          LEFT OUTER JOIN shipform ON (shipform_id=tohead_shipform_id)
WHERE ( (NOT pack_printed)
  AND   (pack_head_type='TO')
  AND   (tohead_status <> 'C') 
<? if exists("shipvia") ?>
  AND   (tohead_shipvia=<? value("shipvia") ?>)
<? endif ?>
  AND	(toitem_duedate BETWEEN <? value("startDate") ?> AND <? value("endDate") ?>)
<? if exists("warehous_id") ?>
  AND   (tohead_src_warehous_id = <? value("warehous_id") ?>)
<? endif ?>
      )
<? endif ?>

;
