mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-05-18 04:08:48 -05:00
fix(inventory): soft-delete suppliers via HTML delete route
Match the behaviour of the API v1 endpoint by marking the supplier as inactive instead of hard-deleting it. This preserves referential integrity with purchase orders, stock items and audit data and removes the "cannot delete supplier with associated stock items" failure mode.
This commit is contained in:
+10
-11
@@ -1747,26 +1747,25 @@ def edit_supplier(supplier_id):
|
||||
@login_required
|
||||
@admin_or_permission_required("manage_suppliers")
|
||||
def delete_supplier(supplier_id):
|
||||
"""Delete supplier"""
|
||||
"""Delete (deactivate) supplier.
|
||||
|
||||
Performs a soft delete by setting ``is_active=False`` so historical
|
||||
purchase orders, stock items and audit data referencing the supplier
|
||||
remain intact. Matches the behaviour of the API v1 endpoint.
|
||||
"""
|
||||
supplier = Supplier.query.get_or_404(supplier_id)
|
||||
|
||||
# Check if supplier has associated stock items
|
||||
item_count = SupplierStockItem.query.filter_by(supplier_id=supplier_id).count()
|
||||
|
||||
if item_count > 0:
|
||||
flash(_("Cannot delete supplier with associated stock items. Remove items first."), "error")
|
||||
return redirect(url_for("inventory.view_supplier", supplier_id=supplier_id))
|
||||
|
||||
try:
|
||||
code = supplier.code
|
||||
db.session.delete(supplier)
|
||||
supplier.is_active = False
|
||||
supplier.updated_at = datetime.utcnow()
|
||||
safe_commit()
|
||||
|
||||
log_event("supplier_deleted", supplier_code=code)
|
||||
flash(_("Supplier deleted successfully."), "success")
|
||||
flash(_("Supplier deactivated successfully."), "success")
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash(_("Error deleting supplier: %(error)s", error=str(e)), "error")
|
||||
flash(_("Error deactivating supplier: %(error)s", error=str(e)), "error")
|
||||
|
||||
return redirect(url_for("inventory.list_suppliers"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user