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

SELECT
  teitem.*,
  CASE WHEN (teitem_type = 'T') THEN
    teitem_qty
  END AS hours,
  CASE WHEN (teitem_type = 'E') THEN
    teitem_total
  END AS expense,
  item_number, item_descrip1,
  cust_number, cust_name,
  tehead.*, emp_code,
  prj_number, prj_name,
  prjtask_number, prjtask_name,
  CASE WHEN teitem_type='E' THEN 0
       ELSE COALESCE(teitem_empcost, te.calcRate(emp_wage, emp_wage_period))
       END AS teitem_hrlycost,
  CASE WHEN teitem_type='E' THEN 0
       ELSE COALESCE(teitem_empcost, te.calcRate(emp_wage, emp_wage_period))
       * teitem_qty END AS teitem_totalcost,
  CASE WHEN (teitem_type='T') THEN
    <? value("time") ?>
       WHEN (teitem_type='E') THEN
    <? value("expense") ?>
       ELSE
    <? value("error") ?>
  END AS teitem_type_qtdisplayrole,
  'qty' AS teitem_qty_xtnumericrole,
  'qty' AS hours_xtnumericrole,
  'qty' AS teitem_qty_xtnumericrole,
  'qty' AS hours_xtnumericrole,
  'extprice' AS expense_xtnumericrole,
  '--' AS hours_xtnullrole,
  '--' AS expense_xtnullrole,
  'salesprice' AS teitem_rate_xtnumericrole,
  'extprice' AS teitem_total_xtnumericrole,
  0 AS hours_xttotalrole,
  0 AS expense_xttotalrole,
  firstLine(teitem_notes) AS f_notes
FROM te.tehead
  JOIN te.teitem ON (tehead_id = teitem_tehead_id)
  JOIN emp ON (tehead_emp_id= emp_id)
  JOIN item ON (teitem_item_id = item_id)
  LEFT OUTER JOIN custinfo ON (teitem_cust_id=cust_id)
  JOIN prjtask ON (teitem_prjtask_id=prjtask_id)
  JOIN prj ON (prjtask_prj_id=prj_id)
WHERE (tehead_id = <? value("tehead_id") ?>)
ORDER BY teitem_linenumber;
