Issue #23958:renumber sections for quotes
[xtuple] / foundation-database / public / tables / metasql / orderActivityByProject-detail.mql
1 -- Group: orderActivityByProject
2 -- Name:  detail
3 -- Notes: All projects
4
5 SELECT *, 
6   formatQty(qty) AS f_qty,
7   formatMoney(value) AS f_value,
8   'curr' AS qty_xtnumericrole,
9   'curr' AS value_xtnumericrole,
10   CASE WHEN COALESCE(hrs_balance,0) < 0 THEN 'red' END AS hrs_balance_qtforegroundrole,
11   CASE WHEN COALESCE(exp_balance,0) < 0 THEN 'red' END AS exp_balance_qtforegroundrole
12 FROM (
13 SELECT prj_id AS id,
14        1 AS type,
15        '0' AS subtype,
16        0 AS section,
17        NULL  AS section_qtdisplayrole,
18        prj_number AS name,
19        CASE                         
20            WHEN prj_status = 'C' THEN <? value('complete') ?>
21            WHEN prj_status = 'O' THEN <? value('inprocess') ?>
22            WHEN prj_status = 'P' THEN <? value('planning') ?>
23        END AS status,
24        prjtype_code AS project_type,
25        prj_name AS item,
26        firstline(prj_descrip) AS descrip,
27        crmacct_name AS customer, 
28        cntct_name AS contact,
29        addr_city AS city,
30        addr_state AS state,
31        NULL::numeric AS qty,
32        NULL::text AS uom,
33        NULL::numeric AS value,
34        prj_due_date AS due,
35        prj_assigned_date AS assigned,
36        prj_start_date AS started,
37        prj_completed_date AS completed,
38        NULL::numeric AS hrs_budget,
39        NULL::numeric AS hrs_actual,
40        NULL::numeric AS hrs_balance,
41        NULL::numeric AS exp_budget,
42        NULL::numeric AS exp_actual,
43        NULL::numeric AS exp_balance,
44        CASE WHEN (prj_status = 'O' AND prj_due_date < current_date) THEN 'red' 
45          WHEN (prj_status = 'O' AND prj_due_date BETWEEN current_date AND current_date + (fetchmetricvalue('ProjectDueDateWarning')||' days')::interval) THEN 'orange' 
46          END AS due_qtforegroundrole,
47        0 AS xtindentrole
48   FROM prj
49   LEFT OUTER JOIN prjtype ON (prj_prjtype_id=prjtype_id)
50   LEFT OUTER JOIN crmacct ON (prj_crmacct_id=crmacct_id)
51   LEFT OUTER JOIN cntct ON (crmacct_cntct_id_1=cntct_id)
52   LEFT OUTER JOIN addr ON (cntct_addr_id=addr_id)
53
54  WHERE (prj_id = <? value("prj_id") ?> )
55  
56 UNION ALL
57 ----- TASKS -----
58 SELECT DISTINCT -1 AS id, 
59        3 AS type,
60        '0' AS subtype,
61        1 AS section,
62        'Tasks' AS section_qtdisplayrole,
63        'Tasks' AS name,
64        NULL::text AS status,
65        NULL::text AS project_type,
66        NULL::text AS item,
67        NULL::text AS descrip,
68        NULL::text AS customer,
69        NULL::text AS contact,
70        NULL::text AS city,
71        NULL::text AS state,
72        NULL::numeric AS qty,
73        NULL::text AS uom,
74        NULL::numeric AS value,
75        NULL::date AS due,
76        NULL::date AS assigned,
77        NULL::date AS started,
78        NULL::date AS completed,
79        NULL::numeric AS hrs_budget,
80        NULL::numeric AS hrs_actual,
81        NULL::numeric AS hrs_balance,
82        NULL::numeric AS exp_budget,
83        NULL::numeric AS exp_actual,
84        NULL::numeric AS exp_balance,
85         
86        NULL::text AS due_qtforegroundrole,
87        1 AS xtindentrole
88
89 UNION ALL
90
91 SELECT prjtask_id AS id,
92        5 AS type,
93        '0' AS subtype,
94        1 AS section,
95        NULL AS section_qtdisplayrole,
96        prjtask_number AS name,
97        CASE                         
98            WHEN prjtask_status = 'C' THEN <? value('complete') ?>
99            WHEN prjtask_status = 'O' THEN <? value('inprocess') ?>
100            WHEN prjtask_status = 'P' THEN <? value('planning') ?>
101        END AS status,
102        NULL::text AS project_type,
103        prjtask_name AS item,
104        prjtask_descrip AS descrip,
105        cust_name as customer,
106        cntct_name AS contact,
107        addr_city AS city,
108        addr_state AS state,
109        NULL::numeric AS qty,
110        NULL::text AS uom,
111        NULL::numeric AS value,
112        prjtask_due_date AS due,
113        prjtask_assigned_date AS assigned,
114        prjtask_start_date AS started,
115        prjtask_completed_date AS completed,
116        prjtask_hours_budget AS hrs_budget,
117        prjtask_hours_actual AS hrs_actual,
118        (prjtask_hours_budget-prjtask_hours_actual) AS hrs_balance,
119        prjtask_exp_budget AS exp_budget,
120        prjtask_exp_actual AS exp_actual,
121        (prjtask_exp_budget-prjtask_exp_actual) AS exp_balance,
122        CASE WHEN (prjtask_status = 'O' AND prjtask_due_date < current_date) THEN 'red' 
123          WHEN (prjtask_status = 'O' AND prjtask_due_date BETWEEN current_date AND current_date + (fetchmetricvalue('ProjectDueDateWarning')||' days')::interval) THEN 'orange' 
124          END AS due_qtforegroundrole,
125        2 AS xtindentrole
126   FROM prjtask
127   LEFT OUTER JOIN te.teprjtask ON (prjtask_id = teprjtask_prjtask_id)
128   LEFT OUTER JOIN custinfo ON (teprjtask_cust_id=cust_id)
129   LEFT OUTER JOIN cntct ON (cust_corrcntct_id=cntct_id)
130   LEFT OUTER JOIN addr ON (cntct_addr_id=addr_id)
131
132  WHERE (prjtask_prj_id = <? value("prj_id") ?> )
133  GROUP BY custinfo.cust_name, prjtask.prjtask_id, addr.addr_city, addr.addr_state, cntct_name
134
135 <? if exists("showIn") ?>
136 UNION ALL
137 ----- INCIDENTS -----
138
139 SELECT DISTINCT -1 AS id, 
140        100 AS type,
141        '0' AS subtype,
142        1 AS section,
143        'Incidents' AS section_qtdisplayrole,
144        'Incidents' AS name,
145        NULL::text AS status,
146        NULL::text AS project_type,
147        NULL::text AS item,
148        NULL::text AS descrip,
149        NULL::text AS customer,
150        NULL::text AS contact,
151        NULL::text AS city,
152        NULL::text AS state,
153        NULL::numeric AS qty,
154        NULL::text AS uom,
155        NULL::numeric AS value,
156        NULL::date AS due,
157        NULL::date AS assigned,
158        NULL::date AS started,
159        NULL::date AS completed,
160        NULL::numeric AS hrs_budget,
161        NULL::numeric AS hrs_actual,
162        NULL::numeric AS hrs_balance,
163        NULL::numeric AS exp_budget,
164        NULL::numeric AS exp_actual,
165        NULL::numeric AS exp_balance,
166         
167        NULL::text AS due_qtforegroundrole,
168        1 AS xtindentrole
169     FROM incdt
170 UNION ALL
171
172 SELECT incdt_id AS id,
173        105 AS type,
174        '0' AS subtype,
175        1 AS section,
176        NULL AS section_qtdisplayrole,
177        incdt_number::text AS name,
178        CASE                         
179            WHEN incdt_status = 'L' THEN <? value('closed') ?>
180            WHEN incdt_status = 'N' THEN <? value('new') ?>
181            WHEN incdt_status = 'F' THEN <? value('feedback') ?>
182            WHEN incdt_status = 'A' THEN <? value('assigned') ?> 
183            WHEN incdt_status = 'R' THEN <? value('resolved') ?>
184            WHEN incdt_status = 'C' THEN <? value('confirmed') ?>
185        END AS status,
186        NULL::text AS project_type,
187        incdt_number::text AS item,
188        incdt_summary AS descrip,
189        crmacct_number AS customer,
190        crmacct_name AS contact,
191        addr_city AS city,
192        addr_state AS state,
193        NULL::numeric AS qty,
194        NULL::text AS uom,
195        NULL::numeric AS value,
196        NULL::date AS due,
197        NULL::date AS assigned,
198        NULL::date AS started,
199        NULL::date AS completed,
200        NULL::numeric AS hrs_budget,
201        NULL::numeric AS hrs_actual,
202        NULL::numeric AS hrs_balance,
203        NULL::numeric AS exp_budget,
204        NULL::numeric AS exp_actual,
205        NULL::numeric AS exp_balance,
206        NULL::text AS due_qtforegroundrole,
207        2 AS xtindentrole
208   FROM incdt
209   LEFT OUTER JOIN crmacct ON (incdt_crmacct_id=crmacct_id)
210   LEFT OUTER JOIN cntct ON (incdt_cntct_id=cntct_id)
211   LEFT OUTER JOIN addr ON (cntct_addr_id=addr_id)
212
213  WHERE (incdt_prj_id = <? value("prj_id") ?> )
214  GROUP BY incdt_id, incdt_prj_id, incdt_number, crmacct_number, crmacct_name, addr.addr_city, addr.addr_state, cntct_name, incdt_number, incdt_status, incdt_assigned_username, incdt_owner_username, incdt_number, incdt_summary 
215 <? endif ?>
216    
217 <? if exists("showSo") ?>
218 UNION ALL
219
220 ----- QUOTES -----
221 SELECT DISTINCT -1 AS id, 
222        10 AS type,
223        '0' AS subtype,
224        2 AS section,
225        <? value("quotes") ?> AS section_qtdisplayrole,
226        <? value("quotes") ?> AS name,
227        NULL::text AS status,
228        NULL::text AS project_type,
229        NULL::text AS item,
230        NULL::text AS descrip,
231        NULL::text AS customer,
232        NULL::text AS contact,
233        NULL::text AS city,
234        NULL::text AS state,
235        NULL::numeric AS qty,
236        NULL::text AS uom,
237        NULL::numeric AS value, 
238        NULL::date AS due,
239        NULL::date AS assigned,
240        NULL::date AS started,
241        NULL::date AS completed,
242        NULL::numeric AS hrs_budget,
243        NULL::numeric AS hrs_actual,
244        NULL::numeric AS hrs_balance,
245        NULL::numeric AS exp_budget,
246        NULL::numeric AS exp_actual,
247        NULL::numeric AS exp_balance,
248         
249        NULL::text AS due_qtforegroundrole,
250        1 AS xtindentrole
251   FROM quhead
252 <? if exists("owner_username") ?>
253     JOIN prj ON (prj_id=quhead_prj_id
254             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
255 <? endif ?>
256  WHERE (quhead_prj_id = <? value("prj_id") ?>)
257 <? if exists("owner_username") ?>
258    AND (quhead_owner_username=<? value("owner_username") ?>)
259 <? endif ?>
260
261 UNION ALL
262
263 SELECT quhead_id AS id, 
264        15 AS type,
265        quhead_number AS subtype,
266        2 AS section,
267        <? value("quotes") ?> AS section_qtdisplayrole,
268        quhead_number AS name,
269        CASE WHEN (quhead_status = 'C') THEN 
270          <? value("converted") ?>
271             WHEN (quhead_status = 'X') THEN
272          <? value("canceled") ?>
273             WHEN (COALESCE(quhead_expire, current_date + 1) > current_date) THEN
274          <? value("open") ?>
275             ELSE
276          <? value("expired") ?>
277        END AS status,
278        NULL::text AS project_type,
279        NULL::text AS item,
280        NULL::text AS descrip,
281        cust_name AS customer,
282        NULL::text AS contact,
283        NULL::text AS city,
284        NULL::text AS state,       
285        NULL AS qty,
286        NULL::text AS uom,
287        NULL AS value,
288        NULL::date AS due,
289        NULL::date AS assigned,
290        NULL::date AS started,
291        NULL::date AS completed,
292        NULL::numeric AS hrs_budget,
293        NULL::numeric AS hrs_actual,
294        NULL::numeric AS hrs_balance,
295        NULL::numeric AS exp_budget,
296        NULL::numeric AS exp_actual,
297        NULL::numeric AS exp_balance,
298         
299        NULL::text AS due_qtforegroundrole,
300        2 AS xtindentrole
301   FROM quhead
302   JOIN custinfo ON (quhead_cust_id=cust_id)
303 <? if exists("owner_username") ?>
304     JOIN prj ON (prj_id=quhead_prj_id
305             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
306 <? endif ?>
307     JOIN quitem ON (quitem_quhead_id = quhead_id)
308  WHERE (quhead_prj_id = <? value("prj_id") ?>)
309 <? if exists("owner_username") ?>
310    AND (quhead_owner_username=<? value("owner_username") ?>)
311 <? endif ?>
312 GROUP BY quhead_id, quhead_number, quhead_status, quhead_expire, quhead_freight, quhead_misc, custinfo.cust_name
313
314 UNION ALL
315
316 SELECT quitem_id AS id, 
317        17 AS type,
318        quhead_number AS subtype,
319        2 AS section,
320        <? value("quotes") ?> AS section_qtdisplayrole,
321        quitem_linenumber::text AS name, 
322        CASE WHEN (quhead_status = 'C') THEN 
323          <? value("converted") ?>
324             WHEN (quhead_status = 'X') THEN
325          <? value("canceled") ?>
326             WHEN (COALESCE(quhead_expire, current_date + 1) > current_date) THEN
327          <? value("open") ?>
328             ELSE
329          <? value("Expired") ?>
330        END AS status,
331        NULL::text AS project_type,
332        item_number AS item,
333        item_descrip1 || ' ' || item_descrip2 AS descrip,
334        NULL::text AS customer,
335        NULL::text AS contact,
336        NULL::text AS city,
337        NULL::text AS state,       
338        quitem_qtyord,
339        uom_name AS uom,
340        (quitem_qtyord * quitem_qty_invuomratio) * (quitem_price / quitem_price_invuomratio) AS value,
341        NULL::date AS due,
342        NULL::date AS assigned,
343        NULL::date AS started,
344        NULL::date AS completed,
345        NULL::numeric AS hrs_budget,
346        NULL::numeric AS hrs_actual,
347        NULL::numeric AS hrs_balance,
348        NULL::numeric AS exp_budget,
349        NULL::numeric AS exp_actual,
350        NULL::numeric AS exp_balance,
351         
352        NULL::text AS due_qtforegroundrole,
353        3 AS xtindentrole
354   FROM quhead
355 <? if exists("owner_username") ?>
356     JOIN prj ON (prj_id=quhead_prj_id
357             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
358 <? endif ?>
359     JOIN quitem ON (quitem_quhead_id = quhead_id)
360     JOIN uom ON (quitem_qty_uom_id = uom_id)
361     JOIN itemsite ON (quitem_itemsite_id = itemsite_id)
362     JOIN item ON (itemsite_item_id = item_id)
363  WHERE (quhead_prj_id = <? value("prj_id") ?>)
364 <? if exists("owner_username") ?>
365    AND (quhead_owner_username=<? value("owner_username") ?>)
366 <? endif ?>
367
368 UNION ALL
369
370 SELECT quhead_id AS id, 
371        18 AS type,
372        quhead_number AS subtype,
373        2 AS section,
374        <? value("quotes") ?> AS section_qtdisplayrole,
375        <? value("total") ?> AS name,
376        NULL AS status,
377        NULL::text AS project_type,
378        NULL::text AS item,
379        NULL::text AS descrip,
380        NULL::text AS customer,
381        NULL::text AS contact,
382        NULL::text AS city,
383        NULL::text AS state,       
384        NULL AS qty,
385        NULL::text AS uom,
386        SUM((quitem_qtyord * quitem_qty_invuomratio) * (quitem_price / quitem_price_invuomratio)) AS value,
387        NULL::date AS due,
388        NULL::date AS assigned,
389        NULL::date AS started,
390        NULL::date AS completed,
391        NULL::numeric AS hrs_budget,
392        NULL::numeric AS hrs_actual,
393        NULL::numeric AS hrs_balance,
394        NULL::numeric AS exp_budget,
395        NULL::numeric AS exp_actual,
396        NULL::numeric AS exp_balance,
397         
398         NULL::text AS due_qtforegroundrole,
399        2 AS xtindentrole
400   FROM quhead
401 <? if exists("owner_username") ?>
402     JOIN prj ON (prj_id=quhead_prj_id
403             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
404 <? endif ?>
405     JOIN quitem ON (quitem_quhead_id = quhead_id)
406  WHERE (quhead_prj_id = <? value("prj_id") ?>)
407 <? if exists("owner_username") ?>
408    AND (quhead_owner_username=<? value("owner_username") ?>)
409 <? endif ?>
410 GROUP BY quhead_id, quhead_number
411
412 UNION ALL
413
414 SELECT -1 AS id, 
415        19 AS type,
416        MAX(quhead_number) AS subtype,
417        2 AS section,
418        <? value("quotes") ?> AS section_qtdisplayrole,
419        <? value("total") ?> || ' ' || <? value("quotes") ?> AS name,
420        NULL AS status,
421        NULL::text AS project_type,
422        NULL::text AS item,
423        NULL::text AS descrip,
424        NULL::text AS customer,
425        NULL::text AS contact,
426        NULL::text AS city,
427        NULL::text AS state,       
428        NULL AS qty,
429        NULL::text AS uom,
430        SUM((quitem_qtyord * quitem_qty_invuomratio) * (quitem_price / quitem_price_invuomratio)) AS value,
431        NULL::date AS due,
432        NULL::date AS assigned,
433        NULL::date AS started,
434        NULL::date AS completed,
435        NULL::numeric AS hrs_budget,
436        NULL::numeric AS hrs_actual,
437        NULL::numeric AS hrs_balance,
438        NULL::numeric AS exp_budget,
439        NULL::numeric AS exp_actual,
440        NULL::numeric AS exp_balance,
441         
442         NULL::text AS due_qtforegroundrole,
443        2 AS xtindentrole
444   FROM quhead
445 <? if exists("owner_username") ?>
446     JOIN prj ON (prj_id=quhead_prj_id
447             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
448 <? endif ?>
449     JOIN quitem ON (quitem_quhead_id = quhead_id)
450  WHERE (quhead_prj_id = <? value("prj_id") ?>)
451 <? if exists("owner_username") ?>
452    AND (quhead_owner_username=<? value("owner_username") ?>)
453 <? endif ?>
454
455 UNION ALL
456
457 ------ SALES ORDERS ------
458 SELECT DISTINCT -1 AS id, 
459        20 AS type,
460        '0' AS subtype,
461        3 AS section,
462        <? value("sos") ?> AS section_qtdisplayrole,
463        <? value("sos") ?> AS name,
464        NULL::text AS status,
465        NULL::text AS project_type,
466        NULL::text AS item,
467        NULL::text AS descrip,
468        NULL::text AS customer,
469        NULL::text AS contact,
470        NULL::text AS city,
471        NULL::text AS state,       
472        NULL::numeric AS qty,
473        NULL::text AS uom,
474        NULL::numeric AS value,
475        NULL::date AS due,
476        NULL::date AS assigned,
477        NULL::date AS started,
478        NULL::date AS completed,
479        NULL::numeric AS hrs_budget,
480        NULL::numeric AS hrs_actual,
481        NULL::numeric AS hrs_balance,
482        NULL::numeric AS exp_budget,
483        NULL::numeric AS exp_actual,
484        NULL::numeric AS exp_balance,
485         
486         NULL::text AS due_qtforegroundrole,
487        1 AS xtindentrole
488   FROM cohead
489 <? if exists("owner_username") ?>
490     JOIN prj ON (prj_id=cohead_prj_id
491             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
492 <? endif ?>
493  WHERE (cohead_prj_id = <? value("prj_id") ?>)
494
495 UNION ALL
496
497 SELECT cohead_id AS id, 
498        25 AS type,
499        cohead_number::text AS subtype,
500        3 AS section,
501        <? value("sos") ?> AS section_qtdisplayrole,
502        cohead_number::text AS name,
503       COALESCE((SELECT 
504                   CASE WHEN (coitem_status = 'O') THEN
505                     <? value("open") ?>
506                        WHEN (coitem_status = 'C') THEN
507                     <? value("closed" ?>
508                       ELSE
509                     <? value("canceled") ?>
510                   END
511                 FROM 
512                (SELECT coitem_status,
513                    CASE 
514                      WHEN (coitem_status = 'O') THEN 1
515                      WHEN (coitem_status = 'C') then 2
516                      ELSE  3
517                   END AS type
518                   FROM coitem
519                  WHERE (coitem_cohead_id=cohead_id)
520                  ORDER BY type
521                  LIMIT 1) AS sts) ,'O')
522         AS status,
523        NULL::text AS project_type,
524        shipto_num AS item,
525        shipto_name AS descrip,
526        cust_name AS customer,
527        NULL::text AS contact,
528        NULL::text AS city,
529        NULL::text AS state,       
530        NULL AS qty,
531        NULL::text AS uom,
532        NULL AS value,
533        NULL::date AS due,
534        NULL::date AS assigned,
535        NULL::date AS started,
536        NULL::date AS completed,
537        NULL::numeric AS hrs_budget,
538        NULL::numeric AS hrs_actual,
539        NULL::numeric AS hrs_balance,
540        NULL::numeric AS exp_budget,
541        NULL::numeric AS exp_actual,
542        NULL::numeric AS exp_balance,
543         
544         NULL::text AS due_qtforegroundrole,
545        2 AS xtindentrole
546   FROM cohead
547     JOIN custinfo ON (cust_id = cohead_cust_id)
548     LEFT OUTER JOIN shiptoinfo ON (cohead_shipto_id = shipto_id)
549 <? if exists("owner_username") ?>
550     JOIN prj ON (prj_id=cohead_prj_id
551             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
552 <? endif ?>
553     JOIN coitem ON (coitem_cohead_id = cohead_id)
554  WHERE (cohead_prj_id = <? value("prj_id") ?>)
555 GROUP BY cohead_id, cohead_number, shipto_num, shipto_name, cust_name
556
557 UNION ALL
558
559 SELECT coitem_id AS id, 
560        27 AS type,
561        cohead_number::text AS subtype,
562        3 AS section,
563        <? value("sos") ?> AS section_qtdisplayrole,
564        coitem_linenumber::text AS name, 
565        CASE WHEN (coitem_status = 'O') THEN
566          <? value("open") ?>
567             WHEN (coitem_status = 'C') THEN
568          <? value("closed") ?>
569             WHEN (coitem_status = 'X') THEN
570          <? value("canceled") ?>
571        END AS status,
572        NULL::text AS project_type,
573        item_number AS item,
574        item_descrip1 || ' ' || item_descrip2 AS descrip,
575        NULL::text AS customer,
576        NULL::text AS contact,
577        NULL::text AS city,
578        NULL::text AS state,       
579        coitem_qtyord,
580        uom_name AS uom,
581        (coitem_qtyord * coitem_qty_invuomratio) * (coitem_price / coitem_price_invuomratio) AS value,
582        NULL::date AS due,
583        NULL::date AS assigned,
584        NULL::date AS started,
585        NULL::date AS completed,
586        NULL::numeric AS hrs_budget,
587        NULL::numeric AS hrs_actual,
588        NULL::numeric AS hrs_balance,
589        NULL::numeric AS exp_budget,
590        NULL::numeric AS exp_actual,
591        NULL::numeric AS exp_balance,
592         
593         NULL::text AS due_qtforegroundrole,
594        3 AS xtindentrole
595   FROM cohead
596 <? if exists("owner_username") ?>
597     JOIN prj ON (prj_id=cohead_prj_id
598             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
599 <? endif ?>
600     JOIN coitem ON (coitem_cohead_id = cohead_id)
601     JOIN uom ON (coitem_qty_uom_id = uom_id)
602     JOIN itemsite ON (coitem_itemsite_id = itemsite_id)
603     JOIN item ON (itemsite_item_id = item_id)
604  WHERE (cohead_prj_id = <? value("prj_id") ?>)
605
606 UNION ALL
607
608 SELECT cohead_id AS id, 
609        28 AS type,
610        cohead_number::text AS subtype,
611        3 AS section,
612        <? value("sos") ?> AS section_qtdisplayrole,
613        <? value("total") ?> AS name,
614        NULL AS status,
615        NULL::text AS project_type,
616        NULL::text AS item,
617        NULL::text AS descrip,
618        NULL::text AS customer,
619        NULL::text AS contact,
620        NULL::text AS city,
621        NULL::text AS state,       
622        NULL AS qty,
623        NULL::text AS uom,
624        SUM((coitem_qtyord * coitem_qty_invuomratio) * (coitem_price / coitem_price_invuomratio)) AS value,
625        NULL::date AS due,
626        NULL::date AS assigned,
627        NULL::date AS started,
628        NULL::date AS completed,
629        NULL::numeric AS hrs_budget,
630        NULL::numeric AS hrs_actual,
631        NULL::numeric AS hrs_balance,
632        NULL::numeric AS exp_budget,
633        NULL::numeric AS exp_actual,
634        NULL::numeric AS exp_balance,
635         
636         NULL::text AS due_qtforegroundrole,
637        2 AS xtindentrole
638   FROM cohead
639 <? if exists("owner_username") ?>
640     JOIN prj ON (prj_id=cohead_prj_id
641             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
642 <? endif ?>
643     JOIN coitem ON (coitem_cohead_id = cohead_id)
644  WHERE (cohead_prj_id = <? value("prj_id") ?>)
645 GROUP BY cohead_id, cohead_number
646
647 UNION ALL
648
649 SELECT -1 AS id, 
650        29 AS type,
651        MAX(cohead_number::text) AS subtype,
652        3 AS section,
653        <? value("sos") ?> AS section_qtdisplayrole,
654        <? value("total") ?> || ' ' || <? value("sos") ?> AS name,
655        NULL AS status,
656        NULL::text AS project_type,
657        NULL::text AS item,
658        NULL::text AS descrip,
659        NULL::text AS customer,
660        NULL::text AS contact,
661        NULL::text AS city,
662        NULL::text AS state,       
663        NULL AS qty,
664        NULL::text AS uom,
665        SUM((coitem_qtyord * coitem_qty_invuomratio) * (coitem_price / coitem_price_invuomratio)) AS value,
666        NULL::date AS due,
667        NULL::date AS assigned,
668        NULL::date AS started,
669        NULL::date AS completed,
670        NULL::numeric AS hrs_budget,
671        NULL::numeric AS hrs_actual,
672        NULL::numeric AS hrs_balance,
673        NULL::numeric AS exp_budget,
674        NULL::numeric AS exp_actual,
675        NULL::numeric AS exp_balance,
676                 
677         NULL::text AS due_qtforegroundrole,
678        2 AS xtindentrole
679   FROM cohead
680 <? if exists("owner_username") ?>
681     JOIN prj ON (prj_id=cohead_prj_id
682             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
683 <? endif ?>
684     JOIN coitem ON (coitem_cohead_id = cohead_id)
685  WHERE (cohead_prj_id = <? value("prj_id") ?>)
686
687 UNION ALL
688
689 ------ INVOICES -------
690 SELECT DISTINCT -1 AS id, 
691        30 AS type,
692        '0' AS subtype,
693        4 AS section,
694        <? value("invoices") ?> AS section_qtdisplayrole,
695        <? value("invoices") ?> AS name,
696        NULL::text AS status,
697        NULL::text AS project_type,
698        NULL::text AS item,
699        NULL::text AS descrip,
700        NULL::text AS customer,
701        NULL::text AS contact,
702        NULL::text AS city,
703        NULL::text AS state,       
704        NULL::numeric AS qty,
705        NULL::text AS uom,
706        NULL::numeric AS value,
707        NULL::date AS due,
708        NULL::date AS assigned,
709        NULL::date AS started,
710        NULL::date AS completed,
711        NULL::numeric AS hrs_budget,
712        NULL::numeric AS hrs_actual,
713        NULL::numeric AS hrs_balance,
714        NULL::numeric AS exp_budget,
715        NULL::numeric AS exp_actual,
716        NULL::numeric AS exp_balance,
717         
718         NULL::text AS due_qtforegroundrole,
719        1 AS xtindentrole
720   FROM invchead
721 <? if exists("owner_username") ?>
722     JOIN prj ON (prj_id=invchead_prj_id
723             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
724 <? endif ?>
725  WHERE (invchead_prj_id = <? value("prj_id") ?>)
726
727 UNION ALL
728
729 SELECT invchead_id AS id,
730        35 AS type,
731        invchead_invcnumber::text AS subtype,
732        4 AS section,
733        <? value("invoices") ?> AS section_qtdisplayrole,
734        invchead_invcnumber::text AS name,
735        CASE WHEN (invchead_posted) THEN
736          <? value("posted") ?>
737        ELSE <? value("unposted") ?>
738        END AS status,
739        NULL::text AS project_type,
740        shipto_num AS item,
741        shipto_name AS descrip,
742        cust_name AS customer,
743        NULL::text AS contact,
744        NULL::text AS city,
745        NULL::text AS state,       
746        NULL AS qty,
747        NULL::text AS uom,
748        NULL AS value,
749        NULL::date AS due,
750        NULL::date AS assigned,
751        NULL::date AS started,
752        NULL::date AS completed,
753        NULL::numeric AS hrs_budget,
754        NULL::numeric AS hrs_actual,
755        NULL::numeric AS hrs_balance,
756        NULL::numeric AS exp_budget,
757        NULL::numeric AS exp_actual,
758        NULL::numeric AS exp_balance,
759         
760         NULL::text AS due_qtforegroundrole,
761        2 AS xtindentrole
762   FROM invchead
763     JOIN custinfo ON (cust_id=invchead_cust_id)
764     LEFT OUTER JOIN shiptoinfo ON (invchead_shipto_id = shipto_id)
765 <? if exists("owner_username") ?>
766     JOIN prj ON (prj_id=invchead_prj_id
767             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
768 <? endif ?>
769     JOIN invcitem ON (invcitem_invchead_id = invchead_id)
770  WHERE (invchead_prj_id = <? value("prj_id") ?>)
771 GROUP BY invchead_id, invchead_invcnumber, shipto_num, shipto_name, invchead_freight, invchead_misc_amount, invchead_posted, cust_name
772
773 UNION ALL
774
775 SELECT invcitem_id AS id, 
776        37 AS type,
777        invchead_invcnumber::text AS subtype,
778        4 AS section,
779        <? value("invoices") ?> AS section_qtdisplayrole,
780        invcitem_linenumber::text AS name, 
781        CASE WHEN (invchead_posted) THEN
782          <? value("posted") ?>
783        ELSE <? value("unposted") ?>
784        END AS status,
785        NULL::text AS project_type,
786        COALESCE(item_number,invcitem_number) AS item,
787        COALESCE(item_descrip1 || ' ' || item_descrip2,invcitem_descrip) AS descrip,
788        NULL::text AS customer,
789        NULL::text AS contact,
790        NULL::text AS city,
791        NULL::text AS state,       
792        invcitem_billed AS qty,
793        uom_name AS uom,
794        (invcitem_billed * invcitem_qty_invuomratio) * (invcitem_price / invcitem_price_invuomratio) AS value,
795        NULL::date AS due,
796        NULL::date AS assigned,
797        NULL::date AS started,
798        NULL::date AS completed,
799        NULL::numeric AS hrs_budget,
800        NULL::numeric AS hrs_actual,
801        NULL::numeric AS hrs_balance,
802        NULL::numeric AS exp_budget,
803        NULL::numeric AS exp_actual,
804        NULL::numeric AS exp_balance,
805         
806         NULL::text AS due_qtforegroundrole,
807        3 AS xtindentrole
808   FROM invchead
809 <? if exists("owner_username") ?>
810     JOIN prj ON (prj_id=invchead_prj_id
811             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
812 <? endif ?>
813     JOIN invcitem ON (invcitem_invchead_id = invchead_id)
814     LEFT OUTER JOIN item ON (invcitem_item_id = item_id)
815     LEFT OUTER JOIN uom ON (invcitem_qty_uom_id = uom_id)
816  WHERE (invchead_prj_id = <? value("prj_id") ?>)
817
818 UNION ALL
819
820 SELECT invchead_id AS id, 
821        38 AS type,
822        invchead_invcnumber::text AS subtype,
823        4 AS section,
824        <? value("invoices") ?> AS section_qtdisplayrole,
825        <? value("total") ?> AS name,
826        NULL AS status,
827        NULL::text AS project_type,
828        NULL::text AS item,
829        NULL::text AS descrip,
830        NULL::text AS customer,
831        NULL::text AS contact,
832        NULL::text AS city,
833        NULL::text AS state,       
834        NULL AS qty,
835        NULL::text AS uom,
836        SUM((invcitem_billed * invcitem_qty_invuomratio) * (invcitem_price / invcitem_price_invuomratio)) AS value,
837        NULL::date AS due,
838        NULL::date AS assigned,
839        NULL::date AS started,
840        NULL::date AS completed,
841        NULL::numeric AS hrs_budget,
842        NULL::numeric AS hrs_actual,
843        NULL::numeric AS hrs_balance,
844        NULL::numeric AS exp_budget,
845        NULL::numeric AS exp_actual,
846        NULL::numeric AS exp_balance,
847        
848         NULL::text AS due_qtforegroundrole,
849        2 AS xtindentrole
850   FROM invchead
851 <? if exists("owner_username") ?>
852     JOIN prj ON (prj_id=invchead_prj_id
853             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
854 <? endif ?>
855     JOIN invcitem ON (invcitem_invchead_id = invchead_id)
856  WHERE (invchead_prj_id = <? value("prj_id") ?>)
857 GROUP BY invchead_id, invchead_invcnumber
858
859 UNION ALL
860
861 SELECT -1 AS id, 
862        39 AS type,
863        MAX(invchead_invcnumber::text) AS subtype,
864        4 AS section,
865        <? value("invoices") ?> AS section_qtdisplayrole,
866        <? value("total") ?> || ' ' || <? value("invoices") ?> AS name,
867        NULL AS status,
868        NULL::text AS project_type,
869        NULL::text AS item,
870        NULL::text AS descrip,
871        NULL::text AS customer,
872        NULL::text AS contact,
873        NULL::text AS city,
874        NULL::text AS state,       
875        NULL AS qty,
876        NULL::text AS uom,
877        SUM((invcitem_billed * invcitem_qty_invuomratio) * (invcitem_price / invcitem_price_invuomratio)) AS value,
878        NULL::date AS due,
879        NULL::date AS assigned,
880        NULL::date AS started,
881        NULL::date AS completed,
882        NULL::numeric AS hrs_budget,
883        NULL::numeric AS hrs_actual,
884        NULL::numeric AS hrs_balance,
885        NULL::numeric AS exp_budget,
886        NULL::numeric AS exp_actual,
887        NULL::numeric AS exp_balance,
888         
889         NULL::text AS due_qtforegroundrole,
890        2 AS xtindentrole
891   FROM invchead
892 <? if exists("owner_username") ?>
893     JOIN prj ON (prj_id=invchead_prj_id
894             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
895 <? endif ?>
896     JOIN invcitem ON (invcitem_invchead_id = invchead_id)
897  WHERE (invchead_prj_id = <? value("prj_id") ?>)
898
899 <? endif ?>
900
901
902 <? if exists("showWo") ?>
903 UNION ALL
904
905 ------ WORK ORDERS -------
906 SELECT DISTINCT -1 AS id, 
907        40 AS type,
908        '0' AS subtype,
909        5 AS section,
910        <? value("wos") ?> AS section_qtdisplayrole,
911        <? value("wos") ?> AS name,
912        NULL::text AS status,
913        NULL::text AS project_type,
914        NULL::text AS item,
915        NULL::text AS descrip,
916        NULL::text AS customer,
917        NULL::text AS contact,
918        NULL::text AS city,
919        NULL::text AS state,       
920        NULL::numeric AS qty,
921        NULL::text AS uom,
922        NULL::numeric AS value,
923        NULL::date AS due,
924        NULL::date AS assigned,
925        NULL::date AS started,
926        NULL::date AS completed,
927        NULL::numeric AS hrs_budget,
928        NULL::numeric AS hrs_actual,
929        NULL::numeric AS hrs_balance,
930        NULL::numeric AS exp_budget,
931        NULL::numeric AS exp_actual,
932        NULL::numeric AS exp_balance,
933         
934         NULL::text AS due_qtforegroundrole,
935        1 AS xtindentrole
936   FROM wo
937 <? if exists("owner_username") ?>
938     JOIN prj ON (prj_id=wo_prj_id
939             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
940 <? endif ?>
941  WHERE (wo_prj_id = <? value("prj_id") ?>)
942
943 UNION ALL
944
945 SELECT wo_id AS id, 
946        45 AS type,
947        formatWoNumber(wo_id) AS subtype,
948        5 AS section,
949        <? value("wos") ?> AS section_qtdisplayrole,
950        formatWoNumber(wo_id) AS name,
951        CASE WHEN (wo_status = 'O') THEN
952          <? value("open") ?>
953             WHEN (wo_status = 'E') THEN
954          <? value("exploded") ?>
955             WHEN (wo_status = 'R') THEN
956          <? value("released") ?>
957             WHEN (wo_status = 'I') THEN
958          <? value("inprocess") ?>
959             WHEN (wo_status = 'C') THEN
960          <? value("closed") ?>
961        END AS status,
962        NULL::text AS project_type,
963        item_number AS item,
964        item_descrip1 || ' ' || item_descrip2 AS descrip,
965        NULL::text AS customer,
966        NULL::text AS contact,
967        NULL::text AS city,
968        NULL::text AS state,       
969        wo_qtyord AS qty,
970        uom_name AS uom,
971        wo_postedvalue AS value,
972        NULL::date AS due,
973        NULL::date AS assigned,
974        NULL::date AS started,
975        NULL::date AS completed,
976        NULL::numeric AS hrs_budget,
977        NULL::numeric AS hrs_actual,
978        NULL::numeric AS hrs_balance,
979        NULL::numeric AS exp_budget,
980        NULL::numeric AS exp_actual,
981        NULL::numeric AS exp_balance,
982        
983         NULL::text AS due_qtforegroundrole,
984        2 AS xtindentrole
985   FROM wo
986 <? if exists("owner_username") ?>
987     JOIN prj ON (prj_id=wo_prj_id
988             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
989 <? endif ?>
990     JOIN itemsite ON (itemsite_id=wo_itemsite_id)
991     JOIN item ON (itemsite_item_id=item_id)
992     JOIN uom ON (item_inv_uom_id=uom_id)
993  WHERE (wo_prj_id = <? value("prj_id") ?>)
994
995 UNION ALL
996
997 SELECT -1 AS id, 
998        49 AS type,
999        MAX(formatWoNumber(wo_id)) AS subtype,
1000        5 AS section,
1001        <? value("wos") ?> AS section_qtdisplayrole,
1002        <? value("total") ?> || ' ' || <? value("wos") ?> AS name,
1003        NULL AS status,
1004        NULL::text AS project_type,
1005        NULL::text AS item,
1006        NULL::text AS descrip,
1007        NULL::text AS customer,
1008        NULL::text AS contact,
1009        NULL::text AS city,
1010        NULL::text AS state,       
1011        NULL AS qty,
1012        NULL::text AS uom,
1013        SUM(wo_postedvalue) AS value,
1014        NULL::date AS due,
1015        NULL::date AS assigned,
1016        NULL::date AS started,
1017        NULL::date AS completed,
1018        NULL::numeric AS hrs_budget,
1019        NULL::numeric AS hrs_actual,
1020        NULL::numeric AS hrs_balance,
1021        NULL::numeric AS exp_budget,
1022        NULL::numeric AS exp_actual,
1023        NULL::numeric AS exp_balance,
1024         
1025         NULL::text AS due_qtforegroundrole,
1026        2 AS xtindentrole
1027   FROM wo
1028 <? if exists("owner_username") ?>
1029     JOIN prj ON (prj_id=wo_prj_id
1030             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1031 <? endif ?>
1032  WHERE (wo_prj_id = <? value("prj_id") ?>)
1033
1034 <? endif ?>
1035
1036
1037 <? if exists("showPo") ?>
1038  UNION ALL
1039
1040 ------ PURCHASE REQUESTS ------
1041 SELECT DISTINCT -1 AS id, 
1042        50 AS type,
1043        '0' AS subtype,
1044        6 AS section,
1045        <? value("prs") ?> AS section_qtdisplayrole,
1046        <? value("prs") ?> AS name,
1047        NULL::text AS status,
1048        NULL::text AS project_type,
1049        NULL::text AS item,
1050        NULL::text AS descrip,
1051        NULL::text AS customer,
1052        NULL::text AS contact,
1053        NULL::text AS city,
1054        NULL::text AS state,       
1055        NULL::numeric AS qty,
1056        NULL::text AS uom,
1057        NULL::numeric AS value,
1058        NULL::date AS due,
1059        NULL::date AS assigned,
1060        NULL::date AS started,
1061        NULL::date AS completed,
1062        NULL::numeric AS hrs_budget,
1063        NULL::numeric AS hrs_actual,
1064        NULL::numeric AS hrs_balance,
1065        NULL::numeric AS exp_budget,
1066        NULL::numeric AS exp_actual,
1067        NULL::numeric AS exp_balance,
1068         
1069         NULL::text AS due_qtforegroundrole,
1070        1 AS xtindentrole
1071   FROM pr
1072 <? if exists("owner_username") ?>
1073     JOIN prj ON (prj_id=pr_prj_id
1074             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1075 <? endif ?>
1076  WHERE (pr_prj_id = <? value("prj_id") ?>)
1077
1078 UNION ALL
1079
1080 SELECT pr_id AS id, 
1081        55 AS type,
1082        pr_number::text || '-' || pr_subnumber::text AS subtype,
1083        6 AS section,
1084        <? value("prs") ?> AS section_qtdisplayrole,
1085        pr_number::text || '-' || pr_subnumber::text AS name,
1086        <? value("open") ?> AS status, 
1087        NULL::text AS project_type,
1088        item_number AS item,
1089        (item_descrip1 || ' ' || item_descrip2) AS descrip,
1090        NULL::text AS customer,
1091        NULL::text AS contact,
1092        NULL::text AS city,
1093        NULL::text AS state,       
1094        pr_qtyreq AS qty,
1095        uom_name AS uom,
1096        stdcost(item_id) * pr_qtyreq AS value,
1097        NULL::date AS due,
1098        NULL::date AS assigned,
1099        NULL::date AS started,
1100        NULL::date AS completed,
1101        NULL::numeric AS hrs_budget,
1102        NULL::numeric AS hrs_actual,
1103        NULL::numeric AS hrs_balance,
1104        NULL::numeric AS exp_budget,
1105        NULL::numeric AS exp_actual,
1106        NULL::numeric AS exp_balance,
1107         
1108         NULL::text AS due_qtforegroundrole,
1109        2 AS xtindentrole
1110   FROM pr
1111 <? if exists("owner_username") ?>
1112     JOIN prj ON (prj_id=pr_prj_id
1113             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1114 <? endif ?>
1115     JOIN itemsite ON (itemsite_id = pr_itemsite_id)
1116     JOIN item ON (itemsite_item_id = item_id)
1117     JOIN uom ON (item_inv_uom_id = uom_id)
1118  WHERE (pr_prj_id=<? value("prj_id") ?>)
1119
1120 UNION ALL
1121
1122 SELECT -1 AS id, 
1123        59 AS type,
1124        MAX(pr_number::text || '-' || pr_subnumber::text) AS subtype,
1125        6 AS section,
1126        <? value("prs") ?> AS section_qtdisplayrole,
1127        <? value("total") ?> || ' ' || <? value("prs") ?> AS name,
1128        NULL AS status,
1129        NULL::text AS project_type,
1130        NULL::text AS item,
1131        NULL::text AS descrip,
1132        NULL::text AS customer,       
1133        NULL::text AS contact,
1134        NULL::text AS city,
1135        NULL::text AS state,       
1136        NULL AS qty,
1137        NULL::text AS uom,
1138        SUM(stdcost(item_id) * pr_qtyreq) AS value,
1139        NULL::date AS due,
1140        NULL::date AS assigned,
1141        NULL::date AS started,
1142        NULL::date AS completed,
1143        NULL::numeric AS hrs_budget,
1144        NULL::numeric AS hrs_actual,
1145        NULL::numeric AS hrs_balance,
1146        NULL::numeric AS exp_budget,
1147        NULL::numeric AS exp_actual,
1148        NULL::numeric AS exp_balance,
1149         
1150         NULL::text AS due_qtforegroundrole,
1151        2 AS xtindentrole
1152   FROM pr
1153 <? if exists("owner_username") ?>
1154     JOIN prj ON (prj_id=pr_prj_id
1155             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1156 <? endif ?>
1157     JOIN itemsite ON (itemsite_id = pr_itemsite_id)
1158     JOIN item ON (itemsite_item_id = item_id)
1159  WHERE (pr_prj_id = <? value("prj_id") ?>)
1160
1161 UNION ALL
1162
1163 ------ PURCHASE ORDERS ------
1164 SELECT DISTINCT -1 AS id, 
1165        60 AS type,
1166        '0' AS subtype,
1167        7 AS section,
1168        <? value("pos") ?> AS section_qtdisplayrole,
1169        <? value("pos") ?> AS name,
1170        NULL::text AS status,
1171        NULL::text AS project_type,
1172        NULL::text AS item,
1173        NULL::text AS descrip,
1174        NULL::text AS customer,
1175        NULL::text AS contact,
1176        NULL::text AS city,
1177        NULL::text AS state,       
1178        NULL::numeric AS qty,
1179        NULL::text AS uom,
1180        NULL::numeric AS value,
1181        NULL::date AS due,
1182        NULL::date AS assigned,
1183        NULL::date AS started,
1184        NULL::date AS completed,
1185        NULL::numeric AS hrs_budget,
1186        NULL::numeric AS hrs_actual,
1187        NULL::numeric AS hrs_balance,
1188        NULL::numeric AS exp_budget,
1189        NULL::numeric AS exp_actual,
1190        NULL::numeric AS exp_balance,
1191         
1192         NULL::text AS due_qtforegroundrole,
1193        1 AS xtindentrole
1194   FROM poitem
1195 <? if exists("owner_username") ?>
1196     JOIN prj ON (prj_id=poitem_prj_id
1197             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1198 <? endif ?>
1199  WHERE (poitem_prj_id = <? value("prj_id") ?>)
1200
1201 UNION ALL
1202
1203 SELECT pohead_id AS id,
1204        65 AS type,
1205        pohead_number::text AS subtype,
1206        7 AS section,
1207        <? value("pos") ?> AS section_qtdisplayrole,
1208        pohead_number::text AS name,
1209        CASE WHEN (pohead_status = 'U') THEN
1210          <? value("unreleased") ?>
1211             WHEN (pohead_status = 'O') THEN
1212          <? value("open") ?>
1213             WHEN (pohead_status = 'C') THEN
1214          <? value("closed") ?>
1215        END AS status,
1216        NULL::text AS project_type,
1217        NULL AS item,
1218        NULL::text AS descrip,
1219        NULL::text AS customer,
1220        NULL::text AS contact,
1221        NULL::text AS city,
1222        NULL::text AS state,       
1223        NULL AS qty,
1224        NULL AS uom,
1225        NULL AS value,
1226        NULL::date AS due,
1227        NULL::date AS assigned,
1228        NULL::date AS started,
1229        NULL::date AS completed,
1230        NULL::numeric AS hrs_budget,
1231        NULL::numeric AS hrs_actual,
1232        NULL::numeric AS hrs_balance,
1233        NULL::numeric AS exp_budget,
1234        NULL::numeric AS exp_actual,
1235        NULL::numeric AS exp_balance,
1236         
1237         NULL::text AS due_qtforegroundrole,
1238        2 AS xtindentrole
1239   FROM pohead
1240     JOIN poitem ON (poitem_pohead_id = pohead_id)
1241 <? if exists("owner_username") ?>
1242     JOIN prj ON (prj_id=poitem_prj_id
1243             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1244 <? endif ?>
1245  WHERE (poitem_prj_id = <? value("prj_id") ?>)
1246 GROUP BY pohead_id, pohead_number, pohead_freight, pohead_status
1247
1248 UNION ALL
1249
1250 SELECT poitem_id AS id, 
1251        67 AS type,
1252        pohead_number::text AS subtype,
1253        7 AS section,
1254        <? value("pos") ?> AS section_qtdisplayrole,
1255        poitem_linenumber::text AS name, 
1256        CASE WHEN (poitem_status = 'U') THEN
1257          <? value("unreleased") ?>
1258             WHEN (poitem_status = 'O') THEN
1259          <? value("open") ?>
1260             WHEN (poitem_status = 'C') THEN
1261          <? value("closed") ?>
1262        END AS status,
1263        NULL::text AS project_type,
1264        COALESCE(item_number,poitem_vend_item_number) AS item,
1265        COALESCE((item_descrip1 || ' ' || item_descrip2),poitem_vend_item_descrip) AS descrip,
1266        NULL::text AS customer,
1267        NULL::text AS contact,
1268        NULL::text AS city,
1269        NULL::text AS state,       
1270        poitem_qty_ordered,
1271        poitem_vend_uom AS uom,
1272        (poitem_qty_ordered * poitem_unitprice) AS value,
1273        NULL::date AS due,
1274        NULL::date AS assigned,
1275        NULL::date AS started,
1276        NULL::date AS completed,
1277        NULL::numeric AS hrs_budget,
1278        NULL::numeric AS hrs_actual,
1279        NULL::numeric AS hrs_balance,
1280        NULL::numeric AS exp_budget,
1281        NULL::numeric AS exp_actual,
1282        NULL::numeric AS exp_balance,
1283         
1284         NULL::text AS due_qtforegroundrole,
1285        3 AS xtindentrole
1286   FROM pohead
1287     JOIN poitem ON (poitem_pohead_id = pohead_id)
1288 <? if exists("owner_username") ?>
1289     JOIN prj ON (prj_id=poitem_prj_id
1290             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1291 <? endif ?>
1292     LEFT OUTER JOIN itemsite ON (poitem_itemsite_id=itemsite_id)
1293     LEFT OUTER JOIN item ON (itemsite_item_id = item_id)
1294  WHERE (poitem_prj_id = <? value("prj_id") ?>)
1295
1296 UNION ALL
1297
1298 SELECT pohead_id AS id, 
1299        68 AS type,
1300        pohead_number::text AS subtype,
1301        7 AS section,
1302        <? value("pos") ?> AS section_qtdisplayrole,
1303        <? value("total") ?> AS name,
1304        NULL AS status,
1305        NULL::text AS project_type,
1306        NULL::text AS item,
1307        NULL::text AS descrip,
1308        NULL::text AS customer,
1309        NULL::text AS contact,
1310        NULL::text AS city,
1311        NULL::text AS state,       
1312        NULL AS qty,
1313        NULL::text AS uom,
1314        SUM(poitem_qty_ordered * poitem_unitprice) AS value,
1315        NULL::date AS due,
1316        NULL::date AS assigned,
1317        NULL::date AS started,
1318        NULL::date AS completed,
1319        NULL::numeric AS hrs_budget,
1320        NULL::numeric AS hrs_actual,
1321        NULL::numeric AS hrs_balance,
1322        NULL::numeric AS exp_budget,
1323        NULL::numeric AS exp_actual,
1324        NULL::numeric AS exp_balance,
1325         
1326         NULL::text AS due_qtforegroundrole,
1327        2 AS xtindentrole
1328   FROM pohead
1329     JOIN poitem ON (poitem_pohead_id = pohead_id)
1330 <? if exists("owner_username") ?>
1331     JOIN prj ON (prj_id=poitem_prj_id
1332             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1333 <? endif ?>
1334  WHERE (poitem_prj_id = <? value("prj_id") ?>)
1335 GROUP BY pohead_id, pohead_number
1336
1337 UNION ALL
1338
1339 SELECT -1 AS id, 
1340        69 AS type,
1341        MAX(pohead_number::text) AS subtype,
1342        7 AS section,
1343        <? value("sos") ?> AS section_qtdisplayrole,
1344        <? value("total") ?> || ' ' || <? value("pos") ?> AS name,
1345        NULL AS status,
1346        NULL::text AS project_type,
1347        NULL::text AS item,
1348        NULL::text AS descrip,
1349        NULL::text AS customer,
1350        NULL::text AS contact,
1351        NULL::text AS city,
1352        NULL::text AS state,       
1353        NULL AS qty,
1354        NULL::text AS uom,
1355        SUM(poitem_qty_ordered * poitem_unitprice) AS value,
1356        NULL::date AS due,
1357        NULL::date AS assigned,
1358        NULL::date AS started,
1359        NULL::date AS completed,
1360        NULL::numeric AS hrs_budget,
1361        NULL::numeric AS hrs_actual,
1362        NULL::numeric AS hrs_balance,
1363        NULL::numeric AS exp_budget,
1364        NULL::numeric AS exp_actual,
1365        NULL::numeric AS exp_balance,
1366         
1367         NULL::text AS due_qtforegroundrole,
1368        2 AS xtindentrole
1369   FROM pohead
1370     JOIN poitem ON (poitem_pohead_id = pohead_id)
1371 <? if exists("owner_username") ?>
1372     JOIN prj ON (prj_id=poitem_prj_id
1373             AND ((prj_owner_username=<? value("owner_username") ?>) OR (prj_username=<? value("owner_username") ?>)))
1374 <? endif ?>
1375  WHERE (poitem_prj_id = <? value("prj_id") ?>)
1376
1377 <? endif ?>
1378
1379 ) data
1380 ORDER BY section, subtype, type, id;