<dtml-comment>
title:
connection_id:MySQL
max_rows:20000
max_cache:100
cache_time:0
class_name:InventoryListBrain
class_file:InventoryBrain.py
</dtml-comment>
<params>resource_uid:list
resource:list
from_date
to_date
node_uid
section_uid
node
section
variation_text="XXX_marker"
node_category
section_category
resource_category
omit_simulation
omit_input
omit_output
simulation_state
query
calculate_asset:int=0
group_by_node:int=1
group_by_section:int=1
group_by_variation:int=1</params>
SELECT
	SUM(stock.quantity) AS inventory,
	<dtml-if "calculate_asset != 0"> SUM(stock.total_asset_price) AS asset_price, </dtml-if>
	node.title AS node_title,
	node.relative_url AS node_relative_url,
	section.title AS section_title,
	section.relative_url AS section_relative_url,
	resource.title AS resource_title,
	resource.relative_url AS resource_relative_url,
	resource.uid AS resource_uid,
	movement.variation_text AS variation_text,
	type_compo.title AS type_title,
        resource.default_source_title AS source_title,
	MAX(catalog.path) AS path
FROM
            movement
LEFT JOIN catalog AS resource ON (movement.resource_uid = resource.uid)
LEFT JOIN category as cat_type_compo ON (cat_type_compo.uid = resource.uid
AND cat_type_compo.category_strict_membership = 1
AND ( cat_type_compo.base_category_uid = <dtml-var "portal_categories.type_composant.uid">
OR cat_type_compo.base_category_uid = <dtml-var "portal_categories.motif.uid"> ) )
LEFT JOIN catalog AS type_compo ON (type_compo.uid = cat_type_compo.category_uid)
LEFT JOIN  catalog ON (catalog.uid = movement.uid)
LEFT JOIN  stock   ON (stock.uid = movement.uid)
LEFT JOIN  catalog AS node    ON (stock.node_uid = node.uid)
LEFT JOIN  catalog AS section    ON (stock.section_uid = section.uid)
  <dtml-if node_category>, catalog AS node_c, catalog AS node_bc, category AS node_membership </dtml-if> <dtml-if section_category>, catalog AS section_c, catalog AS section_bc, category AS section_membership</dtml-if> <dtml-if resource_category>, catalog AS resource_c, catalog AS resource_bc, category AS resource_membership </dtml-if>  <dtml-if query>, category </dtml-if>
WHERE
  1 = 1
<dtml-if resource>AND	(<dtml-in resource> resource.relative_url = <dtml-sqlvar sequence-item type="string"><dtml-if sequence-end><dtml-else> OR </dtml-if></dtml-in>)
</dtml-if><dtml-if resource_uid>AND	(<dtml-in resource_uid> movement.resource_uid = <dtml-sqlvar sequence-item type="int"><dtml-if sequence-end><dtml-else> OR </dtml-if></dtml-in>)
</dtml-if>AND	movement.is_accountable = 1
<dtml-if from_date>AND	movement.stop_date >= <dtml-sqlvar from_date type="string">
</dtml-if><dtml-if to_date>AND	movement.stop_date < <dtml-sqlvar to_date type="string">
</dtml-if><dtml-if node_uid>AND	stock.node_uid  = <dtml-sqlvar node_uid type="int">
</dtml-if><dtml-if section_uid>AND	stock.section_uid  = <dtml-sqlvar section_uid type="int">
</dtml-if><dtml-if "variation_text <> 'XXX_marker'">AND	movement.variation_text = <dtml-sqlvar variation_text type="string">
</dtml-if><dtml-if node>AND	node.relative_url = <dtml-sqlvar node type="string">
</dtml-if><dtml-if section>AND	section.relative_url = <dtml-sqlvar section type="string">
</dtml-if><dtml-if node_category>AND	node_c.relative_url = <dtml-sqlvar node_category type="string">
AND	node_membership.category_uid = node_c.uid
AND	node_membership.base_category_uid = node_bc.uid
AND	node_membership.uid = node.uid
</dtml-if><dtml-if section_category>AND	section_c.relative_url = <dtml-sqlvar section_category type="string">
AND	section_membership.category_uid =  section_c.uid
AND	section_membership.base_category_uid = section_bc.uid
AND	section_membership.uid = section.uid
</dtml-if><dtml-if resource_category>AND	resource_c.relative_url = <dtml-sqlvar resource_category type="string">
AND	resource_membership.category_uid =  resource_c.uid
AND	resource_membership.base_category_uid = resource_bc.uid
AND	resource_membership.uid = resource.uid
</dtml-if><dtml-if omit_simulation>AND	catalog.portal_type != "Simulation Movement"
</dtml-if><dtml-if omit_input>AND	stock.quantity < 0
</dtml-if><dtml-if omit_output>AND	stock.quantity > 0
</dtml-if><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 query>AND	category.uid = node.uid
AND	<dtml-var query></dtml-if>
AND catalog.uid is not NULL
AND resource.uid is not NULL
GROUP BY
	<dtml-if group_by_node>stock.node_uid, </dtml-if><dtml-if group_by_section>stock.section_uid, </dtml-if>movement.resource_uid<dtml-if group_by_variation>, movement.variation_text</dtml-if> 

ORDER BY
	node.title, resource.title, movement.variation_text