From 31b4723926157f06d740eb5ab8cd84acd501f41b Mon Sep 17 00:00:00 2001 From: Sara Monalisa Date: Sat, 22 Nov 2025 19:28:24 -0300 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20ordena=20lista=20de=20produtos=20al?= =?UTF-8?q?fabeticamente=20nos=20relat=C3=B3rios=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/functions.py | 43 ++++++++++++++++++++++++++++++--- app/views/order.py | 29 ++++++++++++++++++++-- templates/pdf/order-report.html | 2 +- templates/pdf/week-report.html | 8 ++++-- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/app/utils/functions.py b/app/utils/functions.py index f2eaed7..b786111 100644 --- a/app/utils/functions.py +++ b/app/utils/functions.py @@ -104,9 +104,12 @@ def calculate_total_products(orders): total_products = {} for order in orders: if (order.status == 'approved') or (order.status == 'delivered'): - for ordered_product in order.call_products.all(): + ordered_products = order.call_products.all().order_by('call_product__product__name') + + for ordered_product in ordered_products: product_name = ordered_product.call_product.product.name product_unit = ordered_product.call_product.product.unit + if ordered_product.status == 'available': ordered_quantity = ordered_product.ordered_quantity elif ordered_product.status == 'parcial': @@ -119,7 +122,8 @@ def calculate_total_products(orders): else: total_products[product_name] = {'quantity': ordered_quantity, 'unit': product_unit} - return total_products + sorted_products = {k: v for k, v in sorted(total_products.items())} + return sorted_products # ######### @@ -155,9 +159,12 @@ def calculate_request_product(orders): total_requests = {} for order in orders: if (order.status == 'approved') or (order.status == 'pending'): - for ordered_product in order.call_products.all(): + ordered_products = order.call_products.all().order_by('call_product__product__name') + + for ordered_product in ordered_products: product_name = ordered_product.call_product.product.name product_unit = ordered_product.call_product.product.unit + if ordered_product.status == 'available': ordered_quantity = ordered_product.ordered_quantity elif ordered_product.status == 'parcial': @@ -170,4 +177,32 @@ def calculate_request_product(orders): else: total_requests[product_name] = {'quantity': ordered_quantity, 'unit': product_unit} - return total_requests + sorted_requests = {k: v for k, v in sorted(total_requests.items())} + return sorted_requests + + +def get_ordered_products_for_report(orders): + """ + Returns ordered products sorted alphabetically for detailed reports. + + Args: + orders (QuerySet): A list of orders. + + Returns: + list: List of ordered products sorted by product name. + """ + ordered_products_list = [] + + for order in orders: + ordered_products = order.call_products.all().order_by('call_product__product__name') + + for ordered_product in ordered_products: + ordered_products_list.append({ + 'order': order, + 'ordered_product': ordered_product, + 'product_name': ordered_product.call_product.product.name, + 'product_unit': ordered_product.call_product.product.unit + }) + + ordered_products_list.sort(key=lambda x: x['product_name']) + return ordered_products_list \ No newline at end of file diff --git a/app/views/order.py b/app/views/order.py index a394d10..e038fbc 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -468,7 +468,10 @@ def OrderReport(request, pk): ) return redirect("detail-order", pk) + available_products = order.available_products.order_by('call_product__product__name') + data["order"] = order + data["available_products"] = available_products today = timezone.now().date() data["today"] = today @@ -496,7 +499,15 @@ def WeekReport(request): data["friday"] = friday orders = get_report_orders(monday, friday) - data["orders"] = orders + + orders_sorted = sorted(orders, key=lambda order: order.institution.name) + + for order in orders_sorted: + order.available_products_sorted = order.call_products.filter( + status__in=['available', 'parcial'] + ).order_by('call_product__product__name') + + data["orders"] = orders_sorted total_products = calculate_total_products(orders) data["total_products"] = total_products @@ -524,7 +535,21 @@ def RequestReport(request): data["friday"] = friday orders = get_report_products(monday, friday) - data["orders"] = orders + + ordered_products_list = [] + for order in orders: + ordered_products = order.call_products.all().order_by('call_product__product__name') + for ordered_product in ordered_products: + ordered_products_list.append({ + 'order': order, + 'ordered_product': ordered_product, + 'product_name': ordered_product.call_product.product.name, + 'product_unit': ordered_product.call_product.product.unit + }) + + ordered_products_list.sort(key=lambda x: x['product_name']) + data["ordered_products"] = ordered_products_list + total_requests = calculate_request_product(orders) data["total_requests"] = total_requests diff --git a/templates/pdf/order-report.html b/templates/pdf/order-report.html index d7b7062..6a3571a 100644 --- a/templates/pdf/order-report.html +++ b/templates/pdf/order-report.html @@ -71,7 +71,7 @@

- {% for ordered_product in order.available_products %} + {% for ordered_product in available_products %} {{forloop.counter}} {{ordered_product.call_product.product.name}} diff --git a/templates/pdf/week-report.html b/templates/pdf/week-report.html index ca6d6dc..ba896a9 100644 --- a/templates/pdf/week-report.html +++ b/templates/pdf/week-report.html @@ -94,7 +94,7 @@

{{order.institution}}

- {% for ordered_product in order.available_products %} + {% for ordered_product in order.available_products_sorted %} {{ ordered_product.call_product.product.name }} {% if ordered_product.status == 'available' %} @@ -103,6 +103,10 @@

{{order.institution}}

{{ ordered_product.available_quantity }} {{ ordered_product.call_product.product.unit }} {% endif %} + {% empty %} + + Sem produtos disponíveis + {% endfor %} @@ -114,4 +118,4 @@

{{order.institution}}

- \ No newline at end of file + \ No newline at end of file From 5cdd2c00c1a25f7a75a0283dd836b68852efef8f Mon Sep 17 00:00:00 2001 From: Sara Monalisa Date: Sun, 23 Nov 2025 13:13:45 -0300 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20exibi=C3=A7=C3=A3o=20dos=20pedidos?= =?UTF-8?q?=20das=20institui=C3=A7=C3=B5es=20no=20rel.=20de=20solicita?= =?UTF-8?q?=C3=A7=C3=B5es=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/order.py | 28 +++++++++------------------- templates/pdf/request-report.html | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/app/views/order.py b/app/views/order.py index e038fbc..e3c5d2b 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -461,13 +461,6 @@ def OrderReport(request, pk): data = {} order = get_object_or_404(Order, pk=pk) - if (order.status != "approved") and (order.status != "delivered"): - messages.warning( - request, - "Não é possível gerar o relatório de um Pedido que não foi aprovado ou entregue", - ) - return redirect("detail-order", pk) - available_products = order.available_products.order_by('call_product__product__name') data["order"] = order @@ -536,20 +529,17 @@ def RequestReport(request): orders = get_report_products(monday, friday) - ordered_products_list = [] for order in orders: - ordered_products = order.call_products.all().order_by('call_product__product__name') - for ordered_product in ordered_products: - ordered_products_list.append({ - 'order': order, - 'ordered_product': ordered_product, - 'product_name': ordered_product.call_product.product.name, - 'product_unit': ordered_product.call_product.product.unit - }) - - ordered_products_list.sort(key=lambda x: x['product_name']) - data["ordered_products"] = ordered_products_list + if order.status == "pending": + order.request_products_sorted = order.call_products.exclude( + status='denied' + ).order_by('call_product__product__name') + else: + order.request_products_sorted = order.call_products.filter( + status__in=['available', 'parcial'] + ).order_by('call_product__product__name') + data["orders"] = orders total_requests = calculate_request_product(orders) data["total_requests"] = total_requests diff --git a/templates/pdf/request-report.html b/templates/pdf/request-report.html index 45bbf39..36626e0 100644 --- a/templates/pdf/request-report.html +++ b/templates/pdf/request-report.html @@ -90,15 +90,21 @@

{{order.institution}} - PEDIDO {{order.id}}

- {% for ordered_product in order.request_products %} + {% for ordered_product in order.request_products_sorted %} {{ ordered_product.call_product.product.name }} - {% if ordered_product.status == 'available' %} - {{ ordered_product.ordered_quantity }} {{ ordered_product.call_product.product.unit }} - {% elif ordered_product.status == 'parcial' %} - {{ ordered_product.available_quantity }} {{ ordered_product.call_product.product.unit }} - {% endif %} + + {% if ordered_product.status == 'available' or ordered_product.status == 'pending' %} + {{ ordered_product.ordered_quantity }} {{ ordered_product.call_product.product.unit }} + {% elif ordered_product.status == 'parcial' %} + {{ ordered_product.available_quantity }} {{ ordered_product.call_product.product.unit }} + {% endif %} + + {% empty %} + + Nenhum produto solicitado + {% endfor %}