{% extends "base.html" %} {% from "components/ui.html" import page_header %} {% block content %} {% set breadcrumbs = [ {'text': 'Inventory', 'url': url_for('inventory.list_stock_items')}, {'text': 'Stock Items', 'url': url_for('inventory.list_stock_items')}, {'text': item.name, 'url': url_for('inventory.view_stock_item', item_id=item.id)}, {'text': 'History'} ] %} {{ page_header( icon_class='fas fa-history', title_text='Movement History', subtitle_text=item.name + ' (' + item.sku + ')', breadcrumbs=breadcrumbs ) }}
{{ _('Warehouse') }}
{{ _('Movement Type') }}
{{ _('Date From') }}
{{ _('Date To') }}

{{ _('Date') }} | {{ _('Warehouse') }} | {{ _('Type') }} | {{ _('Quantity') }} | {{ _('Unit Cost') }} | {{ _('Reference') }} | {{ _('Reason') }} | {{ _('User') }}
--- | --- | --- | --- | --- | --- | --- | ---
{% for movement in movements %}

{{ movement.moved_at|user_datetime if movement.moved_at else '—' }}

{{ movement.warehouse.code }}

{% set ns = namespace(is_devalued=false) %} {% if movement.movement_type == 'devaluation' %} {% set ns.is_devalued = true %} {% elif movement.lot_allocations %} {% for alloc in movement.lot_allocations %} {% if alloc.stock_lot and alloc.stock_lot.lot_type == 'devalued' %} {% set ns.is_devalued = true %} {% endif %} {% endfor %} {% endif %}
{{ movement.movement_type }} {% if ns.is_devalued %} {{ _('Devalued') }} {% endif %}

{{ '+' if movement.quantity > 0 else '' }}{{ movement.quantity }}

{% if movement.unit_cost is not none %} {{ movement.unit_cost }} {% elif movement.lot_allocations %} {% set ns_cost = namespace(total_qty=0, total_val=0) %} {% for alloc in movement.lot_allocations %} {% set q = alloc.quantity %} {% if q < 0 %} {% set q = -q %} {% endif %} {% set ns_cost.total_qty = ns_cost.total_qty + q %} {% set ns_cost.total_val = ns_cost.total_val + (q * alloc.unit_cost) %} {% endfor %} {% if ns_cost.total_qty > 0 %} {{ (ns_cost.total_val / ns_cost.total_qty) | round(2) }} {% else %} — {% endif %} {% else %} — {% endif %}

{% if movement.reference_type and movement.reference_id %} {% if movement.reference_type == 'invoice' %} Invoice #{{ movement.reference_id }} {% elif movement.reference_type == 'quote' %} Quote #{{ movement.reference_id }} {% elif movement.reference_type == 'purchase_order' %} PO #{{ movement.reference_id }} {% else %} {{ movement.reference_type }} #{{ movement.reference_id }} {% endif %} {% else %} — {% endif %}

{{ movement.reason or '—' }}

{{ movement.moved_by_user.username if movement.moved_by_user else '—' }}

{% else %}
{{ _('No movement history found for this item.') }}
{% endfor %}

{% endblock %}