mirror of
https://github.com/almet/copanier.git
synced 2025-04-28 19:42:37 +02:00
Create multiple tabs for full delivery reports
This commit is contained in:
parent
ec291e4c85
commit
1794913997
2 changed files with 50 additions and 22 deletions
|
@ -188,9 +188,13 @@ class Delivery(Base):
|
||||||
def total(self):
|
def total(self):
|
||||||
return round(sum(o.total(self.products) for o in self.orders.values()), 2)
|
return round(sum(o.total(self.products) for o in self.orders.values()), 2)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def producers(self):
|
||||||
|
return list(set([p.producer for p in self.products]))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_multiple_producers(self):
|
def has_multiple_producers(self):
|
||||||
return len(set([p.producer for p in self.products])) > 1
|
return len(self.producers) > 1
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_open(self):
|
def is_open(self):
|
||||||
|
|
|
@ -5,35 +5,52 @@ from openpyxl.writer.excel import save_virtual_workbook
|
||||||
|
|
||||||
from .models import Product
|
from .models import Product
|
||||||
|
|
||||||
|
def summary_for_products(wb, title, delivery, total=None, products=None):
|
||||||
|
if products == None:
|
||||||
|
products = delivery.products
|
||||||
|
if total == None:
|
||||||
|
total = delivery.total
|
||||||
|
|
||||||
def summary(delivery):
|
ws = wb.create_sheet(title)
|
||||||
wb = Workbook()
|
ws.append([
|
||||||
ws = wb.active
|
|
||||||
ws.title = f"{delivery.name} {delivery.from_date.date()}"
|
|
||||||
headers = [
|
|
||||||
"ref",
|
"ref",
|
||||||
"produit",
|
"produit",
|
||||||
"prix unitaire",
|
"prix unitaire",
|
||||||
"quantité commandée",
|
"quantité commandée",
|
||||||
"unité",
|
"unité",
|
||||||
"total",
|
"total",
|
||||||
]
|
])
|
||||||
ws.append(headers)
|
for product in products:
|
||||||
for product in delivery.products:
|
|
||||||
wanted = delivery.product_wanted(product)
|
wanted = delivery.product_wanted(product)
|
||||||
if not wanted:
|
if not wanted:
|
||||||
continue
|
continue
|
||||||
ws.append(
|
ws.append([
|
||||||
[
|
product.ref,
|
||||||
product.ref,
|
str(product),
|
||||||
str(product),
|
product.price,
|
||||||
product.price,
|
wanted,
|
||||||
wanted,
|
product.unit,
|
||||||
product.unit,
|
round(product.price * wanted, 2),
|
||||||
round(product.price * wanted, 2),
|
])
|
||||||
]
|
ws.append(["", "", "", "", "Total", total])
|
||||||
)
|
|
||||||
ws.append(["", "", "", "", "Total", delivery.total])
|
|
||||||
|
|
||||||
|
def summary(delivery):
|
||||||
|
wb = Workbook()
|
||||||
|
wb.remove(wb.active)
|
||||||
|
if delivery.has_multiple_producers:
|
||||||
|
for producer in delivery.producers:
|
||||||
|
summary_for_products(
|
||||||
|
wb,
|
||||||
|
producer,
|
||||||
|
delivery,
|
||||||
|
total=delivery.total_for_producer(producer),
|
||||||
|
products=delivery.get_products_by(producer)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
summary_for_products(wb, f"{delivery.name} {delivery.from_date.date()}", delivery)
|
||||||
|
|
||||||
return save_virtual_workbook(wb)
|
return save_virtual_workbook(wb)
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,10 +58,14 @@ def full(delivery):
|
||||||
wb = Workbook()
|
wb = Workbook()
|
||||||
ws = wb.active
|
ws = wb.active
|
||||||
ws.title = f"{delivery.name} {delivery.from_date.date()}"
|
ws.title = f"{delivery.name} {delivery.from_date.date()}"
|
||||||
headers = ["ref", "produit", "prix", "producer"] + [e for e in delivery.orders] + ["total"]
|
headers = ["ref", "produit", "prix"] + [e for e in delivery.orders] + ["total"]
|
||||||
|
if delivery.has_multiple_producers:
|
||||||
|
headers.insert(1, "producer")
|
||||||
ws.append(headers)
|
ws.append(headers)
|
||||||
for product in delivery.products:
|
for product in delivery.products:
|
||||||
row = [product.ref, str(product), product.price, product.producer]
|
row = [product.ref, str(product), product.price]
|
||||||
|
if delivery.has_multiple_producers:
|
||||||
|
row.insert(1, product.producer)
|
||||||
for order in delivery.orders.values():
|
for order in delivery.orders.values():
|
||||||
wanted = order.products.get(product.ref)
|
wanted = order.products.get(product.ref)
|
||||||
row.append(wanted.quantity if wanted else 0)
|
row.append(wanted.quantity if wanted else 0)
|
||||||
|
@ -55,6 +76,9 @@ def full(delivery):
|
||||||
+ [round(o.total(delivery.products),2) for o in delivery.orders.values()]
|
+ [round(o.total(delivery.products),2) for o in delivery.orders.values()]
|
||||||
+ [round(delivery.total, 2)]
|
+ [round(delivery.total, 2)]
|
||||||
)
|
)
|
||||||
|
if delivery.has_multiple_producers:
|
||||||
|
footer.insert(1, "")
|
||||||
|
|
||||||
ws.append(footer)
|
ws.append(footer)
|
||||||
return save_virtual_workbook(wb)
|
return save_virtual_workbook(wb)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue