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..e3c5d2b 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -461,14 +461,10 @@ 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 + data["available_products"] = available_products today = timezone.now().date() data["today"] = today @@ -496,7 +492,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,6 +528,17 @@ def RequestReport(request): data["friday"] = friday orders = get_report_products(monday, friday) + + for order in orders: + 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/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/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 %} 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