using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Zentral.API.Apps.Purchases.Dtos; using Zentral.API.Services; using Zentral.Domain.Entities.Purchases; using Zentral.Infrastructure.Data; using Microsoft.EntityFrameworkCore; namespace Zentral.API.Apps.Purchases.Services; public class SupplierService { private readonly ZentralDbContext _db; private readonly AutoCodeService _autoCodeService; public SupplierService(ZentralDbContext db, AutoCodeService autoCodeService) { _db = db; _autoCodeService = autoCodeService; } public async Task> GetAllAsync() { return await _db.Suppliers .AsNoTracking() .OrderBy(s => s.Name) .Select(s => new SupplierDto { Id = s.Id, Code = s.Code, Name = s.Name, VatNumber = s.VatNumber, FiscalCode = s.FiscalCode, Address = s.Address, City = s.City, Province = s.Province, ZipCode = s.ZipCode, Country = s.Country, Email = s.Email, Pec = s.Pec, Phone = s.Phone, Website = s.Website, PaymentTerms = s.PaymentTerms, Notes = s.Notes, IsActive = s.IsActive, CreatedAt = s.CreatedAt, UpdatedAt = s.UpdatedAt }) .ToListAsync(); } public async Task GetByIdAsync(int id) { var supplier = await _db.Suppliers .AsNoTracking() .FirstOrDefaultAsync(s => s.Id == id); if (supplier == null) return null; return new SupplierDto { Id = supplier.Id, Code = supplier.Code, Name = supplier.Name, VatNumber = supplier.VatNumber, FiscalCode = supplier.FiscalCode, Address = supplier.Address, City = supplier.City, Province = supplier.Province, ZipCode = supplier.ZipCode, Country = supplier.Country, Email = supplier.Email, Pec = supplier.Pec, Phone = supplier.Phone, Website = supplier.Website, PaymentTerms = supplier.PaymentTerms, Notes = supplier.Notes, IsActive = supplier.IsActive, CreatedAt = supplier.CreatedAt, UpdatedAt = supplier.UpdatedAt }; } public async Task CreateAsync(CreateSupplierDto dto) { // Genera codice automatico var code = await _autoCodeService.GenerateNextCodeAsync("supplier"); if (string.IsNullOrEmpty(code)) { // Fallback se disabilitato code = $"FOR-{DateTime.Now:yyyyMMdd}-{Guid.NewGuid().ToString().Substring(0, 4).ToUpper()}"; } var supplier = new Supplier { Code = code, Name = dto.Name, VatNumber = dto.VatNumber, FiscalCode = dto.FiscalCode, Address = dto.Address, City = dto.City, Province = dto.Province, ZipCode = dto.ZipCode, Country = dto.Country, Email = dto.Email, Pec = dto.Pec, Phone = dto.Phone, Website = dto.Website, PaymentTerms = dto.PaymentTerms, Notes = dto.Notes, IsActive = true, CreatedAt = DateTime.Now }; _db.Suppliers.Add(supplier); await _db.SaveChangesAsync(); return await GetByIdAsync(supplier.Id) ?? throw new InvalidOperationException("Failed to retrieve created supplier"); } public async Task UpdateAsync(int id, UpdateSupplierDto dto) { var supplier = await _db.Suppliers.FindAsync(id); if (supplier == null) return null; supplier.Name = dto.Name; supplier.VatNumber = dto.VatNumber; supplier.FiscalCode = dto.FiscalCode; supplier.Address = dto.Address; supplier.City = dto.City; supplier.Province = dto.Province; supplier.ZipCode = dto.ZipCode; supplier.Country = dto.Country; supplier.Email = dto.Email; supplier.Pec = dto.Pec; supplier.Phone = dto.Phone; supplier.Website = dto.Website; supplier.PaymentTerms = dto.PaymentTerms; supplier.Notes = dto.Notes; supplier.IsActive = dto.IsActive; supplier.UpdatedAt = DateTime.Now; await _db.SaveChangesAsync(); return await GetByIdAsync(id); } public async Task DeleteAsync(int id) { var supplier = await _db.Suppliers.FindAsync(id); if (supplier == null) return false; // Check if used in purchase orders var hasOrders = await _db.PurchaseOrders.AnyAsync(o => o.SupplierId == id); if (hasOrders) { throw new InvalidOperationException("Impossibile eliminare il fornitore perché ha ordini associati."); } _db.Suppliers.Remove(supplier); await _db.SaveChangesAsync(); return true; } }