5 -- Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
6 -- See www.xtuple.com/CPAL for the full text of the software license.
15 -- { 'header' : 'Brand' , 'dataIndex': 'item_brand' },
16 -- { 'header' : 'Customer Name' , 'dataIndex': 'cust_name' },
17 -- { 'header' : 'Country' , 'dataIndex': 'addr_country' },
18 -- { 'header' : 'Post Code' , 'dataIndex': 'addr_postalcode' },
19 -- { 'header' : 'State' , 'dataIndex': 'addr_state' },
20 -- { 'header' : 'City' , 'dataIndex': 'addr_city' },
21 -- { 'header' : 'Qty' , 'dataIndex': 'total_shipped' }
40 SUM(cohist_qtyshipped) as total_shipped
53 to_char(cohist_invcdate, 'Mon YYYY') as invmonth,
55 itemcharvalue(item_id, 'BRAND') as item_brand,
56 COALESCE(item_number, cohist_misc_descrip) AS item_number,
59 currConcat(cohist_curr_id) AS currAbbr,
60 CASE WHEN cohist_unitcost = 0 THEN
64 END AS cohist_unitprice,
66 CASE WHEN cohist_unitcost = 0 THEN
69 round((cohist_qtyshipped * cohist_unitprice), 2)
72 CASE WHEN cohist_unitcost = 0 THEN
75 currtobase(cohist_curr_id, cohist_unitprice, cohist_invcdate)
78 CASE WHEN cohist_unitcost = 0 THEN
81 cohist_qtyshipped * currtobase(cohist_curr_id, cohist_unitprice, cohist_invcdate)
85 (SELECT invfifo_landedunitcost * cohist_qtyshipped FROM invdetailview where
88 invfifo_cohead_id = cohead_id
90 invhist_itemsite_id = cohist_itemsite_id
92 invhist_ordtype = 'SO'
94 invfifo_cohead_id = cohead_id
95 LIMIT 1) as basefifovalue,
99 0 as coitem_linenumber,
100 0 as coitem_subnumber,
105 order_rep.salesrep_name AS order_salesrep_name,
106 cust_rep.salesrep_name AS cust_salesrep_name
114 cohead ON (cohead_number=cohist_ordernumber)
119 custinfo ON (cust_id=cohist_cust_id)
121 JOIN cntct ON cust_cntct_id = cntct_id
123 JOIN addr ON addr_id = cntct_addr_id
125 JOIN invchead ON invchead_invcnumber = cohist_invcnumber
128 JOIN salesrep order_rep ON (order_rep.salesrep_id=cohist_salesrep_id)
130 JOIN salesrep cust_rep ON (cust_rep.salesrep_id=cust_salesrep_id)
132 <? if exists("includeMisc") ?>
133 LEFT OUTER JOIN itemsite ON (itemsite_id=cohist_itemsite_id)
134 LEFT OUTER JOIN site() ON (warehous_id=itemsite_warehous_id)
135 LEFT OUTER JOIN item ON (item_id=itemsite_item_id)
137 LEFT JOIN itemsite ON (itemsite_id=cohist_itemsite_id)
138 LEFT JOIN site() ON (warehous_id=itemsite_warehous_id)
139 LEFT JOIN item ON (item_id=itemsite_item_id)
142 <? if exists("shipzone_id") ?>
143 LEFT JOIN shiptoinfo ON (shipto_id=cohist_shipto_id)
144 LEFT JOIN shipzone ON (shipzone_id=shipto_shipzone_id)
148 -- JOIN coitem ON (coitem_cohead_id = cohead_id AND coitem_itemsite_id = cohist_itemsite_id
149 -- AND cohist_pos(cohist_id, cohist_ordernumber, cohist_itemsite_id) = coitem_pos(cohead_id, coitem_id, coitem_itemsite_id))
153 -- make sure the invoices are not void..
154 AND invchead_void != true
157 <? if exists("includeMisc") ?>
158 AND (COALESCE(cohist_misc_type, '') <> 'F')
159 AND (COALESCE(cohist_misc_type, '') <> 'T')
162 <? if exists("item_id") ?>
163 AND (item_id=<? value("item_id") ?>)
166 <? if exists("cohist_salesrep_id") ?>
167 AND (cohist_salesrep_id=<? value("cohist_salesrep_id") ?>)
170 <? if exists("cust_salesrep_id") ?>
171 AND (cust_salesrep_id=<? value("cust_salesrep_id") ?>)
174 <? if exists("addr_postalcode") ?>
175 AND (addr_postalcode=<? value("addr_postalcode") ?>)
178 <? if exists("startDate") ?>
179 AND (cohist_invcdate >= <? value("startDate") ?>)
182 <? if exists("endDate") ?>
183 AND (cohist_invcdate <= <? value("endDate") ?>)
185 <? if exists("shipStartDate") ?>
186 AND (cohist_shipdate >= <? value("shipStartDate") ?>)
188 <? if exists("shipEndDate") ?>
189 AND (cohist_shipdate <= <? value("shipEndDate") ?>)
191 <? if exists("shipto_id") ?>
192 AND (cohist_shipto_id=<? value("shipto_id") ?>)
194 <? if exists("billToName") ?>
195 AND (UPPER(cohist_billtoname) ~ UPPER(<? value("billToName") ?>))
197 <? if exists("cust_id") ?>
198 AND (cohist_cust_id=<? value("cust_id") ?>)
200 <? if exists("custtype_id") ?>
201 AND (cust_custtype_id=<? value("custtype_id") ?>)
202 <? elseif exists("custtype_pattern") ?>
203 AND (cust_custtype_id IN (SELECT DISTINCT custtype_id
205 WHERE (custtype_code ~ <? value("custtype_pattern") ?>)))
207 <? if exists("by_custgrp") ?>
208 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
211 <? if exists("custgrp_id") ?>
212 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
214 WHERE (custgrpitem_custgrp_id=<? value("custgrp_id") ?>)))
216 <? if exists("custgrp_pattern") ?>
217 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
218 FROM custgrp, custgrpitem
219 WHERE ( (custgrpitem_custgrp_id=custgrp_id)
220 AND (custgrp_name ~ <? value("custgrp_pattern") ?>) )) )
223 <? if exists("item_id") ?>
224 AND (itemsite_item_id=<? value("item_id") ?>)
226 <? if exists("prodcat_id") ?>
227 AND (item_prodcat_id=<? value("prodcat_id") ?>)
229 <? if exists("prodcat_pattern") ?>
230 AND (item_prodcat_id IN (SELECT DISTINCT prodcat_id
232 WHERE (prodcat_code ~ <? value("prodcat_pattern") ?>)))
235 <? if exists("warehous_id") ?>
236 AND (itemsite_warehous_id=<? value("warehous_id") ?>)
238 <? if exists("shipzone_id") ?>
239 AND (shipzone_id=<? value("shipzone_id") ?>)
241 <? if exists("cohead_id") ?>
242 AND (cohead_id=<? value("cohead_id") ?>)
245 <? if exists("addr_country") ?>
246 AND (addr_country=<? value("addr_country") ?>)
256 cohead_number AS cohist_ordernumber,
258 coitem_scheddate AS cohist_invcdate,
259 to_char(coitem_scheddate, 'Mon YYYY') as invmonth,
260 -- (SELECT invchead_invcnumber FROM invchead WHERE invchead_ordernumber = cohead_number) AS cohist_invcnumber,
261 '' AS cohist_invcnumber,
262 itemcharvalue(item_id, 'BRAND') as item_brand,
265 coitem_qtyshipped AS cohist_qtyshipped,
266 currConcat(cohead_curr_id) AS currAbbr,
268 CASE WHEN (SELECT SUM(coitem_qtyshipped * coitem_unitcost)
269 FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber) = 0 THEN
273 ((coitem_qtyshipped * coitem_unitcost) * ((coitem_qtyshipped * coitem_unitcost) /
274 (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE
275 coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber)))
276 END AS cohist_unitprice,
278 CASE WHEN (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber) = 0 THEN
281 round((coitem_qtyshipped *
282 ((coitem_qtyshipped * coitem_unitcost) * ((coitem_qtyshipped * coitem_unitcost) /
283 (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber)))
287 CASE WHEN (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber) = 0 THEN
290 currtobase(cohead_curr_id, ((coitem_qtyshipped * coitem_unitcost) * ((coitem_qtyshipped * coitem_unitcost) /
291 (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber)))
293 END AS baseunitprice,
296 CASE WHEN (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber) = 0 THEN
299 cohist_qtyshipped * (currtobase(cohead_curr_id, ((coitem_qtyshipped * coitem_unitcost) * ((coitem_qtyshipped * coitem_unitcost) /
300 (SELECT SUM(coitem_qtyshipped * coitem_unitcost) FROM coitem WHERE coitem_cohead_id = cohead_id AND coitem_linenumber = join_coitem.coitem_linenumber)))
305 (SELECT invfifo_landedunitcost * cohist_qtyshipped FROM invdetailview where
308 invfifo_cohead_id = cohead_id
309 --invhist_ordnumber like cohead_number || '-%'
311 invhist_itemsite_id = cohist_itemsite_id
313 invhist_ordtype = 'SO'
315 invfifo_cohead_id = cohead_id
316 LIMIT 1) as basefifovalue,
324 order_rep.salesrep_name AS order_salesrep_name,
325 cust_rep.salesrep_name AS cust_salesrep_name
333 LEFT JOIN invchead ON invchead_invcnumber = cohist_invcnumber
335 cohead ON (cohead_number=cohist_ordernumber)
337 coitem AS join_coitem ON (
338 coitem_cohead_id = cohead_id
340 coitem_subnumber != 0
348 (SELECT item_type FROM item LEFT JOIN itemsite ON itemsite_item_id = item_id WHERE itemsite_id = cohist_itemsite_id) = 'K'
350 coitem_cohead_id = cohead_id
352 cohist_itemsite_id = coitem_itemsite_id
354 cohist_pos(cohist_id, cohist_ordernumber, cohist_itemsite_id) = coitem_pos(cohead_id, coitem_id, coitem_itemsite_id)
359 custinfo ON (cust_id=cohist_cust_id)
361 cntct ON cust_cntct_id = cntct_id
363 addr ON addr_id = cntct_addr_id
366 JOIN salesrep order_rep ON (order_rep.salesrep_id=cohist_salesrep_id)
368 JOIN salesrep cust_rep ON (cust_rep.salesrep_id=cust_salesrep_id)
371 <? if exists("includeMisc") ?>
372 LEFT OUTER JOIN itemsite ON (itemsite_id=coitem_itemsite_id)
373 LEFT OUTER JOIN site() ON (warehous_id=itemsite_warehous_id)
374 LEFT OUTER JOIN item ON (item_id=itemsite_item_id)
376 JOIN itemsite ON (itemsite_id=coitem_itemsite_id)
377 JOIN site() ON (warehous_id=itemsite_warehous_id)
378 JOIN item ON (item_id=itemsite_item_id)
380 <? if exists("shipzone_id") ?>
381 JOIN shiptoinfo ON (shipto_id=cohist_shipto_id)
382 JOIN shipzone ON (shipzone_id=shipto_shipzone_id)
386 AND cohist_unitcost = 0
387 -- make sure the invoices are not void..
388 AND invchead_void != true
392 <? if exists("includeMisc") ?>
393 AND (COALESCE(cohist_misc_type, '') <> 'F')
394 AND (COALESCE(cohist_misc_type, '') <> 'T')
397 <? if exists("item_id") ?>
398 AND (item_id=<? value("item_id") ?>)
401 <? if exists("cohist_salesrep_id") ?>
402 AND (cohist_salesrep_id=<? value("cohist_salesrep_id") ?>)
405 <? if exists("cust_salesrep_id") ?>
406 AND (cust_salesrep_id=<? value("cust_salesrep_id") ?>)
409 <? if exists("addr_postalcode") ?>
410 AND (addr_postalcode=<? value("addr_postalcode") ?>)
413 <? if exists("startDate") ?>
414 AND (cohist_invcdate >= <? value("startDate") ?>)
417 <? if exists("endDate") ?>
418 AND (cohist_invcdate <= <? value("endDate") ?>)
420 <? if exists("shipStartDate") ?>
421 AND (cohist_shipdate >= <? value("shipStartDate") ?>)
423 <? if exists("shipEndDate") ?>
424 AND (cohist_shipdate <= <? value("shipEndDate") ?>)
426 <? if exists("shipto_id") ?>
427 AND (cohist_shipto_id=<? value("shipto_id") ?>)
429 <? if exists("billToName") ?>
430 AND (UPPER(cohist_billtoname) ~ UPPER(<? value("billToName") ?>))
432 <? if exists("cust_id") ?>
433 AND (cohist_cust_id=<? value("cust_id") ?>)
435 <? if exists("custtype_id") ?>
436 AND (cust_custtype_id=<? value("custtype_id") ?>)
437 <? elseif exists("custtype_pattern") ?>
438 AND (cust_custtype_id IN (SELECT DISTINCT custtype_id
440 WHERE (custtype_code ~ <? value("custtype_pattern") ?>)))
442 <? if exists("by_custgrp") ?>
443 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
446 <? if exists("custgrp_id") ?>
447 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
449 WHERE (custgrpitem_custgrp_id=<? value("custgrp_id") ?>)))
451 <? if exists("custgrp_pattern") ?>
452 AND (cust_id IN (SELECT DISTINCT custgrpitem_cust_id
453 FROM custgrp, custgrpitem
454 WHERE ( (custgrpitem_custgrp_id=custgrp_id)
455 AND (custgrp_name ~ <? value("custgrp_pattern") ?>) )) )
458 <? if exists("item_id") ?>
459 AND (itemsite_item_id=<? value("item_id") ?>)
461 <? if exists("prodcat_id") ?>
462 AND (item_prodcat_id=<? value("prodcat_id") ?>)
464 <? if exists("prodcat_pattern") ?>
465 AND (item_prodcat_id IN (SELECT DISTINCT prodcat_id
467 WHERE (prodcat_code ~ <? value("prodcat_pattern") ?>)))
470 <? if exists("warehous_id") ?>
471 AND (itemsite_warehous_id=<? value("warehous_id") ?>)
473 <? if exists("shipzone_id") ?>
474 AND (shipzone_id=<? value("shipzone_id") ?>)
476 <? if exists("cohead_id") ?>
477 AND (cohead_id=<? value("cohead_id") ?>)
480 <? if exists("addr_country") ?>
481 AND (addr_country=<? value("addr_country") ?>)