Files
orion/app/templates/admin/inventory.html
Samir Boulahtit 8d8d41808b feat: add admin inventory management (Phase 1)
- Add admin API endpoints for inventory management
- Add inventory page with vendor selector and filtering
- Add admin schemas for cross-vendor inventory operations
- Support digital products with unlimited inventory
- Add integration tests for admin inventory API
- Add inventory management guide documentation

Mirrors vendor inventory functionality with admin-level access.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-18 21:05:12 +01:00

441 lines
22 KiB
HTML

{# app/templates/admin/inventory.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/tables.html' import table_wrapper %}
{% from 'shared/macros/modals.html' import modal_simple %}
{% from 'shared/macros/inputs.html' import vendor_selector %}
{% block title %}Inventory{% endblock %}
{% block alpine_data %}adminInventory(){% endblock %}
{% block content %}
{{ page_header('Inventory', subtitle='Manage stock levels across all vendors') }}
{{ loading_state('Loading inventory...') }}
{{ error_state('Error loading inventory') }}
<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
<!-- Card: Total Entries -->
<div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
<span x-html="$icon('archive', 'w-5 h-5')"></span>
</div>
<div>
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
Total Entries
</p>
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.total_entries || 0">
0
</p>
</div>
</div>
<!-- Card: Total Quantity -->
<div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
<span x-html="$icon('cube', 'w-5 h-5')"></span>
</div>
<div>
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
Total Stock
</p>
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatNumber(stats.total_quantity || 0)">
0
</p>
</div>
</div>
<!-- Card: Available Stock -->
<div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
<span x-html="$icon('check-circle', 'w-5 h-5')"></span>
</div>
<div>
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
Available
</p>
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatNumber(stats.total_available || 0)">
0
</p>
</div>
</div>
<!-- Card: Low Stock Items -->
<div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<div class="p-3 mr-4 text-red-500 bg-red-100 rounded-full dark:text-red-100 dark:bg-red-500">
<span x-html="$icon('exclamation', 'w-5 h-5')"></span>
</div>
<div>
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
Low Stock
</p>
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.low_stock_count || 0">
0
</p>
</div>
</div>
</div>
<!-- Search and Filters Bar -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<div class="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4">
<!-- Search Input -->
<div class="flex-1 max-w-xl">
<div class="relative">
<span class="absolute inset-y-0 left-0 flex items-center pl-3">
<span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
</span>
<input
type="text"
x-model="filters.search"
@input="debouncedSearch()"
placeholder="Search by product title or SKU..."
class="w-full pl-10 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
>
</div>
</div>
<!-- Filters -->
<div class="flex flex-wrap gap-3">
<!-- Vendor Filter (Tom Select) -->
{{ vendor_selector(
ref_name='vendorSelect',
id='inventory-vendor-select',
placeholder='Filter by vendor...',
width='w-64'
) }}
<!-- Location Filter -->
<select
x-model="filters.location"
@change="pagination.page = 1; loadInventory()"
class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
>
<option value="">All Locations</option>
<template x-for="loc in locations" :key="loc">
<option :value="loc" x-text="loc"></option>
</template>
</select>
<!-- Low Stock Filter -->
<select
x-model="filters.low_stock"
@change="pagination.page = 1; loadInventory()"
class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
>
<option value="">All Stock Levels</option>
<option value="5">Low Stock (< 5)</option>
<option value="10">Low Stock (< 10)</option>
<option value="25">Low Stock (< 25)</option>
<option value="50">Low Stock (< 50)</option>
</select>
<!-- Refresh Button -->
<button
@click="refresh()"
class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none transition-colors"
title="Refresh inventory"
>
<span x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
Refresh
</button>
</div>
</div>
</div>
<!-- Inventory Table with Pagination -->
<div x-show="!loading">
{% call table_wrapper() %}
<thead>
<tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
<th class="px-4 py-3">Product</th>
<th class="px-4 py-3">Vendor</th>
<th class="px-4 py-3">Location</th>
<th class="px-4 py-3 text-right">Quantity</th>
<th class="px-4 py-3 text-right">Reserved</th>
<th class="px-4 py-3 text-right">Available</th>
<th class="px-4 py-3">Status</th>
<th class="px-4 py-3">Actions</th>
</tr>
</thead>
<tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
<!-- Empty State -->
<template x-if="inventory.length === 0">
<tr>
<td colspan="8" class="px-4 py-8 text-center text-gray-600 dark:text-gray-400">
<div class="flex flex-col items-center">
<span x-html="$icon('archive', 'w-12 h-12 mb-2 text-gray-300')"></span>
<p class="font-medium">No inventory entries found</p>
<p class="text-xs mt-1" x-text="filters.search || filters.vendor_id || filters.location || filters.low_stock ? 'Try adjusting your filters' : 'Inventory will appear here when products have stock entries'"></p>
</div>
</td>
</tr>
</template>
<!-- Inventory Rows -->
<template x-for="item in inventory" :key="item.id">
<tr class="text-gray-700 dark:text-gray-400">
<!-- Product Info -->
<td class="px-4 py-3">
<div class="flex items-center">
<!-- Product Image -->
<div class="w-10 h-10 mr-3 rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700 flex-shrink-0">
<template x-if="item.product_image">
<img :src="item.product_image" :alt="item.product_title" class="w-full h-full object-cover" loading="lazy" />
</template>
<template x-if="!item.product_image">
<div class="w-full h-full flex items-center justify-center">
<span x-html="$icon('photograph', 'w-5 h-5 text-gray-400')"></span>
</div>
</template>
</div>
<!-- Product Details -->
<div class="min-w-0">
<p class="font-semibold text-sm truncate max-w-[200px]" x-text="item.product_title || 'Untitled'"></p>
<template x-if="item.product_sku">
<p class="text-xs text-gray-400 font-mono">SKU: <span x-text="item.product_sku"></span></p>
</template>
<template x-if="item.gtin">
<p class="text-xs text-gray-400 font-mono">GTIN: <span x-text="item.gtin"></span></p>
</template>
</div>
</div>
</td>
<!-- Vendor Info -->
<td class="px-4 py-3 text-sm">
<p class="font-medium" x-text="item.vendor_name || 'Unknown'"></p>
</td>
<!-- Location -->
<td class="px-4 py-3 text-sm">
<span class="px-2 py-1 text-xs font-medium rounded-full bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300" x-text="item.location"></span>
</td>
<!-- Quantity -->
<td class="px-4 py-3 text-sm text-right font-mono">
<span x-text="formatNumber(item.quantity)"></span>
</td>
<!-- Reserved -->
<td class="px-4 py-3 text-sm text-right font-mono">
<span :class="item.reserved_quantity > 0 ? 'text-orange-600 dark:text-orange-400' : ''" x-text="formatNumber(item.reserved_quantity)"></span>
</td>
<!-- Available -->
<td class="px-4 py-3 text-sm text-right font-mono font-semibold">
<span :class="item.available_quantity <= 0 ? 'text-red-600 dark:text-red-400' : item.available_quantity < 10 ? 'text-orange-600 dark:text-orange-400' : 'text-green-600 dark:text-green-400'" x-text="formatNumber(item.available_quantity)"></span>
</td>
<!-- Status -->
<td class="px-4 py-3 text-sm">
<template x-if="item.available_quantity <= 0">
<span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100">
Out of Stock
</span>
</template>
<template x-if="item.available_quantity > 0 && item.available_quantity < 10">
<span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-orange-700 bg-orange-100 dark:bg-orange-700 dark:text-orange-100">
Low Stock
</span>
</template>
<template x-if="item.available_quantity >= 10">
<span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100">
In Stock
</span>
</template>
</td>
<!-- Actions -->
<td class="px-4 py-3 text-sm">
<div class="flex items-center space-x-2">
<button
@click="openAdjustModal(item)"
class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-purple-600 rounded-lg dark:text-purple-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
title="Adjust Stock"
>
<span x-html="$icon('adjustments', 'w-4 h-4')"></span>
</button>
<button
@click="openSetModal(item)"
class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-blue-600 rounded-lg dark:text-blue-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
title="Set Quantity"
>
<span x-html="$icon('pencil', 'w-4 h-4')"></span>
</button>
<button
@click="confirmDelete(item)"
class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-red-600 rounded-lg dark:text-red-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
title="Delete Entry"
>
<span x-html="$icon('delete', 'w-4 h-4')"></span>
</button>
</div>
</td>
</tr>
</template>
</tbody>
{% endcall %}
{{ pagination(show_condition="!loading && pagination.total > 0") }}
</div>
<!-- Adjust Stock Modal -->
{% call modal_simple('adjustStockModal', 'Adjust Stock', show_var='showAdjustModal', size='sm') %}
<div class="space-y-4">
<div class="text-sm text-gray-600 dark:text-gray-400">
<p class="font-medium text-gray-700 dark:text-gray-300" x-text="selectedItem?.product_title || 'Product'"></p>
<p class="text-xs">Location: <span x-text="selectedItem?.location || '-'"></span></p>
<p class="text-xs">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Adjustment Quantity
</label>
<div class="flex items-center gap-2">
<button
@click="adjustForm.quantity = Math.max(-999999, adjustForm.quantity - 1)"
class="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 border border-gray-300 dark:border-gray-600 rounded"
>
<span x-html="$icon('minus', 'w-4 h-4')"></span>
</button>
{# noqa: FE-008 - Custom stepper with negative values for stock adjustments #}
<input
type="number"
x-model.number="adjustForm.quantity"
class="flex-1 px-3 py-2 text-sm text-center border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
placeholder="0"
>
<button
@click="adjustForm.quantity = Math.min(999999, adjustForm.quantity + 1)"
class="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 border border-gray-300 dark:border-gray-600 rounded"
>
<span x-html="$icon('plus', 'w-4 h-4')"></span>
</button>
</div>
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
Positive = add stock, Negative = remove stock
</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Reason (optional)
</label>
<input
type="text"
x-model="adjustForm.reason"
class="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
placeholder="e.g., Restocking from supplier"
>
</div>
<div class="p-3 bg-gray-50 dark:bg-gray-700 rounded-lg">
<p class="text-sm text-gray-600 dark:text-gray-400">
New quantity will be:
<span class="font-semibold" :class="(selectedItem?.quantity || 0) + adjustForm.quantity < 0 ? 'text-red-600' : 'text-green-600'" x-text="formatNumber(Math.max(0, (selectedItem?.quantity || 0) + adjustForm.quantity))"></span>
</p>
</div>
<div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
<button
@click="showAdjustModal = false"
class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
>
Cancel
</button>
<button
@click="executeAdjust()"
:disabled="saving || adjustForm.quantity === 0"
class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
>
<span x-text="saving ? 'Saving...' : 'Adjust Stock'"></span>
</button>
</div>
</div>
{% endcall %}
<!-- Set Quantity Modal -->
{% call modal_simple('setQuantityModal', 'Set Quantity', show_var='showSetModal', size='sm') %}
<div class="space-y-4">
<div class="text-sm text-gray-600 dark:text-gray-400">
<p class="font-medium text-gray-700 dark:text-gray-300" x-text="selectedItem?.product_title || 'Product'"></p>
<p class="text-xs">Location: <span x-text="selectedItem?.location || '-'"></span></p>
<p class="text-xs">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
New Quantity
</label>
<input
type="number"
x-model.number="setForm.quantity"
min="0"
class="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
placeholder="Enter new quantity"
>
</div>
<div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
<button
@click="showSetModal = false"
class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
>
Cancel
</button>
<button
@click="executeSet()"
:disabled="saving || setForm.quantity < 0"
class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors disabled:opacity-50"
>
<span x-text="saving ? 'Saving...' : 'Set Quantity'"></span>
</button>
</div>
</div>
{% endcall %}
<!-- Delete Confirmation Modal -->
{% call modal_simple('deleteModal', 'Delete Inventory Entry', show_var='showDeleteModal', size='sm') %}
<div class="space-y-4">
<p class="text-sm text-gray-600 dark:text-gray-400">
Are you sure you want to delete this inventory entry?
</p>
<div class="text-sm">
<p class="font-medium text-gray-700 dark:text-gray-300" x-text="selectedItem?.product_title || 'Product'"></p>
<p class="text-xs text-gray-500">Location: <span x-text="selectedItem?.location || '-'"></span></p>
<p class="text-xs text-gray-500">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
</div>
<p class="text-xs text-red-600 dark:text-red-400">
This action cannot be undone.
</p>
<div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
<button
@click="showDeleteModal = false"
class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
>
Cancel
</button>
<button
@click="executeDelete()"
:disabled="saving"
class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50"
>
<span x-text="saving ? 'Deleting...' : 'Delete'"></span>
</button>
</div>
</div>
{% endcall %}
{% endblock %}
{% block extra_scripts %}
<script src="{{ url_for('static', path='admin/js/inventory.js') }}"></script>
{% endblock %}