refactor: Remove stock bin management from ProjectService
Removes project-level stock bin methods since stock is now derived from material stock lengths. Updates queries to include Material on parts. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,8 +16,9 @@ public class ProjectService
|
|||||||
public async Task<List<Project>> GetAllAsync()
|
public async Task<List<Project>> GetAllAsync()
|
||||||
{
|
{
|
||||||
return await _context.Projects
|
return await _context.Projects
|
||||||
.Include(p => p.Material)
|
|
||||||
.Include(p => p.CuttingTool)
|
.Include(p => p.CuttingTool)
|
||||||
|
.Include(p => p.Parts)
|
||||||
|
.ThenInclude(pt => pt.Material)
|
||||||
.OrderByDescending(p => p.UpdatedAt ?? p.CreatedAt)
|
.OrderByDescending(p => p.UpdatedAt ?? p.CreatedAt)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
@@ -25,10 +26,9 @@ public class ProjectService
|
|||||||
public async Task<Project?> GetByIdAsync(int id)
|
public async Task<Project?> GetByIdAsync(int id)
|
||||||
{
|
{
|
||||||
return await _context.Projects
|
return await _context.Projects
|
||||||
.Include(p => p.Material)
|
|
||||||
.Include(p => p.CuttingTool)
|
.Include(p => p.CuttingTool)
|
||||||
.Include(p => p.Parts.OrderBy(pt => pt.SortOrder))
|
.Include(p => p.Parts.OrderBy(pt => pt.SortOrder))
|
||||||
.Include(p => p.StockBins.OrderBy(sb => sb.SortOrder))
|
.ThenInclude(pt => pt.Material)
|
||||||
.FirstOrDefaultAsync(p => p.Id == id);
|
.FirstOrDefaultAsync(p => p.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ public class ProjectService
|
|||||||
var duplicate = new Project
|
var duplicate = new Project
|
||||||
{
|
{
|
||||||
Name = $"{original.Name} (Copy)",
|
Name = $"{original.Name} (Copy)",
|
||||||
MaterialId = original.MaterialId,
|
Customer = original.Customer,
|
||||||
CuttingToolId = original.CuttingToolId,
|
CuttingToolId = original.CuttingToolId,
|
||||||
Notes = original.Notes,
|
Notes = original.Notes,
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
@@ -83,6 +83,7 @@ public class ProjectService
|
|||||||
_context.ProjectParts.Add(new ProjectPart
|
_context.ProjectParts.Add(new ProjectPart
|
||||||
{
|
{
|
||||||
ProjectId = duplicate.Id,
|
ProjectId = duplicate.Id,
|
||||||
|
MaterialId = part.MaterialId,
|
||||||
Name = part.Name,
|
Name = part.Name,
|
||||||
LengthInches = part.LengthInches,
|
LengthInches = part.LengthInches,
|
||||||
Quantity = part.Quantity,
|
Quantity = part.Quantity,
|
||||||
@@ -90,19 +91,6 @@ public class ProjectService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy stock bins
|
|
||||||
foreach (var bin in original.StockBins)
|
|
||||||
{
|
|
||||||
_context.ProjectStockBins.Add(new ProjectStockBin
|
|
||||||
{
|
|
||||||
ProjectId = duplicate.Id,
|
|
||||||
LengthInches = bin.LengthInches,
|
|
||||||
Quantity = bin.Quantity,
|
|
||||||
Priority = bin.Priority,
|
|
||||||
SortOrder = bin.SortOrder
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
@@ -160,102 +148,6 @@ public class ProjectService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stock bins management
|
|
||||||
public async Task<ProjectStockBin> AddStockBinAsync(ProjectStockBin bin)
|
|
||||||
{
|
|
||||||
var maxOrder = await _context.ProjectStockBins
|
|
||||||
.Where(b => b.ProjectId == bin.ProjectId)
|
|
||||||
.MaxAsync(b => (int?)b.SortOrder) ?? -1;
|
|
||||||
bin.SortOrder = maxOrder + 1;
|
|
||||||
|
|
||||||
_context.ProjectStockBins.Add(bin);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var project = await _context.Projects.FindAsync(bin.ProjectId);
|
|
||||||
if (project != null)
|
|
||||||
{
|
|
||||||
project.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
return bin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task UpdateStockBinAsync(ProjectStockBin bin)
|
|
||||||
{
|
|
||||||
_context.ProjectStockBins.Update(bin);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var project = await _context.Projects.FindAsync(bin.ProjectId);
|
|
||||||
if (project != null)
|
|
||||||
{
|
|
||||||
project.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task DeleteStockBinAsync(int id)
|
|
||||||
{
|
|
||||||
var bin = await _context.ProjectStockBins.FindAsync(id);
|
|
||||||
if (bin != null)
|
|
||||||
{
|
|
||||||
var projectId = bin.ProjectId;
|
|
||||||
_context.ProjectStockBins.Remove(bin);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var project = await _context.Projects.FindAsync(projectId);
|
|
||||||
if (project != null)
|
|
||||||
{
|
|
||||||
project.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ImportStockFromSupplierAsync(int projectId, int supplierId, int? materialId = null)
|
|
||||||
{
|
|
||||||
var query = _context.SupplierStocks
|
|
||||||
.Where(s => s.SupplierId == supplierId && s.IsActive);
|
|
||||||
|
|
||||||
if (materialId.HasValue)
|
|
||||||
{
|
|
||||||
query = query.Where(s => s.MaterialId == materialId.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
var stocks = await query.ToListAsync();
|
|
||||||
var maxOrder = await _context.ProjectStockBins
|
|
||||||
.Where(b => b.ProjectId == projectId)
|
|
||||||
.MaxAsync(b => (int?)b.SortOrder) ?? -1;
|
|
||||||
|
|
||||||
foreach (var stock in stocks)
|
|
||||||
{
|
|
||||||
// Check if already exists
|
|
||||||
var exists = await _context.ProjectStockBins
|
|
||||||
.AnyAsync(b => b.ProjectId == projectId && b.LengthInches == stock.LengthInches);
|
|
||||||
|
|
||||||
if (!exists)
|
|
||||||
{
|
|
||||||
_context.ProjectStockBins.Add(new ProjectStockBin
|
|
||||||
{
|
|
||||||
ProjectId = projectId,
|
|
||||||
LengthInches = stock.LengthInches,
|
|
||||||
Quantity = -1,
|
|
||||||
Priority = 25,
|
|
||||||
SortOrder = ++maxOrder
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var project = await _context.Projects.FindAsync(projectId);
|
|
||||||
if (project != null)
|
|
||||||
{
|
|
||||||
project.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cutting tools
|
// Cutting tools
|
||||||
public async Task<List<CuttingTool>> GetCuttingToolsAsync(bool includeInactive = false)
|
public async Task<List<CuttingTool>> GetCuttingToolsAsync(bool includeInactive = false)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user