ciclos

commit 93d7218510f38ff36a89ebafd051bbbd2f83d108

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Display prices of products with the cycle margin

 src/templates/cycle.html | 4 +++-
 src/types/cycle.py | 2 ++
 src/types/order.py | 4 +++-
 src/types/product.py | 3 +++


diff --git a/src/templates/cycle.html b/src/templates/cycle.html
index 98a069feff8240d0f2ecfadb8261d6745a166573..312de3222cfff4188aca04b4c0f92c6b58a68b02 100644
--- a/src/templates/cycle.html
+++ b/src/templates/cycle.html
@@ -24,6 +24,7 @@           
           <th>Nome do Produto</th>
           <th>Grupo</th>
           <th>Categoria</th>
+          <th>Pre├žo</th>
         </tr>
       </thead>
       <tbody>
@@ -38,7 +39,8 @@             
             <td> {{ product.title }} </td>
             <td> {{ product.group.name }} </td>
             <td> {{ product.category.name }}</td>
-          </tr>
+            <td> {{ product.price_on(current_order.cycle) | as_currency }} </td>
+        </tr>
         {% endfor %}
         {% endwith %}
       </tbody>




diff --git a/src/types/cycle.py b/src/types/cycle.py
index 649b7e0977aa4df20704fc9cdfcbef8ae469be33..f4757a29ef139e6a2261f3379652baf8e6fc80f1 100644
--- a/src/types/cycle.py
+++ b/src/types/cycle.py
@@ -30,6 +30,8 @@
     created_at = db.Column(db.DateTime, default = dt.utcnow)
     updated_at = db.Column(db.DateTime, default = dt.utcnow)
 
+    orders = db.relationship("Order", backref = "cycle", lazy = True)
+
     def __init__(self, name, description, start_at, end_at, delivery_start_at, delivery_end_at, status, price_margin):
         self.name = name
         self.description = description




diff --git a/src/types/order.py b/src/types/order.py
index 560e5931796547a1055d8b4fa481faf5a4a233b4..c5b27a93d54f8a33aaaee3594a57c80324a67c45 100644
--- a/src/types/order.py
+++ b/src/types/order.py
@@ -61,7 +61,9 @@     def products(self):
         return list(map(lambda entry_id: Product.query.get(entry_id), self.selected_product_ids()))
     
     def total(self):
-        return Product.query.filter(Product.id.in_(self.selected_product_ids())).with_entities(func.sum(Product.price)).scalar()
+        prices = list(map(lambda product: product.price_on(self.cycle), self.products()))
+
+        return sum(prices, 0)
 
     def to_json(self):
         return {




diff --git a/src/types/product.py b/src/types/product.py
index 5a9e65e591223237384643e1b369adf47a747fcf..a03b52d99f4314c81360b635cf413a252ca83878 100644
--- a/src/types/product.py
+++ b/src/types/product.py
@@ -32,6 +32,9 @@         Balance(0.0, 0.0, self.id).create()
 
         return self
 
+    def price_on(self, cycle):
+        margin = (cycle.price_margin * self.price) / 100
+        return self.price + margin
 
     def to_json(self):
         return {