Browse Source

use preferred unit for display instead of purchase unit in transaction editor

Daniel Sheffield 6 months ago
parent
commit
b9996db4d1
3 changed files with 21 additions and 5 deletions
  1. 2 2
      app/activities/PriceCheck.py
  2. 6 2
      app/activities/TransactionEditor.py
  3. 13 1
      app/data/QueryManager.py

+ 2 - 2
app/activities/PriceCheck.py

@@ -68,8 +68,8 @@ class PriceCheck(FocusWidget):
             )
             if k == 'unit':
                 options = list(filter(lambda x: x, [
-                self.query_manager.get_preferred_unit(self.data['product'])
-            ])) or options
+                    self.query_manager.get_preferred_unit(self.data['product'])
+                ])) or options
             if len(options) == 1:
                 self.apply_changes(k, list(options)[0])
 

+ 6 - 2
app/activities/TransactionEditor.py

@@ -240,14 +240,18 @@ class TransactionEditor(FocusWidget):
     def update_historic_prices(self, data):
         organic = None if data['organic'] == 'mixed' else data['organic']
         #sort = '$/unit' if self.buttons['sort_price'].state else 'ts'
-        product, unit = data['product'] or None, data['unit'] or None
+        product = data['product'] or None
+        unit = self.query_manager.get_preferred_unit(data['product']) or data['unit'] or None
         price = decimal_or_none(data['price'])
         quantity = decimal_or_none(data['quantity'])
-
         if None in (product, unit):
             self.rating.update_rating(None, None, None, unit)
             return
 
+        if quantity is not None:
+            factor = self.query_manager.convert_unit(product, data['unit'], unit)
+            quantity = quantity * factor if factor is not None else None
+
         df = self.query_manager.get_historic_prices_data(unit, product=product, organic=organic, sort='ts').dropna()
         if df.empty:
             self.rating.update_rating(None, None, None, unit)

+ 13 - 1
app/data/QueryManager.py

@@ -149,6 +149,18 @@ class QueryManager(object):
     def insert_new_product(self, product, category, group, unit):
         self.cursor.execute(get_insert_product_statement(product, category, group, unit))
 
+    def convert_unit(self, product, _from, _to):
+        self.cursor.execute(SQL(
+            "SELECT convert_unit({_from}, {_to}, {product}) AS factor"
+        ).format(
+            product=Literal(product), _from=Literal(_from), _to=Literal(_to)
+        ))
+        res = list(cursor_as_dict(self.cursor))
+        if len(res) == 0:
+            return None
+        assert len(res) == 1
+        return res[0]['factor']
+
     def get_preferred_unit(self, product):
         self.cursor.execute(SQL("""SELECT
     units.name AS preferred_unit
@@ -159,4 +171,4 @@ WHERE products.name = {product}""").format(product=Literal(product)))
         if len(res) == 0:
             return None
         assert len(res) == 1
-        return res[0]['preferred_unit']
+        return res[0]['preferred_unit']