<dtml-comment>
title:
connection_id:MySQL
max_rows:1000
max_cache:100
cache_time:0
class_name:DeliveryListBrain
class_file:InventoryBrain.py
</dtml-comment>
<params>simulation_state:list
target_start_date
target_stop_date
query
node_title
resource_title
variation_text
sort_on
resource_tree</params>
SELECT 
	SUM(movement.target_quantity) as target_quantity,
	movement.variation_text AS variation_text,
	node.title AS node_title,
	resource.title AS resource_title,
	resource.uid AS resource_uid,
        resource.relative_url AS resource_relative_url,
	movement.target_start_date AS target_start_date,
	movement.target_stop_date AS target_stop_date
        <dtml-if query>, <dtml-sqlvar query type="string"> AS query </dtml-if>
FROM
	catalog, movement, catalog AS resource, catalog AS node, catalog AS delivery <dtml-if query>, category </dtml-if>
WHERE
	catalog.uid = movement.uid
AND	movement.has_cell_content = 0
AND	node.uid = movement.source_uid
AND	movement.delivery_uid = delivery.uid
AND	movement.target_quantity <> 0.0
AND	delivery.portal_type = "Purchase Order"
AND	movement.resource_uid = resource.uid
<dtml-if simulation_state> AND	(<dtml-in simulation_state> catalog.simulation_state = <dtml-sqlvar sequence-item type="string"><dtml-if sequence-end><dtml-else> OR </dtml-if></dtml-in>)
</dtml-if><dtml-if target_start_date> AND	movement.target_start_date < <dtml-sqlvar target_start_date type="string">
</dtml-if><dtml-if target_stop_date> AND	movement.target_stop_date < <dtml-sqlvar target_stop_date type="string">
</dtml-if><dtml-if query> AND <dtml-if resource_tree>resource.uid = category.uid <dtml-else>movement.destination_uid = category.uid </dtml-if> 
AND <dtml-var query> 
</dtml-if><dtml-if node_title>AND node.title LIKE '%<dtml-var node_title>%'
</dtml-if><dtml-if resource_title>AND resource.title LIKE '%<dtml-var resource_title>%'
</dtml-if><dtml-if variation_text>AND variation_text LIKE '%<dtml-var variation_text>%'
</dtml-if>
GROUP BY
	movement.resource_uid, movement.variation_text
<dtml-if sort_on>
ORDER BY
	<dtml-var sort_on>
<dtml-else>
ORDER BY
	movement.target_stop_date
</dtml-if>