feat(back-end): new stock db and back-office improvements
This commit is contained in:
@@ -3,18 +3,27 @@ package com.printcalculator.controller;
|
||||
import com.printcalculator.dto.OptionsResponse;
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.*; // This line replaces specific entity imports
|
||||
import com.printcalculator.entity.LayerHeightOption;
|
||||
import com.printcalculator.entity.MaterialOrcaProfileMap;
|
||||
import com.printcalculator.entity.NozzleOption;
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.entity.PrinterMachineProfile;
|
||||
import com.printcalculator.repository.FilamentMaterialTypeRepository;
|
||||
import com.printcalculator.repository.FilamentVariantRepository;
|
||||
import com.printcalculator.repository.LayerHeightOptionRepository;
|
||||
import com.printcalculator.repository.MaterialOrcaProfileMapRepository;
|
||||
import com.printcalculator.repository.NozzleOptionRepository;
|
||||
import com.printcalculator.repository.PrinterMachineRepository;
|
||||
import com.printcalculator.service.OrcaProfileResolver;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@@ -24,89 +33,177 @@ public class OptionsController {
|
||||
private final FilamentVariantRepository variantRepo;
|
||||
private final LayerHeightOptionRepository layerHeightRepo;
|
||||
private final NozzleOptionRepository nozzleRepo;
|
||||
private final PrinterMachineRepository printerMachineRepo;
|
||||
private final MaterialOrcaProfileMapRepository materialOrcaMapRepo;
|
||||
private final OrcaProfileResolver orcaProfileResolver;
|
||||
|
||||
public OptionsController(FilamentMaterialTypeRepository materialRepo,
|
||||
FilamentVariantRepository variantRepo,
|
||||
LayerHeightOptionRepository layerHeightRepo,
|
||||
NozzleOptionRepository nozzleRepo) {
|
||||
NozzleOptionRepository nozzleRepo,
|
||||
PrinterMachineRepository printerMachineRepo,
|
||||
MaterialOrcaProfileMapRepository materialOrcaMapRepo,
|
||||
OrcaProfileResolver orcaProfileResolver) {
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.layerHeightRepo = layerHeightRepo;
|
||||
this.nozzleRepo = nozzleRepo;
|
||||
this.printerMachineRepo = printerMachineRepo;
|
||||
this.materialOrcaMapRepo = materialOrcaMapRepo;
|
||||
this.orcaProfileResolver = orcaProfileResolver;
|
||||
}
|
||||
|
||||
@GetMapping("/api/calculator/options")
|
||||
public ResponseEntity<OptionsResponse> getOptions() {
|
||||
// 1. Materials & Variants
|
||||
public ResponseEntity<OptionsResponse> getOptions(
|
||||
@RequestParam(value = "printerMachineId", required = false) Long printerMachineId,
|
||||
@RequestParam(value = "nozzleDiameter", required = false) Double nozzleDiameter
|
||||
) {
|
||||
List<FilamentMaterialType> types = materialRepo.findAll();
|
||||
List<FilamentVariant> allVariants = variantRepo.findAll();
|
||||
List<FilamentVariant> allVariants = variantRepo.findAll().stream()
|
||||
.filter(v -> Boolean.TRUE.equals(v.getIsActive()))
|
||||
.sorted(Comparator
|
||||
.comparing((FilamentVariant v) -> safeMaterialCode(v.getFilamentMaterialType()), String.CASE_INSENSITIVE_ORDER)
|
||||
.thenComparing(v -> safeString(v.getVariantDisplayName()), String.CASE_INSENSITIVE_ORDER))
|
||||
.toList();
|
||||
|
||||
Set<Long> compatibleMaterialTypeIds = resolveCompatibleMaterialTypeIds(printerMachineId, nozzleDiameter);
|
||||
|
||||
List<OptionsResponse.MaterialOption> materialOptions = types.stream()
|
||||
.sorted(Comparator.comparing(t -> safeString(t.getMaterialCode()), String.CASE_INSENSITIVE_ORDER))
|
||||
.map(type -> {
|
||||
if (!compatibleMaterialTypeIds.isEmpty() && !compatibleMaterialTypeIds.contains(type.getId())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<OptionsResponse.VariantOption> variants = allVariants.stream()
|
||||
.filter(v -> v.getFilamentMaterialType().getId().equals(type.getId()) && v.getIsActive())
|
||||
.filter(v -> v.getFilamentMaterialType() != null
|
||||
&& v.getFilamentMaterialType().getId().equals(type.getId()))
|
||||
.map(v -> new OptionsResponse.VariantOption(
|
||||
v.getId(),
|
||||
v.getVariantDisplayName(),
|
||||
v.getColorName(),
|
||||
getColorHex(v.getColorName()), // Need helper or store hex in DB
|
||||
v.getStockSpools().doubleValue() <= 0
|
||||
resolveHexColor(v),
|
||||
v.getFinishType() != null ? v.getFinishType() : "GLOSSY",
|
||||
v.getStockSpools() != null ? v.getStockSpools().doubleValue() : 0d,
|
||||
toStockFilamentGrams(v),
|
||||
v.getStockSpools() == null || v.getStockSpools().doubleValue() <= 0
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Only include material if it has active variants
|
||||
if (variants.isEmpty()) return null;
|
||||
if (variants.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new OptionsResponse.MaterialOption(
|
||||
type.getMaterialCode(),
|
||||
type.getMaterialCode() + (type.getIsFlexible() ? " (Flexible)" : " (Standard)"),
|
||||
type.getMaterialCode() + (Boolean.TRUE.equals(type.getIsFlexible()) ? " (Flexible)" : " (Standard)"),
|
||||
variants
|
||||
);
|
||||
})
|
||||
.filter(m -> m != null)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
// 2. Qualities (Static as per user request)
|
||||
List<OptionsResponse.QualityOption> qualities = List.of(
|
||||
new OptionsResponse.QualityOption("draft", "Draft"),
|
||||
new OptionsResponse.QualityOption("standard", "Standard"),
|
||||
new OptionsResponse.QualityOption("extra_fine", "High Definition")
|
||||
);
|
||||
|
||||
// 3. Infill Patterns (Static as per user request)
|
||||
List<OptionsResponse.InfillPatternOption> patterns = List.of(
|
||||
new OptionsResponse.InfillPatternOption("grid", "Grid"),
|
||||
new OptionsResponse.InfillPatternOption("gyroid", "Gyroid"),
|
||||
new OptionsResponse.InfillPatternOption("cubic", "Cubic")
|
||||
);
|
||||
|
||||
// 4. Layer Heights
|
||||
List<OptionsResponse.LayerHeightOptionDTO> layers = layerHeightRepo.findAll().stream()
|
||||
.filter(l -> l.getIsActive())
|
||||
.filter(l -> Boolean.TRUE.equals(l.getIsActive()))
|
||||
.sorted(Comparator.comparing(LayerHeightOption::getLayerHeightMm))
|
||||
.map(l -> new OptionsResponse.LayerHeightOptionDTO(
|
||||
l.getLayerHeightMm().doubleValue(),
|
||||
String.format("%.2f mm", l.getLayerHeightMm())
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
// 5. Nozzles
|
||||
List<OptionsResponse.NozzleOptionDTO> nozzles = nozzleRepo.findAll().stream()
|
||||
.filter(n -> n.getIsActive())
|
||||
.filter(n -> Boolean.TRUE.equals(n.getIsActive()))
|
||||
.sorted(Comparator.comparing(NozzleOption::getNozzleDiameterMm))
|
||||
.map(n -> new OptionsResponse.NozzleOptionDTO(
|
||||
n.getNozzleDiameterMm().doubleValue(),
|
||||
String.format("%.1f mm%s", n.getNozzleDiameterMm(),
|
||||
n.getExtraNozzleChangeFeeChf().doubleValue() > 0
|
||||
? String.format(" (+ %.2f CHF)", n.getExtraNozzleChangeFeeChf())
|
||||
: " (Standard)")
|
||||
n.getExtraNozzleChangeFeeChf().doubleValue() > 0
|
||||
? String.format(" (+ %.2f CHF)", n.getExtraNozzleChangeFeeChf())
|
||||
: " (Standard)")
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
return ResponseEntity.ok(new OptionsResponse(materialOptions, qualities, patterns, layers, nozzles));
|
||||
}
|
||||
|
||||
// Temporary helper until we add hex to DB
|
||||
private Set<Long> resolveCompatibleMaterialTypeIds(Long printerMachineId, Double nozzleDiameter) {
|
||||
PrinterMachine machine = null;
|
||||
if (printerMachineId != null) {
|
||||
machine = printerMachineRepo.findById(printerMachineId).orElse(null);
|
||||
}
|
||||
if (machine == null) {
|
||||
machine = printerMachineRepo.findFirstByIsActiveTrue().orElse(null);
|
||||
}
|
||||
if (machine == null) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
BigDecimal nozzle = nozzleDiameter != null
|
||||
? BigDecimal.valueOf(nozzleDiameter)
|
||||
: BigDecimal.valueOf(0.40);
|
||||
|
||||
PrinterMachineProfile machineProfile = orcaProfileResolver
|
||||
.resolveMachineProfile(machine, nozzle)
|
||||
.orElse(null);
|
||||
|
||||
if (machineProfile == null) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
List<MaterialOrcaProfileMap> maps = materialOrcaMapRepo.findByPrinterMachineProfileAndIsActiveTrue(machineProfile);
|
||||
return maps.stream()
|
||||
.map(MaterialOrcaProfileMap::getFilamentMaterialType)
|
||||
.filter(m -> m != null && m.getId() != null)
|
||||
.map(FilamentMaterialType::getId)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private String resolveHexColor(FilamentVariant variant) {
|
||||
if (variant.getColorHex() != null && !variant.getColorHex().isBlank()) {
|
||||
return variant.getColorHex();
|
||||
}
|
||||
return getColorHex(variant.getColorName());
|
||||
}
|
||||
|
||||
private double toStockFilamentGrams(FilamentVariant variant) {
|
||||
if (variant.getStockSpools() == null || variant.getSpoolNetKg() == null) {
|
||||
return 0d;
|
||||
}
|
||||
return variant.getStockSpools()
|
||||
.multiply(variant.getSpoolNetKg())
|
||||
.multiply(BigDecimal.valueOf(1000))
|
||||
.doubleValue();
|
||||
}
|
||||
|
||||
private String safeMaterialCode(FilamentMaterialType type) {
|
||||
if (type == null || type.getMaterialCode() == null) {
|
||||
return "";
|
||||
}
|
||||
return type.getMaterialCode();
|
||||
}
|
||||
|
||||
private String safeString(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
// Temporary helper for legacy values where color hex is not yet set in DB
|
||||
private String getColorHex(String colorName) {
|
||||
if (colorName == null) {
|
||||
return "#9e9e9e";
|
||||
}
|
||||
String lower = colorName.toLowerCase();
|
||||
if (lower.contains("black") || lower.contains("nero")) return "#1a1a1a";
|
||||
if (lower.contains("white") || lower.contains("bianco")) return "#f5f5f5";
|
||||
@@ -120,6 +217,6 @@ public class OptionsController {
|
||||
}
|
||||
if (lower.contains("purple") || lower.contains("viola")) return "#7b1fa2";
|
||||
if (lower.contains("yellow") || lower.contains("giallo")) return "#fbc02d";
|
||||
return "#9e9e9e"; // Default grey
|
||||
return "#9e9e9e";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.printcalculator.controller;
|
||||
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.entity.QuoteLineItem;
|
||||
import com.printcalculator.entity.QuoteSession;
|
||||
import com.printcalculator.model.ModelDimensions;
|
||||
import com.printcalculator.model.PrintStats;
|
||||
import com.printcalculator.model.QuoteResult;
|
||||
import com.printcalculator.repository.FilamentMaterialTypeRepository;
|
||||
import com.printcalculator.repository.FilamentVariantRepository;
|
||||
import com.printcalculator.repository.PrinterMachineRepository;
|
||||
import com.printcalculator.repository.QuoteLineItemRepository;
|
||||
import com.printcalculator.repository.QuoteSessionRepository;
|
||||
import com.printcalculator.service.OrcaProfileResolver;
|
||||
import com.printcalculator.service.QuoteCalculator;
|
||||
import com.printcalculator.service.SlicerService;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -43,18 +48,20 @@ public class QuoteSessionController {
|
||||
private final SlicerService slicerService;
|
||||
private final QuoteCalculator quoteCalculator;
|
||||
private final PrinterMachineRepository machineRepo;
|
||||
private final FilamentMaterialTypeRepository materialRepo;
|
||||
private final FilamentVariantRepository variantRepo;
|
||||
private final OrcaProfileResolver orcaProfileResolver;
|
||||
private final com.printcalculator.repository.PricingPolicyRepository pricingRepo;
|
||||
private final com.printcalculator.service.ClamAVService clamAVService;
|
||||
|
||||
// Defaults
|
||||
private static final String DEFAULT_FILAMENT = "pla_basic";
|
||||
private static final String DEFAULT_PROCESS = "standard";
|
||||
|
||||
public QuoteSessionController(QuoteSessionRepository sessionRepo,
|
||||
QuoteLineItemRepository lineItemRepo,
|
||||
SlicerService slicerService,
|
||||
QuoteCalculator quoteCalculator,
|
||||
PrinterMachineRepository machineRepo,
|
||||
FilamentMaterialTypeRepository materialRepo,
|
||||
FilamentVariantRepository variantRepo,
|
||||
OrcaProfileResolver orcaProfileResolver,
|
||||
com.printcalculator.repository.PricingPolicyRepository pricingRepo,
|
||||
com.printcalculator.service.ClamAVService clamAVService) {
|
||||
this.sessionRepo = sessionRepo;
|
||||
@@ -62,6 +69,9 @@ public class QuoteSessionController {
|
||||
this.slicerService = slicerService;
|
||||
this.quoteCalculator = quoteCalculator;
|
||||
this.machineRepo = machineRepo;
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.orcaProfileResolver = orcaProfileResolver;
|
||||
this.pricingRepo = pricingRepo;
|
||||
this.clamAVService = clamAVService;
|
||||
}
|
||||
@@ -129,45 +139,31 @@ public class QuoteSessionController {
|
||||
// Apply Basic/Advanced Logic
|
||||
applyPrintSettings(settings);
|
||||
|
||||
BigDecimal nozzleDiameter = BigDecimal.valueOf(settings.getNozzleDiameter() != null ? settings.getNozzleDiameter() : 0.4);
|
||||
|
||||
// Pick machine (selected machine if provided, otherwise first active)
|
||||
PrinterMachine machine = resolvePrinterMachine(settings.getPrinterMachineId());
|
||||
|
||||
// Resolve selected filament variant
|
||||
FilamentVariant selectedVariant = resolveFilamentVariant(settings);
|
||||
|
||||
// Update session global settings from the most recent item added
|
||||
session.setMaterialCode(settings.getMaterial());
|
||||
session.setNozzleDiameterMm(BigDecimal.valueOf(settings.getNozzleDiameter() != null ? settings.getNozzleDiameter() : 0.4));
|
||||
session.setMaterialCode(selectedVariant.getFilamentMaterialType().getMaterialCode());
|
||||
session.setNozzleDiameterMm(nozzleDiameter);
|
||||
session.setLayerHeightMm(BigDecimal.valueOf(settings.getLayerHeight() != null ? settings.getLayerHeight() : 0.2));
|
||||
session.setInfillPattern(settings.getInfillPattern());
|
||||
session.setInfillPercent(settings.getInfillDensity() != null ? settings.getInfillDensity().intValue() : 20);
|
||||
session.setSupportsEnabled(settings.getSupportsEnabled() != null ? settings.getSupportsEnabled() : false);
|
||||
sessionRepo.save(session);
|
||||
|
||||
// REAL SLICING
|
||||
// 1. Pick Machine (default to first active or specific)
|
||||
PrinterMachine machine = machineRepo.findFirstByIsActiveTrue()
|
||||
.orElseThrow(() -> new RuntimeException("No active printer found"));
|
||||
|
||||
// 2. Pick Profiles
|
||||
String machineProfile = machine.getPrinterDisplayName(); // e.g. "Bambu Lab A1 0.4 nozzle"
|
||||
// If the display name doesn't match the json profile name, we might need a mapping key in DB.
|
||||
// For now assuming display name works or we use a tough default
|
||||
machineProfile = "Bambu Lab A1 0.4 nozzle"; // Force known good for now? Or use DB field if exists.
|
||||
// Ideally: machine.getSlicerProfileName();
|
||||
|
||||
String filamentProfile = "Generic " + (settings.getMaterial() != null ? settings.getMaterial().toUpperCase() : "PLA");
|
||||
// Mapping: "pla_basic" -> "Generic PLA", "petg_basic" -> "Generic PETG"
|
||||
if (settings.getMaterial() != null) {
|
||||
if (settings.getMaterial().toLowerCase().contains("pla")) filamentProfile = "Generic PLA";
|
||||
else if (settings.getMaterial().toLowerCase().contains("petg")) filamentProfile = "Generic PETG";
|
||||
else if (settings.getMaterial().toLowerCase().contains("tpu")) filamentProfile = "Generic TPU";
|
||||
else if (settings.getMaterial().toLowerCase().contains("abs")) filamentProfile = "Generic ABS";
|
||||
}
|
||||
OrcaProfileResolver.ResolvedProfiles profiles = orcaProfileResolver.resolve(machine, nozzleDiameter, selectedVariant);
|
||||
String machineProfile = profiles.machineProfileName();
|
||||
String filamentProfile = profiles.filamentProfileName();
|
||||
|
||||
String processProfile = "0.20mm Standard @BBL A1";
|
||||
// Mapping quality to process
|
||||
// "standard" -> "0.20mm Standard @BBL A1"
|
||||
// "draft" -> "0.28mm Extra Draft @BBL A1"
|
||||
// "high" -> "0.12mm Fine @BBL A1" (approx names, need to be exact for Orca)
|
||||
// Let's use robust defaults or simple overrides
|
||||
String processProfile = "standard";
|
||||
if (settings.getLayerHeight() != null) {
|
||||
if (settings.getLayerHeight() >= 0.28) processProfile = "0.28mm Extra Draft @BBL A1";
|
||||
else if (settings.getLayerHeight() <= 0.12) processProfile = "0.12mm Fine @BBL A1";
|
||||
if (settings.getLayerHeight() >= 0.28) processProfile = "draft";
|
||||
else if (settings.getLayerHeight() <= 0.12) processProfile = "extra_fine";
|
||||
}
|
||||
|
||||
// Build overrides map from settings
|
||||
@@ -189,7 +185,7 @@ public class QuoteSessionController {
|
||||
Optional<ModelDimensions> modelDimensions = slicerService.inspectModelDimensions(persistentPath.toFile());
|
||||
|
||||
// 4. Calculate Quote
|
||||
QuoteResult result = quoteCalculator.calculate(stats, machine.getPrinterDisplayName(), filamentProfile);
|
||||
QuoteResult result = quoteCalculator.calculate(stats, machine.getPrinterDisplayName(), selectedVariant);
|
||||
|
||||
// 5. Create Line Item
|
||||
QuoteLineItem item = new QuoteLineItem();
|
||||
@@ -197,7 +193,8 @@ public class QuoteSessionController {
|
||||
item.setOriginalFilename(file.getOriginalFilename());
|
||||
item.setStoredPath(persistentPath.toString()); // SAVE PATH
|
||||
item.setQuantity(1);
|
||||
item.setColorCode(settings.getColor() != null ? settings.getColor() : "#FFFFFF");
|
||||
item.setColorCode(selectedVariant.getColorName());
|
||||
item.setFilamentVariant(selectedVariant);
|
||||
item.setStatus("READY"); // or CALCULATED
|
||||
|
||||
item.setPrintTimeSeconds((int) stats.printTimeSeconds());
|
||||
@@ -264,6 +261,50 @@ public class QuoteSessionController {
|
||||
}
|
||||
}
|
||||
|
||||
private PrinterMachine resolvePrinterMachine(Long printerMachineId) {
|
||||
if (printerMachineId != null) {
|
||||
PrinterMachine selected = machineRepo.findById(printerMachineId)
|
||||
.orElseThrow(() -> new RuntimeException("Printer machine not found: " + printerMachineId));
|
||||
if (!Boolean.TRUE.equals(selected.getIsActive())) {
|
||||
throw new RuntimeException("Selected printer machine is not active");
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
return machineRepo.findFirstByIsActiveTrue()
|
||||
.orElseThrow(() -> new RuntimeException("No active printer found"));
|
||||
}
|
||||
|
||||
private FilamentVariant resolveFilamentVariant(com.printcalculator.dto.PrintSettingsDto settings) {
|
||||
if (settings.getFilamentVariantId() != null) {
|
||||
FilamentVariant variant = variantRepo.findById(settings.getFilamentVariantId())
|
||||
.orElseThrow(() -> new RuntimeException("Filament variant not found: " + settings.getFilamentVariantId()));
|
||||
if (!Boolean.TRUE.equals(variant.getIsActive())) {
|
||||
throw new RuntimeException("Selected filament variant is not active");
|
||||
}
|
||||
return variant;
|
||||
}
|
||||
|
||||
String requestedMaterialCode = settings.getMaterial() != null
|
||||
? settings.getMaterial().trim().toUpperCase()
|
||||
: "PLA";
|
||||
|
||||
FilamentMaterialType materialType = materialRepo.findByMaterialCode(requestedMaterialCode)
|
||||
.orElseGet(() -> materialRepo.findByMaterialCode("PLA")
|
||||
.orElseThrow(() -> new RuntimeException("Fallback material PLA not configured")));
|
||||
|
||||
String requestedColor = settings.getColor() != null ? settings.getColor().trim() : null;
|
||||
if (requestedColor != null && !requestedColor.isBlank()) {
|
||||
Optional<FilamentVariant> byColor = variantRepo.findByFilamentMaterialTypeAndColorName(materialType, requestedColor);
|
||||
if (byColor.isPresent() && Boolean.TRUE.equals(byColor.get().getIsActive())) {
|
||||
return byColor.get();
|
||||
}
|
||||
}
|
||||
|
||||
return variantRepo.findFirstByFilamentMaterialTypeAndIsActiveTrue(materialType)
|
||||
.orElseThrow(() -> new RuntimeException("No active variant for material: " + requestedMaterialCode));
|
||||
}
|
||||
|
||||
// 3. Update Line Item
|
||||
@PatchMapping("/line-items/{lineItemId}")
|
||||
@Transactional
|
||||
@@ -344,6 +385,7 @@ public class QuoteSessionController {
|
||||
dto.put("printTimeSeconds", item.getPrintTimeSeconds());
|
||||
dto.put("materialGrams", item.getMaterialGrams());
|
||||
dto.put("colorCode", item.getColorCode());
|
||||
dto.put("filamentVariantId", item.getFilamentVariant() != null ? item.getFilamentVariant().getId() : null);
|
||||
dto.put("status", item.getStatus());
|
||||
|
||||
BigDecimal unitPrice = item.getUnitPriceChf();
|
||||
|
||||
@@ -8,6 +8,8 @@ import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.repository.FilamentMaterialTypeRepository;
|
||||
import com.printcalculator.repository.FilamentVariantRepository;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.QuoteLineItemRepository;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -17,8 +19,12 @@ import java.math.BigDecimal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.CONFLICT;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@RestController
|
||||
@@ -26,16 +32,26 @@ import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
@Transactional(readOnly = true)
|
||||
public class AdminFilamentController {
|
||||
private static final BigDecimal MAX_NUMERIC_6_3 = new BigDecimal("999.999");
|
||||
private static final Pattern HEX_COLOR_PATTERN = Pattern.compile("^#[0-9A-Fa-f]{6}$");
|
||||
private static final Set<String> ALLOWED_FINISH_TYPES = Set.of(
|
||||
"GLOSSY", "MATTE", "MARBLE", "SILK", "TRANSLUCENT", "SPECIAL"
|
||||
);
|
||||
|
||||
private final FilamentMaterialTypeRepository materialRepo;
|
||||
private final FilamentVariantRepository variantRepo;
|
||||
private final QuoteLineItemRepository quoteLineItemRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
|
||||
public AdminFilamentController(
|
||||
FilamentMaterialTypeRepository materialRepo,
|
||||
FilamentVariantRepository variantRepo
|
||||
FilamentVariantRepository variantRepo,
|
||||
QuoteLineItemRepository quoteLineItemRepo,
|
||||
OrderItemRepository orderItemRepo
|
||||
) {
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.quoteLineItemRepo = quoteLineItemRepo;
|
||||
this.orderItemRepo = orderItemRepo;
|
||||
}
|
||||
|
||||
@GetMapping("/materials")
|
||||
@@ -130,6 +146,20 @@ public class AdminFilamentController {
|
||||
return ResponseEntity.ok(toVariantDto(saved));
|
||||
}
|
||||
|
||||
@DeleteMapping("/variants/{variantId}")
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteVariant(@PathVariable Long variantId) {
|
||||
FilamentVariant variant = variantRepo.findById(variantId)
|
||||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Filament variant not found"));
|
||||
|
||||
if (quoteLineItemRepo.existsByFilamentVariant_Id(variantId) || orderItemRepo.existsByFilamentVariant_Id(variantId)) {
|
||||
throw new ResponseStatusException(CONFLICT, "Variant is already used in quotes/orders and cannot be deleted");
|
||||
}
|
||||
|
||||
variantRepo.delete(variant);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
private void applyMaterialPayload(
|
||||
FilamentMaterialType material,
|
||||
AdminUpsertFilamentMaterialTypeRequest payload,
|
||||
@@ -156,10 +186,17 @@ public class AdminFilamentController {
|
||||
String normalizedDisplayName,
|
||||
String normalizedColorName
|
||||
) {
|
||||
String normalizedColorHex = normalizeAndValidateColorHex(payload.getColorHex());
|
||||
String normalizedFinishType = normalizeAndValidateFinishType(payload.getFinishType(), payload.getIsMatte());
|
||||
String normalizedBrand = normalizeOptional(payload.getBrand());
|
||||
|
||||
variant.setFilamentMaterialType(material);
|
||||
variant.setVariantDisplayName(normalizedDisplayName);
|
||||
variant.setColorName(normalizedColorName);
|
||||
variant.setIsMatte(Boolean.TRUE.equals(payload.getIsMatte()));
|
||||
variant.setColorHex(normalizedColorHex);
|
||||
variant.setFinishType(normalizedFinishType);
|
||||
variant.setBrand(normalizedBrand);
|
||||
variant.setIsMatte(Boolean.TRUE.equals(payload.getIsMatte()) || "MATTE".equals(normalizedFinishType));
|
||||
variant.setIsSpecial(Boolean.TRUE.equals(payload.getIsSpecial()));
|
||||
variant.setCostChfPerKg(payload.getCostChfPerKg());
|
||||
variant.setStockSpools(payload.getStockSpools());
|
||||
@@ -188,6 +225,35 @@ public class AdminFilamentController {
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
private String normalizeAndValidateColorHex(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String normalized = value.trim();
|
||||
if (!HEX_COLOR_PATTERN.matcher(normalized).matches()) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Color hex must be in format #RRGGBB");
|
||||
}
|
||||
return normalized.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private String normalizeAndValidateFinishType(String finishType, Boolean isMatte) {
|
||||
String normalized = finishType == null || finishType.isBlank()
|
||||
? (Boolean.TRUE.equals(isMatte) ? "MATTE" : "GLOSSY")
|
||||
: finishType.trim().toUpperCase(Locale.ROOT);
|
||||
if (!ALLOWED_FINISH_TYPES.contains(normalized)) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Invalid finish type");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String normalizeOptional(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String normalized = value.trim();
|
||||
return normalized.isBlank() ? null : normalized;
|
||||
}
|
||||
|
||||
private FilamentMaterialType validateAndResolveMaterial(AdminUpsertFilamentVariantRequest payload) {
|
||||
if (payload == null || payload.getMaterialTypeId() == null) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Material type id is required");
|
||||
@@ -268,16 +334,20 @@ public class AdminFilamentController {
|
||||
|
||||
dto.setVariantDisplayName(variant.getVariantDisplayName());
|
||||
dto.setColorName(variant.getColorName());
|
||||
dto.setColorHex(variant.getColorHex());
|
||||
dto.setFinishType(variant.getFinishType());
|
||||
dto.setBrand(variant.getBrand());
|
||||
dto.setIsMatte(variant.getIsMatte());
|
||||
dto.setIsSpecial(variant.getIsSpecial());
|
||||
dto.setCostChfPerKg(variant.getCostChfPerKg());
|
||||
dto.setStockSpools(variant.getStockSpools());
|
||||
dto.setSpoolNetKg(variant.getSpoolNetKg());
|
||||
BigDecimal stockKg = BigDecimal.ZERO;
|
||||
if (variant.getStockSpools() != null && variant.getSpoolNetKg() != null) {
|
||||
dto.setStockKg(variant.getStockSpools().multiply(variant.getSpoolNetKg()));
|
||||
} else {
|
||||
dto.setStockKg(BigDecimal.ZERO);
|
||||
stockKg = variant.getStockSpools().multiply(variant.getSpoolNetKg());
|
||||
}
|
||||
dto.setStockKg(stockKg);
|
||||
dto.setStockFilamentGrams(stockKg.multiply(BigDecimal.valueOf(1000)));
|
||||
dto.setIsActive(variant.getIsActive());
|
||||
dto.setCreatedAt(variant.getCreatedAt());
|
||||
return dto;
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
@@ -105,6 +106,10 @@ public class AdminOperationsController {
|
||||
dto.setStockSpools(stock.getStockSpools());
|
||||
dto.setSpoolNetKg(stock.getSpoolNetKg());
|
||||
dto.setStockKg(stock.getStockKg());
|
||||
BigDecimal grams = stock.getStockKg() != null
|
||||
? stock.getStockKg().multiply(BigDecimal.valueOf(1000))
|
||||
: BigDecimal.ZERO;
|
||||
dto.setStockFilamentGrams(grams);
|
||||
|
||||
if (variant != null) {
|
||||
dto.setMaterialCode(
|
||||
|
||||
@@ -10,6 +10,7 @@ public class AdminFilamentStockDto {
|
||||
private BigDecimal stockSpools;
|
||||
private BigDecimal spoolNetKg;
|
||||
private BigDecimal stockKg;
|
||||
private BigDecimal stockFilamentGrams;
|
||||
private Boolean active;
|
||||
|
||||
public Long getFilamentVariantId() {
|
||||
@@ -68,6 +69,14 @@ public class AdminFilamentStockDto {
|
||||
this.stockKg = stockKg;
|
||||
}
|
||||
|
||||
public BigDecimal getStockFilamentGrams() {
|
||||
return stockFilamentGrams;
|
||||
}
|
||||
|
||||
public void setStockFilamentGrams(BigDecimal stockFilamentGrams) {
|
||||
this.stockFilamentGrams = stockFilamentGrams;
|
||||
}
|
||||
|
||||
public Boolean getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
@@ -12,12 +12,16 @@ public class AdminFilamentVariantDto {
|
||||
private String materialTechnicalTypeLabel;
|
||||
private String variantDisplayName;
|
||||
private String colorName;
|
||||
private String colorHex;
|
||||
private String finishType;
|
||||
private String brand;
|
||||
private Boolean isMatte;
|
||||
private Boolean isSpecial;
|
||||
private BigDecimal costChfPerKg;
|
||||
private BigDecimal stockSpools;
|
||||
private BigDecimal spoolNetKg;
|
||||
private BigDecimal stockKg;
|
||||
private BigDecimal stockFilamentGrams;
|
||||
private Boolean isActive;
|
||||
private OffsetDateTime createdAt;
|
||||
|
||||
@@ -85,6 +89,30 @@ public class AdminFilamentVariantDto {
|
||||
this.colorName = colorName;
|
||||
}
|
||||
|
||||
public String getColorHex() {
|
||||
return colorHex;
|
||||
}
|
||||
|
||||
public void setColorHex(String colorHex) {
|
||||
this.colorHex = colorHex;
|
||||
}
|
||||
|
||||
public String getFinishType() {
|
||||
return finishType;
|
||||
}
|
||||
|
||||
public void setFinishType(String finishType) {
|
||||
this.finishType = finishType;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public Boolean getIsMatte() {
|
||||
return isMatte;
|
||||
}
|
||||
@@ -133,6 +161,14 @@ public class AdminFilamentVariantDto {
|
||||
this.stockKg = stockKg;
|
||||
}
|
||||
|
||||
public BigDecimal getStockFilamentGrams() {
|
||||
return stockFilamentGrams;
|
||||
}
|
||||
|
||||
public void setStockFilamentGrams(BigDecimal stockFilamentGrams) {
|
||||
this.stockFilamentGrams = stockFilamentGrams;
|
||||
}
|
||||
|
||||
public Boolean getIsActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ public class AdminUpsertFilamentVariantRequest {
|
||||
private Long materialTypeId;
|
||||
private String variantDisplayName;
|
||||
private String colorName;
|
||||
private String colorHex;
|
||||
private String finishType;
|
||||
private String brand;
|
||||
private Boolean isMatte;
|
||||
private Boolean isSpecial;
|
||||
private BigDecimal costChfPerKg;
|
||||
@@ -37,6 +40,30 @@ public class AdminUpsertFilamentVariantRequest {
|
||||
this.colorName = colorName;
|
||||
}
|
||||
|
||||
public String getColorHex() {
|
||||
return colorHex;
|
||||
}
|
||||
|
||||
public void setColorHex(String colorHex) {
|
||||
this.colorHex = colorHex;
|
||||
}
|
||||
|
||||
public String getFinishType() {
|
||||
return finishType;
|
||||
}
|
||||
|
||||
public void setFinishType(String finishType) {
|
||||
this.finishType = finishType;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public Boolean getIsMatte() {
|
||||
return isMatte;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,16 @@ public record OptionsResponse(
|
||||
List<NozzleOptionDTO> nozzleDiameters
|
||||
) {
|
||||
public record MaterialOption(String code, String label, List<VariantOption> variants) {}
|
||||
public record VariantOption(String name, String colorName, String hexColor, boolean isOutOfStock) {}
|
||||
public record VariantOption(
|
||||
Long id,
|
||||
String name,
|
||||
String colorName,
|
||||
String hexColor,
|
||||
String finishType,
|
||||
Double stockSpools,
|
||||
Double stockFilamentGrams,
|
||||
boolean isOutOfStock
|
||||
) {}
|
||||
public record QualityOption(String id, String label) {}
|
||||
public record InfillPatternOption(String id, String label) {}
|
||||
public record LayerHeightOptionDTO(double value, String label) {}
|
||||
|
||||
@@ -10,6 +10,8 @@ public class PrintSettingsDto {
|
||||
// Common
|
||||
private String material; // e.g. "PLA", "PETG"
|
||||
private String color; // e.g. "White", "#FFFFFF"
|
||||
private Long filamentVariantId;
|
||||
private Long printerMachineId;
|
||||
|
||||
// Basic Mode
|
||||
private String quality; // "draft", "standard", "high"
|
||||
|
||||
@@ -24,6 +24,16 @@ public class FilamentVariant {
|
||||
@Column(name = "color_name", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String colorName;
|
||||
|
||||
@Column(name = "color_hex", length = Integer.MAX_VALUE)
|
||||
private String colorHex;
|
||||
|
||||
@ColumnDefault("'GLOSSY'")
|
||||
@Column(name = "finish_type", length = Integer.MAX_VALUE)
|
||||
private String finishType;
|
||||
|
||||
@Column(name = "brand", length = Integer.MAX_VALUE)
|
||||
private String brand;
|
||||
|
||||
@ColumnDefault("false")
|
||||
@Column(name = "is_matte", nullable = false)
|
||||
private Boolean isMatte;
|
||||
@@ -83,6 +93,30 @@ public class FilamentVariant {
|
||||
this.colorName = colorName;
|
||||
}
|
||||
|
||||
public String getColorHex() {
|
||||
return colorHex;
|
||||
}
|
||||
|
||||
public void setColorHex(String colorHex) {
|
||||
this.colorHex = colorHex;
|
||||
}
|
||||
|
||||
public String getFinishType() {
|
||||
return finishType;
|
||||
}
|
||||
|
||||
public void setFinishType(String finishType) {
|
||||
this.finishType = finishType;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public Boolean getIsMatte() {
|
||||
return isMatte;
|
||||
}
|
||||
@@ -139,4 +173,4 @@ public class FilamentVariant {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.printcalculator.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
@Entity
|
||||
@Table(name = "filament_variant_orca_override", uniqueConstraints = {
|
||||
@UniqueConstraint(name = "ux_filament_variant_orca_override_variant_machine", columnNames = {
|
||||
"filament_variant_id", "printer_machine_profile_id"
|
||||
})
|
||||
})
|
||||
public class FilamentVariantOrcaOverride {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "filament_variant_orca_override_id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "filament_variant_id", nullable = false)
|
||||
private FilamentVariant filamentVariant;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "printer_machine_profile_id", nullable = false)
|
||||
private PrinterMachineProfile printerMachineProfile;
|
||||
|
||||
@Column(name = "orca_filament_profile_name", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String orcaFilamentProfileName;
|
||||
|
||||
@ColumnDefault("true")
|
||||
@Column(name = "is_active", nullable = false)
|
||||
private Boolean isActive;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public FilamentVariant getFilamentVariant() {
|
||||
return filamentVariant;
|
||||
}
|
||||
|
||||
public void setFilamentVariant(FilamentVariant filamentVariant) {
|
||||
this.filamentVariant = filamentVariant;
|
||||
}
|
||||
|
||||
public PrinterMachineProfile getPrinterMachineProfile() {
|
||||
return printerMachineProfile;
|
||||
}
|
||||
|
||||
public void setPrinterMachineProfile(PrinterMachineProfile printerMachineProfile) {
|
||||
this.printerMachineProfile = printerMachineProfile;
|
||||
}
|
||||
|
||||
public String getOrcaFilamentProfileName() {
|
||||
return orcaFilamentProfileName;
|
||||
}
|
||||
|
||||
public void setOrcaFilamentProfileName(String orcaFilamentProfileName) {
|
||||
this.orcaFilamentProfileName = orcaFilamentProfileName;
|
||||
}
|
||||
|
||||
public Boolean getIsActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setIsActive(Boolean isActive) {
|
||||
this.isActive = isActive;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.printcalculator.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
@Entity
|
||||
@Table(name = "material_orca_profile_map", uniqueConstraints = {
|
||||
@UniqueConstraint(name = "ux_material_orca_profile_map_machine_material", columnNames = {
|
||||
"printer_machine_profile_id", "filament_material_type_id"
|
||||
})
|
||||
})
|
||||
public class MaterialOrcaProfileMap {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "material_orca_profile_map_id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "printer_machine_profile_id", nullable = false)
|
||||
private PrinterMachineProfile printerMachineProfile;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "filament_material_type_id", nullable = false)
|
||||
private FilamentMaterialType filamentMaterialType;
|
||||
|
||||
@Column(name = "orca_filament_profile_name", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String orcaFilamentProfileName;
|
||||
|
||||
@ColumnDefault("true")
|
||||
@Column(name = "is_active", nullable = false)
|
||||
private Boolean isActive;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PrinterMachineProfile getPrinterMachineProfile() {
|
||||
return printerMachineProfile;
|
||||
}
|
||||
|
||||
public void setPrinterMachineProfile(PrinterMachineProfile printerMachineProfile) {
|
||||
this.printerMachineProfile = printerMachineProfile;
|
||||
}
|
||||
|
||||
public FilamentMaterialType getFilamentMaterialType() {
|
||||
return filamentMaterialType;
|
||||
}
|
||||
|
||||
public void setFilamentMaterialType(FilamentMaterialType filamentMaterialType) {
|
||||
this.filamentMaterialType = filamentMaterialType;
|
||||
}
|
||||
|
||||
public String getOrcaFilamentProfileName() {
|
||||
return orcaFilamentProfileName;
|
||||
}
|
||||
|
||||
public void setOrcaFilamentProfileName(String orcaFilamentProfileName) {
|
||||
this.orcaFilamentProfileName = orcaFilamentProfileName;
|
||||
}
|
||||
|
||||
public Boolean getIsActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setIsActive(Boolean isActive) {
|
||||
this.isActive = isActive;
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,10 @@ public class OrderItem {
|
||||
@Column(name = "material_code", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String materialCode;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "filament_variant_id")
|
||||
private FilamentVariant filamentVariant;
|
||||
|
||||
@Column(name = "color_code", length = Integer.MAX_VALUE)
|
||||
private String colorCode;
|
||||
|
||||
@@ -158,6 +162,14 @@ public class OrderItem {
|
||||
this.materialCode = materialCode;
|
||||
}
|
||||
|
||||
public FilamentVariant getFilamentVariant() {
|
||||
return filamentVariant;
|
||||
}
|
||||
|
||||
public void setFilamentVariant(FilamentVariant filamentVariant) {
|
||||
this.filamentVariant = filamentVariant;
|
||||
}
|
||||
|
||||
public String getColorCode() {
|
||||
return colorCode;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.printcalculator.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
@Table(name = "printer_machine_profile", uniqueConstraints = {
|
||||
@UniqueConstraint(name = "ux_printer_machine_profile_machine_nozzle", columnNames = {
|
||||
"printer_machine_id", "nozzle_diameter_mm"
|
||||
})
|
||||
})
|
||||
public class PrinterMachineProfile {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "printer_machine_profile_id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "printer_machine_id", nullable = false)
|
||||
private PrinterMachine printerMachine;
|
||||
|
||||
@Column(name = "nozzle_diameter_mm", nullable = false, precision = 4, scale = 2)
|
||||
private BigDecimal nozzleDiameterMm;
|
||||
|
||||
@Column(name = "orca_machine_profile_name", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String orcaMachineProfileName;
|
||||
|
||||
@ColumnDefault("false")
|
||||
@Column(name = "is_default", nullable = false)
|
||||
private Boolean isDefault;
|
||||
|
||||
@ColumnDefault("true")
|
||||
@Column(name = "is_active", nullable = false)
|
||||
private Boolean isActive;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PrinterMachine getPrinterMachine() {
|
||||
return printerMachine;
|
||||
}
|
||||
|
||||
public void setPrinterMachine(PrinterMachine printerMachine) {
|
||||
this.printerMachine = printerMachine;
|
||||
}
|
||||
|
||||
public BigDecimal getNozzleDiameterMm() {
|
||||
return nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public void setNozzleDiameterMm(BigDecimal nozzleDiameterMm) {
|
||||
this.nozzleDiameterMm = nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public String getOrcaMachineProfileName() {
|
||||
return orcaMachineProfileName;
|
||||
}
|
||||
|
||||
public void setOrcaMachineProfileName(String orcaMachineProfileName) {
|
||||
this.orcaMachineProfileName = orcaMachineProfileName;
|
||||
}
|
||||
|
||||
public Boolean getIsDefault() {
|
||||
return isDefault;
|
||||
}
|
||||
|
||||
public void setIsDefault(Boolean isDefault) {
|
||||
this.isDefault = isDefault;
|
||||
}
|
||||
|
||||
public Boolean getIsActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setIsActive(Boolean isActive) {
|
||||
this.isActive = isActive;
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,11 @@ public class QuoteLineItem {
|
||||
@Column(name = "color_code", length = Integer.MAX_VALUE)
|
||||
private String colorCode;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "filament_variant_id")
|
||||
@com.fasterxml.jackson.annotation.JsonIgnore
|
||||
private FilamentVariant filamentVariant;
|
||||
|
||||
@Column(name = "bounding_box_x_mm", precision = 10, scale = 3)
|
||||
private BigDecimal boundingBoxXMm;
|
||||
|
||||
@@ -124,6 +129,14 @@ public class QuoteLineItem {
|
||||
this.colorCode = colorCode;
|
||||
}
|
||||
|
||||
public FilamentVariant getFilamentVariant() {
|
||||
return filamentVariant;
|
||||
}
|
||||
|
||||
public void setFilamentVariant(FilamentVariant filamentVariant) {
|
||||
this.filamentVariant = filamentVariant;
|
||||
}
|
||||
|
||||
public BigDecimal getBoundingBoxXMm() {
|
||||
return boundingBoxXMm;
|
||||
}
|
||||
@@ -212,4 +225,4 @@ public class QuoteLineItem {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.printcalculator.repository;
|
||||
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.FilamentVariantOrcaOverride;
|
||||
import com.printcalculator.entity.PrinterMachineProfile;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface FilamentVariantOrcaOverrideRepository extends JpaRepository<FilamentVariantOrcaOverride, Long> {
|
||||
Optional<FilamentVariantOrcaOverride> findByFilamentVariantAndPrinterMachineProfileAndIsActiveTrue(
|
||||
FilamentVariant filamentVariant,
|
||||
PrinterMachineProfile printerMachineProfile
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.printcalculator.repository;
|
||||
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.MaterialOrcaProfileMap;
|
||||
import com.printcalculator.entity.PrinterMachineProfile;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface MaterialOrcaProfileMapRepository extends JpaRepository<MaterialOrcaProfileMap, Long> {
|
||||
Optional<MaterialOrcaProfileMap> findByPrinterMachineProfileAndFilamentMaterialTypeAndIsActiveTrue(
|
||||
PrinterMachineProfile printerMachineProfile,
|
||||
FilamentMaterialType filamentMaterialType
|
||||
);
|
||||
|
||||
List<MaterialOrcaProfileMap> findByPrinterMachineProfileAndIsActiveTrue(PrinterMachineProfile printerMachineProfile);
|
||||
}
|
||||
@@ -8,4 +8,5 @@ import java.util.UUID;
|
||||
|
||||
public interface OrderItemRepository extends JpaRepository<OrderItem, UUID> {
|
||||
List<OrderItem> findByOrder_Id(UUID orderId);
|
||||
}
|
||||
boolean existsByFilamentVariant_Id(Long filamentVariantId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.printcalculator.repository;
|
||||
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.entity.PrinterMachineProfile;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PrinterMachineProfileRepository extends JpaRepository<PrinterMachineProfile, Long> {
|
||||
Optional<PrinterMachineProfile> findByPrinterMachineAndNozzleDiameterMmAndIsActiveTrue(PrinterMachine printerMachine, BigDecimal nozzleDiameterMm);
|
||||
Optional<PrinterMachineProfile> findFirstByPrinterMachineAndIsDefaultTrueAndIsActiveTrue(PrinterMachine printerMachine);
|
||||
List<PrinterMachineProfile> findByPrinterMachineAndIsActiveTrue(PrinterMachine printerMachine);
|
||||
}
|
||||
@@ -8,4 +8,5 @@ import java.util.UUID;
|
||||
|
||||
public interface QuoteLineItemRepository extends JpaRepository<QuoteLineItem, UUID> {
|
||||
List<QuoteLineItem> findByQuoteSessionId(UUID quoteSessionId);
|
||||
}
|
||||
boolean existsByFilamentVariant_Id(Long filamentVariantId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.printcalculator.service;
|
||||
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.FilamentVariantOrcaOverride;
|
||||
import com.printcalculator.entity.MaterialOrcaProfileMap;
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.entity.PrinterMachineProfile;
|
||||
import com.printcalculator.repository.FilamentVariantOrcaOverrideRepository;
|
||||
import com.printcalculator.repository.MaterialOrcaProfileMapRepository;
|
||||
import com.printcalculator.repository.PrinterMachineProfileRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class OrcaProfileResolver {
|
||||
|
||||
private final PrinterMachineProfileRepository machineProfileRepo;
|
||||
private final MaterialOrcaProfileMapRepository materialMapRepo;
|
||||
private final FilamentVariantOrcaOverrideRepository variantOverrideRepo;
|
||||
|
||||
public OrcaProfileResolver(
|
||||
PrinterMachineProfileRepository machineProfileRepo,
|
||||
MaterialOrcaProfileMapRepository materialMapRepo,
|
||||
FilamentVariantOrcaOverrideRepository variantOverrideRepo
|
||||
) {
|
||||
this.machineProfileRepo = machineProfileRepo;
|
||||
this.materialMapRepo = materialMapRepo;
|
||||
this.variantOverrideRepo = variantOverrideRepo;
|
||||
}
|
||||
|
||||
public ResolvedProfiles resolve(PrinterMachine printerMachine, BigDecimal nozzleDiameterMm, FilamentVariant variant) {
|
||||
Optional<PrinterMachineProfile> machineProfileOpt = resolveMachineProfile(printerMachine, nozzleDiameterMm);
|
||||
|
||||
String machineProfileName = machineProfileOpt
|
||||
.map(PrinterMachineProfile::getOrcaMachineProfileName)
|
||||
.orElseGet(() -> fallbackMachineProfile(printerMachine, nozzleDiameterMm));
|
||||
|
||||
String filamentProfileName = machineProfileOpt
|
||||
.map(machineProfile -> resolveFilamentProfileWithMachineProfile(machineProfile, variant)
|
||||
.orElseGet(() -> fallbackFilamentProfile(variant.getFilamentMaterialType())))
|
||||
.orElseGet(() -> fallbackFilamentProfile(variant.getFilamentMaterialType()));
|
||||
|
||||
return new ResolvedProfiles(machineProfileName, filamentProfileName, machineProfileOpt.orElse(null));
|
||||
}
|
||||
|
||||
public Optional<PrinterMachineProfile> resolveMachineProfile(PrinterMachine machine, BigDecimal nozzleDiameterMm) {
|
||||
if (machine == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
BigDecimal normalizedNozzle = normalizeNozzle(nozzleDiameterMm);
|
||||
if (normalizedNozzle != null) {
|
||||
Optional<PrinterMachineProfile> exact = machineProfileRepo
|
||||
.findByPrinterMachineAndNozzleDiameterMmAndIsActiveTrue(machine, normalizedNozzle);
|
||||
if (exact.isPresent()) {
|
||||
return exact;
|
||||
}
|
||||
}
|
||||
|
||||
Optional<PrinterMachineProfile> defaultProfile = machineProfileRepo
|
||||
.findFirstByPrinterMachineAndIsDefaultTrueAndIsActiveTrue(machine);
|
||||
if (defaultProfile.isPresent()) {
|
||||
return defaultProfile;
|
||||
}
|
||||
|
||||
return machineProfileRepo.findByPrinterMachineAndIsActiveTrue(machine)
|
||||
.stream()
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private Optional<String> resolveFilamentProfileWithMachineProfile(PrinterMachineProfile machineProfile, FilamentVariant variant) {
|
||||
if (machineProfile == null || variant == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Optional<FilamentVariantOrcaOverride> override = variantOverrideRepo
|
||||
.findByFilamentVariantAndPrinterMachineProfileAndIsActiveTrue(variant, machineProfile);
|
||||
|
||||
if (override.isPresent()) {
|
||||
return Optional.ofNullable(override.get().getOrcaFilamentProfileName());
|
||||
}
|
||||
|
||||
Optional<MaterialOrcaProfileMap> map = materialMapRepo
|
||||
.findByPrinterMachineProfileAndFilamentMaterialTypeAndIsActiveTrue(
|
||||
machineProfile,
|
||||
variant.getFilamentMaterialType()
|
||||
);
|
||||
|
||||
return map.map(MaterialOrcaProfileMap::getOrcaFilamentProfileName);
|
||||
}
|
||||
|
||||
private String fallbackMachineProfile(PrinterMachine machine, BigDecimal nozzleDiameterMm) {
|
||||
if (machine == null || machine.getPrinterDisplayName() == null || machine.getPrinterDisplayName().isBlank()) {
|
||||
return "Bambu Lab A1 0.4 nozzle";
|
||||
}
|
||||
|
||||
String displayName = machine.getPrinterDisplayName();
|
||||
if (displayName.toLowerCase().contains("bambulab a1") || displayName.toLowerCase().contains("bambu lab a1")) {
|
||||
BigDecimal normalizedNozzle = normalizeNozzle(nozzleDiameterMm);
|
||||
if (normalizedNozzle == null) {
|
||||
return "Bambu Lab A1 0.4 nozzle";
|
||||
}
|
||||
return "Bambu Lab A1 " + normalizedNozzle.toPlainString() + " nozzle";
|
||||
}
|
||||
|
||||
return displayName;
|
||||
}
|
||||
|
||||
private String fallbackFilamentProfile(FilamentMaterialType materialType) {
|
||||
String materialCode = materialType != null && materialType.getMaterialCode() != null
|
||||
? materialType.getMaterialCode().trim().toUpperCase()
|
||||
: "PLA";
|
||||
|
||||
return switch (materialCode) {
|
||||
case "PETG" -> "Generic PETG";
|
||||
case "TPU" -> "Generic TPU";
|
||||
case "PC" -> "Generic PC";
|
||||
case "ABS" -> "Generic ABS";
|
||||
default -> "Generic PLA";
|
||||
};
|
||||
}
|
||||
|
||||
private BigDecimal normalizeNozzle(BigDecimal nozzleDiameterMm) {
|
||||
if (nozzleDiameterMm == null) {
|
||||
return null;
|
||||
}
|
||||
return nozzleDiameterMm.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public record ResolvedProfiles(
|
||||
String machineProfileName,
|
||||
String filamentProfileName,
|
||||
PrinterMachineProfile machineProfile
|
||||
) {}
|
||||
}
|
||||
@@ -203,7 +203,14 @@ public class OrderService {
|
||||
oItem.setOriginalFilename(qItem.getOriginalFilename());
|
||||
oItem.setQuantity(qItem.getQuantity());
|
||||
oItem.setColorCode(qItem.getColorCode());
|
||||
oItem.setMaterialCode(session.getMaterialCode());
|
||||
oItem.setFilamentVariant(qItem.getFilamentVariant());
|
||||
if (qItem.getFilamentVariant() != null
|
||||
&& qItem.getFilamentVariant().getFilamentMaterialType() != null
|
||||
&& qItem.getFilamentVariant().getFilamentMaterialType().getMaterialCode() != null) {
|
||||
oItem.setMaterialCode(qItem.getFilamentVariant().getFilamentMaterialType().getMaterialCode());
|
||||
} else {
|
||||
oItem.setMaterialCode(session.getMaterialCode());
|
||||
}
|
||||
|
||||
BigDecimal distributedUnitPrice = qItem.getUnitPriceChf();
|
||||
if (totalSeconds.compareTo(BigDecimal.ZERO) > 0 && qItem.getPrintTimeSeconds() != null) {
|
||||
|
||||
@@ -60,40 +60,49 @@ public class QuoteCalculator {
|
||||
.orElseThrow(() -> new RuntimeException("No active printer found"));
|
||||
}
|
||||
|
||||
// 3. Fetch Filament Info
|
||||
// filamentProfileName might be "bambu_pla_basic_black" or "Generic PLA"
|
||||
// We try to extract material code (PLA, PETG)
|
||||
String materialCode = detectMaterialCode(filamentProfileName);
|
||||
FilamentMaterialType materialType = materialRepo.findByMaterialCode(materialCode)
|
||||
.orElseThrow(() -> new RuntimeException("Unknown material type: " + materialCode));
|
||||
|
||||
// Try to find specific variant (e.g. by color if we could parse it)
|
||||
// For now, get default/first active variant for this material
|
||||
FilamentVariant variant = variantRepo.findFirstByFilamentMaterialTypeAndIsActiveTrue(materialType)
|
||||
.orElseThrow(() -> new RuntimeException("No active variant for material: " + materialCode));
|
||||
|
||||
return calculate(stats, machine, policy, variant);
|
||||
}
|
||||
|
||||
// --- CALCULATIONS ---
|
||||
public QuoteResult calculate(PrintStats stats, String machineName, FilamentVariant variant) {
|
||||
PricingPolicy policy = pricingRepo.findFirstByIsActiveTrueOrderByValidFromDesc();
|
||||
if (policy == null) {
|
||||
throw new RuntimeException("No active pricing policy found");
|
||||
}
|
||||
|
||||
// Material Cost: (weight / 1000) * costPerKg
|
||||
BigDecimal weightKg = BigDecimal.valueOf(stats.filamentWeightGrams()).divide(BigDecimal.valueOf(1000), 4, RoundingMode.HALF_UP);
|
||||
PrinterMachine machine = machineRepo.findByPrinterDisplayName(machineName).orElse(null);
|
||||
if (machine == null) {
|
||||
machine = machineRepo.findFirstByIsActiveTrue()
|
||||
.orElseThrow(() -> new RuntimeException("No active printer found"));
|
||||
}
|
||||
|
||||
return calculate(stats, machine, policy, variant);
|
||||
}
|
||||
|
||||
private QuoteResult calculate(PrintStats stats, PrinterMachine machine, PricingPolicy policy, FilamentVariant variant) {
|
||||
BigDecimal weightKg = BigDecimal.valueOf(stats.filamentWeightGrams())
|
||||
.divide(BigDecimal.valueOf(1000), 4, RoundingMode.HALF_UP);
|
||||
BigDecimal materialCost = weightKg.multiply(variant.getCostChfPerKg());
|
||||
|
||||
// We do NOT add tiered machine cost here anymore - it is calculated globally per session
|
||||
BigDecimal totalHours = BigDecimal.valueOf(stats.printTimeSeconds()).divide(BigDecimal.valueOf(3600), 4, RoundingMode.HALF_UP);
|
||||
BigDecimal totalHours = BigDecimal.valueOf(stats.printTimeSeconds())
|
||||
.divide(BigDecimal.valueOf(3600), 4, RoundingMode.HALF_UP);
|
||||
|
||||
// Energy Cost: (watts / 1000) * hours * costPerKwh
|
||||
BigDecimal kw = BigDecimal.valueOf(machine.getPowerWatts()).divide(BigDecimal.valueOf(1000), 4, RoundingMode.HALF_UP);
|
||||
BigDecimal kw = BigDecimal.valueOf(machine.getPowerWatts())
|
||||
.divide(BigDecimal.valueOf(1000), 4, RoundingMode.HALF_UP);
|
||||
BigDecimal kwh = kw.multiply(totalHours);
|
||||
BigDecimal energyCost = kwh.multiply(policy.getElectricityCostChfPerKwh());
|
||||
|
||||
// Subtotal (Costs without Fixed Fees and without Machine Tiers)
|
||||
BigDecimal subtotal = materialCost.add(energyCost);
|
||||
|
||||
// Markup
|
||||
// Markup is percentage (e.g. 20.0)
|
||||
BigDecimal markupFactor = BigDecimal.ONE.add(policy.getMarkupPercent().divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP));
|
||||
BigDecimal markupFactor = BigDecimal.ONE.add(
|
||||
policy.getMarkupPercent().divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP)
|
||||
);
|
||||
subtotal = subtotal.multiply(markupFactor);
|
||||
|
||||
return new QuoteResult(subtotal.doubleValue(), "CHF", stats);
|
||||
}
|
||||
public BigDecimal calculateSessionMachineCost(PricingPolicy policy, BigDecimal hours) {
|
||||
|
||||
Reference in New Issue
Block a user