Commit e369aa7d authored by Jérome Perrin's avatar Jérome Perrin

Inventory Brain: Avoid calling getObject when not required

* date attribute is converted to timezone only when attribute is accessed (and not in __init__ anymore). Some code that needs performance may be able to use date_utc attribute wich is the date as UTC
* debit, credit, debit_price and credit_price uses "is_cancellation" column instead of getObject().isCancellationAmount()
parent cb98a197
...@@ -42,9 +42,7 @@ at_date</string> </value> ...@@ -42,9 +42,7 @@ at_date</string> </value>
</item> </item>
<item> <item>
<key> <string>connection_hook</string> </key> <key> <string>connection_hook</string> </key>
<value> <value> <string></string> </value>
<none/>
</value>
</item> </item>
<item> <item>
<key> <string>connection_id</string> </key> <key> <string>connection_id</string> </key>
...@@ -77,6 +75,8 @@ Here, a group of movement means:\n ...@@ -77,6 +75,8 @@ Here, a group of movement means:\n
grouping_reference is the same\n grouping_reference is the same\n
\n \n
XXX now that grouping_date exists, this script will become useless.\n XXX now that grouping_date exists, this script will become useless.\n
Please consider using grouping date query with getMovementHistoryList instead of\n
using this obsolete script.\n
\n \n
</dtml-comment>\n </dtml-comment>\n
\n \n
...@@ -84,7 +84,7 @@ XXX now that grouping_date exists, this script will become useless.\n ...@@ -84,7 +84,7 @@ XXX now that grouping_date exists, this script will become useless.\n
catalog.uid as uid,\n catalog.uid as uid,\n
mirror_section.relative_url as mirror_section_relative_url,\n mirror_section.relative_url as mirror_section_relative_url,\n
mirror_section.title as mirror_section_title,\n mirror_section.title as mirror_section_title,\n
stock.date as date,\n stock.date as date_utc,\n
stock.node_uid as node_uid,\n stock.node_uid as node_uid,\n
IFNULL(stock.total_price, 0) as total_price,\n IFNULL(stock.total_price, 0) as total_price,\n
IFNULL(stock.quantity, 0) as total_quantity\n IFNULL(stock.quantity, 0) as total_quantity\n
...@@ -115,7 +115,7 @@ XXX now that grouping_date exists, this script will become useless.\n ...@@ -115,7 +115,7 @@ XXX now that grouping_date exists, this script will become useless.\n
catalog.uid as uid,\n catalog.uid as uid,\n
mirror_section.relative_url as mirror_section_relative_url,\n mirror_section.relative_url as mirror_section_relative_url,\n
mirror_section.title as mirror_section_title,\n mirror_section.title as mirror_section_title,\n
stock.date as date,\n stock.date as date_utc,\n
stock.node_uid as node_uid,\n stock.node_uid as node_uid,\n
IFNULL(stock.total_price, 0) as total_price,\n IFNULL(stock.total_price, 0) as total_price,\n
IFNULL(stock.quantity, 0) as total_quantity\n IFNULL(stock.quantity, 0) as total_quantity\n
...@@ -156,13 +156,13 @@ XXX now that grouping_date exists, this script will become useless.\n ...@@ -156,13 +156,13 @@ XXX now that grouping_date exists, this script will become useless.\n
HAVING max(stock_2.date) > <dtml-sqlvar "at_date" type="datetime">\n HAVING max(stock_2.date) > <dtml-sqlvar "at_date" type="datetime">\n
)\n )\n
\n \n
ORDER BY mirror_section_title, date ORDER BY mirror_section_title, date_utc
]]></string> </value> ]]></string> </value>
</item> </item>
<item> <item>
<key> <string>title</string> </key> <key> <string>title</string> </key>
<value> <string></string> </value> <value> <string>Obsolete.</string> </value>
</item> </item>
</dictionary> </dictionary>
</pickle> </pickle>
......
...@@ -284,17 +284,14 @@ class TrackingListBrain(InventoryListBrain): ...@@ -284,17 +284,14 @@ class TrackingListBrain(InventoryListBrain):
class MovementHistoryListBrain(InventoryListBrain): class MovementHistoryListBrain(InventoryListBrain):
"""Brain for getMovementHistoryList """Brain for getMovementHistoryList
""" """
def __init__(self): def _date(self):
InventoryListBrain.__init__(self) # Convert the date in the movement's original timezone.
if not self.date: # ZSQL method selects date as date_, and here we find the object to get the
return # timezone and convert the date to this timezone
# convert the date in the movement's original timezone.
# This is a somehow heavy operation, but fortunatly it's only called when
# the brain is accessed from the Shared.DC.ZRDB.Results.Results instance
obj = self.getObject() obj = self.getObject()
if obj is not None: if obj is not None:
timezone = None timezone = None
if self.node_uid == obj.getSourceUid(): if self.node_uid == obj.getSourceUid(): # XXX we could expose an "are we source" property on brain
start_date = obj.getStartDate() start_date = obj.getStartDate()
if start_date is not None: if start_date is not None:
timezone = start_date.timezone() timezone = start_date.timezone()
...@@ -303,7 +300,9 @@ class MovementHistoryListBrain(InventoryListBrain): ...@@ -303,7 +300,9 @@ class MovementHistoryListBrain(InventoryListBrain):
if stop_date is not None: if stop_date is not None:
timezone = stop_date.timezone() timezone = stop_date.timezone()
if timezone is not None: if timezone is not None:
self.date = self.date.toZone(timezone) return self.date_utc.toZone(timezone)
return self.date_utc
date = ComputedAttribute(_date, 1)
def getListItemUrl(self, cname_id, selection_index, selection_name): def getListItemUrl(self, cname_id, selection_index, selection_name):
"""Returns the URL for column `cname_id`. Used by ListBox """Returns the URL for column `cname_id`. Used by ListBox
...@@ -316,27 +315,26 @@ class MovementHistoryListBrain(InventoryListBrain): ...@@ -316,27 +315,26 @@ class MovementHistoryListBrain(InventoryListBrain):
return explanation.absolute_url() return explanation.absolute_url()
return '' return ''
def _debit(self): def _debit(self):
if self.getObject().isCancellationAmount(): if self.is_cancellation:
return min(self.total_quantity, 0) return min(self.total_quantity, 0)
return max(self.total_quantity, 0) return max(self.total_quantity, 0)
debit = ComputedAttribute(_debit, 1) debit = ComputedAttribute(_debit, 1)
def _credit(self): def _credit(self):
if self.getObject().isCancellationAmount(): if self.is_cancellation:
return min(-(self.total_quantity or 0), 0) return min(-(self.total_quantity or 0), 0)
return max(-(self.total_quantity or 0), 0) return max(-(self.total_quantity or 0), 0)
credit = ComputedAttribute(_credit, 1) credit = ComputedAttribute(_credit, 1)
def _debit_price(self): def _debit_price(self):
if self.getObject().isCancellationAmount(): if self.is_cancellation:
return min(self.total_price, 0) return min(self.total_price, 0)
return max(self.total_price, 0) return max(self.total_price, 0)
debit_price = ComputedAttribute(_debit_price, 1) debit_price = ComputedAttribute(_debit_price, 1)
def _credit_price(self): def _credit_price(self):
if self.getObject().isCancellationAmount(): if self.is_cancellation:
return min(-(self.total_price or 0), 0) return min(-(self.total_price or 0), 0)
return max(-(self.total_price or 0), 0) return max(-(self.total_price or 0), 0)
credit_price = ComputedAttribute(_credit_price, 1) credit_price = ComputedAttribute(_credit_price, 1)
......
...@@ -489,7 +489,7 @@ SELECT\n ...@@ -489,7 +489,7 @@ SELECT\n
catalog.path as path,\n catalog.path as path,\n
catalog.uid as uid,\n catalog.uid as uid,\n
catalog.relative_url as relative_url,\n catalog.relative_url as relative_url,\n
stock.date AS date,\n stock.date AS date_utc,\n
<dtml-if expr="precision is not None">\n <dtml-if expr="precision is not None">\n
<dtml-if group_by_expression>SUM</dtml-if>(ROUND(stock.quantity, <dtml-var precision>)) AS total_quantity,\n <dtml-if group_by_expression>SUM</dtml-if>(ROUND(stock.quantity, <dtml-var precision>)) AS total_quantity,\n
<dtml-if group_by_expression>SUM</dtml-if>(ROUND(stock.total_price, <dtml-var precision>)) AS total_price,\n <dtml-if group_by_expression>SUM</dtml-if>(ROUND(stock.total_price, <dtml-var precision>)) AS total_price,\n
...@@ -497,6 +497,7 @@ SELECT\n ...@@ -497,6 +497,7 @@ SELECT\n
<dtml-if group_by_expression>SUM</dtml-if>(stock.quantity) AS total_quantity,\n <dtml-if group_by_expression>SUM</dtml-if>(stock.quantity) AS total_quantity,\n
<dtml-if group_by_expression>SUM</dtml-if>(stock.total_price) AS total_price,\n <dtml-if group_by_expression>SUM</dtml-if>(stock.total_price) AS total_price,\n
</dtml-if>\n </dtml-if>\n
stock.is_cancellation AS is_cancellation,\n
stock.variation_text AS variation_text,\n stock.variation_text AS variation_text,\n
stock.simulation_state AS simulation_state,\n stock.simulation_state AS simulation_state,\n
stock.resource_uid AS resource_uid,\n stock.resource_uid AS resource_uid,\n
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment