Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions app/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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

# #########

Expand Down Expand Up @@ -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':
Expand All @@ -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
31 changes: 23 additions & 8 deletions app/views/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/pdf/order-report.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h1 style="font-size: 16px; font-weight: 600; padding: 10px 10px 0 10px;">
</tr>
</thead>
<tbody>
{% for ordered_product in order.available_products %}
{% for ordered_product in available_products %}
<tr>
<td width="50px">{{forloop.counter}}</td>
<td width="170px">{{ordered_product.call_product.product.name}}</td>
Expand Down
18 changes: 12 additions & 6 deletions templates/pdf/request-report.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,21 @@ <h3>{{order.institution}} - PEDIDO {{order.id}}</h3>
</tr>
</thead>
<tbody>
{% for ordered_product in order.request_products %}
{% for ordered_product in order.request_products_sorted %}
<tr>
<td>{{ ordered_product.call_product.product.name }}</td>
{% if ordered_product.status == 'available' %}
<td>{{ ordered_product.ordered_quantity }} {{ ordered_product.call_product.product.unit }}</td>
{% elif ordered_product.status == 'parcial' %}
<td>{{ ordered_product.available_quantity }} {{ ordered_product.call_product.product.unit }}</td>
{% endif %}
<td>
{% 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 %}
</td>
</tr>
{% empty %}
<tr>
<td colspan="2" style="text-align: center;">Nenhum produto solicitado</td>
</tr>
{% endfor %}

</tbody>
Expand Down
8 changes: 6 additions & 2 deletions templates/pdf/week-report.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h4>{{order.institution}}</h4>
</tr>
</thead>
<tbody>
{% for ordered_product in order.available_products %}
{% for ordered_product in order.available_products_sorted %}
<tr>
<td>{{ ordered_product.call_product.product.name }}</td>
{% if ordered_product.status == 'available' %}
Expand All @@ -103,6 +103,10 @@ <h4>{{order.institution}}</h4>
<td>{{ ordered_product.available_quantity }} {{ ordered_product.call_product.product.unit }}</td>
{% endif %}
</tr>
{% empty %}
<tr>
<td colspan="2"><i>Sem produtos disponíveis</i></td>
</tr>
{% endfor %}

</tbody>
Expand All @@ -114,4 +118,4 @@ <h4>{{order.institution}}</h4>
</itens>

</body>
</html>
</html>