Merge pull request #1843 from xtuple/4_6_x
[xtuple] / foundation-database / public / tables / metasql / summarizedSalesHistory-detail.mql
1 -- Group: summarizedSalesHistory
2 -- Name: detail
3 -- Notes:
4 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
5 -- See www.xtuple.com/CPAL for the full text of the software license.
6
7 SELECT 1 AS dummy,
8 -- Columns added to match columns of Printed Report
9 <? foreach("groupLitList") ?>
10   <? literal("groupLitList") ?>
11   <? if not isLast("groupLitList") ?>
12   || E'\n' ||
13   <? endif ?>
14 <? endforeach ?> AS groupsLit,
15 <? foreach("groupList") ?>
16   substring(<? literal("groupList") ?> from 1 for 15)
17   <? if not isLast("groupList") ?>
18   || E'\n' ||
19   <? endif ?>
20 <? endforeach ?> AS groups,
21 <? foreach("groupDescripList") ?>
22   substring(<? literal("groupDescripList") ?> from 1 for 30)
23   <? if not isLast("groupDescripList") ?>
24   || E'\n' ||
25   <? endif ?>
26 <? endforeach ?> AS groupsDescrip,
27 -- End of columns for printed report
28        <? if exists("byCustomer") ?>
29          cust_number, cohist_cust_id AS cust_number_xtidrole, cust_name,
30        <? endif ?>
31        <? if exists("byCustomerType") ?>
32          custtype_code, custtype_id AS custtype_code_xtidrole,
33        <? endif ?>
34        <? if exists("byItem") ?>
35          item_number, item_id AS item_number_xtidrole, itemdescription,
36        <? endif ?>
37        <? if exists("bySalesRep") ?>
38          salesrep_number, salesrep_id AS salesrep_number_xtidrole, salesrep_name,
39        <? endif ?>
40        <? if exists("byShippingZone") ?>
41          shipzone_name, shipzone_id AS shipzone_name_xtidrole,
42        <? endif ?>
43        <? if exists("bySite") ?>
44          warehous_code, warehous_id AS warehous_code_xtidrole,
45        <? endif ?>
46        <? if exists("byCurrency") ?>
47          currAbbr,
48          MIN(custunitprice) AS minprice, MAX(custunitprice) AS maxprice,
49          AVG(custunitprice) AS avgprice,
50          SUM(custextprice) AS totalsales,
51          CASE WHEN (SUM(cohist_qtyshipped) = 0) THEN 0
52          ELSE SUM(custextprice) / SUM(cohist_qtyshipped)
53          END AS wtavgprice,
54        <? else ?>
55          currConcat(baseCurrId()) AS currAbbr,
56          MIN(baseunitprice) AS minprice, MAX(baseunitprice) AS maxprice,
57          AVG(baseunitprice) AS avgprice,
58          SUM(baseextprice) AS totalsales,
59          CASE WHEN (SUM(cohist_qtyshipped) = 0) THEN 0
60          ELSE SUM(baseextprice) / SUM(cohist_qtyshipped)
61          END AS wtavgprice,
62        <? endif ?>
63          SUM(cohist_qtyshipped) AS totalunits,
64          MIN(cohist_invcdate) AS firstdate,
65          MAX(cohist_invcdate) AS lastdate,
66          SUM(cohist_qtyshipped) AS qtyshipped,
67          'salesprice' AS minprice_xtnumericrole,
68          'salesprice' AS maxprice_xtnumericrole,
69          'salesprice' AS avgprice_xtnumericrole,
70          'salesprice' AS wtavgprice_xtnumericrole,
71          'qty' AS qtyshipped_xtnumericrole,
72          'curr' AS extprice_xtnumericrole,
73          'qty' AS totalunits_xtnumericrole,
74          'curr' AS totalsales_xtnumericrole,
75          'qty' AS totalunits_xtnumericrole,
76          'curr' AS totalsales_xtnumericrole,
77          0 AS totalunits_xttotalrole,
78          0 AS totalsales_xttotalrole
79 FROM saleshistorymisc
80 WHERE ( (TRUE)
81 <? if exists("startDate") ?>
82  AND (cohist_invcdate >= <? value("startDate") ?>)
83 <? endif ?>
84
85 <? if exists("endDate") ?>
86  AND (cohist_invcdate <= <? value("endDate") ?>)
87 <? endif ?>
88
89 <? if exists("shipStartDate") ?>
90  AND (cohist_shipdate >= <? value("shipStartDate") ?>)
91 <? endif ?>
92
93 <? if exists("shipEndDate") ?>
94  AND (cohist_shipdate <= <? value("shipEndDate") ?>)
95 <? endif ?>
96
97 <? if exists("warehous_id") ?>
98  AND (warehous_id=<? value("warehous_id") ?>)
99 <? endif ?>
100
101 <? if exists("item_id") ?>
102  AND (item_id=<? value("item_id") ?>)
103 <? endif ?>
104
105 <? if exists("cust_id") ?>
106  AND (cohist_cust_id=<? value("cust_id") ?>)
107 <? endif ?>
108
109 <? if exists("shipto_id") ?>
110  AND (cohist_shipto_id=<? value("shipto_id") ?>)
111 <? endif ?>
112
113 <? if exists("salesrep_id") ?>
114  AND (salesrep_id=<? value("salesrep_id") ?>)
115 <? endif ?>
116
117 <? if exists("prodcat_id") ?>
118  AND (prodcat_id=<? value("prodcat_id") ?>)
119 <? endif ?>
120
121 <? if exists("prodcat_pattern") ?>
122  AND (prodcat_code ~ <? value("prodcat_pattern") ?>)
123 <? endif ?>
124
125 <? if exists("custtype_id") ?>
126  AND (custtype_id=<? value("custtype_id") ?>)
127 <? endif ?>
128
129 <? if exists("custtype_pattern") ?>
130    AND (custtype_code ~ <? value("custtype_pattern") ?>)
131 <? endif ?>
132
133 <? if exists("custgrp_id") ?>
134   AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
135                    FROM custgrpitem
136                    WHERE (custgrpitem_custgrp_id=<? value("custgrp_id") ?>)))
137 <? endif ?>
138
139 <? if exists("custgrp_pattern") ?>
140   AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
141                    FROM custgrp, custgrpitem
142                    WHERE ( (custgrpitem_custgrp_id=custgrp_id)
143                      AND   (custgrp_name ~ <? value("custgrp_pattern") ?>) )) )
144 <? endif ?>
145
146 <? if exists("shipzone_id") ?>
147  AND (shipzone_id=<? value("shipzone_id") ?>)
148 <? endif ?>
149
150 <? if exists("curr_id") ?>
151  AND cust_curr_id = <? value("curr_id") ?>
152 <? endif ?>
153
154 <? if exists("currConcat_pattern") ?>
155  AND (currAbbr ~ <? value("currConcat_pattern") ?>)
156 <? endif ?>
157 )
158 GROUP BY dummy
159 <? if exists("bySalesRep") ?>
160  , salesrep_id, salesrep_number, salesrep_name
161 <? endif ?>
162 <? if exists("byShippingZone") ?>
163  , shipzone_id, shipzone_name, shipzone_descrip
164 <? endif ?>
165 <? if exists("byCustomer") ?>
166  , cohist_cust_id, cust_number, cust_name
167 <? endif ?>
168 <? if exists("byCustomerType") ?>
169  , custtype_id, custtype_code, custtype_descrip
170 <? endif ?>
171 <? if exists("byItem") ?>
172  , item_id, item_number, itemdescription
173 <? endif ?>
174 <? if exists("bySite") ?>
175  , warehous_id, warehous_code, warehous_descrip
176 <? endif ?>
177 <? if exists("byCurrency") ?>
178  , cust_curr_id, currAbbr
179 <? endif ?>
180  ORDER BY dummy
181 <? if exists("bySalesRep") ?>
182  , salesrep_number
183 <? endif ?>
184 <? if exists("byShippingZone") ?>
185  , shipzone_name
186 <? endif ?>
187 <? if exists("byCustomer") ?>
188  , cust_number
189 <? endif ?>
190 <? if exists("byCustomerType") ?>
191  , custtype_code
192 <? endif ?>
193 <? if exists("byItem") ?>
194  , item_number
195 <? endif ?>
196 <? if exists("bySite") ?>
197  , warehous_code
198 <? endif ?>
199 ;