{% extends 'base.html' %} {% block content %}

Sales

{% if current_user.role == 'customer' %} View My Ledger {% endif %} {% if current_user.role == 'admin' %} New Sale {% endif %}
{% if current_user.role == 'admin' %}

Search & Filter Sales

Filter sales by customer:
Clear
{% if search_name or search_phone %}
Filtered Results: {% if search_name %}Name: "{{ search_name }}"{% endif %} {% if search_name and search_phone %} | {% endif %} {% if search_phone %}Phone: "{{ search_phone }}"{% endif %} ({{ sales|length }} sales found)
{% endif %}
{% endif %} {% if current_user.role == 'admin' %} {% set total_purchased = sales|sum(attribute='weight_kg') if sales else 0 %} {% set total_sold = sales|sum(attribute='weight_kg') if sales else 0 %} {% set available_inventory = total_purchased - total_sold %} {% if available_inventory <= 0 %}

⚠️ No Inventory Available

Cannot create new sales until you purchase more meat.

Purchase Meat
{% elif available_inventory < 100 %}

⚠️ Low Inventory

Only {{ available_inventory|kg }} remaining for sales.

Purchase Meat
{% endif %} {% endif %}
{% for s in sales %} {% else %} {% endfor %}
Date Customer Weight (kg) Price/kg Total Status Payment Status Actions
{{ s.date }} {{ s.customer.name }} {{ s.weight_kg|kg }} {{ s.price_per_kg|currency }} {{ s.total_amount|currency }} {{ s.status|capitalize }} {% if s.payment_status == 'unpaid' %} Unpaid {% elif s.payment_status == 'paid_to_admin' %} Paid {% else %} {{ s.payment_status|default('Unpaid') }} {% endif %} {% if current_user.role == 'admin' %} - {% endif %}
No sales yet.
Grand Total {{ (sales|sum(attribute='total_amount'))|currency }}
{% if current_user.role == 'admin' %}

All Customer Balances & Reports

Complete customer overview
{% for customer in filtered_customers %} {% set balance_data = customer_balances.get(customer.id, {}) %} {% else %} {% endfor %}
Customer Name Phone Number Email Total Balance Total Sales Total Payments Orders Count Last Order
{{ customer.name[0].upper() }}
{{ customer.name }}
{% if customer.phone %} {{ customer.phone }} {% else %} - {% endif %} {{ customer.email }}
{{ balance_data.get('balance', 0)|currency }} {% if balance_data.get('balance', 0) > 0 %} OWES {% elif balance_data.get('balance', 0) < 0 %} CREDIT {% else %} ZERO {% endif %}
{{ balance_data.get('total_sales', 0)|currency }} {{ balance_data.get('total_payments', 0)|currency }} {{ balance_data.get('sales_count', 0) }} orders {% if balance_data.get('last_sale') %} {{ balance_data.get('last_sale').strftime('%Y-%m-%d') }} {% else %} No orders {% endif %}
No customers found in the system.
{% if filtered_customers %}
FILTERED CUSTOMERS
{{ filtered_customers|length }}
TOTAL OUTSTANDING
{{ (customer_balances.values() | sum(attribute='balance'))|currency }}
TOTAL SALES VALUE
{{ (customer_balances.values() | sum(attribute='total_sales'))|currency }}
TOTAL PAYMENTS
{{ (customer_balances.values() | sum(attribute='total_payments'))|currency }}
{% endif %}
{% endif %} {% endblock %}