139 lines
5.3 KiB
Python
139 lines
5.3 KiB
Python
from django.shortcuts import render, redirect
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.urls import reverse
|
|
from django.utils.text import slugify
|
|
import openpyxl
|
|
from .models import Category, Supplier
|
|
from .forms_import import ImportFileForm
|
|
|
|
@login_required
|
|
def import_categories(request):
|
|
"""
|
|
Import categories from an Excel (.xlsx) file.
|
|
Expected columns: Name (Eng), Name (Ar)
|
|
"""
|
|
if request.method == 'POST':
|
|
form = ImportFileForm(request.POST, request.FILES)
|
|
if form.is_valid():
|
|
excel_file = request.FILES['file']
|
|
|
|
try:
|
|
wb = openpyxl.load_workbook(excel_file)
|
|
sheet = wb.active
|
|
|
|
count = 0
|
|
updated_count = 0
|
|
errors = []
|
|
|
|
# Skip header row (min_row=2)
|
|
for i, row in enumerate(sheet.iter_rows(min_row=2, values_only=True), start=2):
|
|
if not any(row): continue # Skip empty rows
|
|
|
|
# Unpack columns with fallbacks for safety
|
|
# Format: name_en, name_ar
|
|
name_en = str(row[0]).strip() if row[0] else None
|
|
name_ar = str(row[1]).strip() if len(row) > 1 and row[1] else name_en
|
|
|
|
if not name_en:
|
|
errors.append(f"Row {i}: Missing English Name. Skipped.")
|
|
continue
|
|
|
|
slug = slugify(name_en)
|
|
|
|
category, created = Category.objects.update_or_create(
|
|
slug=slug,
|
|
defaults={
|
|
'name_en': name_en,
|
|
'name_ar': name_ar,
|
|
}
|
|
)
|
|
|
|
if created:
|
|
count += 1
|
|
else:
|
|
updated_count += 1
|
|
|
|
if count > 0 or updated_count > 0:
|
|
msg = f"Import completed: {count} new categories added"
|
|
if updated_count > 0:
|
|
msg += f", {updated_count} categories updated"
|
|
messages.success(request, msg)
|
|
|
|
if errors:
|
|
for error in errors:
|
|
messages.warning(request, error)
|
|
|
|
except Exception as e:
|
|
messages.error(request, f"Error processing file: {str(e)}")
|
|
|
|
return redirect(reverse('inventory') + '#categories-list')
|
|
else:
|
|
form = ImportFileForm()
|
|
|
|
return render(request, 'core/import_categories.html', {'form': form})
|
|
|
|
@login_required
|
|
def import_suppliers(request):
|
|
"""
|
|
Import suppliers from an Excel (.xlsx) file.
|
|
Expected columns: Name, Contact Person, Phone
|
|
"""
|
|
if request.method == 'POST':
|
|
form = ImportFileForm(request.POST, request.FILES)
|
|
if form.is_valid():
|
|
excel_file = request.FILES['file']
|
|
|
|
try:
|
|
wb = openpyxl.load_workbook(excel_file)
|
|
sheet = wb.active
|
|
|
|
count = 0
|
|
updated_count = 0
|
|
errors = []
|
|
|
|
# Skip header row (min_row=2)
|
|
for i, row in enumerate(sheet.iter_rows(min_row=2, values_only=True), start=2):
|
|
if not any(row): continue # Skip empty rows
|
|
|
|
# Unpack columns with fallbacks for safety
|
|
# Format: Name, Contact Person, Phone
|
|
name = str(row[0]).strip() if row[0] else None
|
|
contact_person = str(row[1]).strip() if len(row) > 1 and row[1] else ''
|
|
phone = str(row[2]).strip() if len(row) > 2 and row[2] else ''
|
|
|
|
if not name:
|
|
errors.append(f"Row {i}: Missing Name. Skipped.")
|
|
continue
|
|
|
|
supplier, created = Supplier.objects.update_or_create(
|
|
name=name,
|
|
defaults={
|
|
'contact_person': contact_person,
|
|
'phone': phone,
|
|
}
|
|
)
|
|
|
|
if created:
|
|
count += 1
|
|
else:
|
|
updated_count += 1
|
|
|
|
if count > 0 or updated_count > 0:
|
|
msg = f"Import completed: {count} new suppliers added"
|
|
if updated_count > 0:
|
|
msg += f", {updated_count} suppliers updated"
|
|
messages.success(request, msg)
|
|
|
|
if errors:
|
|
for error in errors:
|
|
messages.warning(request, error)
|
|
|
|
except Exception as e:
|
|
messages.error(request, f"Error processing file: {str(e)}")
|
|
|
|
return redirect('suppliers')
|
|
else:
|
|
form = ImportFileForm()
|
|
|
|
return render(request, 'core/import_suppliers.html', {'form': form}) |