<dtml-comment>
title:
connection_id:MySQL
max_rows:1000
max_cache:100
cache_time:0
class_name:ZSQLBrain
class_file:zsqlbrain.py
</dtml-comment>
<params>state_list=""
group_list=""
sort_on</params>
SELECT DISTINCT catalog.id, catalog.uid, catalog.relative_url, catalog.path, catalog.Description, catalog.simulation_state, catalog.default_destination_title
FROM catalog, catalog AS catalog_order, category AS category_order
<dtml-if expr="_.len(group_list)>0 and not ('' in group_list)">
, catalog AS organigramme
, category AS cat1
</dtml-if>

<dtml-if expr="_.len(group_list)>0 and '' in group_list">
LEFT JOIN category AS cat2 ON (cat2.uid=order_catalog.uid
AND cat2.base_category_uid=<dtml-var "portal_categories.group.uid">)
</dtml-if>

WHERE catalog.portal_type = "Sales Packing List"
AND category_order.uid = catalog.uid
AND category_order.base_category_uid=<dtml-var "portal_categories.causality.uid">
AND category_order.category_uid = catalog_order.uid
AND catalog_order.portal_type = "Sales Order"
<dtml-if expr="_.len(group_list)>0 and not ('' in group_list)">
AND ( organigramme.title LIKE "<dtml-var expr="group_list[0]">"
<dtml-in prefix="loop" expr="_.range(_.len(group_list)-1)">
OR organigramme.title LIKE "<dtml-var expr="group_list[loop_item+1]">"
</dtml-in>
)
AND cat1.uid=catalog_order.uid
AND cat1.category_uid=organigramme.uid
AND cat1.base_category_uid=<dtml-var "portal_categories.group.uid">
</dtml-if>

<dtml-if expr="_.len(state_list)>0">
AND ( catalog.simulation_state = "<dtml-var expr="state_list[0]">"
<dtml-in prefix="loop" expr="_.range(_.len(state_list)-1)">
OR catalog.simulation_state = "<dtml-var expr="state_list[loop_item+1]">"
</dtml-in>
)
</dtml-if>

<dtml-if expr="_.len(group_list)>0 and '' in group_list">
AND cat2.uid IS NULL
</dtml-if>

<dtml-if sort_on>
ORDER BY <dtml-var sort_on>
</dtml-if>