Compare commits
32 Commits
feat/cad-b
...
a7491130fb
| Author | SHA1 | Date | |
|---|---|---|---|
| a7491130fb | |||
| 8e23bd97e6 | |||
| 71424f086e | |||
| b2edf5ec4c | |||
| 8c61990827 | |||
| 54b50028b1 | |||
| 9facf05c10 | |||
| fe3951b6c3 | |||
|
|
d061f21d79 | ||
| 266fab5e17 | |||
| a4b85b01bd | |||
| 30e28cb019 | |||
| 1a36808d9f | |||
| 8a57aa78fb | |||
| de9e473cca | |||
| a7f58175fa | |||
| 460b878fbb | |||
| 4a8925df13 | |||
| db3619e889 | |||
|
|
5e5a3949d4 | ||
| 0ef97eeb9b | |||
| 6149e4ac43 | |||
|
|
57360bacd0 | ||
| db3708aef6 | |||
| 2050ff35f4 | |||
| 038e79e52a | |||
| 6f47d02813 | |||
| 3916f3ace6 | |||
| df3fecf722 | |||
| 2c4fa570e1 | |||
| ab2229ec8b | |||
| d9931a6fae |
@@ -41,25 +41,38 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: "frontend/package-lock.json"
|
||||
|
||||
- name: Install Chromium
|
||||
- name: Resolve Chrome binary
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if command -v chromium >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v chromium)"
|
||||
elif command -v chromium-browser >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v chromium-browser)"
|
||||
elif command -v google-chrome >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v google-chrome)"
|
||||
else
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends chromium
|
||||
CHROME_PATH="$(command -v chromium)"
|
||||
fi
|
||||
|
||||
echo "CHROME_BIN=$CHROME_PATH" >> "$GITHUB_ENV"
|
||||
echo "Using CHROME_BIN=$CHROME_PATH"
|
||||
|
||||
- name: Install frontend dependencies
|
||||
shell: bash
|
||||
run: |
|
||||
cd frontend
|
||||
npm ci --no-audit --no-fund
|
||||
npm ci --no-audit --no-fund --prefer-offline
|
||||
|
||||
- name: Run frontend tests (headless)
|
||||
shell: bash
|
||||
env:
|
||||
CHROME_BIN: /usr/bin/chromium
|
||||
CI: "true"
|
||||
run: |
|
||||
cd frontend
|
||||
echo "Karma CHROME_BIN=$CHROME_BIN"
|
||||
npm run test -- --watch=false --browsers=ChromeHeadlessNoSandbox
|
||||
|
||||
build-and-push:
|
||||
|
||||
@@ -150,23 +150,36 @@ jobs:
|
||||
cache: "npm"
|
||||
cache-dependency-path: "frontend/package-lock.json"
|
||||
|
||||
- name: Install Chromium
|
||||
- name: Resolve Chrome binary
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if command -v chromium >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v chromium)"
|
||||
elif command -v chromium-browser >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v chromium-browser)"
|
||||
elif command -v google-chrome >/dev/null 2>&1; then
|
||||
CHROME_PATH="$(command -v google-chrome)"
|
||||
else
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends chromium
|
||||
CHROME_PATH="$(command -v chromium)"
|
||||
fi
|
||||
|
||||
echo "CHROME_BIN=$CHROME_PATH" >> "$GITHUB_ENV"
|
||||
echo "Using CHROME_BIN=$CHROME_PATH"
|
||||
|
||||
- name: Install frontend dependencies
|
||||
shell: bash
|
||||
run: |
|
||||
cd frontend
|
||||
npm ci --no-audit --no-fund
|
||||
npm ci --no-audit --no-fund --prefer-offline
|
||||
|
||||
- name: Run frontend tests (headless)
|
||||
shell: bash
|
||||
env:
|
||||
CHROME_BIN: /usr/bin/chromium
|
||||
CI: "true"
|
||||
run: |
|
||||
cd frontend
|
||||
echo "Karma CHROME_BIN=$CHROME_BIN"
|
||||
npm run test -- --watch=false --browsers=ChromeHeadlessNoSandbox
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.printcalculator.entity.CustomQuoteRequest;
|
||||
import com.printcalculator.entity.CustomQuoteRequestAttachment;
|
||||
import com.printcalculator.repository.CustomQuoteRequestAttachmentRepository;
|
||||
import com.printcalculator.repository.CustomQuoteRequestRepository;
|
||||
import com.printcalculator.service.ClamAVService;
|
||||
import com.printcalculator.service.storage.ClamAVService;
|
||||
import com.printcalculator.service.email.EmailNotificationService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@@ -3,17 +3,16 @@ package com.printcalculator.controller;
|
||||
import com.printcalculator.dto.OptionsResponse;
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
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.NozzleLayerHeightPolicyService;
|
||||
import com.printcalculator.service.OrcaProfileResolver;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -24,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -32,26 +32,26 @@ public class OptionsController {
|
||||
|
||||
private final FilamentMaterialTypeRepository materialRepo;
|
||||
private final FilamentVariantRepository variantRepo;
|
||||
private final LayerHeightOptionRepository layerHeightRepo;
|
||||
private final NozzleOptionRepository nozzleRepo;
|
||||
private final PrinterMachineRepository printerMachineRepo;
|
||||
private final MaterialOrcaProfileMapRepository materialOrcaMapRepo;
|
||||
private final OrcaProfileResolver orcaProfileResolver;
|
||||
private final NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService;
|
||||
|
||||
public OptionsController(FilamentMaterialTypeRepository materialRepo,
|
||||
FilamentVariantRepository variantRepo,
|
||||
LayerHeightOptionRepository layerHeightRepo,
|
||||
NozzleOptionRepository nozzleRepo,
|
||||
PrinterMachineRepository printerMachineRepo,
|
||||
MaterialOrcaProfileMapRepository materialOrcaMapRepo,
|
||||
OrcaProfileResolver orcaProfileResolver) {
|
||||
OrcaProfileResolver orcaProfileResolver,
|
||||
NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService) {
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.layerHeightRepo = layerHeightRepo;
|
||||
this.nozzleRepo = nozzleRepo;
|
||||
this.printerMachineRepo = printerMachineRepo;
|
||||
this.materialOrcaMapRepo = materialOrcaMapRepo;
|
||||
this.orcaProfileResolver = orcaProfileResolver;
|
||||
this.nozzleLayerHeightPolicyService = nozzleLayerHeightPolicyService;
|
||||
}
|
||||
|
||||
@GetMapping("/api/calculator/options")
|
||||
@@ -116,15 +116,6 @@ public class OptionsController {
|
||||
new OptionsResponse.InfillPatternOption("cubic", "Cubic")
|
||||
);
|
||||
|
||||
List<OptionsResponse.LayerHeightOptionDTO> layers = layerHeightRepo.findAll().stream()
|
||||
.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())
|
||||
))
|
||||
.toList();
|
||||
|
||||
List<OptionsResponse.NozzleOptionDTO> nozzles = nozzleRepo.findAll().stream()
|
||||
.filter(n -> Boolean.TRUE.equals(n.getIsActive()))
|
||||
.sorted(Comparator.comparing(NozzleOption::getNozzleDiameterMm))
|
||||
@@ -137,7 +128,31 @@ public class OptionsController {
|
||||
))
|
||||
.toList();
|
||||
|
||||
return ResponseEntity.ok(new OptionsResponse(materialOptions, qualities, patterns, layers, nozzles));
|
||||
Map<BigDecimal, List<BigDecimal>> rulesByNozzle = nozzleLayerHeightPolicyService.getActiveRulesByNozzle();
|
||||
BigDecimal selectedNozzle = nozzleLayerHeightPolicyService.resolveNozzle(
|
||||
nozzleDiameter != null ? BigDecimal.valueOf(nozzleDiameter) : null
|
||||
);
|
||||
|
||||
List<OptionsResponse.LayerHeightOptionDTO> layers = toLayerDtos(rulesByNozzle.getOrDefault(selectedNozzle, List.of()));
|
||||
if (layers.isEmpty()) {
|
||||
layers = rulesByNozzle.values().stream().findFirst().map(this::toLayerDtos).orElse(List.of());
|
||||
}
|
||||
|
||||
List<OptionsResponse.NozzleLayerHeightOptionsDTO> layerHeightsByNozzle = rulesByNozzle.entrySet().stream()
|
||||
.map(entry -> new OptionsResponse.NozzleLayerHeightOptionsDTO(
|
||||
entry.getKey().doubleValue(),
|
||||
toLayerDtos(entry.getValue())
|
||||
))
|
||||
.toList();
|
||||
|
||||
return ResponseEntity.ok(new OptionsResponse(
|
||||
materialOptions,
|
||||
qualities,
|
||||
patterns,
|
||||
layers,
|
||||
nozzles,
|
||||
layerHeightsByNozzle
|
||||
));
|
||||
}
|
||||
|
||||
private Set<Long> resolveCompatibleMaterialTypeIds(Long printerMachineId, Double nozzleDiameter) {
|
||||
@@ -152,9 +167,9 @@ public class OptionsController {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
BigDecimal nozzle = nozzleDiameter != null
|
||||
? BigDecimal.valueOf(nozzleDiameter)
|
||||
: BigDecimal.valueOf(0.40);
|
||||
BigDecimal nozzle = nozzleLayerHeightPolicyService.resolveNozzle(
|
||||
nozzleDiameter != null ? BigDecimal.valueOf(nozzleDiameter) : null
|
||||
);
|
||||
|
||||
PrinterMachineProfile machineProfile = orcaProfileResolver
|
||||
.resolveMachineProfile(machine, nozzle)
|
||||
@@ -172,6 +187,16 @@ public class OptionsController {
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private List<OptionsResponse.LayerHeightOptionDTO> toLayerDtos(List<BigDecimal> layers) {
|
||||
return layers.stream()
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.map(layer -> new OptionsResponse.LayerHeightOptionDTO(
|
||||
layer.doubleValue(),
|
||||
String.format("%.2f mm", layer)
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private String resolveHexColor(FilamentVariant variant) {
|
||||
if (variant.getColorHex() != null && !variant.getColorHex().isBlank()) {
|
||||
return variant.getColorHex();
|
||||
|
||||
@@ -1,93 +1,42 @@
|
||||
package com.printcalculator.controller;
|
||||
|
||||
import com.printcalculator.dto.*;
|
||||
import com.printcalculator.entity.*;
|
||||
import com.printcalculator.repository.*;
|
||||
import com.printcalculator.service.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.OrderService;
|
||||
import com.printcalculator.service.PaymentService;
|
||||
import com.printcalculator.service.QrBillService;
|
||||
import com.printcalculator.service.StorageService;
|
||||
import com.printcalculator.service.TwintPaymentService;
|
||||
import com.printcalculator.dto.CreateOrderRequest;
|
||||
import com.printcalculator.dto.OrderDto;
|
||||
import com.printcalculator.service.order.OrderControllerService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Base64;
|
||||
import java.util.stream.Collectors;
|
||||
import java.net.URI;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/orders")
|
||||
public class OrderController {
|
||||
private static final Pattern SAFE_EXTENSION_PATTERN = Pattern.compile("^[a-z0-9]{1,10}$");
|
||||
|
||||
private final OrderService orderService;
|
||||
private final OrderRepository orderRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
private final QuoteSessionRepository quoteSessionRepo;
|
||||
private final QuoteLineItemRepository quoteLineItemRepo;
|
||||
private final CustomerRepository customerRepo;
|
||||
private final StorageService storageService;
|
||||
private final InvoicePdfRenderingService invoiceService;
|
||||
private final QrBillService qrBillService;
|
||||
private final TwintPaymentService twintPaymentService;
|
||||
private final PaymentService paymentService;
|
||||
private final PaymentRepository paymentRepo;
|
||||
private final OrderControllerService orderControllerService;
|
||||
|
||||
|
||||
public OrderController(OrderService orderService,
|
||||
OrderRepository orderRepo,
|
||||
OrderItemRepository orderItemRepo,
|
||||
QuoteSessionRepository quoteSessionRepo,
|
||||
QuoteLineItemRepository quoteLineItemRepo,
|
||||
CustomerRepository customerRepo,
|
||||
StorageService storageService,
|
||||
InvoicePdfRenderingService invoiceService,
|
||||
QrBillService qrBillService,
|
||||
TwintPaymentService twintPaymentService,
|
||||
PaymentService paymentService,
|
||||
PaymentRepository paymentRepo) {
|
||||
this.orderService = orderService;
|
||||
this.orderRepo = orderRepo;
|
||||
this.orderItemRepo = orderItemRepo;
|
||||
this.quoteSessionRepo = quoteSessionRepo;
|
||||
this.quoteLineItemRepo = quoteLineItemRepo;
|
||||
this.customerRepo = customerRepo;
|
||||
this.storageService = storageService;
|
||||
this.invoiceService = invoiceService;
|
||||
this.qrBillService = qrBillService;
|
||||
this.twintPaymentService = twintPaymentService;
|
||||
this.paymentService = paymentService;
|
||||
this.paymentRepo = paymentRepo;
|
||||
public OrderController(OrderControllerService orderControllerService) {
|
||||
this.orderControllerService = orderControllerService;
|
||||
}
|
||||
|
||||
|
||||
// 1. Create Order from Quote
|
||||
@PostMapping("/from-quote/{quoteSessionId}")
|
||||
@Transactional
|
||||
public ResponseEntity<OrderDto> createOrderFromQuote(
|
||||
@PathVariable UUID quoteSessionId,
|
||||
@Valid @RequestBody com.printcalculator.dto.CreateOrderRequest request
|
||||
@Valid @RequestBody CreateOrderRequest request
|
||||
) {
|
||||
Order order = orderService.createOrderFromQuote(quoteSessionId, request);
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
return ResponseEntity.ok(convertToDto(order, items));
|
||||
return ResponseEntity.ok(orderControllerService.createOrderFromQuote(quoteSessionId, request));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{orderId}/items/{orderItemId}/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@@ -97,44 +46,17 @@ public class OrderController {
|
||||
@PathVariable UUID orderItemId,
|
||||
@RequestParam("file") MultipartFile file
|
||||
) throws IOException {
|
||||
|
||||
OrderItem item = orderItemRepo.findById(orderItemId)
|
||||
.orElseThrow(() -> new RuntimeException("OrderItem not found"));
|
||||
|
||||
if (!item.getOrder().getId().equals(orderId)) {
|
||||
boolean uploaded = orderControllerService.uploadOrderItemFile(orderId, orderItemId, file);
|
||||
if (!uploaded) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
String relativePath = item.getStoredRelativePath();
|
||||
Path destinationRelativePath;
|
||||
if (relativePath == null || relativePath.equals("PENDING")) {
|
||||
String ext = getExtension(file.getOriginalFilename());
|
||||
String storedFilename = UUID.randomUUID() + "." + ext;
|
||||
destinationRelativePath = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString(), storedFilename);
|
||||
item.setStoredRelativePath(destinationRelativePath.toString());
|
||||
item.setStoredFilename(storedFilename);
|
||||
} else {
|
||||
destinationRelativePath = resolveOrderItemRelativePath(relativePath, orderId, orderItemId);
|
||||
if (destinationRelativePath == null) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
}
|
||||
|
||||
storageService.store(file, destinationRelativePath);
|
||||
item.setFileSizeBytes(file.getSize());
|
||||
item.setMimeType(file.getContentType());
|
||||
orderItemRepo.save(item);
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}")
|
||||
public ResponseEntity<OrderDto> getOrder(@PathVariable UUID orderId) {
|
||||
return orderRepo.findById(orderId)
|
||||
.map(o -> {
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(o.getId());
|
||||
return ResponseEntity.ok(convertToDto(o, items));
|
||||
})
|
||||
return orderControllerService.getOrder(orderId)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@@ -144,89 +66,29 @@ public class OrderController {
|
||||
@PathVariable UUID orderId,
|
||||
@RequestBody Map<String, String> payload
|
||||
) {
|
||||
String method = payload.get("method");
|
||||
paymentService.reportPayment(orderId, method);
|
||||
return getOrder(orderId);
|
||||
return orderControllerService.reportPayment(orderId, payload.get("method"))
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/confirmation")
|
||||
public ResponseEntity<byte[]> getConfirmation(@PathVariable UUID orderId) {
|
||||
return generateDocument(orderId, true);
|
||||
return orderControllerService.getConfirmation(orderId);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/invoice")
|
||||
public ResponseEntity<byte[]> getInvoice(@PathVariable UUID orderId) {
|
||||
// Paid invoices are sent by email after back-office payment confirmation.
|
||||
// The public endpoint must not expose a "paid" invoice download.
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> generateDocument(UUID orderId, boolean isConfirmation) {
|
||||
Order order = orderRepo.findById(orderId)
|
||||
.orElseThrow(() -> new RuntimeException("Order not found"));
|
||||
|
||||
if (isConfirmation) {
|
||||
Path relativePath = buildConfirmationPdfRelativePath(order);
|
||||
try {
|
||||
byte[] existingPdf = storageService.loadAsResource(relativePath).getInputStream().readAllBytes();
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Disposition", "attachment; filename=\"confirmation-" + getDisplayOrderNumber(order) + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(existingPdf);
|
||||
} catch (Exception ignored) {
|
||||
// Fallback to on-the-fly generation if the stored file is missing or unreadable.
|
||||
}
|
||||
}
|
||||
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(orderId);
|
||||
Payment payment = paymentRepo.findByOrder_Id(orderId).orElse(null);
|
||||
|
||||
byte[] pdf = invoiceService.generateDocumentPdf(order, items, isConfirmation, qrBillService, payment);
|
||||
String typePrefix = isConfirmation ? "confirmation-" : "invoice-";
|
||||
String truncatedUuid = order.getId().toString().substring(0, 8);
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Disposition", "attachment; filename=\"" + typePrefix + truncatedUuid + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(pdf);
|
||||
}
|
||||
|
||||
private Path buildConfirmationPdfRelativePath(Order order) {
|
||||
return Path.of(
|
||||
"orders",
|
||||
order.getId().toString(),
|
||||
"documents",
|
||||
"confirmation-" + getDisplayOrderNumber(order) + ".pdf"
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/twint")
|
||||
public ResponseEntity<Map<String, String>> getTwintPayment(@PathVariable UUID orderId) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
byte[] qrPng = twintPaymentService.generateQrPng(order, 360);
|
||||
String qrDataUri = "data:image/png;base64," + Base64.getEncoder().encodeToString(qrPng);
|
||||
|
||||
Map<String, String> data = new HashMap<>();
|
||||
data.put("paymentUrl", twintPaymentService.getTwintPaymentUrl(order));
|
||||
data.put("openUrl", "/api/orders/" + orderId + "/twint/open");
|
||||
data.put("qrImageUrl", "/api/orders/" + orderId + "/twint/qr");
|
||||
data.put("qrImageDataUri", qrDataUri);
|
||||
return ResponseEntity.ok(data);
|
||||
return orderControllerService.getTwintPayment(orderId);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/twint/open")
|
||||
public ResponseEntity<Void> openTwintPayment(@PathVariable UUID orderId) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.status(302)
|
||||
.location(URI.create(twintPaymentService.getTwintPaymentUrl(order)))
|
||||
.build();
|
||||
return orderControllerService.openTwintPayment(orderId);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/twint/qr")
|
||||
@@ -234,132 +96,6 @@ public class OrderController {
|
||||
@PathVariable UUID orderId,
|
||||
@RequestParam(defaultValue = "320") int size
|
||||
) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
return orderControllerService.getTwintQr(orderId, size);
|
||||
}
|
||||
|
||||
int normalizedSize = Math.max(200, Math.min(size, 600));
|
||||
byte[] png = twintPaymentService.generateQrPng(order, normalizedSize);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_PNG)
|
||||
.body(png);
|
||||
}
|
||||
|
||||
private String getExtension(String filename) {
|
||||
if (filename == null) return "stl";
|
||||
String cleaned = StringUtils.cleanPath(filename);
|
||||
if (cleaned.contains("..")) {
|
||||
return "stl";
|
||||
}
|
||||
int i = cleaned.lastIndexOf('.');
|
||||
if (i > 0 && i < cleaned.length() - 1) {
|
||||
String ext = cleaned.substring(i + 1).toLowerCase(Locale.ROOT);
|
||||
if (SAFE_EXTENSION_PATTERN.matcher(ext).matches()) {
|
||||
return ext;
|
||||
}
|
||||
}
|
||||
return "stl";
|
||||
}
|
||||
|
||||
private Path resolveOrderItemRelativePath(String storedRelativePath, UUID orderId, UUID orderItemId) {
|
||||
try {
|
||||
Path candidate = Path.of(storedRelativePath).normalize();
|
||||
if (candidate.isAbsolute()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Path expectedPrefix = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString());
|
||||
if (!candidate.startsWith(expectedPrefix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return candidate;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private OrderDto convertToDto(Order order, List<OrderItem> items) {
|
||||
OrderDto dto = new OrderDto();
|
||||
dto.setId(order.getId());
|
||||
dto.setOrderNumber(getDisplayOrderNumber(order));
|
||||
dto.setStatus(order.getStatus());
|
||||
|
||||
paymentRepo.findByOrder_Id(order.getId()).ifPresent(p -> {
|
||||
dto.setPaymentStatus(p.getStatus());
|
||||
dto.setPaymentMethod(p.getMethod());
|
||||
});
|
||||
|
||||
dto.setCustomerEmail(order.getCustomerEmail());
|
||||
dto.setCustomerPhone(order.getCustomerPhone());
|
||||
dto.setPreferredLanguage(order.getPreferredLanguage());
|
||||
dto.setBillingCustomerType(order.getBillingCustomerType());
|
||||
dto.setCurrency(order.getCurrency());
|
||||
dto.setSetupCostChf(order.getSetupCostChf());
|
||||
dto.setShippingCostChf(order.getShippingCostChf());
|
||||
dto.setDiscountChf(order.getDiscountChf());
|
||||
dto.setSubtotalChf(order.getSubtotalChf());
|
||||
dto.setIsCadOrder(order.getIsCadOrder());
|
||||
dto.setSourceRequestId(order.getSourceRequestId());
|
||||
dto.setCadHours(order.getCadHours());
|
||||
dto.setCadHourlyRateChf(order.getCadHourlyRateChf());
|
||||
dto.setCadTotalChf(order.getCadTotalChf());
|
||||
dto.setTotalChf(order.getTotalChf());
|
||||
dto.setCreatedAt(order.getCreatedAt());
|
||||
dto.setShippingSameAsBilling(order.getShippingSameAsBilling());
|
||||
|
||||
AddressDto billing = new AddressDto();
|
||||
billing.setFirstName(order.getBillingFirstName());
|
||||
billing.setLastName(order.getBillingLastName());
|
||||
billing.setCompanyName(order.getBillingCompanyName());
|
||||
billing.setContactPerson(order.getBillingContactPerson());
|
||||
billing.setAddressLine1(order.getBillingAddressLine1());
|
||||
billing.setAddressLine2(order.getBillingAddressLine2());
|
||||
billing.setZip(order.getBillingZip());
|
||||
billing.setCity(order.getBillingCity());
|
||||
billing.setCountryCode(order.getBillingCountryCode());
|
||||
dto.setBillingAddress(billing);
|
||||
|
||||
if (!order.getShippingSameAsBilling()) {
|
||||
AddressDto shipping = new AddressDto();
|
||||
shipping.setFirstName(order.getShippingFirstName());
|
||||
shipping.setLastName(order.getShippingLastName());
|
||||
shipping.setCompanyName(order.getShippingCompanyName());
|
||||
shipping.setContactPerson(order.getShippingContactPerson());
|
||||
shipping.setAddressLine1(order.getShippingAddressLine1());
|
||||
shipping.setAddressLine2(order.getShippingAddressLine2());
|
||||
shipping.setZip(order.getShippingZip());
|
||||
shipping.setCity(order.getShippingCity());
|
||||
shipping.setCountryCode(order.getShippingCountryCode());
|
||||
dto.setShippingAddress(shipping);
|
||||
}
|
||||
|
||||
List<OrderItemDto> itemDtos = items.stream().map(i -> {
|
||||
OrderItemDto idto = new OrderItemDto();
|
||||
idto.setId(i.getId());
|
||||
idto.setOriginalFilename(i.getOriginalFilename());
|
||||
idto.setMaterialCode(i.getMaterialCode());
|
||||
idto.setColorCode(i.getColorCode());
|
||||
idto.setQuantity(i.getQuantity());
|
||||
idto.setPrintTimeSeconds(i.getPrintTimeSeconds());
|
||||
idto.setMaterialGrams(i.getMaterialGrams());
|
||||
idto.setUnitPriceChf(i.getUnitPriceChf());
|
||||
idto.setLineTotalChf(i.getLineTotalChf());
|
||||
return idto;
|
||||
}).collect(Collectors.toList());
|
||||
dto.setItems(itemDtos);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
return orderNumber;
|
||||
}
|
||||
return order.getId() != null ? order.getId().toString() : "unknown";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,17 +4,23 @@ import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.model.PrintStats;
|
||||
import com.printcalculator.model.QuoteResult;
|
||||
import com.printcalculator.repository.PrinterMachineRepository;
|
||||
import com.printcalculator.service.NozzleLayerHeightPolicyService;
|
||||
import com.printcalculator.service.QuoteCalculator;
|
||||
import com.printcalculator.service.SlicerService;
|
||||
import com.printcalculator.service.storage.ClamAVService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
@RestController
|
||||
public class QuoteController {
|
||||
@@ -22,17 +28,23 @@ public class QuoteController {
|
||||
private final SlicerService slicerService;
|
||||
private final QuoteCalculator quoteCalculator;
|
||||
private final PrinterMachineRepository machineRepo;
|
||||
private final com.printcalculator.service.ClamAVService clamAVService;
|
||||
private final ClamAVService clamAVService;
|
||||
private final NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService;
|
||||
|
||||
// Defaults (using aliases defined in ProfileManager)
|
||||
private static final String DEFAULT_FILAMENT = "pla_basic";
|
||||
private static final String DEFAULT_PROCESS = "standard";
|
||||
|
||||
public QuoteController(SlicerService slicerService, QuoteCalculator quoteCalculator, PrinterMachineRepository machineRepo, com.printcalculator.service.ClamAVService clamAVService) {
|
||||
public QuoteController(SlicerService slicerService,
|
||||
QuoteCalculator quoteCalculator,
|
||||
PrinterMachineRepository machineRepo,
|
||||
ClamAVService clamAVService,
|
||||
NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService) {
|
||||
this.slicerService = slicerService;
|
||||
this.quoteCalculator = quoteCalculator;
|
||||
this.machineRepo = machineRepo;
|
||||
this.clamAVService = clamAVService;
|
||||
this.nozzleLayerHeightPolicyService = nozzleLayerHeightPolicyService;
|
||||
}
|
||||
|
||||
@PostMapping("/api/quote")
|
||||
@@ -69,15 +81,27 @@ public class QuoteController {
|
||||
if (infillPattern != null && !infillPattern.isEmpty()) {
|
||||
processOverrides.put("sparse_infill_pattern", infillPattern);
|
||||
}
|
||||
BigDecimal normalizedNozzle = nozzleLayerHeightPolicyService.resolveNozzle(
|
||||
nozzleDiameter != null ? BigDecimal.valueOf(nozzleDiameter) : null
|
||||
);
|
||||
if (layerHeight != null) {
|
||||
processOverrides.put("layer_height", String.valueOf(layerHeight));
|
||||
BigDecimal normalizedLayer = nozzleLayerHeightPolicyService.normalizeLayer(BigDecimal.valueOf(layerHeight));
|
||||
if (!nozzleLayerHeightPolicyService.isAllowed(normalizedNozzle, normalizedLayer)) {
|
||||
throw new ResponseStatusException(
|
||||
BAD_REQUEST,
|
||||
"Layer height " + normalizedLayer.stripTrailingZeros().toPlainString()
|
||||
+ " is not allowed for nozzle " + normalizedNozzle.stripTrailingZeros().toPlainString()
|
||||
+ ". Allowed: " + nozzleLayerHeightPolicyService.allowedLayersLabel(normalizedNozzle)
|
||||
);
|
||||
}
|
||||
processOverrides.put("layer_height", normalizedLayer.stripTrailingZeros().toPlainString());
|
||||
}
|
||||
if (supportEnabled != null) {
|
||||
processOverrides.put("enable_support", supportEnabled ? "1" : "0");
|
||||
}
|
||||
|
||||
if (nozzleDiameter != null) {
|
||||
machineOverrides.put("nozzle_diameter", String.valueOf(nozzleDiameter));
|
||||
machineOverrides.put("nozzle_diameter", normalizedNozzle.stripTrailingZeros().toPlainString());
|
||||
// Also need to ensure the printer profile is compatible or just override?
|
||||
// Usually nozzle diameter changes require a different printer profile or deep overrides.
|
||||
// For now, we trust the override key works on the base profile.
|
||||
|
||||
@@ -1,103 +1,68 @@
|
||||
package com.printcalculator.controller;
|
||||
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.dto.PrintSettingsDto;
|
||||
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.QuoteSessionTotalsService;
|
||||
import com.printcalculator.service.SlicerService;
|
||||
import com.printcalculator.service.quote.QuoteSessionItemService;
|
||||
import com.printcalculator.service.quote.QuoteSessionResponseAssembler;
|
||||
import com.printcalculator.service.quote.QuoteStorageService;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.Optional;
|
||||
import java.util.Locale;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/quote-sessions")
|
||||
|
||||
public class QuoteSessionController {
|
||||
private static final Path QUOTE_STORAGE_ROOT = Paths.get("storage_quotes").toAbsolutePath().normalize();
|
||||
|
||||
private final QuoteSessionRepository sessionRepo;
|
||||
private final QuoteLineItemRepository lineItemRepo;
|
||||
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;
|
||||
private final QuoteSessionTotalsService quoteSessionTotalsService;
|
||||
private final QuoteSessionItemService quoteSessionItemService;
|
||||
private final QuoteStorageService quoteStorageService;
|
||||
private final QuoteSessionResponseAssembler quoteSessionResponseAssembler;
|
||||
|
||||
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,
|
||||
QuoteSessionTotalsService quoteSessionTotalsService) {
|
||||
QuoteSessionTotalsService quoteSessionTotalsService,
|
||||
QuoteSessionItemService quoteSessionItemService,
|
||||
QuoteStorageService quoteStorageService,
|
||||
QuoteSessionResponseAssembler quoteSessionResponseAssembler) {
|
||||
this.sessionRepo = sessionRepo;
|
||||
this.lineItemRepo = lineItemRepo;
|
||||
this.slicerService = slicerService;
|
||||
this.quoteCalculator = quoteCalculator;
|
||||
this.machineRepo = machineRepo;
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.orcaProfileResolver = orcaProfileResolver;
|
||||
this.pricingRepo = pricingRepo;
|
||||
this.clamAVService = clamAVService;
|
||||
this.quoteSessionTotalsService = quoteSessionTotalsService;
|
||||
this.quoteSessionItemService = quoteSessionItemService;
|
||||
this.quoteStorageService = quoteStorageService;
|
||||
this.quoteSessionResponseAssembler = quoteSessionResponseAssembler;
|
||||
}
|
||||
|
||||
// 1. Start a new empty session
|
||||
@PostMapping(value = "")
|
||||
@Transactional
|
||||
public ResponseEntity<QuoteSession> createSession() {
|
||||
QuoteSession session = new QuoteSession();
|
||||
session.setStatus("ACTIVE");
|
||||
session.setPricingVersion("v1");
|
||||
// Default material/settings will be set when items are added or updated?
|
||||
// For now set safe defaults
|
||||
session.setMaterialCode("PLA");
|
||||
session.setSupportsEnabled(false);
|
||||
session.setCreatedAt(OffsetDateTime.now());
|
||||
@@ -110,277 +75,143 @@ public class QuoteSessionController {
|
||||
return ResponseEntity.ok(session);
|
||||
}
|
||||
|
||||
// 2. Add item to existing session
|
||||
@PostMapping(value = "/{id}/line-items", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Transactional
|
||||
public ResponseEntity<QuoteLineItem> addItemToExistingSession(
|
||||
@PathVariable UUID id,
|
||||
@RequestPart("settings") com.printcalculator.dto.PrintSettingsDto settings,
|
||||
@RequestPart("file") MultipartFile file
|
||||
) throws IOException {
|
||||
public ResponseEntity<QuoteLineItem> addItemToExistingSession(@PathVariable UUID id,
|
||||
@RequestPart("settings") PrintSettingsDto settings,
|
||||
@RequestPart("file") MultipartFile file) throws IOException {
|
||||
QuoteSession session = sessionRepo.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Session not found"));
|
||||
|
||||
QuoteLineItem item = addItemToSession(session, file, settings);
|
||||
QuoteLineItem item = quoteSessionItemService.addItemToSession(session, file, settings);
|
||||
return ResponseEntity.ok(item);
|
||||
}
|
||||
|
||||
// Helper to add item
|
||||
private QuoteLineItem addItemToSession(QuoteSession session, MultipartFile file, com.printcalculator.dto.PrintSettingsDto settings) throws IOException {
|
||||
if (file.isEmpty()) throw new IllegalArgumentException("File is empty");
|
||||
@PatchMapping("/line-items/{lineItemId}")
|
||||
@Transactional
|
||||
public ResponseEntity<QuoteLineItem> updateLineItem(@PathVariable UUID lineItemId,
|
||||
@RequestBody Map<String, Object> updates) {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
QuoteSession session = item.getQuoteSession();
|
||||
if ("CONVERTED".equals(session.getStatus())) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Cannot modify a converted session");
|
||||
}
|
||||
|
||||
// Scan for virus
|
||||
clamAVService.scan(file.getInputStream());
|
||||
|
||||
// 1. Define Persistent Storage Path
|
||||
// Structure: storage_quotes/{sessionId}/{uuid}.{ext}
|
||||
Path sessionStorageDir = QUOTE_STORAGE_ROOT.resolve(session.getId().toString()).normalize();
|
||||
if (!sessionStorageDir.startsWith(QUOTE_STORAGE_ROOT)) {
|
||||
throw new IOException("Invalid quote session storage path");
|
||||
if (updates.containsKey("quantity")) {
|
||||
item.setQuantity(parsePositiveQuantity(updates.get("quantity")));
|
||||
}
|
||||
Files.createDirectories(sessionStorageDir);
|
||||
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String ext = getSafeExtension(originalFilename, "stl");
|
||||
String storedFilename = UUID.randomUUID() + "." + ext;
|
||||
Path persistentPath = sessionStorageDir.resolve(storedFilename).normalize();
|
||||
if (!persistentPath.startsWith(sessionStorageDir)) {
|
||||
throw new IOException("Invalid quote line-item storage path");
|
||||
}
|
||||
|
||||
// Save file
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
Files.copy(inputStream, persistentPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
Path convertedPersistentPath = null;
|
||||
try {
|
||||
boolean cadSession = "CAD_ACTIVE".equals(session.getStatus());
|
||||
|
||||
// In CAD sessions, print settings are locked server-side.
|
||||
if (cadSession) {
|
||||
enforceCadPrintSettings(session, settings);
|
||||
} else {
|
||||
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);
|
||||
|
||||
if (cadSession
|
||||
&& session.getMaterialCode() != null
|
||||
&& selectedVariant.getFilamentMaterialType() != null
|
||||
&& selectedVariant.getFilamentMaterialType().getMaterialCode() != null) {
|
||||
String lockedMaterial = normalizeRequestedMaterialCode(session.getMaterialCode());
|
||||
String selectedMaterial = normalizeRequestedMaterialCode(selectedVariant.getFilamentMaterialType().getMaterialCode());
|
||||
if (!lockedMaterial.equals(selectedMaterial)) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Selected filament does not match locked CAD material");
|
||||
if (updates.containsKey("color_code")) {
|
||||
Object colorValue = updates.get("color_code");
|
||||
if (colorValue != null) {
|
||||
item.setColorCode(String.valueOf(colorValue));
|
||||
}
|
||||
}
|
||||
|
||||
// Update session global settings from the most recent item added
|
||||
if (!cadSession) {
|
||||
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);
|
||||
}
|
||||
|
||||
OrcaProfileResolver.ResolvedProfiles profiles = orcaProfileResolver.resolve(machine, nozzleDiameter, selectedVariant);
|
||||
String machineProfile = profiles.machineProfileName();
|
||||
String filamentProfile = profiles.filamentProfileName();
|
||||
|
||||
String processProfile = "standard";
|
||||
if (settings.getLayerHeight() != null) {
|
||||
if (settings.getLayerHeight() >= 0.28) processProfile = "draft";
|
||||
else if (settings.getLayerHeight() <= 0.12) processProfile = "extra_fine";
|
||||
}
|
||||
|
||||
// Build overrides map from settings
|
||||
Map<String, String> processOverrides = new HashMap<>();
|
||||
if (settings.getLayerHeight() != null) processOverrides.put("layer_height", String.valueOf(settings.getLayerHeight()));
|
||||
if (settings.getInfillDensity() != null) processOverrides.put("sparse_infill_density", settings.getInfillDensity() + "%");
|
||||
if (settings.getInfillPattern() != null) processOverrides.put("sparse_infill_pattern", settings.getInfillPattern());
|
||||
|
||||
Path slicerInputPath = persistentPath;
|
||||
if ("3mf".equals(ext)) {
|
||||
String convertedFilename = UUID.randomUUID() + "-converted.stl";
|
||||
convertedPersistentPath = sessionStorageDir.resolve(convertedFilename).normalize();
|
||||
if (!convertedPersistentPath.startsWith(sessionStorageDir)) {
|
||||
throw new IOException("Invalid converted STL storage path");
|
||||
}
|
||||
slicerService.convert3mfToPersistentStl(persistentPath.toFile(), convertedPersistentPath);
|
||||
slicerInputPath = convertedPersistentPath;
|
||||
}
|
||||
|
||||
// 3. Slice (Use persistent path)
|
||||
PrintStats stats = slicerService.slice(
|
||||
slicerInputPath.toFile(),
|
||||
machineProfile,
|
||||
filamentProfile,
|
||||
processProfile,
|
||||
null, // machine overrides
|
||||
processOverrides
|
||||
);
|
||||
|
||||
Optional<ModelDimensions> modelDimensions = slicerService.inspectModelDimensions(slicerInputPath.toFile());
|
||||
|
||||
// 4. Calculate Quote
|
||||
QuoteResult result = quoteCalculator.calculate(stats, machine.getPrinterDisplayName(), selectedVariant);
|
||||
|
||||
// 5. Create Line Item
|
||||
QuoteLineItem item = new QuoteLineItem();
|
||||
item.setQuoteSession(session);
|
||||
item.setOriginalFilename(file.getOriginalFilename());
|
||||
item.setStoredPath(QUOTE_STORAGE_ROOT.relativize(persistentPath).toString()); // SAVE PATH (relative to root)
|
||||
item.setQuantity(1);
|
||||
item.setColorCode(selectedVariant.getColorName());
|
||||
item.setFilamentVariant(selectedVariant);
|
||||
item.setStatus("READY"); // or CALCULATED
|
||||
|
||||
item.setPrintTimeSeconds((int) stats.printTimeSeconds());
|
||||
item.setMaterialGrams(BigDecimal.valueOf(stats.filamentWeightGrams()));
|
||||
item.setUnitPriceChf(BigDecimal.valueOf(result.getTotalPrice()));
|
||||
|
||||
// Store breakdown
|
||||
Map<String, Object> breakdown = new HashMap<>();
|
||||
breakdown.put("machine_cost", result.getTotalPrice()); // Excludes setup fee which is at session level
|
||||
breakdown.put("setup_fee", 0);
|
||||
if (convertedPersistentPath != null) {
|
||||
breakdown.put("convertedStoredPath", QUOTE_STORAGE_ROOT.relativize(convertedPersistentPath).toString());
|
||||
}
|
||||
item.setPricingBreakdown(breakdown);
|
||||
|
||||
// Dimensions for shipping/package checks are computed server-side from the uploaded model.
|
||||
item.setBoundingBoxXMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.xMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxX() != null ? BigDecimal.valueOf(settings.getBoundingBoxX()) : BigDecimal.ZERO));
|
||||
item.setBoundingBoxYMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.yMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxY() != null ? BigDecimal.valueOf(settings.getBoundingBoxY()) : BigDecimal.ZERO));
|
||||
item.setBoundingBoxZMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.zMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxZ() != null ? BigDecimal.valueOf(settings.getBoundingBoxZ()) : BigDecimal.ZERO));
|
||||
|
||||
item.setCreatedAt(OffsetDateTime.now());
|
||||
item.setUpdatedAt(OffsetDateTime.now());
|
||||
|
||||
return lineItemRepo.save(item);
|
||||
|
||||
} catch (Exception e) {
|
||||
// Cleanup if failed
|
||||
Files.deleteIfExists(persistentPath);
|
||||
if (convertedPersistentPath != null) {
|
||||
Files.deleteIfExists(convertedPersistentPath);
|
||||
return ResponseEntity.ok(lineItemRepo.save(item));
|
||||
}
|
||||
throw e;
|
||||
|
||||
@DeleteMapping("/{sessionId}/line-items/{lineItemId}")
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteLineItem(@PathVariable UUID sessionId,
|
||||
@PathVariable UUID lineItemId) {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
if (!item.getQuoteSession().getId().equals(sessionId)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
lineItemRepo.delete(item);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> getQuoteSession(@PathVariable UUID id) {
|
||||
QuoteSession session = sessionRepo.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Session not found"));
|
||||
|
||||
List<QuoteLineItem> items = lineItemRepo.findByQuoteSessionId(id);
|
||||
QuoteSessionTotalsService.QuoteSessionTotals totals = quoteSessionTotalsService.compute(session, items);
|
||||
return ResponseEntity.ok(quoteSessionResponseAssembler.assemble(session, items, totals));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{sessionId}/line-items/{lineItemId}/content")
|
||||
public ResponseEntity<Resource> downloadLineItemContent(@PathVariable UUID sessionId,
|
||||
@PathVariable UUID lineItemId,
|
||||
@RequestParam(name = "preview", required = false, defaultValue = "false") boolean preview)
|
||||
throws IOException {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
if (!item.getQuoteSession().getId().equals(sessionId)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
String targetStoredPath = item.getStoredPath();
|
||||
if (preview) {
|
||||
String convertedPath = quoteStorageService.extractConvertedStoredPath(item);
|
||||
if (convertedPath != null && !convertedPath.isBlank()) {
|
||||
targetStoredPath = convertedPath;
|
||||
}
|
||||
}
|
||||
|
||||
private void applyPrintSettings(com.printcalculator.dto.PrintSettingsDto settings) {
|
||||
if ("BASIC".equalsIgnoreCase(settings.getComplexityMode())) {
|
||||
// Set defaults based on Quality
|
||||
String quality = settings.getQuality() != null ? settings.getQuality().toLowerCase() : "standard";
|
||||
|
||||
switch (quality) {
|
||||
case "draft":
|
||||
settings.setLayerHeight(0.28);
|
||||
settings.setInfillDensity(15.0);
|
||||
settings.setInfillPattern("grid");
|
||||
break;
|
||||
case "high":
|
||||
settings.setLayerHeight(0.12);
|
||||
settings.setInfillDensity(20.0);
|
||||
settings.setInfillPattern("gyroid");
|
||||
break;
|
||||
case "standard":
|
||||
default:
|
||||
settings.setLayerHeight(0.20);
|
||||
settings.setInfillDensity(15.0);
|
||||
settings.setInfillPattern("grid");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// ADVANCED Mode: Use values from Frontend, set defaults if missing
|
||||
if (settings.getLayerHeight() == null) settings.setLayerHeight(0.20);
|
||||
if (settings.getInfillDensity() == null) settings.setInfillDensity(20.0);
|
||||
if (settings.getInfillPattern() == null) settings.setInfillPattern("grid");
|
||||
}
|
||||
if (targetStoredPath == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
private void enforceCadPrintSettings(QuoteSession session, com.printcalculator.dto.PrintSettingsDto settings) {
|
||||
settings.setComplexityMode("ADVANCED");
|
||||
settings.setMaterial(session.getMaterialCode() != null ? session.getMaterialCode() : "PLA");
|
||||
settings.setNozzleDiameter(session.getNozzleDiameterMm() != null ? session.getNozzleDiameterMm().doubleValue() : 0.4);
|
||||
settings.setLayerHeight(session.getLayerHeightMm() != null ? session.getLayerHeightMm().doubleValue() : 0.2);
|
||||
settings.setInfillPattern(session.getInfillPattern() != null ? session.getInfillPattern() : "grid");
|
||||
settings.setInfillDensity(session.getInfillPercent() != null ? session.getInfillPercent().doubleValue() : 20.0);
|
||||
settings.setSupportsEnabled(Boolean.TRUE.equals(session.getSupportsEnabled()));
|
||||
java.nio.file.Path path = quoteStorageService.resolveStoredQuotePath(targetStoredPath, sessionId);
|
||||
if (path == null || !java.nio.file.Files.exists(path)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
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;
|
||||
Resource resource = new UrlResource(path.toUri());
|
||||
String downloadName = preview ? path.getFileName().toString() : item.getOriginalFilename();
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(org.springframework.http.HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + downloadName + "\"")
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
return machineRepo.findFirstByIsActiveTrue()
|
||||
.orElseThrow(() -> new RuntimeException("No active printer found"));
|
||||
@GetMapping(value = "/{sessionId}/line-items/{lineItemId}/stl-preview")
|
||||
public ResponseEntity<Resource> downloadLineItemStlPreview(@PathVariable UUID sessionId,
|
||||
@PathVariable UUID lineItemId)
|
||||
throws IOException {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
if (!item.getQuoteSession().getId().equals(sessionId)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
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;
|
||||
if (!"stl".equals(quoteStorageService.getSafeExtension(item.getOriginalFilename(), ""))) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
String requestedMaterialCode = normalizeRequestedMaterialCode(settings.getMaterial());
|
||||
|
||||
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();
|
||||
}
|
||||
String targetStoredPath = item.getStoredPath();
|
||||
if (targetStoredPath == null || targetStoredPath.isBlank()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return variantRepo.findFirstByFilamentMaterialTypeAndIsActiveTrue(materialType)
|
||||
.orElseThrow(() -> new RuntimeException("No active variant for material: " + requestedMaterialCode));
|
||||
java.nio.file.Path path = quoteStorageService.resolveStoredQuotePath(targetStoredPath, sessionId);
|
||||
if (path == null || !java.nio.file.Files.exists(path)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
private String normalizeRequestedMaterialCode(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return "PLA";
|
||||
if (!"stl".equals(quoteStorageService.getSafeExtension(path.getFileName().toString(), ""))) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return value.trim()
|
||||
.toUpperCase(Locale.ROOT)
|
||||
.replace('_', ' ')
|
||||
.replace('-', ' ')
|
||||
.replaceAll("\\s+", " ");
|
||||
Resource resource = new UrlResource(path.toUri());
|
||||
String downloadName = path.getFileName().toString();
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType("model/stl"))
|
||||
.header(org.springframework.http.HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + downloadName + "\"")
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
private int parsePositiveQuantity(Object raw) {
|
||||
@@ -408,198 +239,4 @@ public class QuoteSessionController {
|
||||
}
|
||||
return quantity;
|
||||
}
|
||||
|
||||
// 3. Update Line Item
|
||||
@PatchMapping("/line-items/{lineItemId}")
|
||||
@Transactional
|
||||
public ResponseEntity<QuoteLineItem> updateLineItem(
|
||||
@PathVariable UUID lineItemId,
|
||||
@RequestBody Map<String, Object> updates
|
||||
) {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
QuoteSession session = item.getQuoteSession();
|
||||
if ("CONVERTED".equals(session.getStatus())) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Cannot modify a converted session");
|
||||
}
|
||||
|
||||
if (updates.containsKey("quantity")) {
|
||||
item.setQuantity(parsePositiveQuantity(updates.get("quantity")));
|
||||
}
|
||||
if (updates.containsKey("color_code")) {
|
||||
Object colorValue = updates.get("color_code");
|
||||
if (colorValue != null) {
|
||||
item.setColorCode(String.valueOf(colorValue));
|
||||
}
|
||||
}
|
||||
|
||||
// Recalculate price if needed?
|
||||
// For now, unit price is fixed in mock. Total is calculated on GET.
|
||||
|
||||
item.setUpdatedAt(OffsetDateTime.now());
|
||||
return ResponseEntity.ok(lineItemRepo.save(item));
|
||||
}
|
||||
|
||||
// 4. Delete Line Item
|
||||
@DeleteMapping("/{sessionId}/line-items/{lineItemId}")
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteLineItem(
|
||||
@PathVariable UUID sessionId,
|
||||
@PathVariable UUID lineItemId
|
||||
) {
|
||||
// Verify item belongs to session?
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
if (!item.getQuoteSession().getId().equals(sessionId)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
lineItemRepo.delete(item);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
// 5. Get Session (Session + Items + Total)
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> getQuoteSession(@PathVariable UUID id) {
|
||||
QuoteSession session = sessionRepo.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Session not found"));
|
||||
|
||||
List<QuoteLineItem> items = lineItemRepo.findByQuoteSessionId(id);
|
||||
QuoteSessionTotalsService.QuoteSessionTotals totals = quoteSessionTotalsService.compute(session, items);
|
||||
|
||||
// Map items to DTO to embed distributed machine cost
|
||||
List<Map<String, Object>> itemsDto = new ArrayList<>();
|
||||
for (QuoteLineItem item : items) {
|
||||
Map<String, Object> dto = new HashMap<>();
|
||||
dto.put("id", item.getId());
|
||||
dto.put("originalFilename", item.getOriginalFilename());
|
||||
dto.put("quantity", item.getQuantity());
|
||||
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());
|
||||
dto.put("convertedStoredPath", extractConvertedStoredPath(item));
|
||||
|
||||
BigDecimal unitPrice = item.getUnitPriceChf() != null ? item.getUnitPriceChf() : BigDecimal.ZERO;
|
||||
int quantity = item.getQuantity() != null && item.getQuantity() > 0 ? item.getQuantity() : 1;
|
||||
if (totals.totalPrintSeconds().compareTo(BigDecimal.ZERO) > 0 && item.getPrintTimeSeconds() != null) {
|
||||
BigDecimal itemSeconds = BigDecimal.valueOf(item.getPrintTimeSeconds()).multiply(BigDecimal.valueOf(quantity));
|
||||
BigDecimal share = itemSeconds.divide(totals.totalPrintSeconds(), 8, RoundingMode.HALF_UP);
|
||||
BigDecimal itemMachineCost = totals.globalMachineCostChf().multiply(share);
|
||||
BigDecimal unitMachineCost = itemMachineCost.divide(BigDecimal.valueOf(quantity), 2, RoundingMode.HALF_UP);
|
||||
unitPrice = unitPrice.add(unitMachineCost);
|
||||
}
|
||||
dto.put("unitPriceChf", unitPrice);
|
||||
itemsDto.add(dto);
|
||||
}
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("session", session);
|
||||
response.put("items", itemsDto);
|
||||
response.put("printItemsTotalChf", totals.printItemsTotalChf());
|
||||
response.put("cadTotalChf", totals.cadTotalChf());
|
||||
response.put("itemsTotalChf", totals.itemsTotalChf());
|
||||
response.put("shippingCostChf", totals.shippingCostChf());
|
||||
response.put("globalMachineCostChf", totals.globalMachineCostChf());
|
||||
response.put("grandTotalChf", totals.grandTotalChf());
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
// 6. Download Line Item Content
|
||||
@GetMapping(value = "/{sessionId}/line-items/{lineItemId}/content")
|
||||
public ResponseEntity<org.springframework.core.io.Resource> downloadLineItemContent(
|
||||
@PathVariable UUID sessionId,
|
||||
@PathVariable UUID lineItemId,
|
||||
@RequestParam(name = "preview", required = false, defaultValue = "false") boolean preview
|
||||
) throws IOException {
|
||||
QuoteLineItem item = lineItemRepo.findById(lineItemId)
|
||||
.orElseThrow(() -> new RuntimeException("Item not found"));
|
||||
|
||||
if (!item.getQuoteSession().getId().equals(sessionId)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
String targetStoredPath = item.getStoredPath();
|
||||
if (preview) {
|
||||
String convertedPath = extractConvertedStoredPath(item);
|
||||
if (convertedPath != null && !convertedPath.isBlank()) {
|
||||
targetStoredPath = convertedPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (targetStoredPath == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
Path path = resolveStoredQuotePath(targetStoredPath, sessionId);
|
||||
if (path == null || !Files.exists(path)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
org.springframework.core.io.Resource resource = new UrlResource(path.toUri());
|
||||
String downloadName = item.getOriginalFilename();
|
||||
if (preview) {
|
||||
downloadName = path.getFileName().toString();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(org.springframework.http.HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + downloadName + "\"")
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
private String getSafeExtension(String filename, String fallback) {
|
||||
if (filename == null) {
|
||||
return fallback;
|
||||
}
|
||||
String cleaned = StringUtils.cleanPath(filename);
|
||||
if (cleaned.contains("..")) {
|
||||
return fallback;
|
||||
}
|
||||
int index = cleaned.lastIndexOf('.');
|
||||
if (index <= 0 || index >= cleaned.length() - 1) {
|
||||
return fallback;
|
||||
}
|
||||
String ext = cleaned.substring(index + 1).toLowerCase(Locale.ROOT);
|
||||
return switch (ext) {
|
||||
case "stl" -> "stl";
|
||||
case "3mf" -> "3mf";
|
||||
case "step", "stp" -> "step";
|
||||
default -> fallback;
|
||||
};
|
||||
}
|
||||
|
||||
private Path resolveStoredQuotePath(String storedPath, UUID expectedSessionId) {
|
||||
if (storedPath == null || storedPath.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Path raw = Path.of(storedPath).normalize();
|
||||
Path resolved = raw.isAbsolute() ? raw : QUOTE_STORAGE_ROOT.resolve(raw).normalize();
|
||||
Path expectedSessionRoot = QUOTE_STORAGE_ROOT.resolve(expectedSessionId.toString()).normalize();
|
||||
if (!resolved.startsWith(expectedSessionRoot)) {
|
||||
return null;
|
||||
}
|
||||
return resolved;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String extractConvertedStoredPath(QuoteLineItem item) {
|
||||
Map<String, Object> breakdown = item.getPricingBreakdown();
|
||||
if (breakdown == null) {
|
||||
return null;
|
||||
}
|
||||
Object converted = breakdown.get("convertedStoredPath");
|
||||
if (converted == null) {
|
||||
return null;
|
||||
}
|
||||
String path = String.valueOf(converted).trim();
|
||||
return path.isEmpty() ? null : path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,9 @@
|
||||
package com.printcalculator.controller.admin;
|
||||
|
||||
import com.printcalculator.dto.AddressDto;
|
||||
import com.printcalculator.dto.AdminOrderStatusUpdateRequest;
|
||||
import com.printcalculator.dto.OrderDto;
|
||||
import com.printcalculator.dto.OrderItemDto;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.entity.OrderItem;
|
||||
import com.printcalculator.entity.Payment;
|
||||
import com.printcalculator.entity.QuoteSession;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.PaymentRepository;
|
||||
import com.printcalculator.service.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.PaymentService;
|
||||
import com.printcalculator.service.QrBillService;
|
||||
import com.printcalculator.service.StorageService;
|
||||
import com.printcalculator.service.order.AdminOrderControllerService;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -27,83 +12,39 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/orders")
|
||||
@Transactional(readOnly = true)
|
||||
public class AdminOrderController {
|
||||
private static final List<String> ALLOWED_ORDER_STATUSES = List.of(
|
||||
"PENDING_PAYMENT",
|
||||
"PAID",
|
||||
"IN_PRODUCTION",
|
||||
"SHIPPED",
|
||||
"COMPLETED",
|
||||
"CANCELLED"
|
||||
);
|
||||
|
||||
private final OrderRepository orderRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
private final PaymentRepository paymentRepo;
|
||||
private final PaymentService paymentService;
|
||||
private final StorageService storageService;
|
||||
private final InvoicePdfRenderingService invoiceService;
|
||||
private final QrBillService qrBillService;
|
||||
private final AdminOrderControllerService adminOrderControllerService;
|
||||
|
||||
public AdminOrderController(
|
||||
OrderRepository orderRepo,
|
||||
OrderItemRepository orderItemRepo,
|
||||
PaymentRepository paymentRepo,
|
||||
PaymentService paymentService,
|
||||
StorageService storageService,
|
||||
InvoicePdfRenderingService invoiceService,
|
||||
QrBillService qrBillService
|
||||
) {
|
||||
this.orderRepo = orderRepo;
|
||||
this.orderItemRepo = orderItemRepo;
|
||||
this.paymentRepo = paymentRepo;
|
||||
this.paymentService = paymentService;
|
||||
this.storageService = storageService;
|
||||
this.invoiceService = invoiceService;
|
||||
this.qrBillService = qrBillService;
|
||||
public AdminOrderController(AdminOrderControllerService adminOrderControllerService) {
|
||||
this.adminOrderControllerService = adminOrderControllerService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<OrderDto>> listOrders() {
|
||||
List<OrderDto> response = orderRepo.findAllByOrderByCreatedAtDesc()
|
||||
.stream()
|
||||
.map(this::toOrderDto)
|
||||
.toList();
|
||||
return ResponseEntity.ok(response);
|
||||
return ResponseEntity.ok(adminOrderControllerService.listOrders());
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}")
|
||||
public ResponseEntity<OrderDto> getOrder(@PathVariable UUID orderId) {
|
||||
return ResponseEntity.ok(toOrderDto(getOrderOrThrow(orderId)));
|
||||
return ResponseEntity.ok(adminOrderControllerService.getOrder(orderId));
|
||||
}
|
||||
|
||||
@PostMapping("/{orderId}/payments/confirm")
|
||||
@Transactional
|
||||
public ResponseEntity<OrderDto> confirmPayment(
|
||||
public ResponseEntity<OrderDto> updatePaymentMethod(
|
||||
@PathVariable UUID orderId,
|
||||
@RequestBody(required = false) Map<String, String> payload
|
||||
) {
|
||||
getOrderOrThrow(orderId);
|
||||
String method = payload != null ? payload.get("method") : null;
|
||||
paymentService.confirmPayment(orderId, method);
|
||||
return ResponseEntity.ok(toOrderDto(getOrderOrThrow(orderId)));
|
||||
return ResponseEntity.ok(adminOrderControllerService.updatePaymentMethod(orderId, payload));
|
||||
}
|
||||
|
||||
@PostMapping("/{orderId}/status")
|
||||
@@ -112,22 +53,7 @@ public class AdminOrderController {
|
||||
@PathVariable UUID orderId,
|
||||
@RequestBody AdminOrderStatusUpdateRequest payload
|
||||
) {
|
||||
if (payload == null || payload.getStatus() == null || payload.getStatus().isBlank()) {
|
||||
throw new ResponseStatusException(org.springframework.http.HttpStatus.BAD_REQUEST, "Status is required");
|
||||
}
|
||||
|
||||
Order order = getOrderOrThrow(orderId);
|
||||
String normalizedStatus = payload.getStatus().trim().toUpperCase(Locale.ROOT);
|
||||
if (!ALLOWED_ORDER_STATUSES.contains(normalizedStatus)) {
|
||||
throw new ResponseStatusException(
|
||||
BAD_REQUEST,
|
||||
"Invalid order status. Allowed values: " + String.join(", ", ALLOWED_ORDER_STATUSES)
|
||||
);
|
||||
}
|
||||
order.setStatus(normalizedStatus);
|
||||
orderRepo.save(order);
|
||||
|
||||
return ResponseEntity.ok(toOrderDto(order));
|
||||
return ResponseEntity.ok(adminOrderControllerService.updateOrderStatus(orderId, payload));
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/items/{orderItemId}/file")
|
||||
@@ -135,198 +61,16 @@ public class AdminOrderController {
|
||||
@PathVariable UUID orderId,
|
||||
@PathVariable UUID orderItemId
|
||||
) {
|
||||
OrderItem item = orderItemRepo.findById(orderItemId)
|
||||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Order item not found"));
|
||||
|
||||
if (!item.getOrder().getId().equals(orderId)) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "Order item not found for order");
|
||||
}
|
||||
|
||||
String relativePath = item.getStoredRelativePath();
|
||||
if (relativePath == null || relativePath.isBlank() || "PENDING".equals(relativePath)) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
Path safeRelativePath = resolveOrderItemRelativePath(relativePath, orderId, orderItemId);
|
||||
if (safeRelativePath == null) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
|
||||
try {
|
||||
Resource resource = storageService.loadAsResource(safeRelativePath);
|
||||
MediaType contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
if (item.getMimeType() != null && !item.getMimeType().isBlank()) {
|
||||
try {
|
||||
contentType = MediaType.parseMediaType(item.getMimeType());
|
||||
} catch (Exception ignored) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
}
|
||||
|
||||
String filename = item.getOriginalFilename() != null && !item.getOriginalFilename().isBlank()
|
||||
? item.getOriginalFilename()
|
||||
: "order-item-" + orderItemId;
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(contentType)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
|
||||
.filename(filename, StandardCharsets.UTF_8)
|
||||
.build()
|
||||
.toString())
|
||||
.body(resource);
|
||||
} catch (Exception e) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
return adminOrderControllerService.downloadOrderItemFile(orderId, orderItemId);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/documents/confirmation")
|
||||
public ResponseEntity<byte[]> downloadOrderConfirmation(@PathVariable UUID orderId) {
|
||||
return generateDocument(getOrderOrThrow(orderId), true);
|
||||
return adminOrderControllerService.downloadOrderConfirmation(orderId);
|
||||
}
|
||||
|
||||
@GetMapping("/{orderId}/documents/invoice")
|
||||
public ResponseEntity<byte[]> downloadOrderInvoice(@PathVariable UUID orderId) {
|
||||
return generateDocument(getOrderOrThrow(orderId), false);
|
||||
}
|
||||
|
||||
private Order getOrderOrThrow(UUID orderId) {
|
||||
return orderRepo.findById(orderId)
|
||||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Order not found"));
|
||||
}
|
||||
|
||||
private OrderDto toOrderDto(Order order) {
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
OrderDto dto = new OrderDto();
|
||||
dto.setId(order.getId());
|
||||
dto.setOrderNumber(getDisplayOrderNumber(order));
|
||||
dto.setStatus(order.getStatus());
|
||||
|
||||
paymentRepo.findByOrder_Id(order.getId()).ifPresent(p -> {
|
||||
dto.setPaymentStatus(p.getStatus());
|
||||
dto.setPaymentMethod(p.getMethod());
|
||||
});
|
||||
|
||||
dto.setCustomerEmail(order.getCustomerEmail());
|
||||
dto.setCustomerPhone(order.getCustomerPhone());
|
||||
dto.setPreferredLanguage(order.getPreferredLanguage());
|
||||
dto.setBillingCustomerType(order.getBillingCustomerType());
|
||||
dto.setCurrency(order.getCurrency());
|
||||
dto.setSetupCostChf(order.getSetupCostChf());
|
||||
dto.setShippingCostChf(order.getShippingCostChf());
|
||||
dto.setDiscountChf(order.getDiscountChf());
|
||||
dto.setSubtotalChf(order.getSubtotalChf());
|
||||
dto.setIsCadOrder(order.getIsCadOrder());
|
||||
dto.setSourceRequestId(order.getSourceRequestId());
|
||||
dto.setCadHours(order.getCadHours());
|
||||
dto.setCadHourlyRateChf(order.getCadHourlyRateChf());
|
||||
dto.setCadTotalChf(order.getCadTotalChf());
|
||||
dto.setTotalChf(order.getTotalChf());
|
||||
dto.setCreatedAt(order.getCreatedAt());
|
||||
dto.setShippingSameAsBilling(order.getShippingSameAsBilling());
|
||||
QuoteSession sourceSession = order.getSourceQuoteSession();
|
||||
if (sourceSession != null) {
|
||||
dto.setPrintMaterialCode(sourceSession.getMaterialCode());
|
||||
dto.setPrintNozzleDiameterMm(sourceSession.getNozzleDiameterMm());
|
||||
dto.setPrintLayerHeightMm(sourceSession.getLayerHeightMm());
|
||||
dto.setPrintInfillPattern(sourceSession.getInfillPattern());
|
||||
dto.setPrintInfillPercent(sourceSession.getInfillPercent());
|
||||
dto.setPrintSupportsEnabled(sourceSession.getSupportsEnabled());
|
||||
}
|
||||
|
||||
AddressDto billing = new AddressDto();
|
||||
billing.setFirstName(order.getBillingFirstName());
|
||||
billing.setLastName(order.getBillingLastName());
|
||||
billing.setCompanyName(order.getBillingCompanyName());
|
||||
billing.setContactPerson(order.getBillingContactPerson());
|
||||
billing.setAddressLine1(order.getBillingAddressLine1());
|
||||
billing.setAddressLine2(order.getBillingAddressLine2());
|
||||
billing.setZip(order.getBillingZip());
|
||||
billing.setCity(order.getBillingCity());
|
||||
billing.setCountryCode(order.getBillingCountryCode());
|
||||
dto.setBillingAddress(billing);
|
||||
|
||||
if (!Boolean.TRUE.equals(order.getShippingSameAsBilling())) {
|
||||
AddressDto shipping = new AddressDto();
|
||||
shipping.setFirstName(order.getShippingFirstName());
|
||||
shipping.setLastName(order.getShippingLastName());
|
||||
shipping.setCompanyName(order.getShippingCompanyName());
|
||||
shipping.setContactPerson(order.getShippingContactPerson());
|
||||
shipping.setAddressLine1(order.getShippingAddressLine1());
|
||||
shipping.setAddressLine2(order.getShippingAddressLine2());
|
||||
shipping.setZip(order.getShippingZip());
|
||||
shipping.setCity(order.getShippingCity());
|
||||
shipping.setCountryCode(order.getShippingCountryCode());
|
||||
dto.setShippingAddress(shipping);
|
||||
}
|
||||
|
||||
List<OrderItemDto> itemDtos = items.stream().map(i -> {
|
||||
OrderItemDto idto = new OrderItemDto();
|
||||
idto.setId(i.getId());
|
||||
idto.setOriginalFilename(i.getOriginalFilename());
|
||||
idto.setMaterialCode(i.getMaterialCode());
|
||||
idto.setColorCode(i.getColorCode());
|
||||
idto.setQuantity(i.getQuantity());
|
||||
idto.setPrintTimeSeconds(i.getPrintTimeSeconds());
|
||||
idto.setMaterialGrams(i.getMaterialGrams());
|
||||
idto.setUnitPriceChf(i.getUnitPriceChf());
|
||||
idto.setLineTotalChf(i.getLineTotalChf());
|
||||
return idto;
|
||||
}).toList();
|
||||
dto.setItems(itemDtos);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
return orderNumber;
|
||||
}
|
||||
return order.getId() != null ? order.getId().toString() : "unknown";
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> generateDocument(Order order, boolean isConfirmation) {
|
||||
String displayOrderNumber = getDisplayOrderNumber(order);
|
||||
if (isConfirmation) {
|
||||
Path relativePath = buildConfirmationPdfRelativePath(order.getId(), displayOrderNumber);
|
||||
try {
|
||||
byte[] existingPdf = storageService.loadAsResource(relativePath).getInputStream().readAllBytes();
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"confirmation-" + displayOrderNumber + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(existingPdf);
|
||||
} catch (Exception ignored) {
|
||||
// fallback to generated confirmation document
|
||||
}
|
||||
}
|
||||
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
Payment payment = paymentRepo.findByOrder_Id(order.getId()).orElse(null);
|
||||
byte[] pdf = invoiceService.generateDocumentPdf(order, items, isConfirmation, qrBillService, payment);
|
||||
|
||||
String prefix = isConfirmation ? "confirmation-" : "invoice-";
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + prefix + displayOrderNumber + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(pdf);
|
||||
}
|
||||
|
||||
private Path resolveOrderItemRelativePath(String storedRelativePath, UUID orderId, UUID orderItemId) {
|
||||
try {
|
||||
Path candidate = Path.of(storedRelativePath).normalize();
|
||||
if (candidate.isAbsolute()) {
|
||||
return null;
|
||||
}
|
||||
Path expectedPrefix = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString());
|
||||
if (!candidate.startsWith(expectedPrefix)) {
|
||||
return null;
|
||||
}
|
||||
return candidate;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Path buildConfirmationPdfRelativePath(UUID orderId, String orderNumber) {
|
||||
return Path.of("orders", orderId.toString(), "documents", "confirmation-" + orderNumber + ".pdf");
|
||||
return adminOrderControllerService.downloadOrderInvoice(orderId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ public record OptionsResponse(
|
||||
List<QualityOption> qualities,
|
||||
List<InfillPatternOption> infillPatterns,
|
||||
List<LayerHeightOptionDTO> layerHeights,
|
||||
List<NozzleOptionDTO> nozzleDiameters
|
||||
List<NozzleOptionDTO> nozzleDiameters,
|
||||
List<NozzleLayerHeightOptionsDTO> layerHeightsByNozzle
|
||||
) {
|
||||
public record MaterialOption(String code, String label, List<VariantOption> variants) {}
|
||||
public record VariantOption(
|
||||
@@ -24,4 +25,5 @@ public record OptionsResponse(
|
||||
public record InfillPatternOption(String id, String label) {}
|
||||
public record LayerHeightOptionDTO(double value, String label) {}
|
||||
public record NozzleOptionDTO(double value, String label) {}
|
||||
public record NozzleLayerHeightOptionsDTO(double nozzleDiameter, List<LayerHeightOptionDTO> layerHeights) {}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ public class OrderItemDto {
|
||||
private String originalFilename;
|
||||
private String materialCode;
|
||||
private String colorCode;
|
||||
private String quality;
|
||||
private BigDecimal nozzleDiameterMm;
|
||||
private BigDecimal layerHeightMm;
|
||||
private Integer infillPercent;
|
||||
private String infillPattern;
|
||||
private Boolean supportsEnabled;
|
||||
private Integer quantity;
|
||||
private Integer printTimeSeconds;
|
||||
private BigDecimal materialGrams;
|
||||
@@ -27,6 +33,24 @@ public class OrderItemDto {
|
||||
public String getColorCode() { return colorCode; }
|
||||
public void setColorCode(String colorCode) { this.colorCode = colorCode; }
|
||||
|
||||
public String getQuality() { return quality; }
|
||||
public void setQuality(String quality) { this.quality = quality; }
|
||||
|
||||
public BigDecimal getNozzleDiameterMm() { return nozzleDiameterMm; }
|
||||
public void setNozzleDiameterMm(BigDecimal nozzleDiameterMm) { this.nozzleDiameterMm = nozzleDiameterMm; }
|
||||
|
||||
public BigDecimal getLayerHeightMm() { return layerHeightMm; }
|
||||
public void setLayerHeightMm(BigDecimal layerHeightMm) { this.layerHeightMm = layerHeightMm; }
|
||||
|
||||
public Integer getInfillPercent() { return infillPercent; }
|
||||
public void setInfillPercent(Integer infillPercent) { this.infillPercent = infillPercent; }
|
||||
|
||||
public String getInfillPattern() { return infillPattern; }
|
||||
public void setInfillPattern(String infillPattern) { this.infillPattern = infillPattern; }
|
||||
|
||||
public Boolean getSupportsEnabled() { return supportsEnabled; }
|
||||
public void setSupportsEnabled(Boolean supportsEnabled) { this.supportsEnabled = supportsEnabled; }
|
||||
|
||||
public Integer getQuantity() { return quantity; }
|
||||
public void setQuantity(Integer quantity) { this.quantity = quantity; }
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.printcalculator.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PrintSettingsDto {
|
||||
// Mode: "BASIC" or "ADVANCED"
|
||||
private String complexityMode;
|
||||
@@ -28,4 +25,124 @@ public class PrintSettingsDto {
|
||||
private Double boundingBoxX;
|
||||
private Double boundingBoxY;
|
||||
private Double boundingBoxZ;
|
||||
|
||||
public String getComplexityMode() {
|
||||
return complexityMode;
|
||||
}
|
||||
|
||||
public void setComplexityMode(String complexityMode) {
|
||||
this.complexityMode = complexityMode;
|
||||
}
|
||||
|
||||
public String getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public void setMaterial(String material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Long getFilamentVariantId() {
|
||||
return filamentVariantId;
|
||||
}
|
||||
|
||||
public void setFilamentVariantId(Long filamentVariantId) {
|
||||
this.filamentVariantId = filamentVariantId;
|
||||
}
|
||||
|
||||
public Long getPrinterMachineId() {
|
||||
return printerMachineId;
|
||||
}
|
||||
|
||||
public void setPrinterMachineId(Long printerMachineId) {
|
||||
this.printerMachineId = printerMachineId;
|
||||
}
|
||||
|
||||
public String getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(String quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public Double getNozzleDiameter() {
|
||||
return nozzleDiameter;
|
||||
}
|
||||
|
||||
public void setNozzleDiameter(Double nozzleDiameter) {
|
||||
this.nozzleDiameter = nozzleDiameter;
|
||||
}
|
||||
|
||||
public Double getLayerHeight() {
|
||||
return layerHeight;
|
||||
}
|
||||
|
||||
public void setLayerHeight(Double layerHeight) {
|
||||
this.layerHeight = layerHeight;
|
||||
}
|
||||
|
||||
public Double getInfillDensity() {
|
||||
return infillDensity;
|
||||
}
|
||||
|
||||
public void setInfillDensity(Double infillDensity) {
|
||||
this.infillDensity = infillDensity;
|
||||
}
|
||||
|
||||
public String getInfillPattern() {
|
||||
return infillPattern;
|
||||
}
|
||||
|
||||
public void setInfillPattern(String infillPattern) {
|
||||
this.infillPattern = infillPattern;
|
||||
}
|
||||
|
||||
public Boolean getSupportsEnabled() {
|
||||
return supportsEnabled;
|
||||
}
|
||||
|
||||
public void setSupportsEnabled(Boolean supportsEnabled) {
|
||||
this.supportsEnabled = supportsEnabled;
|
||||
}
|
||||
|
||||
public String getNotes() {
|
||||
return notes;
|
||||
}
|
||||
|
||||
public void setNotes(String notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public Double getBoundingBoxX() {
|
||||
return boundingBoxX;
|
||||
}
|
||||
|
||||
public void setBoundingBoxX(Double boundingBoxX) {
|
||||
this.boundingBoxX = boundingBoxX;
|
||||
}
|
||||
|
||||
public Double getBoundingBoxY() {
|
||||
return boundingBoxY;
|
||||
}
|
||||
|
||||
public void setBoundingBoxY(Double boundingBoxY) {
|
||||
this.boundingBoxY = boundingBoxY;
|
||||
}
|
||||
|
||||
public Double getBoundingBoxZ() {
|
||||
return boundingBoxZ;
|
||||
}
|
||||
|
||||
public void setBoundingBoxZ(Double boundingBoxZ) {
|
||||
this.boundingBoxZ = boundingBoxZ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.printcalculator.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
name = "nozzle_layer_height_option",
|
||||
uniqueConstraints = @UniqueConstraint(
|
||||
name = "ux_nozzle_layer_height_option_nozzle_layer",
|
||||
columnNames = {"nozzle_diameter_mm", "layer_height_mm"}
|
||||
)
|
||||
)
|
||||
public class NozzleLayerHeightOption {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "nozzle_layer_height_option_id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "nozzle_diameter_mm", nullable = false, precision = 4, scale = 2)
|
||||
private BigDecimal nozzleDiameterMm;
|
||||
|
||||
@Column(name = "layer_height_mm", nullable = false, precision = 5, scale = 3)
|
||||
private BigDecimal layerHeightMm;
|
||||
|
||||
@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 BigDecimal getNozzleDiameterMm() {
|
||||
return nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public void setNozzleDiameterMm(BigDecimal nozzleDiameterMm) {
|
||||
this.nozzleDiameterMm = nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public BigDecimal getLayerHeightMm() {
|
||||
return layerHeightMm;
|
||||
}
|
||||
|
||||
public void setLayerHeightMm(BigDecimal layerHeightMm) {
|
||||
this.layerHeightMm = layerHeightMm;
|
||||
}
|
||||
|
||||
public Boolean getIsActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setIsActive(Boolean isActive) {
|
||||
this.isActive = isActive;
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,24 @@ public class OrderItem {
|
||||
@Column(name = "material_code", nullable = false, length = Integer.MAX_VALUE)
|
||||
private String materialCode;
|
||||
|
||||
@Column(name = "quality", length = Integer.MAX_VALUE)
|
||||
private String quality;
|
||||
|
||||
@Column(name = "nozzle_diameter_mm", precision = 4, scale = 2)
|
||||
private BigDecimal nozzleDiameterMm;
|
||||
|
||||
@Column(name = "layer_height_mm", precision = 5, scale = 3)
|
||||
private BigDecimal layerHeightMm;
|
||||
|
||||
@Column(name = "infill_percent")
|
||||
private Integer infillPercent;
|
||||
|
||||
@Column(name = "infill_pattern", length = Integer.MAX_VALUE)
|
||||
private String infillPattern;
|
||||
|
||||
@Column(name = "supports_enabled")
|
||||
private Boolean supportsEnabled;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "filament_variant_id")
|
||||
private FilamentVariant filamentVariant;
|
||||
@@ -162,6 +180,54 @@ public class OrderItem {
|
||||
this.materialCode = materialCode;
|
||||
}
|
||||
|
||||
public String getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(String quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public BigDecimal getNozzleDiameterMm() {
|
||||
return nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public void setNozzleDiameterMm(BigDecimal nozzleDiameterMm) {
|
||||
this.nozzleDiameterMm = nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public BigDecimal getLayerHeightMm() {
|
||||
return layerHeightMm;
|
||||
}
|
||||
|
||||
public void setLayerHeightMm(BigDecimal layerHeightMm) {
|
||||
this.layerHeightMm = layerHeightMm;
|
||||
}
|
||||
|
||||
public Integer getInfillPercent() {
|
||||
return infillPercent;
|
||||
}
|
||||
|
||||
public void setInfillPercent(Integer infillPercent) {
|
||||
this.infillPercent = infillPercent;
|
||||
}
|
||||
|
||||
public String getInfillPattern() {
|
||||
return infillPattern;
|
||||
}
|
||||
|
||||
public void setInfillPattern(String infillPattern) {
|
||||
this.infillPattern = infillPattern;
|
||||
}
|
||||
|
||||
public Boolean getSupportsEnabled() {
|
||||
return supportsEnabled;
|
||||
}
|
||||
|
||||
public void setSupportsEnabled(Boolean supportsEnabled) {
|
||||
this.supportsEnabled = supportsEnabled;
|
||||
}
|
||||
|
||||
public FilamentVariant getFilamentVariant() {
|
||||
return filamentVariant;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,27 @@ public class QuoteLineItem {
|
||||
@com.fasterxml.jackson.annotation.JsonIgnore
|
||||
private FilamentVariant filamentVariant;
|
||||
|
||||
@Column(name = "material_code", length = Integer.MAX_VALUE)
|
||||
private String materialCode;
|
||||
|
||||
@Column(name = "quality", length = Integer.MAX_VALUE)
|
||||
private String quality;
|
||||
|
||||
@Column(name = "nozzle_diameter_mm", precision = 5, scale = 2)
|
||||
private BigDecimal nozzleDiameterMm;
|
||||
|
||||
@Column(name = "layer_height_mm", precision = 6, scale = 3)
|
||||
private BigDecimal layerHeightMm;
|
||||
|
||||
@Column(name = "infill_percent")
|
||||
private Integer infillPercent;
|
||||
|
||||
@Column(name = "infill_pattern", length = Integer.MAX_VALUE)
|
||||
private String infillPattern;
|
||||
|
||||
@Column(name = "supports_enabled")
|
||||
private Boolean supportsEnabled;
|
||||
|
||||
@Column(name = "bounding_box_x_mm", precision = 10, scale = 3)
|
||||
private BigDecimal boundingBoxXMm;
|
||||
|
||||
@@ -137,6 +158,62 @@ public class QuoteLineItem {
|
||||
this.filamentVariant = filamentVariant;
|
||||
}
|
||||
|
||||
public String getMaterialCode() {
|
||||
return materialCode;
|
||||
}
|
||||
|
||||
public void setMaterialCode(String materialCode) {
|
||||
this.materialCode = materialCode;
|
||||
}
|
||||
|
||||
public String getQuality() {
|
||||
return quality;
|
||||
}
|
||||
|
||||
public void setQuality(String quality) {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
public BigDecimal getNozzleDiameterMm() {
|
||||
return nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public void setNozzleDiameterMm(BigDecimal nozzleDiameterMm) {
|
||||
this.nozzleDiameterMm = nozzleDiameterMm;
|
||||
}
|
||||
|
||||
public BigDecimal getLayerHeightMm() {
|
||||
return layerHeightMm;
|
||||
}
|
||||
|
||||
public void setLayerHeightMm(BigDecimal layerHeightMm) {
|
||||
this.layerHeightMm = layerHeightMm;
|
||||
}
|
||||
|
||||
public Integer getInfillPercent() {
|
||||
return infillPercent;
|
||||
}
|
||||
|
||||
public void setInfillPercent(Integer infillPercent) {
|
||||
this.infillPercent = infillPercent;
|
||||
}
|
||||
|
||||
public String getInfillPattern() {
|
||||
return infillPattern;
|
||||
}
|
||||
|
||||
public void setInfillPattern(String infillPattern) {
|
||||
this.infillPattern = infillPattern;
|
||||
}
|
||||
|
||||
public Boolean getSupportsEnabled() {
|
||||
return supportsEnabled;
|
||||
}
|
||||
|
||||
public void setSupportsEnabled(Boolean supportsEnabled) {
|
||||
this.supportsEnabled = supportsEnabled;
|
||||
}
|
||||
|
||||
public BigDecimal getBoundingBoxXMm() {
|
||||
return boundingBoxXMm;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.printcalculator.event;
|
||||
|
||||
import com.printcalculator.entity.Order;
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class OrderShippedEvent extends ApplicationEvent {
|
||||
|
||||
private final Order order;
|
||||
|
||||
public OrderShippedEvent(Object source, Order order) {
|
||||
super(source);
|
||||
this.order = order;
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,13 @@ import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.entity.OrderItem;
|
||||
import com.printcalculator.entity.Payment;
|
||||
import com.printcalculator.event.OrderCreatedEvent;
|
||||
import com.printcalculator.event.OrderShippedEvent;
|
||||
import com.printcalculator.event.PaymentConfirmedEvent;
|
||||
import com.printcalculator.event.PaymentReportedEvent;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.service.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.QrBillService;
|
||||
import com.printcalculator.service.StorageService;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import com.printcalculator.service.email.EmailNotificationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -95,6 +96,19 @@ public class OrderEmailListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Async
|
||||
@EventListener
|
||||
public void handleOrderShippedEvent(OrderShippedEvent event) {
|
||||
Order order = event.getOrder();
|
||||
log.info("Processing OrderShippedEvent for order id: {}", order.getId());
|
||||
|
||||
try {
|
||||
sendOrderShippedEmail(order);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to send order shipped email for order id: {}", order.getId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendCustomerConfirmationEmail(Order order) {
|
||||
String language = resolveLanguage(order.getPreferredLanguage());
|
||||
String orderNumber = getDisplayOrderNumber(order);
|
||||
@@ -153,6 +167,21 @@ public class OrderEmailListener {
|
||||
);
|
||||
}
|
||||
|
||||
private void sendOrderShippedEmail(Order order) {
|
||||
String language = resolveLanguage(order.getPreferredLanguage());
|
||||
String orderNumber = getDisplayOrderNumber(order);
|
||||
|
||||
Map<String, Object> templateData = buildBaseTemplateData(order, language);
|
||||
String subject = applyOrderShippedTexts(templateData, language, orderNumber);
|
||||
|
||||
emailNotificationService.sendEmail(
|
||||
order.getCustomer().getEmail(),
|
||||
subject,
|
||||
"order-shipped",
|
||||
templateData
|
||||
);
|
||||
}
|
||||
|
||||
private void sendAdminNotificationEmail(Order order) {
|
||||
String orderNumber = getDisplayOrderNumber(order);
|
||||
Map<String, Object> templateData = buildBaseTemplateData(order, DEFAULT_LANGUAGE);
|
||||
@@ -381,6 +410,63 @@ public class OrderEmailListener {
|
||||
};
|
||||
}
|
||||
|
||||
private String applyOrderShippedTexts(Map<String, Object> templateData, String language, String orderNumber) {
|
||||
return switch (language) {
|
||||
case "en" -> {
|
||||
templateData.put("emailTitle", "Order Shipped");
|
||||
templateData.put("headlineText", "Your order #" + orderNumber + " has been shipped");
|
||||
templateData.put("greetingText", "Hi " + templateData.get("customerName") + ",");
|
||||
templateData.put("introText", "Good news: your package has left our workshop and is on its way.");
|
||||
templateData.put("statusText", "Current status: Shipped.");
|
||||
templateData.put("orderDetailsCtaText", "View order status");
|
||||
templateData.put("supportText", "If you need assistance, reply to this email.");
|
||||
templateData.put("footerText", "Automated message from 3D-Fab.");
|
||||
templateData.put("labelOrderNumber", "Order number");
|
||||
templateData.put("labelTotal", "Total");
|
||||
yield "Your order has been shipped (Order #" + orderNumber + ") - 3D-Fab";
|
||||
}
|
||||
case "de" -> {
|
||||
templateData.put("emailTitle", "Bestellung versandt");
|
||||
templateData.put("headlineText", "Ihre Bestellung #" + orderNumber + " wurde versandt");
|
||||
templateData.put("greetingText", "Hallo " + templateData.get("customerName") + ",");
|
||||
templateData.put("introText", "Gute Nachricht: Ihr Paket hat unsere Werkstatt verlassen und ist unterwegs.");
|
||||
templateData.put("statusText", "Aktueller Status: Versandt.");
|
||||
templateData.put("orderDetailsCtaText", "Bestellstatus ansehen");
|
||||
templateData.put("supportText", "Wenn Sie Hilfe benoetigen, antworten Sie auf diese E-Mail.");
|
||||
templateData.put("footerText", "Automatische Nachricht von 3D-Fab.");
|
||||
templateData.put("labelOrderNumber", "Bestellnummer");
|
||||
templateData.put("labelTotal", "Gesamtbetrag");
|
||||
yield "Ihre Bestellung wurde versandt (Bestellung #" + orderNumber + ") - 3D-Fab";
|
||||
}
|
||||
case "fr" -> {
|
||||
templateData.put("emailTitle", "Commande expediee");
|
||||
templateData.put("headlineText", "Votre commande #" + orderNumber + " a ete expediee");
|
||||
templateData.put("greetingText", "Bonjour " + templateData.get("customerName") + ",");
|
||||
templateData.put("introText", "Bonne nouvelle: votre colis a quitte notre atelier et est en route.");
|
||||
templateData.put("statusText", "Statut actuel: Expediee.");
|
||||
templateData.put("orderDetailsCtaText", "Voir le statut de la commande");
|
||||
templateData.put("supportText", "Si vous avez besoin d'aide, repondez a cet email.");
|
||||
templateData.put("footerText", "Message automatique de 3D-Fab.");
|
||||
templateData.put("labelOrderNumber", "Numero de commande");
|
||||
templateData.put("labelTotal", "Total");
|
||||
yield "Votre commande a ete expediee (Commande #" + orderNumber + ") - 3D-Fab";
|
||||
}
|
||||
default -> {
|
||||
templateData.put("emailTitle", "Ordine spedito");
|
||||
templateData.put("headlineText", "Il tuo ordine #" + orderNumber + " e' stato spedito");
|
||||
templateData.put("greetingText", "Ciao " + templateData.get("customerName") + ",");
|
||||
templateData.put("introText", "Buone notizie: il tuo pacco e' partito dal nostro laboratorio ed e' in viaggio.");
|
||||
templateData.put("statusText", "Stato attuale: spedito.");
|
||||
templateData.put("orderDetailsCtaText", "Visualizza stato ordine");
|
||||
templateData.put("supportText", "Se hai bisogno di assistenza, rispondi a questa email.");
|
||||
templateData.put("footerText", "Messaggio automatico di 3D-Fab.");
|
||||
templateData.put("labelOrderNumber", "Numero ordine");
|
||||
templateData.put("labelTotal", "Totale");
|
||||
yield "Il tuo ordine e' stato spedito (Ordine #" + orderNumber + ") - 3D-Fab";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.printcalculator.repository;
|
||||
|
||||
import com.printcalculator.entity.NozzleLayerHeightOption;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface NozzleLayerHeightOptionRepository extends JpaRepository<NozzleLayerHeightOption, Long> {
|
||||
List<NozzleLayerHeightOption> findByIsActiveTrueOrderByNozzleDiameterMmAscLayerHeightMmAsc();
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.printcalculator.service;
|
||||
|
||||
import com.printcalculator.entity.NozzleLayerHeightOption;
|
||||
import com.printcalculator.repository.NozzleLayerHeightOptionRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class NozzleLayerHeightPolicyService {
|
||||
private static final BigDecimal DEFAULT_NOZZLE = BigDecimal.valueOf(0.40).setScale(2, RoundingMode.HALF_UP);
|
||||
private static final BigDecimal DEFAULT_LAYER = BigDecimal.valueOf(0.20).setScale(3, RoundingMode.HALF_UP);
|
||||
|
||||
private final NozzleLayerHeightOptionRepository ruleRepo;
|
||||
|
||||
public NozzleLayerHeightPolicyService(NozzleLayerHeightOptionRepository ruleRepo) {
|
||||
this.ruleRepo = ruleRepo;
|
||||
}
|
||||
|
||||
public Map<BigDecimal, List<BigDecimal>> getActiveRulesByNozzle() {
|
||||
List<NozzleLayerHeightOption> rules = ruleRepo.findByIsActiveTrueOrderByNozzleDiameterMmAscLayerHeightMmAsc();
|
||||
if (rules.isEmpty()) {
|
||||
return fallbackRules();
|
||||
}
|
||||
|
||||
Map<BigDecimal, List<BigDecimal>> byNozzle = new LinkedHashMap<>();
|
||||
for (NozzleLayerHeightOption rule : rules) {
|
||||
BigDecimal nozzle = normalizeNozzle(rule.getNozzleDiameterMm());
|
||||
BigDecimal layer = normalizeLayer(rule.getLayerHeightMm());
|
||||
if (nozzle == null || layer == null) {
|
||||
continue;
|
||||
}
|
||||
byNozzle.computeIfAbsent(nozzle, ignored -> new ArrayList<>()).add(layer);
|
||||
}
|
||||
|
||||
byNozzle.values().forEach(this::sortAndDeduplicate);
|
||||
return byNozzle;
|
||||
}
|
||||
|
||||
public BigDecimal normalizeNozzle(BigDecimal value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return value.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public BigDecimal normalizeLayer(BigDecimal value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return value.setScale(3, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public BigDecimal resolveNozzle(BigDecimal requestedNozzle) {
|
||||
return normalizeNozzle(requestedNozzle != null ? requestedNozzle : DEFAULT_NOZZLE);
|
||||
}
|
||||
|
||||
public BigDecimal resolveLayer(BigDecimal requestedLayer, BigDecimal nozzleDiameter) {
|
||||
if (requestedLayer != null) {
|
||||
return normalizeLayer(requestedLayer);
|
||||
}
|
||||
return defaultLayerForNozzle(nozzleDiameter);
|
||||
}
|
||||
|
||||
public List<BigDecimal> allowedLayersForNozzle(BigDecimal nozzleDiameter) {
|
||||
BigDecimal nozzle = resolveNozzle(nozzleDiameter);
|
||||
List<BigDecimal> allowed = getActiveRulesByNozzle().get(nozzle);
|
||||
return allowed != null ? allowed : List.of();
|
||||
}
|
||||
|
||||
public boolean isAllowed(BigDecimal nozzleDiameter, BigDecimal layerHeight) {
|
||||
BigDecimal layer = normalizeLayer(layerHeight);
|
||||
if (layer == null) {
|
||||
return false;
|
||||
}
|
||||
return allowedLayersForNozzle(nozzleDiameter)
|
||||
.stream()
|
||||
.anyMatch(allowed -> allowed.compareTo(layer) == 0);
|
||||
}
|
||||
|
||||
public BigDecimal defaultLayerForNozzle(BigDecimal nozzleDiameter) {
|
||||
List<BigDecimal> allowed = allowedLayersForNozzle(nozzleDiameter);
|
||||
if (allowed.isEmpty()) {
|
||||
return DEFAULT_LAYER;
|
||||
}
|
||||
|
||||
BigDecimal preferred = normalizeLayer(DEFAULT_LAYER);
|
||||
for (BigDecimal candidate : allowed) {
|
||||
if (candidate.compareTo(preferred) == 0) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return allowed.get(0);
|
||||
}
|
||||
|
||||
public String allowedLayersLabel(BigDecimal nozzleDiameter) {
|
||||
List<BigDecimal> allowed = allowedLayersForNozzle(nozzleDiameter);
|
||||
if (allowed.isEmpty()) {
|
||||
return "none";
|
||||
}
|
||||
return allowed.stream()
|
||||
.map(value -> String.format(Locale.ROOT, "%.2f", value))
|
||||
.reduce((a, b) -> a + ", " + b)
|
||||
.orElse("none");
|
||||
}
|
||||
|
||||
private void sortAndDeduplicate(List<BigDecimal> values) {
|
||||
values.sort(Comparator.naturalOrder());
|
||||
for (int i = values.size() - 1; i > 0; i--) {
|
||||
if (values.get(i).compareTo(values.get(i - 1)) == 0) {
|
||||
values.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Map<BigDecimal, List<BigDecimal>> fallbackRules() {
|
||||
Map<BigDecimal, List<BigDecimal>> fallback = new LinkedHashMap<>();
|
||||
fallback.put(scaleNozzle(0.20), scaleLayers(0.04, 0.06, 0.08, 0.10, 0.12));
|
||||
fallback.put(scaleNozzle(0.40), scaleLayers(0.08, 0.12, 0.16, 0.20, 0.24, 0.28));
|
||||
fallback.put(scaleNozzle(0.60), scaleLayers(0.16, 0.20, 0.24, 0.30, 0.36));
|
||||
fallback.put(scaleNozzle(0.80), scaleLayers(0.20, 0.28, 0.36, 0.40, 0.48, 0.56));
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private BigDecimal scaleNozzle(double value) {
|
||||
return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
private List<BigDecimal> scaleLayers(double... values) {
|
||||
List<BigDecimal> scaled = new ArrayList<>();
|
||||
for (double value : values) {
|
||||
scaled.add(BigDecimal.valueOf(value).setScale(3, RoundingMode.HALF_UP));
|
||||
}
|
||||
return scaled;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,10 @@ import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.QuoteLineItemRepository;
|
||||
import com.printcalculator.repository.QuoteSessionRepository;
|
||||
import com.printcalculator.event.OrderCreatedEvent;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.PaymentService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -16,6 +20,7 @@ import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.OffsetDateTime;
|
||||
@@ -23,6 +28,7 @@ import java.util.*;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
private static final Path QUOTE_STORAGE_ROOT = Paths.get("storage_quotes").toAbsolutePath().normalize();
|
||||
|
||||
private final OrderRepository orderRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
@@ -178,6 +184,12 @@ public class OrderService {
|
||||
} else {
|
||||
oItem.setMaterialCode(session.getMaterialCode());
|
||||
}
|
||||
oItem.setQuality(qItem.getQuality());
|
||||
oItem.setNozzleDiameterMm(qItem.getNozzleDiameterMm());
|
||||
oItem.setLayerHeightMm(qItem.getLayerHeightMm());
|
||||
oItem.setInfillPercent(qItem.getInfillPercent());
|
||||
oItem.setInfillPattern(qItem.getInfillPattern());
|
||||
oItem.setSupportsEnabled(qItem.getSupportsEnabled());
|
||||
|
||||
BigDecimal distributedUnitPrice = qItem.getUnitPriceChf() != null ? qItem.getUnitPriceChf() : BigDecimal.ZERO;
|
||||
if (totals.totalPrintSeconds().compareTo(BigDecimal.ZERO) > 0 && qItem.getPrintTimeSeconds() != null) {
|
||||
@@ -210,16 +222,15 @@ public class OrderService {
|
||||
String relativePath = "orders/" + order.getId() + "/3d-files/" + oItem.getId() + "/" + storedFilename;
|
||||
oItem.setStoredRelativePath(relativePath);
|
||||
|
||||
if (qItem.getStoredPath() != null) {
|
||||
Path sourcePath = resolveStoredQuotePath(qItem.getStoredPath(), session.getId());
|
||||
if (sourcePath == null || !Files.exists(sourcePath)) {
|
||||
throw new IllegalStateException("Source file not available for quote line item " + qItem.getId());
|
||||
}
|
||||
try {
|
||||
Path sourcePath = Paths.get(qItem.getStoredPath());
|
||||
if (Files.exists(sourcePath)) {
|
||||
storageService.store(sourcePath, Paths.get(relativePath));
|
||||
oItem.setFileSizeBytes(Files.size(sourcePath));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new RuntimeException("Failed to copy quote file for line item " + qItem.getId(), e);
|
||||
}
|
||||
|
||||
oItem = orderItemRepo.save(oItem);
|
||||
@@ -291,6 +302,23 @@ public class OrderService {
|
||||
return "stl";
|
||||
}
|
||||
|
||||
private Path resolveStoredQuotePath(String storedPath, UUID expectedSessionId) {
|
||||
if (storedPath == null || storedPath.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Path raw = Path.of(storedPath).normalize();
|
||||
Path resolved = raw.isAbsolute() ? raw : QUOTE_STORAGE_ROOT.resolve(raw).normalize();
|
||||
Path expectedSessionRoot = QUOTE_STORAGE_ROOT.resolve(expectedSessionId.toString()).normalize();
|
||||
if (!resolved.startsWith(expectedSessionRoot)) {
|
||||
return null;
|
||||
}
|
||||
return resolved;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
|
||||
@@ -0,0 +1,423 @@
|
||||
package com.printcalculator.service.order;
|
||||
|
||||
import com.printcalculator.dto.AddressDto;
|
||||
import com.printcalculator.dto.AdminOrderStatusUpdateRequest;
|
||||
import com.printcalculator.dto.OrderDto;
|
||||
import com.printcalculator.dto.OrderItemDto;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.entity.OrderItem;
|
||||
import com.printcalculator.entity.Payment;
|
||||
import com.printcalculator.entity.QuoteLineItem;
|
||||
import com.printcalculator.entity.QuoteSession;
|
||||
import com.printcalculator.event.OrderShippedEvent;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.PaymentRepository;
|
||||
import com.printcalculator.repository.QuoteLineItemRepository;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.PaymentService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
public class AdminOrderControllerService {
|
||||
private static final Path QUOTE_STORAGE_ROOT = Paths.get("storage_quotes").toAbsolutePath().normalize();
|
||||
private static final List<String> ALLOWED_ORDER_STATUSES = List.of(
|
||||
"PENDING_PAYMENT",
|
||||
"PAID",
|
||||
"IN_PRODUCTION",
|
||||
"SHIPPED",
|
||||
"COMPLETED",
|
||||
"CANCELLED"
|
||||
);
|
||||
|
||||
private final OrderRepository orderRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
private final PaymentRepository paymentRepo;
|
||||
private final QuoteLineItemRepository quoteLineItemRepo;
|
||||
private final PaymentService paymentService;
|
||||
private final StorageService storageService;
|
||||
private final InvoicePdfRenderingService invoiceService;
|
||||
private final QrBillService qrBillService;
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
public AdminOrderControllerService(OrderRepository orderRepo,
|
||||
OrderItemRepository orderItemRepo,
|
||||
PaymentRepository paymentRepo,
|
||||
QuoteLineItemRepository quoteLineItemRepo,
|
||||
PaymentService paymentService,
|
||||
StorageService storageService,
|
||||
InvoicePdfRenderingService invoiceService,
|
||||
QrBillService qrBillService,
|
||||
ApplicationEventPublisher eventPublisher) {
|
||||
this.orderRepo = orderRepo;
|
||||
this.orderItemRepo = orderItemRepo;
|
||||
this.paymentRepo = paymentRepo;
|
||||
this.quoteLineItemRepo = quoteLineItemRepo;
|
||||
this.paymentService = paymentService;
|
||||
this.storageService = storageService;
|
||||
this.invoiceService = invoiceService;
|
||||
this.qrBillService = qrBillService;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
public List<OrderDto> listOrders() {
|
||||
return orderRepo.findAllByOrderByCreatedAtDesc()
|
||||
.stream()
|
||||
.map(this::toOrderDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public OrderDto getOrder(UUID orderId) {
|
||||
return toOrderDto(getOrderOrThrow(orderId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrderDto updatePaymentMethod(UUID orderId, Map<String, String> payload) {
|
||||
getOrderOrThrow(orderId);
|
||||
String method = payload != null ? payload.get("method") : null;
|
||||
if (method == null || method.isBlank()) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Payment method is required");
|
||||
}
|
||||
paymentService.updatePaymentMethod(orderId, method);
|
||||
return toOrderDto(getOrderOrThrow(orderId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrderDto updateOrderStatus(UUID orderId, AdminOrderStatusUpdateRequest payload) {
|
||||
if (payload == null || payload.getStatus() == null || payload.getStatus().isBlank()) {
|
||||
throw new ResponseStatusException(BAD_REQUEST, "Status is required");
|
||||
}
|
||||
|
||||
Order order = getOrderOrThrow(orderId);
|
||||
String normalizedStatus = payload.getStatus().trim().toUpperCase(Locale.ROOT);
|
||||
if (!ALLOWED_ORDER_STATUSES.contains(normalizedStatus)) {
|
||||
throw new ResponseStatusException(
|
||||
BAD_REQUEST,
|
||||
"Invalid order status. Allowed values: " + String.join(", ", ALLOWED_ORDER_STATUSES)
|
||||
);
|
||||
}
|
||||
String previousStatus = order.getStatus();
|
||||
order.setStatus(normalizedStatus);
|
||||
Order savedOrder = orderRepo.save(order);
|
||||
|
||||
if (!"SHIPPED".equals(previousStatus) && "SHIPPED".equals(normalizedStatus)) {
|
||||
eventPublisher.publishEvent(new OrderShippedEvent(this, savedOrder));
|
||||
}
|
||||
|
||||
return toOrderDto(savedOrder);
|
||||
}
|
||||
|
||||
public ResponseEntity<Resource> downloadOrderItemFile(UUID orderId, UUID orderItemId) {
|
||||
OrderItem item = orderItemRepo.findById(orderItemId)
|
||||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Order item not found"));
|
||||
|
||||
if (!item.getOrder().getId().equals(orderId)) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "Order item not found for order");
|
||||
}
|
||||
|
||||
String relativePath = item.getStoredRelativePath();
|
||||
if (relativePath == null || relativePath.isBlank() || "PENDING".equals(relativePath)) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
Path safeRelativePath = resolveOrderItemRelativePath(relativePath, orderId, orderItemId);
|
||||
if (safeRelativePath == null) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
|
||||
try {
|
||||
Resource resource = loadOrderItemResourceWithRecovery(item, safeRelativePath);
|
||||
MediaType contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
if (item.getMimeType() != null && !item.getMimeType().isBlank()) {
|
||||
try {
|
||||
contentType = MediaType.parseMediaType(item.getMimeType());
|
||||
} catch (Exception ignored) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
}
|
||||
|
||||
String filename = item.getOriginalFilename() != null && !item.getOriginalFilename().isBlank()
|
||||
? item.getOriginalFilename()
|
||||
: "order-item-" + orderItemId;
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(contentType)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
|
||||
.filename(filename, StandardCharsets.UTF_8)
|
||||
.build()
|
||||
.toString())
|
||||
.body(resource);
|
||||
} catch (ResponseStatusException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> downloadOrderConfirmation(UUID orderId) {
|
||||
return generateDocument(getOrderOrThrow(orderId), true);
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> downloadOrderInvoice(UUID orderId) {
|
||||
return generateDocument(getOrderOrThrow(orderId), false);
|
||||
}
|
||||
|
||||
private Order getOrderOrThrow(UUID orderId) {
|
||||
return orderRepo.findById(orderId)
|
||||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Order not found"));
|
||||
}
|
||||
|
||||
private OrderDto toOrderDto(Order order) {
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
OrderDto dto = new OrderDto();
|
||||
dto.setId(order.getId());
|
||||
dto.setOrderNumber(getDisplayOrderNumber(order));
|
||||
dto.setStatus(order.getStatus());
|
||||
|
||||
paymentRepo.findByOrder_Id(order.getId()).ifPresent(payment -> {
|
||||
dto.setPaymentStatus(payment.getStatus());
|
||||
dto.setPaymentMethod(payment.getMethod());
|
||||
});
|
||||
|
||||
dto.setCustomerEmail(order.getCustomerEmail());
|
||||
dto.setCustomerPhone(order.getCustomerPhone());
|
||||
dto.setPreferredLanguage(order.getPreferredLanguage());
|
||||
dto.setBillingCustomerType(order.getBillingCustomerType());
|
||||
dto.setCurrency(order.getCurrency());
|
||||
dto.setSetupCostChf(order.getSetupCostChf());
|
||||
dto.setShippingCostChf(order.getShippingCostChf());
|
||||
dto.setDiscountChf(order.getDiscountChf());
|
||||
dto.setSubtotalChf(order.getSubtotalChf());
|
||||
dto.setIsCadOrder(order.getIsCadOrder());
|
||||
dto.setSourceRequestId(order.getSourceRequestId());
|
||||
dto.setCadHours(order.getCadHours());
|
||||
dto.setCadHourlyRateChf(order.getCadHourlyRateChf());
|
||||
dto.setCadTotalChf(order.getCadTotalChf());
|
||||
dto.setTotalChf(order.getTotalChf());
|
||||
dto.setCreatedAt(order.getCreatedAt());
|
||||
dto.setShippingSameAsBilling(order.getShippingSameAsBilling());
|
||||
QuoteSession sourceSession = order.getSourceQuoteSession();
|
||||
if (sourceSession != null) {
|
||||
dto.setPrintMaterialCode(sourceSession.getMaterialCode());
|
||||
dto.setPrintNozzleDiameterMm(sourceSession.getNozzleDiameterMm());
|
||||
dto.setPrintLayerHeightMm(sourceSession.getLayerHeightMm());
|
||||
dto.setPrintInfillPattern(sourceSession.getInfillPattern());
|
||||
dto.setPrintInfillPercent(sourceSession.getInfillPercent());
|
||||
dto.setPrintSupportsEnabled(sourceSession.getSupportsEnabled());
|
||||
}
|
||||
|
||||
AddressDto billing = new AddressDto();
|
||||
billing.setFirstName(order.getBillingFirstName());
|
||||
billing.setLastName(order.getBillingLastName());
|
||||
billing.setCompanyName(order.getBillingCompanyName());
|
||||
billing.setContactPerson(order.getBillingContactPerson());
|
||||
billing.setAddressLine1(order.getBillingAddressLine1());
|
||||
billing.setAddressLine2(order.getBillingAddressLine2());
|
||||
billing.setZip(order.getBillingZip());
|
||||
billing.setCity(order.getBillingCity());
|
||||
billing.setCountryCode(order.getBillingCountryCode());
|
||||
dto.setBillingAddress(billing);
|
||||
|
||||
if (!Boolean.TRUE.equals(order.getShippingSameAsBilling())) {
|
||||
AddressDto shipping = new AddressDto();
|
||||
shipping.setFirstName(order.getShippingFirstName());
|
||||
shipping.setLastName(order.getShippingLastName());
|
||||
shipping.setCompanyName(order.getShippingCompanyName());
|
||||
shipping.setContactPerson(order.getShippingContactPerson());
|
||||
shipping.setAddressLine1(order.getShippingAddressLine1());
|
||||
shipping.setAddressLine2(order.getShippingAddressLine2());
|
||||
shipping.setZip(order.getShippingZip());
|
||||
shipping.setCity(order.getShippingCity());
|
||||
shipping.setCountryCode(order.getShippingCountryCode());
|
||||
dto.setShippingAddress(shipping);
|
||||
}
|
||||
|
||||
List<OrderItemDto> itemDtos = items.stream().map(item -> {
|
||||
OrderItemDto itemDto = new OrderItemDto();
|
||||
itemDto.setId(item.getId());
|
||||
itemDto.setOriginalFilename(item.getOriginalFilename());
|
||||
itemDto.setMaterialCode(item.getMaterialCode());
|
||||
itemDto.setColorCode(item.getColorCode());
|
||||
itemDto.setQuantity(item.getQuantity());
|
||||
itemDto.setPrintTimeSeconds(item.getPrintTimeSeconds());
|
||||
itemDto.setMaterialGrams(item.getMaterialGrams());
|
||||
itemDto.setUnitPriceChf(item.getUnitPriceChf());
|
||||
itemDto.setLineTotalChf(item.getLineTotalChf());
|
||||
return itemDto;
|
||||
}).toList();
|
||||
dto.setItems(itemDtos);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
return orderNumber;
|
||||
}
|
||||
return order.getId() != null ? order.getId().toString() : "unknown";
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> generateDocument(Order order, boolean isConfirmation) {
|
||||
String displayOrderNumber = getDisplayOrderNumber(order);
|
||||
if (isConfirmation) {
|
||||
Path relativePath = buildConfirmationPdfRelativePath(order.getId(), displayOrderNumber);
|
||||
try {
|
||||
byte[] existingPdf = storageService.loadAsResource(relativePath).getInputStream().readAllBytes();
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"confirmation-" + displayOrderNumber + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(existingPdf);
|
||||
} catch (Exception ignored) {
|
||||
// fallback to generated confirmation document
|
||||
}
|
||||
}
|
||||
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
Payment payment = paymentRepo.findByOrder_Id(order.getId()).orElse(null);
|
||||
byte[] pdf = invoiceService.generateDocumentPdf(order, items, isConfirmation, qrBillService, payment);
|
||||
|
||||
String prefix = isConfirmation ? "confirmation-" : "invoice-";
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + prefix + displayOrderNumber + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(pdf);
|
||||
}
|
||||
|
||||
private Path resolveOrderItemRelativePath(String storedRelativePath, UUID orderId, UUID orderItemId) {
|
||||
try {
|
||||
Path candidate = Path.of(storedRelativePath).normalize();
|
||||
if (candidate.isAbsolute()) {
|
||||
return null;
|
||||
}
|
||||
Path expectedPrefix = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString());
|
||||
if (!candidate.startsWith(expectedPrefix)) {
|
||||
return null;
|
||||
}
|
||||
return candidate;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Resource loadOrderItemResourceWithRecovery(OrderItem item, Path safeRelativePath) {
|
||||
try {
|
||||
return storageService.loadAsResource(safeRelativePath);
|
||||
} catch (Exception primaryFailure) {
|
||||
Path sourceQuotePath = resolveFallbackQuoteItemPath(item);
|
||||
if (sourceQuotePath == null) {
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
try {
|
||||
storageService.store(sourceQuotePath, safeRelativePath);
|
||||
return storageService.loadAsResource(safeRelativePath);
|
||||
} catch (Exception copyFailure) {
|
||||
try {
|
||||
Resource quoteResource = new UrlResource(sourceQuotePath.toUri());
|
||||
if (quoteResource.exists() || quoteResource.isReadable()) {
|
||||
return quoteResource;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// fall through to 404
|
||||
}
|
||||
throw new ResponseStatusException(NOT_FOUND, "File not available");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolveFallbackQuoteItemPath(OrderItem orderItem) {
|
||||
Order order = orderItem.getOrder();
|
||||
QuoteSession sourceSession = order != null ? order.getSourceQuoteSession() : null;
|
||||
UUID sourceSessionId = sourceSession != null ? sourceSession.getId() : null;
|
||||
if (sourceSessionId == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String targetFilename = normalizeFilename(orderItem.getOriginalFilename());
|
||||
if (targetFilename == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return quoteLineItemRepo.findByQuoteSessionId(sourceSessionId).stream()
|
||||
.filter(quoteItem -> targetFilename.equals(normalizeFilename(quoteItem.getOriginalFilename())))
|
||||
.sorted(Comparator.comparingInt((QuoteLineItem quoteItem) -> scoreQuoteMatch(orderItem, quoteItem)).reversed())
|
||||
.map(quoteItem -> resolveStoredQuotePath(quoteItem.getStoredPath(), sourceSessionId))
|
||||
.filter(path -> path != null && Files.exists(path))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private int scoreQuoteMatch(OrderItem orderItem, QuoteLineItem quoteItem) {
|
||||
int score = 0;
|
||||
if (orderItem.getQuantity() != null && orderItem.getQuantity().equals(quoteItem.getQuantity())) {
|
||||
score += 4;
|
||||
}
|
||||
if (orderItem.getPrintTimeSeconds() != null && orderItem.getPrintTimeSeconds().equals(quoteItem.getPrintTimeSeconds())) {
|
||||
score += 3;
|
||||
}
|
||||
if (orderItem.getMaterialCode() != null
|
||||
&& quoteItem.getMaterialCode() != null
|
||||
&& orderItem.getMaterialCode().equalsIgnoreCase(quoteItem.getMaterialCode())) {
|
||||
score += 3;
|
||||
}
|
||||
if (orderItem.getMaterialGrams() != null
|
||||
&& quoteItem.getMaterialGrams() != null
|
||||
&& orderItem.getMaterialGrams().compareTo(quoteItem.getMaterialGrams()) == 0) {
|
||||
score += 2;
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
private String normalizeFilename(String filename) {
|
||||
if (filename == null || filename.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return filename.trim();
|
||||
}
|
||||
|
||||
private Path resolveStoredQuotePath(String storedPath, UUID expectedSessionId) {
|
||||
if (storedPath == null || storedPath.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Path raw = Path.of(storedPath).normalize();
|
||||
Path resolved = raw.isAbsolute() ? raw : QUOTE_STORAGE_ROOT.resolve(raw).normalize();
|
||||
Path expectedSessionRoot = QUOTE_STORAGE_ROOT.resolve(expectedSessionId.toString()).normalize();
|
||||
if (!resolved.startsWith(expectedSessionRoot)) {
|
||||
return null;
|
||||
}
|
||||
return resolved;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Path buildConfirmationPdfRelativePath(UUID orderId, String orderNumber) {
|
||||
return Path.of("orders", orderId.toString(), "documents", "confirmation-" + orderNumber + ".pdf");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
package com.printcalculator.service.order;
|
||||
|
||||
import com.printcalculator.dto.AddressDto;
|
||||
import com.printcalculator.dto.CreateOrderRequest;
|
||||
import com.printcalculator.dto.OrderDto;
|
||||
import com.printcalculator.dto.OrderItemDto;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.entity.OrderItem;
|
||||
import com.printcalculator.entity.Payment;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.PaymentRepository;
|
||||
import com.printcalculator.service.OrderService;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.PaymentService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.payment.TwintPaymentService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
public class OrderControllerService {
|
||||
private static final Pattern SAFE_EXTENSION_PATTERN = Pattern.compile("^[a-z0-9]{1,10}$");
|
||||
private static final Set<String> PERSONAL_DATA_REDACTED_STATUSES = Set.of(
|
||||
"IN_PRODUCTION",
|
||||
"SHIPPED",
|
||||
"COMPLETED"
|
||||
);
|
||||
|
||||
private final OrderService orderService;
|
||||
private final OrderRepository orderRepo;
|
||||
private final OrderItemRepository orderItemRepo;
|
||||
private final StorageService storageService;
|
||||
private final InvoicePdfRenderingService invoiceService;
|
||||
private final QrBillService qrBillService;
|
||||
private final TwintPaymentService twintPaymentService;
|
||||
private final PaymentService paymentService;
|
||||
private final PaymentRepository paymentRepo;
|
||||
|
||||
public OrderControllerService(OrderService orderService,
|
||||
OrderRepository orderRepo,
|
||||
OrderItemRepository orderItemRepo,
|
||||
StorageService storageService,
|
||||
InvoicePdfRenderingService invoiceService,
|
||||
QrBillService qrBillService,
|
||||
TwintPaymentService twintPaymentService,
|
||||
PaymentService paymentService,
|
||||
PaymentRepository paymentRepo) {
|
||||
this.orderService = orderService;
|
||||
this.orderRepo = orderRepo;
|
||||
this.orderItemRepo = orderItemRepo;
|
||||
this.storageService = storageService;
|
||||
this.invoiceService = invoiceService;
|
||||
this.qrBillService = qrBillService;
|
||||
this.twintPaymentService = twintPaymentService;
|
||||
this.paymentService = paymentService;
|
||||
this.paymentRepo = paymentRepo;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrderDto createOrderFromQuote(UUID quoteSessionId, CreateOrderRequest request) {
|
||||
Order order = orderService.createOrderFromQuote(quoteSessionId, request);
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
return convertToDto(order, items);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean uploadOrderItemFile(UUID orderId, UUID orderItemId, MultipartFile file) throws IOException {
|
||||
OrderItem item = orderItemRepo.findById(orderItemId)
|
||||
.orElseThrow(() -> new RuntimeException("OrderItem not found"));
|
||||
|
||||
if (!item.getOrder().getId().equals(orderId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String relativePath = item.getStoredRelativePath();
|
||||
Path destinationRelativePath;
|
||||
if (relativePath == null || relativePath.equals("PENDING")) {
|
||||
String ext = getExtension(file.getOriginalFilename());
|
||||
String storedFilename = UUID.randomUUID() + "." + ext;
|
||||
destinationRelativePath = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString(), storedFilename);
|
||||
item.setStoredRelativePath(destinationRelativePath.toString());
|
||||
item.setStoredFilename(storedFilename);
|
||||
} else {
|
||||
destinationRelativePath = resolveOrderItemRelativePath(relativePath, orderId, orderItemId);
|
||||
if (destinationRelativePath == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
storageService.store(file, destinationRelativePath);
|
||||
item.setFileSizeBytes(file.getSize());
|
||||
item.setMimeType(file.getContentType());
|
||||
orderItemRepo.save(item);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Optional<OrderDto> getOrder(UUID orderId) {
|
||||
return orderRepo.findById(orderId)
|
||||
.map(order -> {
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(order.getId());
|
||||
return convertToDto(order, items);
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Optional<OrderDto> reportPayment(UUID orderId, String method) {
|
||||
paymentService.reportPayment(orderId, method);
|
||||
return getOrder(orderId);
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getConfirmation(UUID orderId) {
|
||||
return generateDocument(orderId, true);
|
||||
}
|
||||
|
||||
public ResponseEntity<Map<String, String>> getTwintPayment(UUID orderId) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
byte[] qrPng = twintPaymentService.generateQrPng(order, 360);
|
||||
String qrDataUri = "data:image/png;base64," + Base64.getEncoder().encodeToString(qrPng);
|
||||
|
||||
Map<String, String> data = new HashMap<>();
|
||||
data.put("paymentUrl", twintPaymentService.getTwintPaymentUrl(order));
|
||||
data.put("openUrl", "/api/orders/" + orderId + "/twint/open");
|
||||
data.put("qrImageUrl", "/api/orders/" + orderId + "/twint/qr");
|
||||
data.put("qrImageDataUri", qrDataUri);
|
||||
return ResponseEntity.ok(data);
|
||||
}
|
||||
|
||||
public ResponseEntity<Void> openTwintPayment(UUID orderId) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.status(302)
|
||||
.location(URI.create(twintPaymentService.getTwintPaymentUrl(order)))
|
||||
.build();
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getTwintQr(UUID orderId, int size) {
|
||||
Order order = orderRepo.findById(orderId).orElse(null);
|
||||
if (order == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
int normalizedSize = Math.max(200, Math.min(size, 600));
|
||||
byte[] png = twintPaymentService.generateQrPng(order, normalizedSize);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_PNG)
|
||||
.body(png);
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> generateDocument(UUID orderId, boolean isConfirmation) {
|
||||
Order order = orderRepo.findById(orderId)
|
||||
.orElseThrow(() -> new RuntimeException("Order not found"));
|
||||
|
||||
if (isConfirmation) {
|
||||
Path relativePath = buildConfirmationPdfRelativePath(order);
|
||||
try {
|
||||
byte[] existingPdf = storageService.loadAsResource(relativePath).getInputStream().readAllBytes();
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Disposition", "attachment; filename=\"confirmation-" + getDisplayOrderNumber(order) + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(existingPdf);
|
||||
} catch (Exception ignored) {
|
||||
// Fallback to on-the-fly generation if the stored file is missing or unreadable.
|
||||
}
|
||||
}
|
||||
|
||||
List<OrderItem> items = orderItemRepo.findByOrder_Id(orderId);
|
||||
Payment payment = paymentRepo.findByOrder_Id(orderId).orElse(null);
|
||||
|
||||
byte[] pdf = invoiceService.generateDocumentPdf(order, items, isConfirmation, qrBillService, payment);
|
||||
String typePrefix = isConfirmation ? "confirmation-" : "invoice-";
|
||||
String truncatedUuid = order.getId().toString().substring(0, 8);
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Disposition", "attachment; filename=\"" + typePrefix + truncatedUuid + ".pdf\"")
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(pdf);
|
||||
}
|
||||
|
||||
private Path buildConfirmationPdfRelativePath(Order order) {
|
||||
return Path.of(
|
||||
"orders",
|
||||
order.getId().toString(),
|
||||
"documents",
|
||||
"confirmation-" + getDisplayOrderNumber(order) + ".pdf"
|
||||
);
|
||||
}
|
||||
|
||||
private String getExtension(String filename) {
|
||||
if (filename == null) {
|
||||
return "stl";
|
||||
}
|
||||
String cleaned = StringUtils.cleanPath(filename);
|
||||
if (cleaned.contains("..")) {
|
||||
return "stl";
|
||||
}
|
||||
int i = cleaned.lastIndexOf('.');
|
||||
if (i > 0 && i < cleaned.length() - 1) {
|
||||
String ext = cleaned.substring(i + 1).toLowerCase(Locale.ROOT);
|
||||
if (SAFE_EXTENSION_PATTERN.matcher(ext).matches()) {
|
||||
return ext;
|
||||
}
|
||||
}
|
||||
return "stl";
|
||||
}
|
||||
|
||||
private Path resolveOrderItemRelativePath(String storedRelativePath, UUID orderId, UUID orderItemId) {
|
||||
try {
|
||||
Path candidate = Path.of(storedRelativePath).normalize();
|
||||
if (candidate.isAbsolute()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Path expectedPrefix = Path.of("orders", orderId.toString(), "3d-files", orderItemId.toString());
|
||||
if (!candidate.startsWith(expectedPrefix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return candidate;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private OrderDto convertToDto(Order order, List<OrderItem> items) {
|
||||
OrderDto dto = new OrderDto();
|
||||
dto.setId(order.getId());
|
||||
dto.setOrderNumber(getDisplayOrderNumber(order));
|
||||
dto.setStatus(order.getStatus());
|
||||
|
||||
paymentRepo.findByOrder_Id(order.getId()).ifPresent(payment -> {
|
||||
dto.setPaymentStatus(payment.getStatus());
|
||||
dto.setPaymentMethod(payment.getMethod());
|
||||
});
|
||||
|
||||
boolean redactPersonalData = shouldRedactPersonalData(order.getStatus());
|
||||
if (!redactPersonalData) {
|
||||
dto.setCustomerEmail(order.getCustomerEmail());
|
||||
dto.setCustomerPhone(order.getCustomerPhone());
|
||||
dto.setBillingCustomerType(order.getBillingCustomerType());
|
||||
}
|
||||
dto.setPreferredLanguage(order.getPreferredLanguage());
|
||||
dto.setCurrency(order.getCurrency());
|
||||
dto.setSetupCostChf(order.getSetupCostChf());
|
||||
dto.setShippingCostChf(order.getShippingCostChf());
|
||||
dto.setDiscountChf(order.getDiscountChf());
|
||||
dto.setSubtotalChf(order.getSubtotalChf());
|
||||
dto.setIsCadOrder(order.getIsCadOrder());
|
||||
dto.setSourceRequestId(order.getSourceRequestId());
|
||||
dto.setCadHours(order.getCadHours());
|
||||
dto.setCadHourlyRateChf(order.getCadHourlyRateChf());
|
||||
dto.setCadTotalChf(order.getCadTotalChf());
|
||||
dto.setTotalChf(order.getTotalChf());
|
||||
dto.setCreatedAt(order.getCreatedAt());
|
||||
dto.setShippingSameAsBilling(order.getShippingSameAsBilling());
|
||||
|
||||
if (!redactPersonalData) {
|
||||
AddressDto billing = new AddressDto();
|
||||
billing.setFirstName(order.getBillingFirstName());
|
||||
billing.setLastName(order.getBillingLastName());
|
||||
billing.setCompanyName(order.getBillingCompanyName());
|
||||
billing.setContactPerson(order.getBillingContactPerson());
|
||||
billing.setAddressLine1(order.getBillingAddressLine1());
|
||||
billing.setAddressLine2(order.getBillingAddressLine2());
|
||||
billing.setZip(order.getBillingZip());
|
||||
billing.setCity(order.getBillingCity());
|
||||
billing.setCountryCode(order.getBillingCountryCode());
|
||||
dto.setBillingAddress(billing);
|
||||
|
||||
if (!Boolean.TRUE.equals(order.getShippingSameAsBilling())) {
|
||||
AddressDto shipping = new AddressDto();
|
||||
shipping.setFirstName(order.getShippingFirstName());
|
||||
shipping.setLastName(order.getShippingLastName());
|
||||
shipping.setCompanyName(order.getShippingCompanyName());
|
||||
shipping.setContactPerson(order.getShippingContactPerson());
|
||||
shipping.setAddressLine1(order.getShippingAddressLine1());
|
||||
shipping.setAddressLine2(order.getShippingAddressLine2());
|
||||
shipping.setZip(order.getShippingZip());
|
||||
shipping.setCity(order.getShippingCity());
|
||||
shipping.setCountryCode(order.getShippingCountryCode());
|
||||
dto.setShippingAddress(shipping);
|
||||
}
|
||||
}
|
||||
|
||||
List<OrderItemDto> itemDtos = items.stream().map(item -> {
|
||||
OrderItemDto itemDto = new OrderItemDto();
|
||||
itemDto.setId(item.getId());
|
||||
itemDto.setOriginalFilename(item.getOriginalFilename());
|
||||
itemDto.setMaterialCode(item.getMaterialCode());
|
||||
itemDto.setColorCode(item.getColorCode());
|
||||
itemDto.setQuality(item.getQuality());
|
||||
itemDto.setNozzleDiameterMm(item.getNozzleDiameterMm());
|
||||
itemDto.setLayerHeightMm(item.getLayerHeightMm());
|
||||
itemDto.setInfillPercent(item.getInfillPercent());
|
||||
itemDto.setInfillPattern(item.getInfillPattern());
|
||||
itemDto.setSupportsEnabled(item.getSupportsEnabled());
|
||||
itemDto.setQuantity(item.getQuantity());
|
||||
itemDto.setPrintTimeSeconds(item.getPrintTimeSeconds());
|
||||
itemDto.setMaterialGrams(item.getMaterialGrams());
|
||||
itemDto.setUnitPriceChf(item.getUnitPriceChf());
|
||||
itemDto.setLineTotalChf(item.getLineTotalChf());
|
||||
return itemDto;
|
||||
}).collect(Collectors.toList());
|
||||
dto.setItems(itemDtos);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private boolean shouldRedactPersonalData(String status) {
|
||||
if (status == null || status.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
return PERSONAL_DATA_REDACTED_STATUSES.contains(status.trim().toUpperCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
private String getDisplayOrderNumber(Order order) {
|
||||
String orderNumber = order.getOrderNumber();
|
||||
if (orderNumber != null && !orderNumber.isBlank()) {
|
||||
return orderNumber;
|
||||
}
|
||||
return order.getId() != null ? order.getId().toString() : "unknown";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.payment;
|
||||
|
||||
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
||||
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.payment;
|
||||
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.entity.Payment;
|
||||
@@ -65,7 +65,7 @@ public class PaymentService {
|
||||
payment.setReportedAt(OffsetDateTime.now());
|
||||
|
||||
// We intentionally do not update the payment method here based on user input,
|
||||
// because the user cannot reliably determine the actual method without an integration.
|
||||
// because the system cannot reliably determine the actual method without an integration.
|
||||
// It will be updated by the backoffice admin manually.
|
||||
|
||||
payment = paymentRepo.save(payment);
|
||||
@@ -98,4 +98,20 @@ public class PaymentService {
|
||||
|
||||
return payment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Payment updatePaymentMethod(UUID orderId, String method) {
|
||||
if (method == null || method.isBlank()) {
|
||||
throw new IllegalArgumentException("Payment method is required");
|
||||
}
|
||||
|
||||
Order order = orderRepo.findById(orderId)
|
||||
.orElseThrow(() -> new RuntimeException("Order not found with id " + orderId));
|
||||
|
||||
Payment payment = paymentRepo.findByOrder_Id(orderId)
|
||||
.orElseGet(() -> getOrCreatePaymentForOrder(order, "OTHER"));
|
||||
|
||||
payment.setMethod(method.trim().toUpperCase());
|
||||
return paymentRepo.save(payment);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.payment;
|
||||
|
||||
import com.printcalculator.entity.Order;
|
||||
import net.codecrete.qrbill.generator.Bill;
|
||||
import net.codecrete.qrbill.generator.GraphicsFormat;
|
||||
import net.codecrete.qrbill.generator.QRBill;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Service
|
||||
public class QrBillService {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.payment;
|
||||
|
||||
import io.nayuki.qrcodegen.QrCode;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -0,0 +1,251 @@
|
||||
package com.printcalculator.service.quote;
|
||||
|
||||
import com.printcalculator.dto.PrintSettingsDto;
|
||||
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.QuoteLineItemRepository;
|
||||
import com.printcalculator.repository.QuoteSessionRepository;
|
||||
import com.printcalculator.service.OrcaProfileResolver;
|
||||
import com.printcalculator.service.QuoteCalculator;
|
||||
import com.printcalculator.service.SlicerService;
|
||||
import com.printcalculator.service.storage.ClamAVService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@Service
|
||||
public class QuoteSessionItemService {
|
||||
private final QuoteLineItemRepository lineItemRepo;
|
||||
private final QuoteSessionRepository sessionRepo;
|
||||
private final SlicerService slicerService;
|
||||
private final QuoteCalculator quoteCalculator;
|
||||
private final OrcaProfileResolver orcaProfileResolver;
|
||||
private final ClamAVService clamAVService;
|
||||
private final QuoteStorageService quoteStorageService;
|
||||
private final QuoteSessionSettingsService settingsService;
|
||||
|
||||
public QuoteSessionItemService(QuoteLineItemRepository lineItemRepo,
|
||||
QuoteSessionRepository sessionRepo,
|
||||
SlicerService slicerService,
|
||||
QuoteCalculator quoteCalculator,
|
||||
OrcaProfileResolver orcaProfileResolver,
|
||||
ClamAVService clamAVService,
|
||||
QuoteStorageService quoteStorageService,
|
||||
QuoteSessionSettingsService settingsService) {
|
||||
this.lineItemRepo = lineItemRepo;
|
||||
this.sessionRepo = sessionRepo;
|
||||
this.slicerService = slicerService;
|
||||
this.quoteCalculator = quoteCalculator;
|
||||
this.orcaProfileResolver = orcaProfileResolver;
|
||||
this.clamAVService = clamAVService;
|
||||
this.quoteStorageService = quoteStorageService;
|
||||
this.settingsService = settingsService;
|
||||
}
|
||||
|
||||
public QuoteLineItem addItemToSession(QuoteSession session, MultipartFile file, PrintSettingsDto settings) throws IOException {
|
||||
if (file.isEmpty()) {
|
||||
throw new IllegalArgumentException("File is empty");
|
||||
}
|
||||
if ("CONVERTED".equals(session.getStatus())) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot modify a converted session");
|
||||
}
|
||||
|
||||
clamAVService.scan(file.getInputStream());
|
||||
|
||||
Path sessionStorageDir = quoteStorageService.sessionStorageDir(session.getId());
|
||||
String ext = quoteStorageService.getSafeExtension(file.getOriginalFilename(), "stl");
|
||||
String storedFilename = UUID.randomUUID() + "." + ext;
|
||||
Path persistentPath = quoteStorageService.resolveSessionPath(sessionStorageDir, storedFilename);
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
Files.copy(inputStream, persistentPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
Path convertedPersistentPath = null;
|
||||
try {
|
||||
boolean cadSession = "CAD_ACTIVE".equals(session.getStatus());
|
||||
|
||||
if (cadSession) {
|
||||
settingsService.enforceCadPrintSettings(session, settings);
|
||||
} else {
|
||||
settingsService.applyPrintSettings(settings);
|
||||
}
|
||||
|
||||
QuoteSessionSettingsService.NozzleLayerSettings nozzleAndLayer = settingsService.resolveNozzleAndLayer(settings);
|
||||
BigDecimal nozzleDiameter = nozzleAndLayer.nozzleDiameter();
|
||||
BigDecimal layerHeight = nozzleAndLayer.layerHeight();
|
||||
|
||||
PrinterMachine machine = settingsService.resolvePrinterMachine(settings.getPrinterMachineId());
|
||||
FilamentVariant selectedVariant = settingsService.resolveFilamentVariant(settings);
|
||||
|
||||
validateCadMaterialLock(session, cadSession, selectedVariant);
|
||||
|
||||
if (!cadSession) {
|
||||
session.setMaterialCode(selectedVariant.getFilamentMaterialType().getMaterialCode());
|
||||
session.setNozzleDiameterMm(nozzleDiameter);
|
||||
session.setLayerHeightMm(layerHeight);
|
||||
session.setInfillPattern(settings.getInfillPattern());
|
||||
session.setInfillPercent(settings.getInfillDensity() != null ? settings.getInfillDensity().intValue() : 20);
|
||||
session.setSupportsEnabled(settings.getSupportsEnabled() != null ? settings.getSupportsEnabled() : false);
|
||||
sessionRepo.save(session);
|
||||
}
|
||||
|
||||
OrcaProfileResolver.ResolvedProfiles profiles = orcaProfileResolver.resolve(machine, nozzleDiameter, selectedVariant);
|
||||
String processProfile = resolveProcessProfile(settings);
|
||||
|
||||
Map<String, String> processOverrides = new HashMap<>();
|
||||
processOverrides.put("layer_height", layerHeight.stripTrailingZeros().toPlainString());
|
||||
if (settings.getInfillDensity() != null) {
|
||||
processOverrides.put("sparse_infill_density", settings.getInfillDensity() + "%");
|
||||
}
|
||||
if (settings.getInfillPattern() != null) {
|
||||
processOverrides.put("sparse_infill_pattern", settings.getInfillPattern());
|
||||
}
|
||||
|
||||
Path slicerInputPath = persistentPath;
|
||||
if ("3mf".equals(ext)) {
|
||||
String convertedFilename = UUID.randomUUID() + "-converted.stl";
|
||||
convertedPersistentPath = quoteStorageService.resolveSessionPath(sessionStorageDir, convertedFilename);
|
||||
slicerService.convert3mfToPersistentStl(persistentPath.toFile(), convertedPersistentPath);
|
||||
slicerInputPath = convertedPersistentPath;
|
||||
}
|
||||
|
||||
PrintStats stats = slicerService.slice(
|
||||
slicerInputPath.toFile(),
|
||||
profiles.machineProfileName(),
|
||||
profiles.filamentProfileName(),
|
||||
processProfile,
|
||||
null,
|
||||
processOverrides
|
||||
);
|
||||
|
||||
Optional<ModelDimensions> modelDimensions = slicerService.inspectModelDimensions(slicerInputPath.toFile());
|
||||
QuoteResult result = quoteCalculator.calculate(stats, machine.getPrinterDisplayName(), selectedVariant);
|
||||
|
||||
QuoteLineItem item = buildLineItem(
|
||||
session,
|
||||
file.getOriginalFilename(),
|
||||
settings,
|
||||
selectedVariant,
|
||||
nozzleDiameter,
|
||||
layerHeight,
|
||||
stats,
|
||||
result,
|
||||
modelDimensions,
|
||||
persistentPath,
|
||||
convertedPersistentPath
|
||||
);
|
||||
|
||||
return lineItemRepo.save(item);
|
||||
} catch (Exception e) {
|
||||
Files.deleteIfExists(persistentPath);
|
||||
if (convertedPersistentPath != null) {
|
||||
Files.deleteIfExists(convertedPersistentPath);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void validateCadMaterialLock(QuoteSession session, boolean cadSession, FilamentVariant selectedVariant) {
|
||||
if (!cadSession
|
||||
|| session.getMaterialCode() == null
|
||||
|| selectedVariant.getFilamentMaterialType() == null
|
||||
|| selectedVariant.getFilamentMaterialType().getMaterialCode() == null) {
|
||||
return;
|
||||
}
|
||||
String lockedMaterial = settingsService.normalizeRequestedMaterialCode(session.getMaterialCode());
|
||||
String selectedMaterial = settingsService.normalizeRequestedMaterialCode(
|
||||
selectedVariant.getFilamentMaterialType().getMaterialCode()
|
||||
);
|
||||
if (!lockedMaterial.equals(selectedMaterial)) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Selected filament does not match locked CAD material");
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveProcessProfile(PrintSettingsDto settings) {
|
||||
if (settings.getLayerHeight() == null) {
|
||||
return "standard";
|
||||
}
|
||||
if (settings.getLayerHeight() >= 0.28) {
|
||||
return "draft";
|
||||
}
|
||||
if (settings.getLayerHeight() <= 0.12) {
|
||||
return "extra_fine";
|
||||
}
|
||||
return "standard";
|
||||
}
|
||||
|
||||
private QuoteLineItem buildLineItem(QuoteSession session,
|
||||
String originalFilename,
|
||||
PrintSettingsDto settings,
|
||||
FilamentVariant selectedVariant,
|
||||
BigDecimal nozzleDiameter,
|
||||
BigDecimal layerHeight,
|
||||
PrintStats stats,
|
||||
QuoteResult result,
|
||||
Optional<ModelDimensions> modelDimensions,
|
||||
Path persistentPath,
|
||||
Path convertedPersistentPath) {
|
||||
QuoteLineItem item = new QuoteLineItem();
|
||||
item.setQuoteSession(session);
|
||||
item.setOriginalFilename(originalFilename);
|
||||
item.setStoredPath(quoteStorageService.toStoredPath(persistentPath));
|
||||
item.setQuantity(1);
|
||||
item.setColorCode(selectedVariant.getColorName());
|
||||
item.setFilamentVariant(selectedVariant);
|
||||
item.setMaterialCode(selectedVariant.getFilamentMaterialType() != null
|
||||
? selectedVariant.getFilamentMaterialType().getMaterialCode()
|
||||
: settingsService.normalizeRequestedMaterialCode(settings.getMaterial()));
|
||||
item.setQuality(settingsService.resolveQuality(settings, layerHeight));
|
||||
item.setNozzleDiameterMm(nozzleDiameter);
|
||||
item.setLayerHeightMm(layerHeight);
|
||||
item.setInfillPercent(settings.getInfillDensity() != null ? settings.getInfillDensity().intValue() : 20);
|
||||
item.setInfillPattern(settings.getInfillPattern());
|
||||
item.setSupportsEnabled(settings.getSupportsEnabled() != null ? settings.getSupportsEnabled() : false);
|
||||
item.setStatus("READY");
|
||||
|
||||
item.setPrintTimeSeconds((int) stats.printTimeSeconds());
|
||||
item.setMaterialGrams(BigDecimal.valueOf(stats.filamentWeightGrams()));
|
||||
item.setUnitPriceChf(BigDecimal.valueOf(result.getTotalPrice()));
|
||||
|
||||
Map<String, Object> breakdown = new HashMap<>();
|
||||
breakdown.put("machine_cost", result.getTotalPrice());
|
||||
breakdown.put("setup_fee", 0);
|
||||
if (convertedPersistentPath != null) {
|
||||
breakdown.put("convertedStoredPath", quoteStorageService.toStoredPath(convertedPersistentPath));
|
||||
}
|
||||
item.setPricingBreakdown(breakdown);
|
||||
|
||||
item.setBoundingBoxXMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.xMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxX() != null ? BigDecimal.valueOf(settings.getBoundingBoxX()) : BigDecimal.ZERO));
|
||||
item.setBoundingBoxYMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.yMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxY() != null ? BigDecimal.valueOf(settings.getBoundingBoxY()) : BigDecimal.ZERO));
|
||||
item.setBoundingBoxZMm(modelDimensions
|
||||
.map(dim -> BigDecimal.valueOf(dim.zMm()))
|
||||
.orElseGet(() -> settings.getBoundingBoxZ() != null ? BigDecimal.valueOf(settings.getBoundingBoxZ()) : BigDecimal.ZERO));
|
||||
|
||||
item.setCreatedAt(OffsetDateTime.now());
|
||||
item.setUpdatedAt(OffsetDateTime.now());
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.printcalculator.service.quote;
|
||||
|
||||
import com.printcalculator.entity.QuoteLineItem;
|
||||
import com.printcalculator.entity.QuoteSession;
|
||||
import com.printcalculator.service.QuoteSessionTotalsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class QuoteSessionResponseAssembler {
|
||||
private final QuoteStorageService quoteStorageService;
|
||||
|
||||
public QuoteSessionResponseAssembler(QuoteStorageService quoteStorageService) {
|
||||
this.quoteStorageService = quoteStorageService;
|
||||
}
|
||||
|
||||
public Map<String, Object> assemble(QuoteSession session,
|
||||
List<QuoteLineItem> items,
|
||||
QuoteSessionTotalsService.QuoteSessionTotals totals) {
|
||||
List<Map<String, Object>> itemsDto = new ArrayList<>();
|
||||
for (QuoteLineItem item : items) {
|
||||
itemsDto.add(toItemDto(item, totals));
|
||||
}
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("session", session);
|
||||
response.put("items", itemsDto);
|
||||
response.put("printItemsTotalChf", totals.printItemsTotalChf());
|
||||
response.put("cadTotalChf", totals.cadTotalChf());
|
||||
response.put("itemsTotalChf", totals.itemsTotalChf());
|
||||
response.put("shippingCostChf", totals.shippingCostChf());
|
||||
response.put("globalMachineCostChf", totals.globalMachineCostChf());
|
||||
response.put("grandTotalChf", totals.grandTotalChf());
|
||||
return response;
|
||||
}
|
||||
|
||||
private Map<String, Object> toItemDto(QuoteLineItem item, QuoteSessionTotalsService.QuoteSessionTotals totals) {
|
||||
Map<String, Object> dto = new HashMap<>();
|
||||
dto.put("id", item.getId());
|
||||
dto.put("originalFilename", item.getOriginalFilename());
|
||||
dto.put("quantity", item.getQuantity());
|
||||
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("materialCode", item.getMaterialCode());
|
||||
dto.put("quality", item.getQuality());
|
||||
dto.put("nozzleDiameterMm", item.getNozzleDiameterMm());
|
||||
dto.put("layerHeightMm", item.getLayerHeightMm());
|
||||
dto.put("infillPercent", item.getInfillPercent());
|
||||
dto.put("infillPattern", item.getInfillPattern());
|
||||
dto.put("supportsEnabled", item.getSupportsEnabled());
|
||||
dto.put("status", item.getStatus());
|
||||
dto.put("convertedStoredPath", quoteStorageService.extractConvertedStoredPath(item));
|
||||
dto.put("unitPriceChf", resolveDistributedUnitPrice(item, totals));
|
||||
return dto;
|
||||
}
|
||||
|
||||
private BigDecimal resolveDistributedUnitPrice(QuoteLineItem item, QuoteSessionTotalsService.QuoteSessionTotals totals) {
|
||||
BigDecimal unitPrice = item.getUnitPriceChf() != null ? item.getUnitPriceChf() : BigDecimal.ZERO;
|
||||
int quantity = item.getQuantity() != null && item.getQuantity() > 0 ? item.getQuantity() : 1;
|
||||
if (totals.totalPrintSeconds().compareTo(BigDecimal.ZERO) > 0 && item.getPrintTimeSeconds() != null) {
|
||||
BigDecimal itemSeconds = BigDecimal.valueOf(item.getPrintTimeSeconds()).multiply(BigDecimal.valueOf(quantity));
|
||||
BigDecimal share = itemSeconds.divide(totals.totalPrintSeconds(), 8, RoundingMode.HALF_UP);
|
||||
BigDecimal itemMachineCost = totals.globalMachineCostChf().multiply(share);
|
||||
BigDecimal unitMachineCost = itemMachineCost.divide(BigDecimal.valueOf(quantity), 2, RoundingMode.HALF_UP);
|
||||
unitPrice = unitPrice.add(unitMachineCost);
|
||||
}
|
||||
return unitPrice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.printcalculator.service.quote;
|
||||
|
||||
import com.printcalculator.dto.PrintSettingsDto;
|
||||
import com.printcalculator.entity.FilamentMaterialType;
|
||||
import com.printcalculator.entity.FilamentVariant;
|
||||
import com.printcalculator.entity.PrinterMachine;
|
||||
import com.printcalculator.entity.QuoteSession;
|
||||
import com.printcalculator.repository.FilamentMaterialTypeRepository;
|
||||
import com.printcalculator.repository.FilamentVariantRepository;
|
||||
import com.printcalculator.repository.PrinterMachineRepository;
|
||||
import com.printcalculator.service.NozzleLayerHeightPolicyService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class QuoteSessionSettingsService {
|
||||
private final PrinterMachineRepository machineRepo;
|
||||
private final FilamentMaterialTypeRepository materialRepo;
|
||||
private final FilamentVariantRepository variantRepo;
|
||||
private final NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService;
|
||||
|
||||
public QuoteSessionSettingsService(PrinterMachineRepository machineRepo,
|
||||
FilamentMaterialTypeRepository materialRepo,
|
||||
FilamentVariantRepository variantRepo,
|
||||
NozzleLayerHeightPolicyService nozzleLayerHeightPolicyService) {
|
||||
this.machineRepo = machineRepo;
|
||||
this.materialRepo = materialRepo;
|
||||
this.variantRepo = variantRepo;
|
||||
this.nozzleLayerHeightPolicyService = nozzleLayerHeightPolicyService;
|
||||
}
|
||||
|
||||
public void applyPrintSettings(PrintSettingsDto settings) {
|
||||
if (settings.getNozzleDiameter() == null) {
|
||||
settings.setNozzleDiameter(0.40);
|
||||
}
|
||||
|
||||
if ("BASIC".equalsIgnoreCase(settings.getComplexityMode())) {
|
||||
String quality = settings.getQuality() != null ? settings.getQuality().toLowerCase() : "standard";
|
||||
|
||||
switch (quality) {
|
||||
case "draft" -> {
|
||||
settings.setLayerHeight(0.28);
|
||||
settings.setInfillDensity(15.0);
|
||||
settings.setInfillPattern("grid");
|
||||
}
|
||||
case "extra_fine", "high_definition", "high" -> {
|
||||
settings.setLayerHeight(0.12);
|
||||
settings.setInfillDensity(20.0);
|
||||
settings.setInfillPattern("gyroid");
|
||||
}
|
||||
case "standard" -> {
|
||||
settings.setLayerHeight(0.20);
|
||||
settings.setInfillDensity(15.0);
|
||||
settings.setInfillPattern("grid");
|
||||
}
|
||||
default -> {
|
||||
settings.setLayerHeight(0.20);
|
||||
settings.setInfillDensity(15.0);
|
||||
settings.setInfillPattern("grid");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (settings.getInfillDensity() == null) {
|
||||
settings.setInfillDensity(20.0);
|
||||
}
|
||||
if (settings.getInfillPattern() == null) {
|
||||
settings.setInfillPattern("grid");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void enforceCadPrintSettings(QuoteSession session, PrintSettingsDto settings) {
|
||||
settings.setComplexityMode("ADVANCED");
|
||||
settings.setMaterial(session.getMaterialCode() != null ? session.getMaterialCode() : "PLA");
|
||||
settings.setNozzleDiameter(session.getNozzleDiameterMm() != null ? session.getNozzleDiameterMm().doubleValue() : 0.4);
|
||||
settings.setLayerHeight(session.getLayerHeightMm() != null ? session.getLayerHeightMm().doubleValue() : 0.2);
|
||||
settings.setInfillPattern(session.getInfillPattern() != null ? session.getInfillPattern() : "grid");
|
||||
settings.setInfillDensity(session.getInfillPercent() != null ? session.getInfillPercent().doubleValue() : 20.0);
|
||||
settings.setSupportsEnabled(Boolean.TRUE.equals(session.getSupportsEnabled()));
|
||||
}
|
||||
|
||||
public NozzleLayerSettings resolveNozzleAndLayer(PrintSettingsDto settings) {
|
||||
BigDecimal nozzleDiameter = nozzleLayerHeightPolicyService.resolveNozzle(
|
||||
settings.getNozzleDiameter() != null ? BigDecimal.valueOf(settings.getNozzleDiameter()) : null
|
||||
);
|
||||
BigDecimal layerHeight = nozzleLayerHeightPolicyService.resolveLayer(
|
||||
settings.getLayerHeight() != null ? BigDecimal.valueOf(settings.getLayerHeight()) : null,
|
||||
nozzleDiameter
|
||||
);
|
||||
if (!nozzleLayerHeightPolicyService.isAllowed(nozzleDiameter, layerHeight)) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Layer height " + layerHeight.stripTrailingZeros().toPlainString()
|
||||
+ " is not allowed for nozzle " + nozzleDiameter.stripTrailingZeros().toPlainString()
|
||||
+ ". Allowed: " + nozzleLayerHeightPolicyService.allowedLayersLabel(nozzleDiameter)
|
||||
);
|
||||
}
|
||||
settings.setNozzleDiameter(nozzleDiameter.doubleValue());
|
||||
settings.setLayerHeight(layerHeight.doubleValue());
|
||||
return new NozzleLayerSettings(nozzleDiameter, layerHeight);
|
||||
}
|
||||
|
||||
public 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"));
|
||||
}
|
||||
|
||||
public FilamentVariant resolveFilamentVariant(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 = normalizeRequestedMaterialCode(settings.getMaterial());
|
||||
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));
|
||||
}
|
||||
|
||||
public String normalizeRequestedMaterialCode(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return "PLA";
|
||||
}
|
||||
|
||||
return value.trim()
|
||||
.toUpperCase(Locale.ROOT)
|
||||
.replace('_', ' ')
|
||||
.replace('-', ' ')
|
||||
.replaceAll("\\s+", " ");
|
||||
}
|
||||
|
||||
public String resolveQuality(PrintSettingsDto settings, BigDecimal layerHeight) {
|
||||
if (settings.getQuality() != null && !settings.getQuality().isBlank()) {
|
||||
return settings.getQuality().trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
if (layerHeight == null) {
|
||||
return "standard";
|
||||
}
|
||||
if (layerHeight.compareTo(BigDecimal.valueOf(0.24)) >= 0) {
|
||||
return "draft";
|
||||
}
|
||||
if (layerHeight.compareTo(BigDecimal.valueOf(0.12)) <= 0) {
|
||||
return "extra_fine";
|
||||
}
|
||||
return "standard";
|
||||
}
|
||||
|
||||
public record NozzleLayerSettings(BigDecimal nozzleDiameter, BigDecimal layerHeight) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.printcalculator.service.quote;
|
||||
|
||||
import com.printcalculator.entity.QuoteLineItem;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class QuoteStorageService {
|
||||
private static final Path QUOTE_STORAGE_ROOT = Paths.get("storage_quotes").toAbsolutePath().normalize();
|
||||
|
||||
public Path sessionStorageDir(UUID sessionId) throws IOException {
|
||||
Path sessionStorageDir = QUOTE_STORAGE_ROOT.resolve(sessionId.toString()).normalize();
|
||||
if (!sessionStorageDir.startsWith(QUOTE_STORAGE_ROOT)) {
|
||||
throw new IOException("Invalid quote session storage path");
|
||||
}
|
||||
Files.createDirectories(sessionStorageDir);
|
||||
return sessionStorageDir;
|
||||
}
|
||||
|
||||
public Path resolveSessionPath(Path sessionStorageDir, String filename) throws IOException {
|
||||
Path resolved = sessionStorageDir.resolve(filename).normalize();
|
||||
if (!resolved.startsWith(sessionStorageDir)) {
|
||||
throw new IOException("Invalid quote line-item storage path");
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
public String toStoredPath(Path absolutePath) {
|
||||
return QUOTE_STORAGE_ROOT.relativize(absolutePath).toString();
|
||||
}
|
||||
|
||||
public String getSafeExtension(String filename, String fallback) {
|
||||
if (filename == null) {
|
||||
return fallback;
|
||||
}
|
||||
String cleaned = StringUtils.cleanPath(filename);
|
||||
if (cleaned.contains("..")) {
|
||||
return fallback;
|
||||
}
|
||||
int index = cleaned.lastIndexOf('.');
|
||||
if (index <= 0 || index >= cleaned.length() - 1) {
|
||||
return fallback;
|
||||
}
|
||||
String ext = cleaned.substring(index + 1).toLowerCase(Locale.ROOT);
|
||||
return switch (ext) {
|
||||
case "stl" -> "stl";
|
||||
case "3mf" -> "3mf";
|
||||
case "step", "stp" -> "step";
|
||||
default -> fallback;
|
||||
};
|
||||
}
|
||||
|
||||
public Path resolveStoredQuotePath(String storedPath, UUID expectedSessionId) {
|
||||
if (storedPath == null || storedPath.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Path raw = Path.of(storedPath).normalize();
|
||||
Path resolved = raw.isAbsolute() ? raw : QUOTE_STORAGE_ROOT.resolve(raw).normalize();
|
||||
Path expectedSessionRoot = QUOTE_STORAGE_ROOT.resolve(expectedSessionId.toString()).normalize();
|
||||
if (!resolved.startsWith(expectedSessionRoot)) {
|
||||
return null;
|
||||
}
|
||||
return resolved;
|
||||
} catch (InvalidPathException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String extractConvertedStoredPath(QuoteLineItem item) {
|
||||
Map<String, Object> breakdown = item.getPricingBreakdown();
|
||||
if (breakdown == null) {
|
||||
return null;
|
||||
}
|
||||
Object converted = breakdown.get("convertedStoredPath");
|
||||
if (converted == null) {
|
||||
return null;
|
||||
}
|
||||
String path = String.valueOf(converted).trim();
|
||||
return path.isEmpty() ? null : path;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.storage;
|
||||
|
||||
import com.printcalculator.exception.VirusDetectedException;
|
||||
import org.slf4j.Logger;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.storage;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -7,7 +7,6 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.printcalculator.exception.StorageException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.printcalculator.service;
|
||||
package com.printcalculator.service.storage;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -27,7 +27,7 @@ clamav.port=${CLAMAV_PORT:3310}
|
||||
clamav.enabled=${CLAMAV_ENABLED:false}
|
||||
|
||||
# TWINT Configuration
|
||||
payment.twint.url=${TWINT_PAYMENT_URL:}
|
||||
payment.twint.url=${TWINT_PAYMENT_URL:https://go.twint.ch/1/e/tw?tw=acq.gERQQytOTnyIMuQHUqn4hlxgciHE5X7nnqHnNSPAr2OF2K3uBlXJDr2n9JU3sgxa.}
|
||||
|
||||
# Mail Configuration
|
||||
spring.mail.host=${MAIL_HOST:mail.infomaniak.com}
|
||||
@@ -43,7 +43,7 @@ app.mail.from=${APP_MAIL_FROM:${MAIL_USERNAME:noreply@printcalculator.local}}
|
||||
app.mail.admin.enabled=${APP_MAIL_ADMIN_ENABLED:true}
|
||||
app.mail.admin.address=${APP_MAIL_ADMIN_ADDRESS:admin@printcalculator.local}
|
||||
app.mail.contact-request.admin.enabled=${APP_MAIL_CONTACT_REQUEST_ADMIN_ENABLED:true}
|
||||
app.mail.contact-request.admin.address=${APP_MAIL_CONTACT_REQUEST_ADMIN_ADDRESS:infog@3d-fab.ch}
|
||||
app.mail.contact-request.admin.address=${APP_MAIL_CONTACT_REQUEST_ADMIN_ADDRESS:info@3d-fab.ch}
|
||||
app.mail.contact-request.customer.enabled=${APP_MAIL_CONTACT_REQUEST_CUSTOMER_ENABLED:true}
|
||||
app.frontend.base-url=${APP_FRONTEND_BASE_URL:http://localhost:4200}
|
||||
|
||||
|
||||
110
backend/src/main/resources/templates/email/order-shipped.html
Normal file
110
backend/src/main/resources/templates/email/order-shipped.html
Normal file
@@ -0,0 +1,110 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title th:text="${emailTitle}">Order Shipped</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f4f4f4;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 20px auto;
|
||||
background-color: #ffffff;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
border-bottom: 1px solid #eeeeee;
|
||||
padding-bottom: 20px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.content {
|
||||
color: #555555;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.status-box {
|
||||
background-color: #e9f3ff;
|
||||
border: 1px solid #a9c8ef;
|
||||
border-radius: 5px;
|
||||
padding: 12px;
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
.order-box {
|
||||
background-color: #f9f9f9;
|
||||
border-radius: 5px;
|
||||
padding: 12px;
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
.order-box th {
|
||||
text-align: left;
|
||||
padding-right: 18px;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
font-size: 0.9em;
|
||||
color: #999999;
|
||||
margin-top: 30px;
|
||||
border-top: 1px solid #eeeeee;
|
||||
padding-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1 th:text="${headlineText}">Your order #00000000 has been shipped</h1>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<p th:text="${greetingText}">Hi Customer,</p>
|
||||
<p th:text="${introText}">Good news: your package is on its way.</p>
|
||||
|
||||
<div class="status-box">
|
||||
<strong th:text="${statusText}">Current status: Shipped.</strong>
|
||||
</div>
|
||||
|
||||
<div class="order-box">
|
||||
<table>
|
||||
<tr>
|
||||
<th th:text="${labelOrderNumber}">Order number</th>
|
||||
<td th:text="${orderNumber}">00000000</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th th:text="${labelTotal}">Total</th>
|
||||
<td th:text="${totalCost}">CHF 0.00</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<span th:text="${orderDetailsCtaText}">View order status</span>:
|
||||
<a th:href="${orderDetailsUrl}" th:text="${orderDetailsUrl}">https://example.com/en/co/00000000-0000-0000-0000-000000000000</a>
|
||||
</p>
|
||||
<p th:text="${supportText}">If you need assistance, reply to this email.</p>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<p>© <span th:text="${currentYear}">2026</span> 3D-Fab</p>
|
||||
<p th:text="${footerText}">Automated message.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.printcalculator.controller;
|
||||
|
||||
import com.printcalculator.dto.OrderDto;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.PaymentRepository;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.OrderService;
|
||||
import com.printcalculator.service.order.OrderControllerService;
|
||||
import com.printcalculator.service.payment.PaymentService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import com.printcalculator.service.payment.TwintPaymentService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class OrderControllerPrivacyTest {
|
||||
|
||||
@Mock
|
||||
private OrderService orderService;
|
||||
@Mock
|
||||
private OrderRepository orderRepo;
|
||||
@Mock
|
||||
private OrderItemRepository orderItemRepo;
|
||||
@Mock
|
||||
private StorageService storageService;
|
||||
@Mock
|
||||
private InvoicePdfRenderingService invoiceService;
|
||||
@Mock
|
||||
private QrBillService qrBillService;
|
||||
@Mock
|
||||
private TwintPaymentService twintPaymentService;
|
||||
@Mock
|
||||
private PaymentService paymentService;
|
||||
@Mock
|
||||
private PaymentRepository paymentRepo;
|
||||
|
||||
private OrderController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
OrderControllerService orderControllerService = new OrderControllerService(
|
||||
orderService,
|
||||
orderRepo,
|
||||
orderItemRepo,
|
||||
storageService,
|
||||
invoiceService,
|
||||
qrBillService,
|
||||
twintPaymentService,
|
||||
paymentService,
|
||||
paymentRepo
|
||||
);
|
||||
controller = new OrderController(orderControllerService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOrder_pendingPayment_keepsPersonalData() {
|
||||
UUID orderId = UUID.randomUUID();
|
||||
Order order = buildOrder(orderId, "PENDING_PAYMENT");
|
||||
|
||||
when(orderRepo.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderItemRepo.findByOrder_Id(orderId)).thenReturn(List.of());
|
||||
when(paymentRepo.findByOrder_Id(orderId)).thenReturn(Optional.empty());
|
||||
|
||||
ResponseEntity<OrderDto> response = controller.getOrder(orderId);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertEquals("customer@example.com", response.getBody().getCustomerEmail());
|
||||
assertEquals("+41790000000", response.getBody().getCustomerPhone());
|
||||
assertNotNull(response.getBody().getBillingAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOrder_advancedStatuses_redactsPersonalData() {
|
||||
List<String> statuses = List.of("IN_PRODUCTION", "SHIPPED", "COMPLETED");
|
||||
|
||||
for (String status : statuses) {
|
||||
UUID orderId = UUID.randomUUID();
|
||||
Order order = buildOrder(orderId, status);
|
||||
|
||||
when(orderRepo.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderItemRepo.findByOrder_Id(orderId)).thenReturn(List.of());
|
||||
when(paymentRepo.findByOrder_Id(orderId)).thenReturn(Optional.empty());
|
||||
|
||||
ResponseEntity<OrderDto> response = controller.getOrder(orderId);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertNull(response.getBody().getCustomerEmail());
|
||||
assertNull(response.getBody().getCustomerPhone());
|
||||
assertNull(response.getBody().getBillingCustomerType());
|
||||
assertNull(response.getBody().getBillingAddress());
|
||||
assertNull(response.getBody().getShippingAddress());
|
||||
}
|
||||
}
|
||||
|
||||
private Order buildOrder(UUID orderId, String status) {
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setStatus(status);
|
||||
order.setCustomerEmail("customer@example.com");
|
||||
order.setCustomerPhone("+41790000000");
|
||||
order.setBillingCustomerType("PRIVATE");
|
||||
order.setBillingFirstName("Joe");
|
||||
order.setBillingLastName("Kung");
|
||||
order.setBillingAddressLine1("Via G. Pioda 1");
|
||||
order.setBillingZip("6900");
|
||||
order.setBillingCity("Lugano");
|
||||
order.setBillingCountryCode("CH");
|
||||
order.setShippingSameAsBilling(true);
|
||||
return order;
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,18 @@ import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.repository.OrderRepository;
|
||||
import com.printcalculator.repository.PaymentRepository;
|
||||
import com.printcalculator.service.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.PaymentService;
|
||||
import com.printcalculator.service.QrBillService;
|
||||
import com.printcalculator.service.StorageService;
|
||||
import com.printcalculator.repository.QuoteLineItemRepository;
|
||||
import com.printcalculator.service.order.AdminOrderControllerService;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.PaymentService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
@@ -40,6 +43,8 @@ class AdminOrderControllerStatusValidationTest {
|
||||
@Mock
|
||||
private PaymentRepository paymentRepository;
|
||||
@Mock
|
||||
private QuoteLineItemRepository quoteLineItemRepository;
|
||||
@Mock
|
||||
private PaymentService paymentService;
|
||||
@Mock
|
||||
private StorageService storageService;
|
||||
@@ -47,20 +52,25 @@ class AdminOrderControllerStatusValidationTest {
|
||||
private InvoicePdfRenderingService invoicePdfRenderingService;
|
||||
@Mock
|
||||
private QrBillService qrBillService;
|
||||
@Mock
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
private AdminOrderController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
controller = new AdminOrderController(
|
||||
AdminOrderControllerService adminOrderControllerService = new AdminOrderControllerService(
|
||||
orderRepository,
|
||||
orderItemRepository,
|
||||
paymentRepository,
|
||||
quoteLineItemRepository,
|
||||
paymentService,
|
||||
storageService,
|
||||
invoicePdfRenderingService,
|
||||
qrBillService
|
||||
qrBillService,
|
||||
eventPublisher
|
||||
);
|
||||
controller = new AdminOrderController(adminOrderControllerService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,6 +102,7 @@ class AdminOrderControllerStatusValidationTest {
|
||||
order.setStatus("PENDING_PAYMENT");
|
||||
|
||||
when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));
|
||||
when(orderRepository.save(any(Order.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(orderItemRepository.findByOrder_Id(orderId)).thenReturn(List.of());
|
||||
when(paymentRepository.findByOrder_Id(orderId)).thenReturn(Optional.empty());
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import com.printcalculator.entity.Customer;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.event.OrderCreatedEvent;
|
||||
import com.printcalculator.repository.OrderItemRepository;
|
||||
import com.printcalculator.service.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.QrBillService;
|
||||
import com.printcalculator.service.StorageService;
|
||||
import com.printcalculator.service.payment.InvoicePdfRenderingService;
|
||||
import com.printcalculator.service.payment.QrBillService;
|
||||
import com.printcalculator.service.storage.StorageService;
|
||||
import com.printcalculator.service.email.EmailNotificationService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
24
db.sql
24
db.sql
@@ -660,6 +660,12 @@ CREATE TABLE IF NOT EXISTS quote_line_items
|
||||
quantity integer NOT NULL DEFAULT 1 CHECK (quantity >= 1),
|
||||
color_code text, -- es: white/black o codice interno
|
||||
filament_variant_id bigint REFERENCES filament_variant (filament_variant_id),
|
||||
material_code text,
|
||||
nozzle_diameter_mm numeric(5, 2),
|
||||
layer_height_mm numeric(6, 3),
|
||||
infill_pattern text,
|
||||
infill_percent integer CHECK (infill_percent BETWEEN 0 AND 100),
|
||||
supports_enabled boolean,
|
||||
|
||||
-- Output slicing / calcolo
|
||||
bounding_box_x_mm numeric(10, 3),
|
||||
@@ -680,6 +686,24 @@ CREATE TABLE IF NOT EXISTS quote_line_items
|
||||
CREATE INDEX IF NOT EXISTS ix_quote_line_items_session
|
||||
ON quote_line_items (quote_session_id);
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS material_code text;
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS nozzle_diameter_mm numeric(5, 2);
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS layer_height_mm numeric(6, 3);
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS infill_pattern text;
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS infill_percent integer;
|
||||
|
||||
ALTER TABLE quote_line_items
|
||||
ADD COLUMN IF NOT EXISTS supports_enabled boolean;
|
||||
|
||||
-- Vista utile per totale quote
|
||||
CREATE OR REPLACE VIEW quote_session_totals AS
|
||||
SELECT qs.quote_session_id,
|
||||
|
||||
@@ -13,6 +13,7 @@ services:
|
||||
- CLAMAV_HOST=${CLAMAV_HOST}
|
||||
- CLAMAV_PORT=${CLAMAV_PORT}
|
||||
- CLAMAV_ENABLED=${CLAMAV_ENABLED}
|
||||
- TWINT_PAYMENT_URL=${TWINT_PAYMENT_URL:-}
|
||||
- MAIL_HOST=${MAIL_HOST:-mail.infomaniak.com}
|
||||
- MAIL_PORT=${MAIL_PORT:-587}
|
||||
- MAIL_USERNAME=${MAIL_USERNAME:-info@3d-fab.ch}
|
||||
|
||||
21
frontend/public/robots.txt
Normal file
21
frontend/public/robots.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
Disallow: /admin
|
||||
Disallow: /admin/
|
||||
Disallow: /*/admin
|
||||
Disallow: /*/admin/
|
||||
Disallow: /order/
|
||||
Disallow: /*/order/
|
||||
Disallow: /co/
|
||||
Disallow: /*/co/
|
||||
Disallow: /checkout
|
||||
Disallow: /checkout/
|
||||
Disallow: /*/checkout
|
||||
Disallow: /*/checkout/
|
||||
Disallow: /shop
|
||||
Disallow: /shop/
|
||||
Disallow: /*/shop
|
||||
Disallow: /*/shop/
|
||||
|
||||
Sitemap: https://3d-fab.ch/sitemap.xml
|
||||
144
frontend/public/sitemap.xml
Normal file
144
frontend/public/sitemap.xml
Normal file
@@ -0,0 +1,144 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset
|
||||
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
xmlns:xhtml="http://www.w3.org/1999/xhtml"
|
||||
>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it</loc>
|
||||
<xhtml:link rel="alternate" hreflang="it" href="https://3d-fab.ch/it" />
|
||||
<xhtml:link rel="alternate" hreflang="en" href="https://3d-fab.ch/en" />
|
||||
<xhtml:link rel="alternate" hreflang="de" href="https://3d-fab.ch/de" />
|
||||
<xhtml:link rel="alternate" hreflang="fr" href="https://3d-fab.ch/fr" />
|
||||
<xhtml:link rel="alternate" hreflang="x-default" href="https://3d-fab.ch/it" />
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>1.0</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/calculator/basic</loc>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="it"
|
||||
href="https://3d-fab.ch/it/calculator/basic"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="en"
|
||||
href="https://3d-fab.ch/en/calculator/basic"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="de"
|
||||
href="https://3d-fab.ch/de/calculator/basic"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="fr"
|
||||
href="https://3d-fab.ch/fr/calculator/basic"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="x-default"
|
||||
href="https://3d-fab.ch/it/calculator/basic"
|
||||
/>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.9</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/calculator/advanced</loc>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="it"
|
||||
href="https://3d-fab.ch/it/calculator/advanced"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="en"
|
||||
href="https://3d-fab.ch/en/calculator/advanced"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="de"
|
||||
href="https://3d-fab.ch/de/calculator/advanced"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="fr"
|
||||
href="https://3d-fab.ch/fr/calculator/advanced"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="x-default"
|
||||
href="https://3d-fab.ch/it/calculator/advanced"
|
||||
/>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.8</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/about</loc>
|
||||
<xhtml:link rel="alternate" hreflang="it" href="https://3d-fab.ch/it/about" />
|
||||
<xhtml:link rel="alternate" hreflang="en" href="https://3d-fab.ch/en/about" />
|
||||
<xhtml:link rel="alternate" hreflang="de" href="https://3d-fab.ch/de/about" />
|
||||
<xhtml:link rel="alternate" hreflang="fr" href="https://3d-fab.ch/fr/about" />
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="x-default"
|
||||
href="https://3d-fab.ch/it/about"
|
||||
/>
|
||||
<changefreq>monthly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/contact</loc>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="it"
|
||||
href="https://3d-fab.ch/it/contact"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="en"
|
||||
href="https://3d-fab.ch/en/contact"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="de"
|
||||
href="https://3d-fab.ch/de/contact"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="fr"
|
||||
href="https://3d-fab.ch/fr/contact"
|
||||
/>
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="x-default"
|
||||
href="https://3d-fab.ch/it/contact"
|
||||
/>
|
||||
<changefreq>monthly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/privacy</loc>
|
||||
<xhtml:link rel="alternate" hreflang="it" href="https://3d-fab.ch/it/privacy" />
|
||||
<xhtml:link rel="alternate" hreflang="en" href="https://3d-fab.ch/en/privacy" />
|
||||
<xhtml:link rel="alternate" hreflang="de" href="https://3d-fab.ch/de/privacy" />
|
||||
<xhtml:link rel="alternate" hreflang="fr" href="https://3d-fab.ch/fr/privacy" />
|
||||
<xhtml:link
|
||||
rel="alternate"
|
||||
hreflang="x-default"
|
||||
href="https://3d-fab.ch/it/privacy"
|
||||
/>
|
||||
<changefreq>yearly</changefreq>
|
||||
<priority>0.4</priority>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://3d-fab.ch/it/terms</loc>
|
||||
<xhtml:link rel="alternate" hreflang="it" href="https://3d-fab.ch/it/terms" />
|
||||
<xhtml:link rel="alternate" hreflang="en" href="https://3d-fab.ch/en/terms" />
|
||||
<xhtml:link rel="alternate" hreflang="de" href="https://3d-fab.ch/de/terms" />
|
||||
<xhtml:link rel="alternate" hreflang="fr" href="https://3d-fab.ch/fr/terms" />
|
||||
<xhtml:link rel="alternate" hreflang="x-default" href="https://3d-fab.ch/it/terms" />
|
||||
<changefreq>yearly</changefreq>
|
||||
<priority>0.4</priority>
|
||||
</url>
|
||||
</urlset>
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { Component, inject } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import { SeoService } from './core/services/seo.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
@@ -8,4 +9,6 @@ import { RouterOutlet } from '@angular/router';
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.scss',
|
||||
})
|
||||
export class AppComponent {}
|
||||
export class AppComponent {
|
||||
private readonly seoService = inject(SeoService);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ const appChildRoutes: Routes = [
|
||||
path: '',
|
||||
loadComponent: () =>
|
||||
import('./features/home/home.component').then((m) => m.HomeComponent),
|
||||
data: {
|
||||
seoTitle: '3D fab | Stampa 3D su misura',
|
||||
seoDescription:
|
||||
'Servizio di stampa 3D con preventivo online immediato per prototipi, piccole serie e pezzi personalizzati.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'calculator',
|
||||
@@ -12,21 +17,42 @@ const appChildRoutes: Routes = [
|
||||
import('./features/calculator/calculator.routes').then(
|
||||
(m) => m.CALCULATOR_ROUTES,
|
||||
),
|
||||
data: {
|
||||
seoTitle: 'Calcolatore preventivo stampa 3D | 3D fab',
|
||||
seoDescription:
|
||||
'Carica il file 3D e ottieni prezzo e tempi in pochi secondi con slicing reale.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'shop',
|
||||
loadChildren: () =>
|
||||
import('./features/shop/shop.routes').then((m) => m.SHOP_ROUTES),
|
||||
data: {
|
||||
seoTitle: 'Shop 3D fab',
|
||||
seoDescription:
|
||||
'Catalogo prodotti stampati in 3D e soluzioni tecniche pronte all uso.',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'about',
|
||||
loadChildren: () =>
|
||||
import('./features/about/about.routes').then((m) => m.ABOUT_ROUTES),
|
||||
data: {
|
||||
seoTitle: 'Chi siamo | 3D fab',
|
||||
seoDescription:
|
||||
'Scopri il team 3D fab e il laboratorio di stampa 3D con sedi in Ticino e Bienne.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'contact',
|
||||
loadChildren: () =>
|
||||
import('./features/contact/contact.routes').then((m) => m.CONTACT_ROUTES),
|
||||
data: {
|
||||
seoTitle: 'Contatti | 3D fab',
|
||||
seoDescription:
|
||||
'Contatta 3D fab per preventivi, supporto tecnico e richieste personalizzate di stampa 3D.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'checkout/cad',
|
||||
@@ -34,6 +60,10 @@ const appChildRoutes: Routes = [
|
||||
import('./features/checkout/checkout.component').then(
|
||||
(m) => m.CheckoutComponent,
|
||||
),
|
||||
data: {
|
||||
seoTitle: 'Checkout | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'checkout',
|
||||
@@ -41,16 +71,28 @@ const appChildRoutes: Routes = [
|
||||
import('./features/checkout/checkout.component').then(
|
||||
(m) => m.CheckoutComponent,
|
||||
),
|
||||
data: {
|
||||
seoTitle: 'Checkout | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'order/:orderId',
|
||||
loadComponent: () =>
|
||||
import('./features/order/order.component').then((m) => m.OrderComponent),
|
||||
data: {
|
||||
seoTitle: 'Ordine | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'co/:orderId',
|
||||
loadComponent: () =>
|
||||
import('./features/order/order.component').then((m) => m.OrderComponent),
|
||||
data: {
|
||||
seoTitle: 'Ordine | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '',
|
||||
@@ -61,6 +103,10 @@ const appChildRoutes: Routes = [
|
||||
path: 'admin',
|
||||
loadChildren: () =>
|
||||
import('./features/admin/admin.routes').then((m) => m.ADMIN_ROUTES),
|
||||
data: {
|
||||
seoTitle: 'Admin | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '**',
|
||||
|
||||
129
frontend/src/app/core/services/seo.service.ts
Normal file
129
frontend/src/app/core/services/seo.service.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
import { Title, Meta } from '@angular/platform-browser';
|
||||
import { ActivatedRouteSnapshot, NavigationEnd, Router } from '@angular/router';
|
||||
import { filter } from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class SeoService {
|
||||
private readonly defaultTitle = '3D fab | Stampa 3D su misura';
|
||||
private readonly defaultDescription =
|
||||
'Stampa 3D su misura con preventivo online immediato. Carica il file, scegli materiale e qualità, ricevi prezzo e tempi in pochi secondi.';
|
||||
private readonly supportedLangs = new Set(['it', 'en', 'de', 'fr']);
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private titleService: Title,
|
||||
private metaService: Meta,
|
||||
@Inject(DOCUMENT) private document: Document,
|
||||
) {
|
||||
this.applyRouteSeo(this.router.routerState.snapshot.root);
|
||||
this.router.events
|
||||
.pipe(
|
||||
filter(
|
||||
(event): event is NavigationEnd => event instanceof NavigationEnd,
|
||||
),
|
||||
)
|
||||
.subscribe(() => {
|
||||
this.applyRouteSeo(this.router.routerState.snapshot.root);
|
||||
});
|
||||
}
|
||||
|
||||
private applyRouteSeo(rootSnapshot: ActivatedRouteSnapshot): void {
|
||||
const mergedData = this.getMergedRouteData(rootSnapshot);
|
||||
const title = this.asString(mergedData['seoTitle']) ?? this.defaultTitle;
|
||||
const description =
|
||||
this.asString(mergedData['seoDescription']) ?? this.defaultDescription;
|
||||
const robots = this.asString(mergedData['seoRobots']) ?? 'index, follow';
|
||||
|
||||
this.titleService.setTitle(title);
|
||||
this.metaService.updateTag({ name: 'description', content: description });
|
||||
this.metaService.updateTag({ name: 'robots', content: robots });
|
||||
this.metaService.updateTag({ property: 'og:title', content: title });
|
||||
this.metaService.updateTag({
|
||||
property: 'og:description',
|
||||
content: description,
|
||||
});
|
||||
this.metaService.updateTag({ property: 'og:type', content: 'website' });
|
||||
this.metaService.updateTag({ name: 'twitter:card', content: 'summary' });
|
||||
|
||||
const cleanPath = this.getCleanPath(this.router.url);
|
||||
const canonical = `${this.document.location.origin}${cleanPath}`;
|
||||
this.metaService.updateTag({ property: 'og:url', content: canonical });
|
||||
this.updateCanonicalTag(canonical);
|
||||
this.updateLangAndAlternates(cleanPath);
|
||||
}
|
||||
|
||||
private getMergedRouteData(
|
||||
snapshot: ActivatedRouteSnapshot,
|
||||
): Record<string, unknown> {
|
||||
const merged: Record<string, unknown> = {};
|
||||
let cursor: ActivatedRouteSnapshot | null = snapshot;
|
||||
while (cursor) {
|
||||
Object.assign(merged, cursor.data ?? {});
|
||||
cursor = cursor.firstChild;
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
private asString(value: unknown): string | undefined {
|
||||
return typeof value === 'string' ? value : undefined;
|
||||
}
|
||||
|
||||
private getCleanPath(url: string): string {
|
||||
const path = (url || '/').split('?')[0].split('#')[0];
|
||||
return path || '/';
|
||||
}
|
||||
|
||||
private updateCanonicalTag(url: string): void {
|
||||
let link = this.document.head.querySelector(
|
||||
'link[rel="canonical"]',
|
||||
) as HTMLLinkElement | null;
|
||||
if (!link) {
|
||||
link = this.document.createElement('link');
|
||||
link.setAttribute('rel', 'canonical');
|
||||
this.document.head.appendChild(link);
|
||||
}
|
||||
link.setAttribute('href', url);
|
||||
}
|
||||
|
||||
private updateLangAndAlternates(path: string): void {
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
const firstSegment = segments[0]?.toLowerCase();
|
||||
const hasLang = Boolean(
|
||||
firstSegment && this.supportedLangs.has(firstSegment),
|
||||
);
|
||||
const lang = hasLang ? firstSegment : 'it';
|
||||
const suffixSegments = hasLang ? segments.slice(1) : segments;
|
||||
const suffix =
|
||||
suffixSegments.length > 0 ? `/${suffixSegments.join('/')}` : '';
|
||||
|
||||
this.document.documentElement.lang = lang;
|
||||
|
||||
this.document.head
|
||||
.querySelectorAll('link[rel="alternate"][data-seo-managed="true"]')
|
||||
.forEach((node) => node.remove());
|
||||
|
||||
for (const alt of ['it', 'en', 'de', 'fr']) {
|
||||
this.appendAlternateLink(
|
||||
alt,
|
||||
`${this.document.location.origin}/${alt}${suffix}`,
|
||||
);
|
||||
}
|
||||
this.appendAlternateLink(
|
||||
'x-default',
|
||||
`${this.document.location.origin}/it${suffix}`,
|
||||
);
|
||||
}
|
||||
|
||||
private appendAlternateLink(hreflang: string, href: string): void {
|
||||
const link = this.document.createElement('link');
|
||||
link.setAttribute('rel', 'alternate');
|
||||
link.setAttribute('hreflang', hreflang);
|
||||
link.setAttribute('href', href);
|
||||
link.setAttribute('data-seo-managed', 'true');
|
||||
this.document.head.appendChild(link);
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,13 @@ import { Routes } from '@angular/router';
|
||||
import { AboutPageComponent } from './about-page.component';
|
||||
|
||||
export const ABOUT_ROUTES: Routes = [
|
||||
{ path: '', component: AboutPageComponent },
|
||||
{
|
||||
path: '',
|
||||
component: AboutPageComponent,
|
||||
data: {
|
||||
seoTitle: 'Chi siamo | 3D fab',
|
||||
seoDescription:
|
||||
'Siamo un laboratorio di stampa 3D orientato a prototipi, ricambi e produzioni su misura.',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -163,12 +163,12 @@
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
(click)="confirmPayment()"
|
||||
[disabled]="
|
||||
confirmingPayment || selectedOrder.paymentStatus === 'COMPLETED'
|
||||
"
|
||||
(click)="updatePaymentMethod()"
|
||||
[disabled]="confirmingPayment"
|
||||
>
|
||||
{{ confirmingPayment ? "Invio..." : "Conferma pagamento" }}
|
||||
{{
|
||||
confirmingPayment ? "Salvataggio..." : "Cambia metodo pagamento"
|
||||
}}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -192,13 +192,18 @@
|
||||
<strong>{{ item.originalFilename }}</strong>
|
||||
</p>
|
||||
<p class="item-meta">
|
||||
Qta: {{ item.quantity }} | Colore:
|
||||
Qta: {{ item.quantity }} | Materiale:
|
||||
{{ item.materialCode || "-" }} | Colore:
|
||||
<span
|
||||
class="color-swatch"
|
||||
*ngIf="isHexColor(item.colorCode)"
|
||||
[style.background-color]="item.colorCode"
|
||||
></span>
|
||||
<span>{{ item.colorCode || "-" }}</span>
|
||||
| Nozzle: {{ item.nozzleDiameterMm ?? "-" }} mm | Layer:
|
||||
{{ item.layerHeightMm ?? "-" }} mm | Infill:
|
||||
{{ item.infillPercent ?? "-" }}% | Supporti:
|
||||
{{ item.supportsEnabled ? "Sì" : "No" }}
|
||||
| Riga:
|
||||
{{ item.lineTotalChf | currency: "CHF" : "symbol" : "1.2-2" }}
|
||||
</p>
|
||||
@@ -273,17 +278,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h4>Colori file</h4>
|
||||
<h4>Parametri per file</h4>
|
||||
<div class="file-color-list">
|
||||
<div class="file-color-row" *ngFor="let item of selectedOrder.items">
|
||||
<span class="filename">{{ item.originalFilename }}</span>
|
||||
<span class="file-color">
|
||||
<span
|
||||
class="color-swatch"
|
||||
*ngIf="isHexColor(item.colorCode)"
|
||||
[style.background-color]="item.colorCode"
|
||||
></span>
|
||||
{{ item.colorCode || "-" }}
|
||||
{{ item.materialCode || "-" }} | {{ item.nozzleDiameterMm ?? "-" }} mm
|
||||
| {{ item.layerHeightMm ?? "-" }} mm | {{ item.infillPercent ?? "-" }}%
|
||||
| {{ item.infillPattern || "-" }} |
|
||||
{{ item.supportsEnabled ? "Supporti ON" : "Supporti OFF" }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -132,14 +132,14 @@ export class AdminDashboardComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
confirmPayment(): void {
|
||||
updatePaymentMethod(): void {
|
||||
if (!this.selectedOrder || this.confirmingPayment) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.confirmingPayment = true;
|
||||
this.adminOrdersService
|
||||
.confirmPayment(this.selectedOrder.id, this.selectedPaymentMethod)
|
||||
.updatePaymentMethod(this.selectedOrder.id, this.selectedPaymentMethod)
|
||||
.subscribe({
|
||||
next: (updatedOrder) => {
|
||||
this.confirmingPayment = false;
|
||||
@@ -147,7 +147,7 @@ export class AdminDashboardComponent implements OnInit {
|
||||
},
|
||||
error: () => {
|
||||
this.confirmingPayment = false;
|
||||
this.errorMessage = 'Conferma pagamento non riuscita.';
|
||||
this.errorMessage = 'Aggiornamento metodo pagamento non riuscito.';
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
<th>Qta</th>
|
||||
<th>Tempo</th>
|
||||
<th>Materiale</th>
|
||||
<th>Scelte utente</th>
|
||||
<th>Stato</th>
|
||||
<th>Prezzo unit.</th>
|
||||
</tr>
|
||||
@@ -142,6 +143,14 @@
|
||||
: "-"
|
||||
}}
|
||||
</td>
|
||||
<td>
|
||||
{{ item.materialCode || "-" }} |
|
||||
{{ item.nozzleDiameterMm ?? "-" }} mm |
|
||||
{{ item.layerHeightMm ?? "-" }} mm |
|
||||
{{ item.infillPercent ?? "-" }}% |
|
||||
{{ item.infillPattern || "-" }} |
|
||||
{{ item.supportsEnabled ? "Supporti ON" : "Supporti OFF" }}
|
||||
</td>
|
||||
<td>{{ item.status }}</td>
|
||||
<td>{{ item.unitPriceChf | currency: "CHF" }}</td>
|
||||
</tr>
|
||||
|
||||
@@ -127,7 +127,15 @@ export interface AdminQuoteSessionDetailItem {
|
||||
quantity: number;
|
||||
printTimeSeconds?: number;
|
||||
materialGrams?: number;
|
||||
materialCode?: string;
|
||||
quality?: string;
|
||||
nozzleDiameterMm?: number;
|
||||
layerHeightMm?: number;
|
||||
infillPercent?: number;
|
||||
infillPattern?: string;
|
||||
supportsEnabled?: boolean;
|
||||
colorCode?: string;
|
||||
filamentVariantId?: number;
|
||||
status: string;
|
||||
unitPriceChf: number;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ export interface AdminOrderItem {
|
||||
originalFilename: string;
|
||||
materialCode: string;
|
||||
colorCode: string;
|
||||
quality?: string;
|
||||
nozzleDiameterMm?: number;
|
||||
layerHeightMm?: number;
|
||||
infillPercent?: number;
|
||||
infillPattern?: string;
|
||||
supportsEnabled?: boolean;
|
||||
quantity: number;
|
||||
printTimeSeconds: number;
|
||||
materialGrams: number;
|
||||
@@ -59,7 +65,7 @@ export class AdminOrdersService {
|
||||
});
|
||||
}
|
||||
|
||||
confirmPayment(orderId: string, method: string): Observable<AdminOrder> {
|
||||
updatePaymentMethod(orderId: string, method: string): Observable<AdminOrder> {
|
||||
return this.http.post<AdminOrder>(
|
||||
`${this.baseUrl}/${orderId}/payments/confirm`,
|
||||
{ method },
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
class="mode-option"
|
||||
[class.active]="mode() === 'easy'"
|
||||
[class.disabled]="cadSessionLocked()"
|
||||
(click)="!cadSessionLocked() && mode.set('easy')"
|
||||
(click)="switchMode('easy')"
|
||||
>
|
||||
{{ "CALC.MODE_EASY" | translate }}
|
||||
</div>
|
||||
@@ -32,7 +32,7 @@
|
||||
class="mode-option"
|
||||
[class.active]="mode() === 'advanced'"
|
||||
[class.disabled]="cadSessionLocked()"
|
||||
(click)="!cadSessionLocked() && mode.set('advanced')"
|
||||
(click)="switchMode('advanced')"
|
||||
>
|
||||
{{ "CALC.MODE_ADVANCED" | translate }}
|
||||
</div>
|
||||
@@ -45,6 +45,9 @@
|
||||
[loading]="loading()"
|
||||
[uploadProgress]="uploadProgress()"
|
||||
(submitRequest)="onCalculate($event)"
|
||||
(itemQuantityChange)="onUploadItemQuantityChange($event)"
|
||||
(printSettingsChange)="onUploadPrintSettingsChange($event)"
|
||||
(itemSettingsDiffChange)="onItemSettingsDiffChange($event)"
|
||||
></app-upload-form>
|
||||
</app-card>
|
||||
</div>
|
||||
@@ -64,8 +67,11 @@
|
||||
} @else if (result()) {
|
||||
<app-quote-result
|
||||
[result]="result()!"
|
||||
[recalculationRequired]="requiresRecalculation()"
|
||||
[itemSettingsDiffByFileName]="itemSettingsDiffByFileName()"
|
||||
(consult)="onConsult()"
|
||||
(proceed)="onProceed()"
|
||||
(itemQuantityPreviewChange)="onQuoteItemQuantityPreviewChange($event)"
|
||||
(itemChange)="onItemChange($event)"
|
||||
></app-quote-result>
|
||||
} @else if (isZeroQuoteError()) {
|
||||
|
||||
@@ -25,6 +25,17 @@ import { SuccessStateComponent } from '../../shared/components/success-state/suc
|
||||
import { Router, ActivatedRoute } from '@angular/router';
|
||||
import { LanguageService } from '../../core/services/language.service';
|
||||
|
||||
type TrackedPrintSettings = {
|
||||
mode: 'easy' | 'advanced';
|
||||
material: string;
|
||||
quality: string;
|
||||
nozzleDiameter: number;
|
||||
layerHeight: number;
|
||||
infillDensity: number;
|
||||
infillPattern: string;
|
||||
supportEnabled: boolean;
|
||||
};
|
||||
|
||||
@Component({
|
||||
selector: 'app-calculator-page',
|
||||
standalone: true,
|
||||
@@ -42,7 +53,7 @@ import { LanguageService } from '../../core/services/language.service';
|
||||
styleUrl: './calculator-page.component.scss',
|
||||
})
|
||||
export class CalculatorPageComponent implements OnInit {
|
||||
mode = signal<any>('easy');
|
||||
mode = signal<'easy' | 'advanced'>('easy');
|
||||
step = signal<'upload' | 'quote' | 'details' | 'success'>('upload');
|
||||
|
||||
loading = signal(false);
|
||||
@@ -56,6 +67,12 @@ export class CalculatorPageComponent implements OnInit {
|
||||
);
|
||||
|
||||
orderSuccess = signal(false);
|
||||
requiresRecalculation = signal(false);
|
||||
itemSettingsDiffByFileName = signal<
|
||||
Record<string, { differences: string[] }>
|
||||
>({});
|
||||
private baselinePrintSettings: TrackedPrintSettings | null = null;
|
||||
private baselineItemSettingsByFileName = new Map<string, TrackedPrintSettings>();
|
||||
|
||||
@ViewChild('uploadForm') uploadForm!: UploadFormComponent;
|
||||
@ViewChild('resultCol') resultCol!: ElementRef;
|
||||
@@ -101,6 +118,15 @@ export class CalculatorPageComponent implements OnInit {
|
||||
this.error.set(false);
|
||||
this.errorKey.set('CALC.ERROR_GENERIC');
|
||||
this.result.set(result);
|
||||
this.baselinePrintSettings = this.toTrackedSettingsFromSession(
|
||||
data.session,
|
||||
);
|
||||
this.baselineItemSettingsByFileName = this.buildBaselineMapFromSession(
|
||||
data.items || [],
|
||||
this.baselinePrintSettings,
|
||||
);
|
||||
this.requiresRecalculation.set(false);
|
||||
this.itemSettingsDiffByFileName.set({});
|
||||
const isCadSession = data?.session?.status === 'CAD_ACTIVE';
|
||||
this.cadSessionLocked.set(isCadSession);
|
||||
this.step.set('quote');
|
||||
@@ -173,14 +199,21 @@ export class CalculatorPageComponent implements OnInit {
|
||||
});
|
||||
this.uploadForm.patchSettings(session);
|
||||
|
||||
// Also restore colors?
|
||||
// setFiles inits with 'Black'. We need to update them if they differ.
|
||||
// items has colorCode.
|
||||
setTimeout(() => {
|
||||
if (this.uploadForm) {
|
||||
items.forEach((item, index) => {
|
||||
// Assuming index matches.
|
||||
// Need to be careful if items order changed, but usually ID sort or insert order.
|
||||
const tracked = this.toTrackedSettingsFromSessionItem(
|
||||
item,
|
||||
this.toTrackedSettingsFromSession(session),
|
||||
);
|
||||
this.uploadForm.setItemPrintSettingsByIndex(index, {
|
||||
material: tracked.material.toUpperCase(),
|
||||
quality: tracked.quality,
|
||||
nozzleDiameter: tracked.nozzleDiameter,
|
||||
layerHeight: tracked.layerHeight,
|
||||
infillDensity: tracked.infillDensity,
|
||||
infillPattern: tracked.infillPattern,
|
||||
supportEnabled: tracked.supportEnabled,
|
||||
});
|
||||
|
||||
if (item.colorCode) {
|
||||
this.uploadForm.updateItemColor(index, {
|
||||
colorName: item.colorCode,
|
||||
@@ -188,8 +221,11 @@ export class CalculatorPageComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const selected = this.uploadForm.selectedFile();
|
||||
if (selected) {
|
||||
this.uploadForm.selectFile(selected);
|
||||
}
|
||||
});
|
||||
}
|
||||
this.loading.set(false);
|
||||
},
|
||||
@@ -238,6 +274,11 @@ export class CalculatorPageComponent implements OnInit {
|
||||
this.error.set(false);
|
||||
this.errorKey.set('CALC.ERROR_GENERIC');
|
||||
this.result.set(res);
|
||||
this.baselinePrintSettings = this.toTrackedSettingsFromRequest(req);
|
||||
this.baselineItemSettingsByFileName =
|
||||
this.buildBaselineMapFromRequest(req);
|
||||
this.requiresRecalculation.set(false);
|
||||
this.itemSettingsDiffByFileName.set({});
|
||||
this.loading.set(false);
|
||||
this.uploadProgress.set(100);
|
||||
this.step.set('quote');
|
||||
@@ -295,9 +336,10 @@ export class CalculatorPageComponent implements OnInit {
|
||||
index: number;
|
||||
fileName: string;
|
||||
quantity: number;
|
||||
source?: 'left' | 'right';
|
||||
}) {
|
||||
// 1. Update local form for consistency (UI feedback)
|
||||
if (this.uploadForm) {
|
||||
if (event.source !== 'left' && this.uploadForm) {
|
||||
this.uploadForm.updateItemQuantityByIndex(event.index, event.quantity);
|
||||
this.uploadForm.updateItemQuantityByName(event.fileName, event.quantity);
|
||||
}
|
||||
@@ -340,6 +382,33 @@ export class CalculatorPageComponent implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
onUploadItemQuantityChange(event: {
|
||||
index: number;
|
||||
fileName: string;
|
||||
quantity: number;
|
||||
}) {
|
||||
const resultItems = this.result()?.items || [];
|
||||
const byIndex = resultItems[event.index];
|
||||
const byName = resultItems.find((item) => item.fileName === event.fileName);
|
||||
const id = byIndex?.id ?? byName?.id;
|
||||
|
||||
this.onItemChange({
|
||||
...event,
|
||||
id,
|
||||
source: 'left',
|
||||
});
|
||||
}
|
||||
|
||||
onQuoteItemQuantityPreviewChange(event: {
|
||||
index: number;
|
||||
fileName: string;
|
||||
quantity: number;
|
||||
}) {
|
||||
if (!this.uploadForm) return;
|
||||
this.uploadForm.updateItemQuantityByIndex(event.index, event.quantity);
|
||||
this.uploadForm.updateItemQuantityByName(event.fileName, event.quantity);
|
||||
}
|
||||
|
||||
onSubmitOrder(orderData: any) {
|
||||
console.log('Order Submitted:', orderData);
|
||||
this.orderSuccess.set(true);
|
||||
@@ -349,15 +418,37 @@ export class CalculatorPageComponent implements OnInit {
|
||||
onNewQuote() {
|
||||
this.step.set('upload');
|
||||
this.result.set(null);
|
||||
this.requiresRecalculation.set(false);
|
||||
this.itemSettingsDiffByFileName.set({});
|
||||
this.baselinePrintSettings = null;
|
||||
this.baselineItemSettingsByFileName = new Map<
|
||||
string,
|
||||
TrackedPrintSettings
|
||||
>();
|
||||
this.cadSessionLocked.set(false);
|
||||
this.orderSuccess.set(false);
|
||||
this.mode.set('easy'); // Reset to default
|
||||
this.switchMode('easy'); // Reset to default and sync URL
|
||||
}
|
||||
|
||||
private currentRequest: QuoteRequest | null = null;
|
||||
|
||||
onUploadPrintSettingsChange(_: TrackedPrintSettings) {
|
||||
void _;
|
||||
if (!this.result()) return;
|
||||
this.refreshRecalculationRequirement();
|
||||
}
|
||||
|
||||
onItemSettingsDiffChange(
|
||||
diffByFileName: Record<string, { differences: string[] }>,
|
||||
) {
|
||||
this.itemSettingsDiffByFileName.set(diffByFileName || {});
|
||||
}
|
||||
|
||||
onConsult() {
|
||||
if (!this.currentRequest) {
|
||||
const currentFormRequest = this.uploadForm?.getCurrentRequestDraft();
|
||||
const req = currentFormRequest ?? this.currentRequest;
|
||||
|
||||
if (!req) {
|
||||
this.router.navigate([
|
||||
'/',
|
||||
this.languageService.selectedLang(),
|
||||
@@ -366,7 +457,6 @@ export class CalculatorPageComponent implements OnInit {
|
||||
return;
|
||||
}
|
||||
|
||||
const req = this.currentRequest;
|
||||
let details = `Richiesta Preventivo:\n`;
|
||||
details += `- Materiale: ${req.material}\n`;
|
||||
details += `- Qualità: ${req.quality}\n`;
|
||||
@@ -411,5 +501,231 @@ export class CalculatorPageComponent implements OnInit {
|
||||
this.errorKey.set(key);
|
||||
this.error.set(true);
|
||||
this.result.set(null);
|
||||
this.requiresRecalculation.set(false);
|
||||
this.itemSettingsDiffByFileName.set({});
|
||||
this.baselinePrintSettings = null;
|
||||
this.baselineItemSettingsByFileName = new Map<
|
||||
string,
|
||||
TrackedPrintSettings
|
||||
>();
|
||||
}
|
||||
|
||||
switchMode(nextMode: 'easy' | 'advanced'): void {
|
||||
if (this.cadSessionLocked()) return;
|
||||
|
||||
const targetPath = nextMode === 'easy' ? 'basic' : 'advanced';
|
||||
const currentPath = this.route.snapshot.routeConfig?.path;
|
||||
|
||||
this.mode.set(nextMode);
|
||||
|
||||
if (currentPath === targetPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.router.navigate(['..', targetPath], {
|
||||
relativeTo: this.route,
|
||||
queryParamsHandling: 'preserve',
|
||||
});
|
||||
}
|
||||
|
||||
private toTrackedSettingsFromRequest(req: QuoteRequest): TrackedPrintSettings {
|
||||
return {
|
||||
mode: req.mode,
|
||||
material: this.normalizeString(req.material || 'PLA'),
|
||||
quality: this.normalizeString(req.quality || 'standard'),
|
||||
nozzleDiameter: this.normalizeNumber(req.nozzleDiameter, 0.4, 2),
|
||||
layerHeight: this.normalizeNumber(req.layerHeight, 0.2, 3),
|
||||
infillDensity: this.normalizeNumber(req.infillDensity, 20, 2),
|
||||
infillPattern: this.normalizeString(req.infillPattern || 'grid'),
|
||||
supportEnabled: Boolean(req.supportEnabled),
|
||||
};
|
||||
}
|
||||
|
||||
private toTrackedSettingsFromItem(
|
||||
req: QuoteRequest,
|
||||
item: QuoteRequest['items'][number],
|
||||
): TrackedPrintSettings {
|
||||
return {
|
||||
mode: req.mode,
|
||||
material: this.normalizeString(item.material || req.material || 'PLA'),
|
||||
quality: this.normalizeString(item.quality || req.quality || 'standard'),
|
||||
nozzleDiameter: this.normalizeNumber(
|
||||
item.nozzleDiameter ?? req.nozzleDiameter,
|
||||
0.4,
|
||||
2,
|
||||
),
|
||||
layerHeight: this.normalizeNumber(
|
||||
item.layerHeight ?? req.layerHeight,
|
||||
0.2,
|
||||
3,
|
||||
),
|
||||
infillDensity: this.normalizeNumber(
|
||||
item.infillDensity ?? req.infillDensity,
|
||||
20,
|
||||
2,
|
||||
),
|
||||
infillPattern: this.normalizeString(
|
||||
item.infillPattern || req.infillPattern || 'grid',
|
||||
),
|
||||
supportEnabled: Boolean(item.supportEnabled ?? req.supportEnabled),
|
||||
};
|
||||
}
|
||||
|
||||
private toTrackedSettingsFromSession(session: any): TrackedPrintSettings {
|
||||
const layer = this.normalizeNumber(session?.layerHeightMm, 0.2, 3);
|
||||
return {
|
||||
mode: this.mode(),
|
||||
material: this.normalizeString(session?.materialCode || 'PLA'),
|
||||
quality:
|
||||
layer >= 0.24 ? 'draft' : layer <= 0.12 ? 'extra_fine' : 'standard',
|
||||
nozzleDiameter: this.normalizeNumber(session?.nozzleDiameterMm, 0.4, 2),
|
||||
layerHeight: layer,
|
||||
infillDensity: this.normalizeNumber(session?.infillPercent, 20, 2),
|
||||
infillPattern: this.normalizeString(session?.infillPattern || 'grid'),
|
||||
supportEnabled: Boolean(session?.supportsEnabled),
|
||||
};
|
||||
}
|
||||
|
||||
private toTrackedSettingsFromSessionItem(
|
||||
item: any,
|
||||
fallback: TrackedPrintSettings,
|
||||
): TrackedPrintSettings {
|
||||
const layer = this.normalizeNumber(item?.layerHeightMm, fallback.layerHeight, 3);
|
||||
return {
|
||||
mode: this.mode(),
|
||||
material: this.normalizeString(item?.materialCode || fallback.material),
|
||||
quality: this.normalizeString(
|
||||
item?.quality ||
|
||||
(layer >= 0.24
|
||||
? 'draft'
|
||||
: layer <= 0.12
|
||||
? 'extra_fine'
|
||||
: 'standard'),
|
||||
),
|
||||
nozzleDiameter: this.normalizeNumber(
|
||||
item?.nozzleDiameterMm,
|
||||
fallback.nozzleDiameter,
|
||||
2,
|
||||
),
|
||||
layerHeight: layer,
|
||||
infillDensity: this.normalizeNumber(
|
||||
item?.infillPercent,
|
||||
fallback.infillDensity,
|
||||
2,
|
||||
),
|
||||
infillPattern: this.normalizeString(
|
||||
item?.infillPattern || fallback.infillPattern,
|
||||
),
|
||||
supportEnabled: Boolean(
|
||||
item?.supportsEnabled ?? fallback.supportEnabled,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
private buildBaselineMapFromRequest(
|
||||
req: QuoteRequest,
|
||||
): Map<string, TrackedPrintSettings> {
|
||||
const map = new Map<string, TrackedPrintSettings>();
|
||||
req.items.forEach((item) => {
|
||||
map.set(
|
||||
this.normalizeFileName(item.file?.name || ''),
|
||||
this.toTrackedSettingsFromItem(req, item),
|
||||
);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
private buildBaselineMapFromSession(
|
||||
items: any[],
|
||||
defaultSettings: TrackedPrintSettings | null,
|
||||
): Map<string, TrackedPrintSettings> {
|
||||
const map = new Map<string, TrackedPrintSettings>();
|
||||
const fallback = defaultSettings ?? this.defaultTrackedSettings();
|
||||
items.forEach((item) => {
|
||||
map.set(
|
||||
this.normalizeFileName(item?.originalFilename || ''),
|
||||
this.toTrackedSettingsFromSessionItem(item, fallback),
|
||||
);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
private defaultTrackedSettings(): TrackedPrintSettings {
|
||||
return {
|
||||
mode: this.mode(),
|
||||
material: 'pla',
|
||||
quality: 'standard',
|
||||
nozzleDiameter: 0.4,
|
||||
layerHeight: 0.2,
|
||||
infillDensity: 20,
|
||||
infillPattern: 'grid',
|
||||
supportEnabled: false,
|
||||
};
|
||||
}
|
||||
|
||||
private refreshRecalculationRequirement(): void {
|
||||
if (!this.result()) return;
|
||||
|
||||
const draft = this.uploadForm?.getCurrentRequestDraft();
|
||||
if (!draft || draft.items.length === 0) {
|
||||
this.requiresRecalculation.set(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fallback = this.baselinePrintSettings;
|
||||
if (!fallback) {
|
||||
this.requiresRecalculation.set(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const changed = draft.items.some((item) => {
|
||||
const key = this.normalizeFileName(item.file?.name || '');
|
||||
const baseline = this.baselineItemSettingsByFileName.get(key) || fallback;
|
||||
const current = this.toTrackedSettingsFromItem(draft, item);
|
||||
return !this.sameTrackedSettings(baseline, current);
|
||||
});
|
||||
|
||||
this.requiresRecalculation.set(changed);
|
||||
}
|
||||
|
||||
private sameTrackedSettings(
|
||||
a: TrackedPrintSettings,
|
||||
b: TrackedPrintSettings,
|
||||
): boolean {
|
||||
return (
|
||||
a.mode === b.mode &&
|
||||
a.material === this.normalizeString(b.material) &&
|
||||
a.quality === this.normalizeString(b.quality) &&
|
||||
Math.abs(
|
||||
a.nozzleDiameter - this.normalizeNumber(b.nozzleDiameter, 0.4, 2),
|
||||
) < 0.0001 &&
|
||||
Math.abs(a.layerHeight - this.normalizeNumber(b.layerHeight, 0.2, 3)) <
|
||||
0.0001 &&
|
||||
Math.abs(a.infillDensity - this.normalizeNumber(b.infillDensity, 20, 2)) <
|
||||
0.0001 &&
|
||||
a.infillPattern === this.normalizeString(b.infillPattern) &&
|
||||
a.supportEnabled === Boolean(b.supportEnabled)
|
||||
);
|
||||
}
|
||||
|
||||
private normalizeFileName(fileName: string): string {
|
||||
return (fileName || '').split(/[\\/]/).pop()?.trim().toLowerCase() ?? '';
|
||||
}
|
||||
|
||||
private normalizeString(value: string): string {
|
||||
return String(value || '')
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
private normalizeNumber(
|
||||
value: unknown,
|
||||
fallback: number,
|
||||
decimals: number,
|
||||
): number {
|
||||
const numeric = Number(value);
|
||||
const resolved = Number.isFinite(numeric) ? numeric : fallback;
|
||||
const factor = 10 ** decimals;
|
||||
return Math.round(resolved * factor) / factor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,24 @@ import { CalculatorPageComponent } from './calculator-page.component';
|
||||
|
||||
export const CALCULATOR_ROUTES: Routes = [
|
||||
{ path: '', redirectTo: 'basic', pathMatch: 'full' },
|
||||
{ path: 'basic', component: CalculatorPageComponent, data: { mode: 'easy' } },
|
||||
{
|
||||
path: 'basic',
|
||||
component: CalculatorPageComponent,
|
||||
data: {
|
||||
mode: 'easy',
|
||||
seoTitle: 'Calcolatore stampa 3D base | 3D fab',
|
||||
seoDescription:
|
||||
'Calcola rapidamente il prezzo della tua stampa 3D in modalita base.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'advanced',
|
||||
component: CalculatorPageComponent,
|
||||
data: { mode: 'advanced' },
|
||||
data: {
|
||||
mode: 'advanced',
|
||||
seoTitle: 'Calcolatore stampa 3D avanzato | 3D fab',
|
||||
seoDescription:
|
||||
'Configura parametri avanzati e ottieni un preventivo preciso con slicing reale.',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -45,6 +45,12 @@
|
||||
<p>{{ result().notes }}</p>
|
||||
</div>
|
||||
}
|
||||
@if (recalculationRequired()) {
|
||||
<div class="recalc-banner">
|
||||
Hai modificato i parametri di stampa. Ricalcola il preventivo prima di
|
||||
procedere con l'ordine.
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
@@ -56,7 +62,14 @@
|
||||
<span class="file-name">{{ item.fileName }}</span>
|
||||
<span class="file-details">
|
||||
{{ item.unitTime / 3600 | number: "1.1-1" }}h |
|
||||
{{ item.unitWeight | number: "1.0-0" }}g
|
||||
{{ item.unitWeight | number: "1.0-0" }}g |
|
||||
<span class="material-chip">{{ item.material || "N/D" }}</span>
|
||||
@if (getItemDifferenceLabel(item.fileName, item.material)) {
|
||||
|
|
||||
<small class="item-settings-diff">
|
||||
{{ getItemDifferenceLabel(item.fileName, item.material) }}
|
||||
</small>
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -96,12 +109,18 @@
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<app-button variant="outline" (click)="consult.emit()">
|
||||
<div class="actions-left">
|
||||
<app-button variant="secondary" (click)="consult.emit()">
|
||||
{{ "QUOTE.CONSULT" | translate }}
|
||||
</app-button>
|
||||
</div>
|
||||
|
||||
<div class="actions-right">
|
||||
@if (!hasQuantityOverLimit()) {
|
||||
<app-button (click)="proceed.emit()">
|
||||
<app-button
|
||||
[disabled]="recalculationRequired()"
|
||||
(click)="proceed.emit()"
|
||||
>
|
||||
{{ "QUOTE.PROCEED_ORDER" | translate }}
|
||||
</app-button>
|
||||
} @else {
|
||||
@@ -109,5 +128,11 @@
|
||||
"QUOTE.MAX_QTY_NOTICE" | translate: { max: directOrderLimit }
|
||||
}}</small>
|
||||
}
|
||||
@if (recalculationRequired()) {
|
||||
<small class="limit-note">
|
||||
Ricalcola il preventivo per riattivare il checkout.
|
||||
</small>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</app-card>
|
||||
|
||||
@@ -20,10 +20,11 @@
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: var(--space-3);
|
||||
background: var(--color-neutral-50);
|
||||
padding: var(--space-3) var(--space-4);
|
||||
background: var(--color-bg-card);
|
||||
border-radius: var(--radius-md);
|
||||
border: 1px solid var(--color-border);
|
||||
box-shadow: 0 2px 6px rgba(10, 20, 30, 0.04);
|
||||
}
|
||||
|
||||
.item-info {
|
||||
@@ -41,11 +42,32 @@
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.item-settings-diff {
|
||||
margin-left: 2px;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 600;
|
||||
color: #8a6d1f;
|
||||
white-space: normal;
|
||||
}
|
||||
.file-details {
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
.material-chip {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid #d9d4bd;
|
||||
background: #fbf7e9;
|
||||
color: #6d5b1d;
|
||||
font-size: 0.72rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.2px;
|
||||
}
|
||||
|
||||
.item-controls {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -126,15 +148,40 @@
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: var(--space-3);
|
||||
margin-top: var(--space-2);
|
||||
|
||||
@media (max-width: 640px) {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
}
|
||||
|
||||
.actions-left,
|
||||
.actions-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
.actions-right {
|
||||
justify-content: flex-end;
|
||||
|
||||
@media (max-width: 640px) {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
}
|
||||
|
||||
.limit-note {
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-text-muted);
|
||||
text-align: center;
|
||||
margin-top: calc(var(--space-2) * -1);
|
||||
text-align: right;
|
||||
|
||||
@media (max-width: 640px) {
|
||||
text-align: left;
|
||||
}
|
||||
}
|
||||
|
||||
.notes-section {
|
||||
@@ -160,3 +207,14 @@
|
||||
white-space: pre-wrap; /* Preserve line breaks */
|
||||
}
|
||||
}
|
||||
|
||||
.recalc-banner {
|
||||
margin-top: var(--space-4);
|
||||
margin-bottom: var(--space-4);
|
||||
padding: var(--space-3);
|
||||
border: 1px solid #f0c95a;
|
||||
background: #fff8e1;
|
||||
border-radius: var(--radius-md);
|
||||
color: #6f5b1a;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ export class QuoteResultComponent implements OnDestroy {
|
||||
readonly quantityAutoRefreshMs = 2000;
|
||||
|
||||
result = input.required<QuoteResult>();
|
||||
recalculationRequired = input<boolean>(false);
|
||||
itemSettingsDiffByFileName = input<Record<string, { differences: string[] }>>(
|
||||
{},
|
||||
);
|
||||
consult = output<void>();
|
||||
proceed = output<void>();
|
||||
itemChange = output<{
|
||||
@@ -43,6 +47,12 @@ export class QuoteResultComponent implements OnDestroy {
|
||||
fileName: string;
|
||||
quantity: number;
|
||||
}>();
|
||||
itemQuantityPreviewChange = output<{
|
||||
id?: string;
|
||||
index: number;
|
||||
fileName: string;
|
||||
quantity: number;
|
||||
}>();
|
||||
|
||||
// Local mutable state for items to handle quantity changes
|
||||
items = signal<QuoteItem[]>([]);
|
||||
@@ -87,6 +97,13 @@ export class QuoteResultComponent implements OnDestroy {
|
||||
return updated;
|
||||
});
|
||||
|
||||
this.itemQuantityPreviewChange.emit({
|
||||
id: item.id,
|
||||
index,
|
||||
fileName: item.fileName,
|
||||
quantity: normalizedQty,
|
||||
});
|
||||
|
||||
this.scheduleQuantityRefresh(index, key);
|
||||
}
|
||||
|
||||
@@ -171,4 +188,31 @@ export class QuoteResultComponent implements OnDestroy {
|
||||
this.quantityTimers.forEach((timer) => clearTimeout(timer));
|
||||
this.quantityTimers.clear();
|
||||
}
|
||||
|
||||
getItemDifferenceLabel(fileName: string, materialCode?: string): string {
|
||||
const differences =
|
||||
this.itemSettingsDiffByFileName()[fileName]?.differences || [];
|
||||
if (differences.length === 0) return '';
|
||||
|
||||
const normalizedMaterial = String(materialCode || '')
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
const filtered = differences.filter((entry) => {
|
||||
const normalized = String(entry || '')
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
const isMaterialOnly = !normalized.includes(':');
|
||||
return !(isMaterialOnly && normalized === normalizedMaterial);
|
||||
});
|
||||
|
||||
if (filtered.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const materialOnly = filtered.find(
|
||||
(entry) => !entry.includes(':') && entry.trim().length > 0,
|
||||
);
|
||||
return materialOnly || filtered.join(' | ');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,11 +13,9 @@
|
||||
>
|
||||
</app-stl-viewer>
|
||||
}
|
||||
<!-- Close button removed as requested -->
|
||||
</div>
|
||||
}
|
||||
|
||||
<!-- Initial Dropzone (Visible only when no files) -->
|
||||
@if (items().length === 0) {
|
||||
<app-dropzone
|
||||
[label]="'CALC.UPLOAD_LABEL'"
|
||||
@@ -29,7 +27,6 @@
|
||||
</app-dropzone>
|
||||
}
|
||||
|
||||
<!-- New File List with Details -->
|
||||
@if (items().length > 0) {
|
||||
<div class="items-grid">
|
||||
@for (item of items(); track item.file.name; let i = $index) {
|
||||
@@ -63,7 +60,7 @@
|
||||
<app-color-selector
|
||||
[selectedColor]="item.color"
|
||||
[selectedVariantId]="item.filamentVariantId ?? null"
|
||||
[variants]="currentMaterialVariants()"
|
||||
[variants]="getVariantsForMaterial(item.material)"
|
||||
(colorSelected)="updateItemColor(i, $event)"
|
||||
>
|
||||
</app-color-selector>
|
||||
@@ -83,7 +80,6 @@
|
||||
}
|
||||
</div>
|
||||
|
||||
<!-- "Add Files" Button (Visible only when files exist) -->
|
||||
<div class="add-more-container">
|
||||
<input
|
||||
#additionalInput
|
||||
@@ -102,11 +98,6 @@
|
||||
+ {{ "CALC.ADD_FILES" | translate }}
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (items().length === 0 && form.get("itemsTouched")?.value) {
|
||||
<div class="error-msg">{{ "CALC.ERR_FILE_REQUIRED" | translate }}</div>
|
||||
}
|
||||
|
||||
<p class="upload-privacy-note">
|
||||
{{ "LEGAL.CONSENT.UPLOAD_NOTICE_PREFIX" | translate }}
|
||||
@@ -115,66 +106,159 @@
|
||||
}}</a
|
||||
>.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
@if (lockedSettings()) {
|
||||
<p class="upload-privacy-note">
|
||||
Parametri stampa bloccati per sessione CAD: materiale, nozzle, layer,
|
||||
infill e supporti sono definiti dal back-office.
|
||||
</p>
|
||||
}
|
||||
<app-select
|
||||
formControlName="material"
|
||||
[label]="'CALC.MATERIAL' | translate"
|
||||
[options]="materials()"
|
||||
></app-select>
|
||||
|
||||
@if (mode() === "easy") {
|
||||
<app-select
|
||||
formControlName="quality"
|
||||
[label]="'CALC.QUALITY' | translate"
|
||||
[options]="qualities()"
|
||||
></app-select>
|
||||
} @else {
|
||||
<app-select
|
||||
formControlName="nozzleDiameter"
|
||||
[label]="'CALC.NOZZLE' | translate"
|
||||
[options]="nozzleDiameters()"
|
||||
></app-select>
|
||||
}
|
||||
</div>
|
||||
|
||||
<!-- Global quantity removed, now per item -->
|
||||
|
||||
@if (mode() === "advanced") {
|
||||
<div class="grid">
|
||||
<app-select
|
||||
formControlName="infillPattern"
|
||||
[label]="'CALC.PATTERN' | translate"
|
||||
[options]="infillPatterns()"
|
||||
></app-select>
|
||||
|
||||
<app-select
|
||||
formControlName="layerHeight"
|
||||
[label]="'CALC.LAYER_HEIGHT' | translate"
|
||||
[options]="layerHeights()"
|
||||
></app-select>
|
||||
<div class="sync-settings">
|
||||
<label class="sync-settings-toggle">
|
||||
<input
|
||||
type="checkbox"
|
||||
[checked]="sameSettingsForAll()"
|
||||
[disabled]="lockedSettings()"
|
||||
(change)="onSameSettingsToggle($any($event.target).checked)"
|
||||
/>
|
||||
<span class="sync-settings-copy">
|
||||
<span class="sync-settings-title">
|
||||
Stesse impostazioni per tutti i file
|
||||
</span>
|
||||
<span class="sync-settings-subtitle">Colore escluso</span>
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<app-input
|
||||
formControlName="infillDensity"
|
||||
type="number"
|
||||
[label]="'CALC.INFILL' | translate"
|
||||
></app-input>
|
||||
@if (sameSettingsForAll()) {
|
||||
<div class="item-settings-panel">
|
||||
<h4 class="item-settings-title">Impostazioni globali</h4>
|
||||
|
||||
<div class="checkbox-row">
|
||||
<input type="checkbox" formControlName="supportEnabled" id="support" />
|
||||
<label for="support">{{ "CALC.SUPPORT" | translate }}</label>
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.MATERIAL" | translate }}
|
||||
<select formControlName="material">
|
||||
@for (mat of materials(); track mat.value) {
|
||||
<option [value]="mat.value">{{ mat.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
{{ "CALC.NOZZLE" | translate }}
|
||||
<select formControlName="nozzleDiameter">
|
||||
@for (n of nozzleDiameters(); track n.value) {
|
||||
<option [value]="n.value">{{ n.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.PATTERN" | translate }}
|
||||
<select formControlName="infillPattern">
|
||||
@for (p of infillPatterns(); track p.value) {
|
||||
<option [value]="p.value">{{ p.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
{{ "CALC.LAYER_HEIGHT" | translate }}
|
||||
<select formControlName="layerHeight">
|
||||
@for (l of layerHeights(); track l.value) {
|
||||
<option [value]="l.value">{{ l.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.INFILL" | translate }}
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
formControlName="infillDensity"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="item-settings-checkbox">
|
||||
<input type="checkbox" formControlName="supportEnabled" />
|
||||
<span>{{ "CALC.SUPPORT" | translate }}</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
} @else {
|
||||
@if (getSelectedItem(); as selectedItem) {
|
||||
<div class="item-settings-panel">
|
||||
<h4 class="item-settings-title">
|
||||
Impostazioni file: {{ selectedItem.file.name }}
|
||||
</h4>
|
||||
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.MATERIAL" | translate }}
|
||||
<select formControlName="material">
|
||||
@for (mat of materials(); track mat.value) {
|
||||
<option [value]="mat.value">{{ mat.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
{{ "CALC.NOZZLE" | translate }}
|
||||
<select formControlName="nozzleDiameter">
|
||||
@for (n of nozzleDiameters(); track n.value) {
|
||||
<option [value]="n.value">{{ n.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.PATTERN" | translate }}
|
||||
<select formControlName="infillPattern">
|
||||
@for (p of infillPatterns(); track p.value) {
|
||||
<option [value]="p.value">{{ p.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
{{ "CALC.LAYER_HEIGHT" | translate }}
|
||||
<select formControlName="layerHeight">
|
||||
@for (l of getLayerHeightOptionsForNozzle(form.get('nozzleDiameter')?.value); track l.value) {
|
||||
<option [value]="l.value">{{ l.label }}</option>
|
||||
}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="item-settings-grid">
|
||||
<label>
|
||||
{{ "CALC.INFILL" | translate }}
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
formControlName="infillDensity"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="item-settings-checkbox">
|
||||
<input type="checkbox" formControlName="supportEnabled" />
|
||||
<span>{{ "CALC.SUPPORT" | translate }}</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@if (items().length === 0 && form.get("itemsTouched")?.value) {
|
||||
<div class="error-msg">{{ "CALC.ERR_FILE_REQUIRED" | translate }}</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<app-input
|
||||
formControlName="notes"
|
||||
@@ -183,7 +267,6 @@
|
||||
></app-input>
|
||||
|
||||
<div class="actions">
|
||||
<!-- Progress Bar (Only when uploading i.e. progress < 100) -->
|
||||
@if (loading() && uploadProgress() < 100) {
|
||||
<div class="progress-container">
|
||||
<div class="progress-bar">
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
margin-bottom: var(--space-6);
|
||||
}
|
||||
.upload-privacy-note {
|
||||
margin-top: var(--space-3);
|
||||
margin-bottom: 0;
|
||||
font-size: 0.78rem;
|
||||
margin-top: var(--space-4);
|
||||
margin-bottom: var(--space-1);
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-text-muted);
|
||||
text-align: left;
|
||||
}
|
||||
@@ -35,48 +35,50 @@
|
||||
/* Grid Layout for Files */
|
||||
.items-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr; /* Force 2 columns on mobile */
|
||||
gap: var(--space-2); /* Tighten gap for mobile */
|
||||
grid-template-columns: 1fr;
|
||||
gap: var(--space-3);
|
||||
margin-top: var(--space-4);
|
||||
margin-bottom: var(--space-4);
|
||||
|
||||
@media (min-width: 640px) {
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: var(--space-3);
|
||||
}
|
||||
}
|
||||
|
||||
.file-card {
|
||||
padding: var(--space-2); /* Reduced from space-3 */
|
||||
background: var(--color-neutral-100);
|
||||
padding: var(--space-3);
|
||||
background: var(--color-bg-card);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-md);
|
||||
transition: all 0.2s;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px; /* Reduced gap */
|
||||
position: relative; /* For absolute positioning of remove btn */
|
||||
min-width: 0; /* Allow flex item to shrink below content size if needed */
|
||||
gap: var(--space-2);
|
||||
position: relative;
|
||||
min-width: 0;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--color-neutral-300);
|
||||
box-shadow: 0 4px 10px rgba(10, 20, 30, 0.07);
|
||||
}
|
||||
&.active {
|
||||
border-color: var(--color-brand);
|
||||
background: rgba(250, 207, 10, 0.05);
|
||||
background: rgba(250, 207, 10, 0.08);
|
||||
box-shadow: 0 0 0 1px var(--color-brand);
|
||||
}
|
||||
}
|
||||
|
||||
.card-header {
|
||||
overflow: hidden;
|
||||
padding-right: 25px; /* Adjusted */
|
||||
margin-bottom: 2px;
|
||||
padding-right: 28px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.file-name {
|
||||
font-weight: 500;
|
||||
font-size: 0.8rem; /* Smaller font */
|
||||
font-weight: 600;
|
||||
font-size: 0.92rem;
|
||||
color: var(--color-text);
|
||||
display: block;
|
||||
white-space: nowrap;
|
||||
@@ -92,47 +94,46 @@
|
||||
|
||||
.card-controls {
|
||||
display: flex;
|
||||
align-items: flex-end; /* Align bottom of input and color circle */
|
||||
gap: 16px; /* Space between Qty and Color */
|
||||
align-items: flex-end;
|
||||
gap: var(--space-4);
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.qty-group,
|
||||
.color-group {
|
||||
display: flex;
|
||||
flex-direction: column; /* Stack label and input */
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0px;
|
||||
gap: 2px;
|
||||
|
||||
label {
|
||||
font-size: 0.6rem;
|
||||
font-size: 0.72rem;
|
||||
color: var(--color-text-muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
letter-spacing: 0.3px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 2px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.color-group {
|
||||
align-items: flex-start; /* Align label left */
|
||||
/* margin-right removed */
|
||||
align-items: flex-start;
|
||||
|
||||
/* Override margin in selector for this context */
|
||||
::ng-deep .color-selector-container {
|
||||
margin-left: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.qty-input {
|
||||
width: 36px; /* Slightly smaller */
|
||||
padding: 1px 2px;
|
||||
width: 54px;
|
||||
padding: 4px 6px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-sm);
|
||||
text-align: center;
|
||||
font-size: 0.85rem;
|
||||
font-size: 0.95rem;
|
||||
font-weight: 600;
|
||||
background: white;
|
||||
height: 24px; /* Explicit height to match color circle somewhat */
|
||||
height: 34px;
|
||||
&:focus {
|
||||
outline: none;
|
||||
border-color: var(--color-brand);
|
||||
@@ -141,10 +142,10 @@
|
||||
|
||||
.btn-remove {
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 4px;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
top: 6px;
|
||||
right: 6px;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border-radius: 4px;
|
||||
border: none;
|
||||
background: transparent;
|
||||
@@ -155,7 +156,7 @@
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: all 0.2s;
|
||||
font-size: 0.8rem;
|
||||
font-size: 0.9rem;
|
||||
|
||||
&:hover {
|
||||
background: var(--color-danger-100);
|
||||
@@ -170,7 +171,7 @@
|
||||
|
||||
.btn-add-more {
|
||||
width: 100%;
|
||||
padding: var(--space-3);
|
||||
padding: 0.75rem var(--space-3);
|
||||
background: var(--color-neutral-800);
|
||||
color: white;
|
||||
border: none;
|
||||
@@ -193,6 +194,50 @@
|
||||
}
|
||||
}
|
||||
|
||||
.sync-settings {
|
||||
margin-top: var(--space-4);
|
||||
margin-bottom: var(--space-4);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--color-neutral-50);
|
||||
padding: var(--space-3);
|
||||
}
|
||||
|
||||
.sync-settings-toggle {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--space-3);
|
||||
cursor: pointer;
|
||||
|
||||
input[type="checkbox"] {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
margin-top: 2px;
|
||||
accent-color: var(--color-brand);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.sync-settings-copy {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.sync-settings-title {
|
||||
font-size: 0.95rem;
|
||||
font-weight: 700;
|
||||
color: var(--color-text);
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.sync-settings-subtitle {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 500;
|
||||
color: var(--color-text-muted);
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.checkbox-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -211,6 +256,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
.sync-all-row {
|
||||
margin-top: var(--space-2);
|
||||
margin-bottom: var(--space-4);
|
||||
padding-top: 0;
|
||||
}
|
||||
|
||||
/* Progress Bar */
|
||||
.progress-container {
|
||||
margin-bottom: var(--space-3);
|
||||
@@ -244,3 +295,74 @@
|
||||
color: var(--color-text-muted);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.item-settings-panel {
|
||||
margin-top: var(--space-4);
|
||||
padding: var(--space-4);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--color-bg-card);
|
||||
}
|
||||
|
||||
.item-settings-title {
|
||||
margin: 0 0 var(--space-4);
|
||||
font-size: 1.05rem;
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.item-settings-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: var(--space-3);
|
||||
margin-bottom: var(--space-3);
|
||||
|
||||
@media (min-width: 640px) {
|
||||
grid-template-columns: 1fr 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.item-settings-grid label,
|
||||
.item-settings-checkbox {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-1);
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.item-settings-grid input,
|
||||
.item-settings-grid select {
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-md);
|
||||
padding: 0.5rem 0.75rem;
|
||||
background: var(--color-bg-card);
|
||||
font-size: 1rem;
|
||||
color: var(--color-text);
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
border-color: var(--color-brand);
|
||||
box-shadow: 0 0 0 2px rgba(250, 207, 10, 0.25);
|
||||
}
|
||||
}
|
||||
|
||||
.item-settings-checkbox {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
|
||||
input[type="checkbox"] {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
accent-color: var(--color-brand);
|
||||
}
|
||||
}
|
||||
|
||||
.item-settings-checkbox--top {
|
||||
margin-top: var(--space-4);
|
||||
margin-bottom: var(--space-4);
|
||||
color: var(--color-text);
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,24 @@
|
||||
import { Injectable, inject, signal } from '@angular/core';
|
||||
import { HttpClient, HttpEventType } from '@angular/common/http';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, catchError, tap } from 'rxjs/operators';
|
||||
import { environment } from '../../../../environments/environment';
|
||||
|
||||
export interface QuoteRequest {
|
||||
items: {
|
||||
export interface QuoteRequestItem {
|
||||
file: File;
|
||||
quantity: number;
|
||||
material?: string;
|
||||
quality?: string;
|
||||
color?: string;
|
||||
filamentVariantId?: number;
|
||||
}[];
|
||||
supportEnabled?: boolean;
|
||||
infillDensity?: number;
|
||||
infillPattern?: string;
|
||||
layerHeight?: number;
|
||||
nozzleDiameter?: number;
|
||||
}
|
||||
|
||||
export interface QuoteRequest {
|
||||
items: QuoteRequestItem[];
|
||||
material: string;
|
||||
quality: string;
|
||||
notes?: string;
|
||||
@@ -26,12 +34,18 @@ export interface QuoteItem {
|
||||
id?: string;
|
||||
fileName: string;
|
||||
unitPrice: number;
|
||||
unitTime: number; // seconds
|
||||
unitWeight: number; // grams
|
||||
unitTime: number;
|
||||
unitWeight: number;
|
||||
quantity: number;
|
||||
material?: string;
|
||||
quality?: string;
|
||||
color?: string;
|
||||
filamentVariantId?: number;
|
||||
supportEnabled?: boolean;
|
||||
infillDensity?: number;
|
||||
infillPattern?: string;
|
||||
layerHeight?: number;
|
||||
nozzleDiameter?: number;
|
||||
}
|
||||
|
||||
export interface QuoteResult {
|
||||
@@ -49,36 +63,12 @@ export interface QuoteResult {
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
interface BackendResponse {
|
||||
success: boolean;
|
||||
data: {
|
||||
print_time_seconds: number;
|
||||
material_grams: number;
|
||||
cost: {
|
||||
total: number;
|
||||
};
|
||||
};
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface BackendQuoteResult {
|
||||
totalPrice: number;
|
||||
currency: string;
|
||||
setupCost: number;
|
||||
stats: {
|
||||
printTimeSeconds: number;
|
||||
printTimeFormatted: string;
|
||||
filamentWeightGrams: number;
|
||||
filamentLengthMm: number;
|
||||
};
|
||||
}
|
||||
|
||||
// Options Interfaces
|
||||
export interface MaterialOption {
|
||||
code: string;
|
||||
label: string;
|
||||
variants: VariantOption[];
|
||||
}
|
||||
|
||||
export interface VariantOption {
|
||||
id: number;
|
||||
name: string;
|
||||
@@ -89,28 +79,36 @@ export interface VariantOption {
|
||||
stockFilamentGrams: number;
|
||||
isOutOfStock: boolean;
|
||||
}
|
||||
|
||||
export interface QualityOption {
|
||||
id: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface InfillOption {
|
||||
id: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface NumericOption {
|
||||
value: number;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface NozzleLayerHeightOptions {
|
||||
nozzleDiameter: number;
|
||||
layerHeights: NumericOption[];
|
||||
}
|
||||
|
||||
export interface OptionsResponse {
|
||||
materials: MaterialOption[];
|
||||
qualities: QualityOption[];
|
||||
infillPatterns: InfillOption[];
|
||||
layerHeights: NumericOption[];
|
||||
nozzleDiameters: NumericOption[];
|
||||
layerHeightsByNozzle: NozzleLayerHeightOptions[];
|
||||
}
|
||||
|
||||
// UI Option for Select Component
|
||||
export interface SimpleOption {
|
||||
value: string | number;
|
||||
label: string;
|
||||
@@ -122,70 +120,23 @@ export interface SimpleOption {
|
||||
export class QuoteEstimatorService {
|
||||
private http = inject(HttpClient);
|
||||
|
||||
private buildEasyModePreset(quality: string | undefined): {
|
||||
quality: string;
|
||||
layerHeight: number;
|
||||
infillDensity: number;
|
||||
infillPattern: string;
|
||||
nozzleDiameter: number;
|
||||
} {
|
||||
const normalized = (quality || 'standard').toLowerCase();
|
||||
|
||||
// Legacy alias support.
|
||||
if (normalized === 'high' || normalized === 'extra_fine') {
|
||||
return {
|
||||
quality: 'extra_fine',
|
||||
layerHeight: 0.12,
|
||||
infillDensity: 20,
|
||||
infillPattern: 'grid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
|
||||
if (normalized === 'draft') {
|
||||
return {
|
||||
quality: 'extra_fine',
|
||||
layerHeight: 0.24,
|
||||
infillDensity: 12,
|
||||
infillPattern: 'grid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
quality: 'standard',
|
||||
layerHeight: 0.2,
|
||||
infillDensity: 15,
|
||||
infillPattern: 'grid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
private pendingConsultation = signal<{
|
||||
files: File[];
|
||||
message: string;
|
||||
} | null>(null);
|
||||
|
||||
getOptions(): Observable<OptionsResponse> {
|
||||
console.log('QuoteEstimatorService: Requesting options...');
|
||||
const headers: any = {};
|
||||
return this.http
|
||||
.get<OptionsResponse>(`${environment.apiUrl}/api/calculator/options`, {
|
||||
return this.http.get<OptionsResponse>(`${environment.apiUrl}/api/calculator/options`, {
|
||||
headers,
|
||||
})
|
||||
.pipe(
|
||||
tap({
|
||||
next: (res) =>
|
||||
console.log('QuoteEstimatorService: Options loaded', res),
|
||||
error: (err) =>
|
||||
console.error('QuoteEstimatorService: Options failed', err),
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// NEW METHODS for Order Flow
|
||||
|
||||
getQuoteSession(sessionId: string): Observable<any> {
|
||||
const headers: any = {};
|
||||
return this.http.get(
|
||||
`${environment.apiUrl}/api/quote-sessions/${sessionId}`,
|
||||
{ headers },
|
||||
);
|
||||
return this.http.get(`${environment.apiUrl}/api/quote-sessions/${sessionId}`, {
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
updateLineItem(lineItemId: string, changes: any): Observable<any> {
|
||||
@@ -224,13 +175,10 @@ export class QuoteEstimatorService {
|
||||
|
||||
getOrderInvoice(orderId: string): Observable<Blob> {
|
||||
const headers: any = {};
|
||||
return this.http.get(
|
||||
`${environment.apiUrl}/api/orders/${orderId}/invoice`,
|
||||
{
|
||||
return this.http.get(`${environment.apiUrl}/api/orders/${orderId}/invoice`, {
|
||||
headers,
|
||||
responseType: 'blob',
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
getOrderConfirmation(orderId: string): Observable<Blob> {
|
||||
@@ -252,73 +200,68 @@ export class QuoteEstimatorService {
|
||||
}
|
||||
|
||||
calculate(request: QuoteRequest): Observable<number | QuoteResult> {
|
||||
console.log('QuoteEstimatorService: Calculating quote...', request);
|
||||
if (request.items.length === 0) {
|
||||
console.warn('QuoteEstimatorService: No items to calculate');
|
||||
return of();
|
||||
if (!request.items || request.items.length === 0) {
|
||||
return of(0);
|
||||
}
|
||||
|
||||
return new Observable((observer) => {
|
||||
// 1. Create Session first
|
||||
return new Observable<number | QuoteResult>((observer) => {
|
||||
const headers: any = {};
|
||||
|
||||
this.http
|
||||
.post<any>(`${environment.apiUrl}/api/quote-sessions`, {}, { headers })
|
||||
.subscribe({
|
||||
next: (sessionRes) => {
|
||||
const sessionId = sessionRes.id;
|
||||
const sessionSetupCost = sessionRes.setupCostChf || 0;
|
||||
const sessionId = String(sessionRes?.id || '');
|
||||
if (!sessionId) {
|
||||
observer.error('Could not initialize quote session');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Upload files to this session
|
||||
const totalItems = request.items.length;
|
||||
const allProgress: number[] = new Array(totalItems).fill(0);
|
||||
const finalResponses: any[] = [];
|
||||
let completedRequests = 0;
|
||||
const uploadProgress = new Array(totalItems).fill(0);
|
||||
const uploadResults: { success: boolean }[] = new Array(totalItems)
|
||||
.fill(null)
|
||||
.map(() => ({ success: false }));
|
||||
let completed = 0;
|
||||
|
||||
const checkCompletion = () => {
|
||||
const emitProgress = () => {
|
||||
const avg = Math.round(
|
||||
allProgress.reduce((a, b) => a + b, 0) / totalItems,
|
||||
uploadProgress.reduce((sum, value) => sum + value, 0) / totalItems,
|
||||
);
|
||||
observer.next(avg);
|
||||
};
|
||||
|
||||
if (completedRequests === totalItems) {
|
||||
finalize(finalResponses, sessionSetupCost, sessionId);
|
||||
const finalize = () => {
|
||||
emitProgress();
|
||||
if (completed !== totalItems) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasFailure = uploadResults.some((entry) => !entry.success);
|
||||
if (hasFailure) {
|
||||
observer.error('One or more files failed during upload/analysis');
|
||||
return;
|
||||
}
|
||||
|
||||
this.getQuoteSession(sessionId).subscribe({
|
||||
next: (sessionData) => {
|
||||
observer.next(100);
|
||||
const result = this.mapSessionToQuoteResult(sessionData);
|
||||
result.notes = request.notes;
|
||||
observer.next(result);
|
||||
observer.complete();
|
||||
},
|
||||
error: () => {
|
||||
observer.error('Failed to calculate final quote');
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
request.items.forEach((item, index) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', item.file);
|
||||
|
||||
const easyPreset =
|
||||
request.mode === 'easy'
|
||||
? this.buildEasyModePreset(request.quality)
|
||||
: null;
|
||||
|
||||
const settings = {
|
||||
complexityMode:
|
||||
request.mode === 'easy'
|
||||
? 'ADVANCED'
|
||||
: request.mode.toUpperCase(),
|
||||
material: request.material,
|
||||
filamentVariantId: item.filamentVariantId,
|
||||
quality: easyPreset ? easyPreset.quality : request.quality,
|
||||
supportsEnabled: request.supportEnabled,
|
||||
color: item.color || '#FFFFFF',
|
||||
layerHeight: easyPreset
|
||||
? easyPreset.layerHeight
|
||||
: request.layerHeight,
|
||||
infillDensity: easyPreset
|
||||
? easyPreset.infillDensity
|
||||
: request.infillDensity,
|
||||
infillPattern: easyPreset
|
||||
? easyPreset.infillPattern
|
||||
: request.infillPattern,
|
||||
nozzleDiameter: easyPreset
|
||||
? easyPreset.nozzleDiameter
|
||||
: request.nozzleDiameter,
|
||||
};
|
||||
|
||||
const settings = this.buildSettingsPayload(request, item);
|
||||
const settingsBlob = new Blob([JSON.stringify(settings)], {
|
||||
type: 'application/json',
|
||||
});
|
||||
@@ -340,84 +283,46 @@ export class QuoteEstimatorService {
|
||||
event.type === HttpEventType.UploadProgress &&
|
||||
event.total
|
||||
) {
|
||||
allProgress[index] = Math.round(
|
||||
uploadProgress[index] = Math.round(
|
||||
(100 * event.loaded) / event.total,
|
||||
);
|
||||
checkCompletion();
|
||||
} else if (event.type === HttpEventType.Response) {
|
||||
allProgress[index] = 100;
|
||||
finalResponses[index] = {
|
||||
...event.body,
|
||||
success: true,
|
||||
fileName: item.file.name,
|
||||
originalQty: item.quantity,
|
||||
originalItem: item,
|
||||
};
|
||||
completedRequests++;
|
||||
checkCompletion();
|
||||
emitProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === HttpEventType.Response) {
|
||||
uploadProgress[index] = 100;
|
||||
uploadResults[index] = { success: true };
|
||||
completed += 1;
|
||||
finalize();
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Item upload failed', err);
|
||||
finalResponses[index] = {
|
||||
success: false,
|
||||
fileName: item.file.name,
|
||||
};
|
||||
completedRequests++;
|
||||
checkCompletion();
|
||||
error: () => {
|
||||
uploadProgress[index] = 100;
|
||||
uploadResults[index] = { success: false };
|
||||
completed += 1;
|
||||
finalize();
|
||||
},
|
||||
});
|
||||
});
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Failed to create session', err);
|
||||
error: () => {
|
||||
observer.error('Could not initialize quote session');
|
||||
},
|
||||
});
|
||||
|
||||
const finalize = (
|
||||
responses: any[],
|
||||
setupCost: number,
|
||||
sessionId: string,
|
||||
) => {
|
||||
this.http
|
||||
.get<any>(`${environment.apiUrl}/api/quote-sessions/${sessionId}`, {
|
||||
headers,
|
||||
})
|
||||
.subscribe({
|
||||
next: (sessionData) => {
|
||||
observer.next(100);
|
||||
const result = this.mapSessionToQuoteResult(sessionData);
|
||||
result.notes = request.notes;
|
||||
observer.next(result);
|
||||
observer.complete();
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Failed to fetch final session calculation', err);
|
||||
observer.error('Failed to calculate final quote');
|
||||
},
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Consultation Data Transfer
|
||||
private pendingConsultation = signal<{
|
||||
files: File[];
|
||||
message: string;
|
||||
} | null>(null);
|
||||
|
||||
setPendingConsultation(data: { files: File[]; message: string }) {
|
||||
this.pendingConsultation.set(data);
|
||||
}
|
||||
|
||||
getPendingConsultation() {
|
||||
const data = this.pendingConsultation();
|
||||
this.pendingConsultation.set(null); // Clear after reading
|
||||
this.pendingConsultation.set(null);
|
||||
return data;
|
||||
}
|
||||
|
||||
// Session File Retrieval
|
||||
getLineItemContent(
|
||||
sessionId: string,
|
||||
lineItemId: string,
|
||||
@@ -434,48 +339,156 @@ export class QuoteEstimatorService {
|
||||
);
|
||||
}
|
||||
|
||||
getLineItemStlPreview(
|
||||
sessionId: string,
|
||||
lineItemId: string,
|
||||
): Observable<Blob> {
|
||||
const headers: any = {};
|
||||
return this.http.get(
|
||||
`${environment.apiUrl}/api/quote-sessions/${sessionId}/line-items/${lineItemId}/stl-preview`,
|
||||
{
|
||||
headers,
|
||||
responseType: 'blob',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
mapSessionToQuoteResult(sessionData: any): QuoteResult {
|
||||
const session = sessionData.session;
|
||||
const items = sessionData.items || [];
|
||||
const session = sessionData?.session || {};
|
||||
const items = Array.isArray(sessionData?.items) ? sessionData.items : [];
|
||||
|
||||
const totalTime = items.reduce(
|
||||
(acc: number, item: any) =>
|
||||
acc + (item.printTimeSeconds || 0) * item.quantity,
|
||||
0,
|
||||
);
|
||||
const totalWeight = items.reduce(
|
||||
(acc: number, item: any) =>
|
||||
acc + (item.materialGrams || 0) * item.quantity,
|
||||
acc + Number(item?.printTimeSeconds || 0) * Number(item?.quantity || 1),
|
||||
0,
|
||||
);
|
||||
|
||||
const totalWeight = items.reduce(
|
||||
(acc: number, item: any) =>
|
||||
acc + Number(item?.materialGrams || 0) * Number(item?.quantity || 1),
|
||||
0,
|
||||
);
|
||||
|
||||
const grandTotal = Number(sessionData?.grandTotalChf);
|
||||
const fallbackTotal =
|
||||
Number(sessionData?.itemsTotalChf || 0) +
|
||||
Number(session?.setupCostChf || 0) +
|
||||
Number(sessionData?.shippingCostChf || 0);
|
||||
|
||||
return {
|
||||
sessionId: session.id,
|
||||
sessionId: session?.id,
|
||||
items: items.map((item: any) => ({
|
||||
id: item.id,
|
||||
fileName: item.originalFilename,
|
||||
unitPrice: item.unitPriceChf,
|
||||
unitTime: item.printTimeSeconds,
|
||||
unitWeight: item.materialGrams,
|
||||
quantity: item.quantity,
|
||||
material: session.materialCode, // Assumption: session has one material for all? or items have it?
|
||||
// Backend model QuoteSession has materialCode.
|
||||
// But line items might have different colors.
|
||||
color: item.colorCode,
|
||||
filamentVariantId: item.filamentVariantId,
|
||||
id: item?.id,
|
||||
fileName: item?.originalFilename,
|
||||
unitPrice: Number(item?.unitPriceChf || 0),
|
||||
unitTime: Number(item?.printTimeSeconds || 0),
|
||||
unitWeight: Number(item?.materialGrams || 0),
|
||||
quantity: Number(item?.quantity || 1),
|
||||
material: item?.materialCode || session?.materialCode,
|
||||
quality: item?.quality,
|
||||
color: item?.colorCode,
|
||||
filamentVariantId: item?.filamentVariantId,
|
||||
supportEnabled: Boolean(item?.supportsEnabled),
|
||||
infillDensity:
|
||||
item?.infillPercent != null ? Number(item.infillPercent) : undefined,
|
||||
infillPattern: item?.infillPattern,
|
||||
layerHeight:
|
||||
item?.layerHeightMm != null ? Number(item.layerHeightMm) : undefined,
|
||||
nozzleDiameter:
|
||||
item?.nozzleDiameterMm != null
|
||||
? Number(item.nozzleDiameterMm)
|
||||
: undefined,
|
||||
})),
|
||||
setupCost: session.setupCostChf || 0,
|
||||
globalMachineCost: sessionData.globalMachineCostChf || 0,
|
||||
cadHours: session.cadHours || 0,
|
||||
cadTotal: sessionData.cadTotalChf || 0,
|
||||
currency: 'CHF', // Fixed for now
|
||||
totalPrice:
|
||||
(sessionData.itemsTotalChf || 0) +
|
||||
(session.setupCostChf || 0) +
|
||||
(sessionData.shippingCostChf || 0),
|
||||
setupCost: Number(session?.setupCostChf || 0),
|
||||
globalMachineCost: Number(sessionData?.globalMachineCostChf || 0),
|
||||
cadHours: Number(session?.cadHours || 0),
|
||||
cadTotal: Number(sessionData?.cadTotalChf || 0),
|
||||
currency: 'CHF',
|
||||
totalPrice: Number.isFinite(grandTotal) ? grandTotal : fallbackTotal,
|
||||
totalTimeHours: Math.floor(totalTime / 3600),
|
||||
totalTimeMinutes: Math.ceil((totalTime % 3600) / 60),
|
||||
totalWeight: Math.ceil(totalWeight),
|
||||
notes: session.notes,
|
||||
notes: session?.notes,
|
||||
};
|
||||
}
|
||||
|
||||
private buildSettingsPayload(request: QuoteRequest, item: QuoteRequestItem): any {
|
||||
const normalizedQuality = this.normalizeQuality(item.quality || request.quality);
|
||||
const easyPreset =
|
||||
request.mode === 'easy'
|
||||
? this.buildEasyModePreset(normalizedQuality)
|
||||
: null;
|
||||
|
||||
return {
|
||||
complexityMode: request.mode === 'easy' ? 'BASIC' : 'ADVANCED',
|
||||
material: String(item.material || request.material || 'PLA'),
|
||||
color: item.color || '#FFFFFF',
|
||||
filamentVariantId: item.filamentVariantId,
|
||||
quality: easyPreset ? easyPreset.quality : normalizedQuality,
|
||||
supportsEnabled: item.supportEnabled ?? request.supportEnabled ?? false,
|
||||
layerHeight:
|
||||
easyPreset?.layerHeight ?? item.layerHeight ?? request.layerHeight ?? 0.2,
|
||||
infillDensity:
|
||||
easyPreset?.infillDensity ??
|
||||
item.infillDensity ??
|
||||
request.infillDensity ??
|
||||
20,
|
||||
infillPattern:
|
||||
easyPreset?.infillPattern ??
|
||||
item.infillPattern ??
|
||||
request.infillPattern ??
|
||||
'grid',
|
||||
nozzleDiameter:
|
||||
easyPreset?.nozzleDiameter ??
|
||||
item.nozzleDiameter ??
|
||||
request.nozzleDiameter ??
|
||||
0.4,
|
||||
};
|
||||
}
|
||||
|
||||
private normalizeQuality(value: string | undefined): string {
|
||||
const normalized = String(value || 'standard').trim().toLowerCase();
|
||||
if (normalized === 'high' || normalized === 'high_definition') {
|
||||
return 'extra_fine';
|
||||
}
|
||||
return normalized || 'standard';
|
||||
}
|
||||
|
||||
private buildEasyModePreset(quality: string): {
|
||||
quality: string;
|
||||
layerHeight: number;
|
||||
infillDensity: number;
|
||||
infillPattern: string;
|
||||
nozzleDiameter: number;
|
||||
} {
|
||||
const normalized = this.normalizeQuality(quality);
|
||||
|
||||
if (normalized === 'draft') {
|
||||
return {
|
||||
quality: 'draft',
|
||||
layerHeight: 0.28,
|
||||
infillDensity: 15,
|
||||
infillPattern: 'grid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
|
||||
if (normalized === 'extra_fine') {
|
||||
return {
|
||||
quality: 'extra_fine',
|
||||
layerHeight: 0.12,
|
||||
infillDensity: 20,
|
||||
infillPattern: 'gyroid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
quality: 'standard',
|
||||
layerHeight: 0.2,
|
||||
infillDensity: 15,
|
||||
infillPattern: 'grid',
|
||||
nozzleDiameter: 0.4,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,6 +245,10 @@
|
||||
<span
|
||||
>{{ "CHECKOUT.QTY" | translate }}: {{ item.quantity }}</span
|
||||
>
|
||||
<span>
|
||||
{{ "CHECKOUT.MATERIAL" | translate }}:
|
||||
{{ itemMaterial(item) }}
|
||||
</span>
|
||||
<span
|
||||
*ngIf="item.colorCode"
|
||||
class="color-dot"
|
||||
@@ -255,6 +259,41 @@
|
||||
{{ item.printTimeSeconds / 3600 | number: "1.1-1" }}h |
|
||||
{{ item.materialGrams | number: "1.0-0" }}g
|
||||
</div>
|
||||
<div class="item-preview" *ngIf="isCadSession() && isStlItem(item)">
|
||||
<ng-container
|
||||
*ngIf="previewFile(item) as itemPreview; else previewState"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="preview-trigger"
|
||||
(click)="openPreview(item)"
|
||||
[attr.aria-label]="'CHECKOUT.PREVIEW_OPEN' | translate"
|
||||
>
|
||||
<div class="preview-surface">
|
||||
<app-stl-viewer
|
||||
[file]="itemPreview"
|
||||
[height]="116"
|
||||
[color]="previewColor(item)"
|
||||
[borderRadius]="'var(--radius-lg)'"
|
||||
></app-stl-viewer>
|
||||
<span class="preview-pill">{{
|
||||
"CHECKOUT.PREVIEW_OPEN" | translate
|
||||
}}</span>
|
||||
</div>
|
||||
</button>
|
||||
</ng-container>
|
||||
<ng-template #previewState>
|
||||
<div class="preview-state" *ngIf="isPreviewLoading(item)">
|
||||
{{ "CHECKOUT.PREVIEW_LOADING" | translate }}
|
||||
</div>
|
||||
<div
|
||||
class="preview-state preview-state-error"
|
||||
*ngIf="!isPreviewLoading(item) && hasPreviewError(item)"
|
||||
>
|
||||
{{ "CHECKOUT.PREVIEW_UNAVAILABLE" | translate }}
|
||||
</div>
|
||||
</ng-template>
|
||||
</div>
|
||||
</div>
|
||||
<div class="item-price">
|
||||
<span class="item-total-price">
|
||||
@@ -302,3 +341,30 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="preview-modal-backdrop"
|
||||
*ngIf="previewModalOpen()"
|
||||
(click)="closePreview()"
|
||||
>
|
||||
<div class="preview-modal" (click)="$event.stopPropagation()">
|
||||
<div class="preview-modal-header">
|
||||
<h4>{{ selectedPreviewName() }}</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="preview-modal-close"
|
||||
(click)="closePreview()"
|
||||
[attr.aria-label]="'CHECKOUT.PREVIEW_CLOSE' | translate"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
<app-stl-viewer
|
||||
*ngIf="selectedPreviewFile() as preview"
|
||||
[file]="preview"
|
||||
[height]="460"
|
||||
[color]="selectedPreviewColor()"
|
||||
[borderRadius]="'var(--radius-lg)'"
|
||||
></app-stl-viewer>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -244,6 +244,77 @@ app-toggle-selector.user-type-selector-compact {
|
||||
color: var(--color-text-muted);
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.item-preview {
|
||||
margin-top: var(--space-3);
|
||||
|
||||
.preview-trigger {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
text-align: center;
|
||||
|
||||
.preview-surface {
|
||||
position: relative;
|
||||
width: min(320px, 100%);
|
||||
margin-inline: auto;
|
||||
border: 2px solid var(--color-border);
|
||||
border-radius: var(--radius-lg);
|
||||
overflow: hidden;
|
||||
transition:
|
||||
transform 0.18s ease,
|
||||
box-shadow 0.18s ease,
|
||||
border-color 0.18s ease;
|
||||
}
|
||||
|
||||
.preview-pill {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
background: rgba(17, 24, 39, 0.84);
|
||||
color: #fff;
|
||||
border-radius: 999px;
|
||||
font-size: 0.72rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.02em;
|
||||
padding: 4px 10px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
&:hover .preview-surface,
|
||||
&:focus-visible .preview-surface {
|
||||
border-color: var(--color-brand);
|
||||
box-shadow: 0 8px 18px rgba(0, 0, 0, 0.14);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
.preview-state {
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-lg);
|
||||
background: var(--color-neutral-50);
|
||||
color: var(--color-text-muted);
|
||||
font-size: 0.8rem;
|
||||
min-height: 74px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
padding: var(--space-2);
|
||||
}
|
||||
|
||||
.preview-state-error {
|
||||
color: var(--color-danger-600, #dc2626);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.item-price {
|
||||
@@ -316,6 +387,53 @@ app-toggle-selector.user-type-selector-compact {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.preview-modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(12, 16, 22, 0.72);
|
||||
z-index: 1200;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: var(--space-4);
|
||||
}
|
||||
|
||||
.preview-modal {
|
||||
width: min(820px, 96vw);
|
||||
border-radius: var(--radius-lg);
|
||||
border: 1px solid var(--color-border);
|
||||
background: var(--color-bg-card);
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
|
||||
padding: var(--space-4);
|
||||
}
|
||||
|
||||
.preview-modal-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-3);
|
||||
margin-bottom: var(--space-3);
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
line-height: 1.2;
|
||||
word-break: break-word;
|
||||
}
|
||||
}
|
||||
|
||||
.preview-modal-close {
|
||||
border: 1px solid var(--color-border);
|
||||
background: var(--color-neutral-50);
|
||||
color: var(--color-text);
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-size: 1.1rem;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.mb-6 {
|
||||
margin-bottom: var(--space-6);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
ToggleOption,
|
||||
} from '../../shared/components/app-toggle-selector/app-toggle-selector.component';
|
||||
import { LanguageService } from '../../core/services/language.service';
|
||||
import { StlViewerComponent } from '../../shared/components/stl-viewer/stl-viewer.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-checkout',
|
||||
@@ -29,6 +30,7 @@ import { LanguageService } from '../../core/services/language.service';
|
||||
AppButtonComponent,
|
||||
AppCardComponent,
|
||||
AppToggleSelectorComponent,
|
||||
StlViewerComponent,
|
||||
],
|
||||
templateUrl: './checkout.component.html',
|
||||
styleUrls: ['./checkout.component.scss'],
|
||||
@@ -46,6 +48,13 @@ export class CheckoutComponent implements OnInit {
|
||||
error: string | null = null;
|
||||
isSubmitting = signal(false); // Add signal for submit state
|
||||
quoteSession = signal<any>(null); // Add signal for session details
|
||||
previewFiles = signal<Record<string, File>>({});
|
||||
previewLoading = signal<Record<string, boolean>>({});
|
||||
previewErrors = signal<Record<string, boolean>>({});
|
||||
previewModalOpen = signal(false);
|
||||
selectedPreviewFile = signal<File | null>(null);
|
||||
selectedPreviewName = signal('');
|
||||
selectedPreviewColor = signal('#c9ced6');
|
||||
|
||||
userTypeOptions: ToggleOption[] = [
|
||||
{ label: 'CONTACT.TYPE_PRIVATE', value: 'PRIVATE' },
|
||||
@@ -153,6 +162,11 @@ export class CheckoutComponent implements OnInit {
|
||||
this.quoteService.getQuoteSession(this.sessionId).subscribe({
|
||||
next: (session) => {
|
||||
this.quoteSession.set(session);
|
||||
if (this.isCadSessionData(session)) {
|
||||
this.loadStlPreviews(session);
|
||||
} else {
|
||||
this.resetPreviewState();
|
||||
}
|
||||
console.log('Loaded session:', session);
|
||||
},
|
||||
error: (err) => {
|
||||
@@ -163,7 +177,7 @@ export class CheckoutComponent implements OnInit {
|
||||
}
|
||||
|
||||
isCadSession(): boolean {
|
||||
return this.quoteSession()?.session?.status === 'CAD_ACTIVE';
|
||||
return this.isCadSessionData(this.quoteSession());
|
||||
}
|
||||
|
||||
cadRequestId(): string | null {
|
||||
@@ -178,6 +192,115 @@ export class CheckoutComponent implements OnInit {
|
||||
return this.quoteSession()?.cadTotalChf ?? 0;
|
||||
}
|
||||
|
||||
itemMaterial(item: any): string {
|
||||
return String(
|
||||
item?.materialCode ?? this.quoteSession()?.session?.materialCode ?? '-',
|
||||
);
|
||||
}
|
||||
|
||||
isStlItem(item: any): boolean {
|
||||
const name = String(item?.originalFilename ?? '').toLowerCase();
|
||||
return name.endsWith('.stl');
|
||||
}
|
||||
|
||||
previewFile(item: any): File | null {
|
||||
const id = String(item?.id ?? '');
|
||||
if (!id) {
|
||||
return null;
|
||||
}
|
||||
return this.previewFiles()[id] ?? null;
|
||||
}
|
||||
|
||||
previewColor(item: any): string {
|
||||
const raw = String(item?.colorCode ?? '').trim();
|
||||
return raw || '#c9ced6';
|
||||
}
|
||||
|
||||
isPreviewLoading(item: any): boolean {
|
||||
const id = String(item?.id ?? '');
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
return !!this.previewLoading()[id];
|
||||
}
|
||||
|
||||
hasPreviewError(item: any): boolean {
|
||||
const id = String(item?.id ?? '');
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
return !!this.previewErrors()[id];
|
||||
}
|
||||
|
||||
openPreview(item: any): void {
|
||||
const file = this.previewFile(item);
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
this.selectedPreviewFile.set(file);
|
||||
this.selectedPreviewName.set(String(item?.originalFilename ?? file.name));
|
||||
this.selectedPreviewColor.set(this.previewColor(item));
|
||||
this.previewModalOpen.set(true);
|
||||
}
|
||||
|
||||
closePreview(): void {
|
||||
this.previewModalOpen.set(false);
|
||||
this.selectedPreviewFile.set(null);
|
||||
this.selectedPreviewName.set('');
|
||||
this.selectedPreviewColor.set('#c9ced6');
|
||||
}
|
||||
|
||||
private loadStlPreviews(session: any): void {
|
||||
if (
|
||||
!this.sessionId ||
|
||||
!this.isCadSessionData(session) ||
|
||||
!Array.isArray(session?.items)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const item of session.items) {
|
||||
if (!this.isStlItem(item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const id = String(item?.id ?? '');
|
||||
if (!id || this.previewFiles()[id] || this.previewLoading()[id]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.previewLoading.update((prev) => ({ ...prev, [id]: true }));
|
||||
this.previewErrors.update((prev) => ({ ...prev, [id]: false }));
|
||||
|
||||
this.quoteService.getLineItemStlPreview(this.sessionId, id).subscribe({
|
||||
next: (blob) => {
|
||||
const originalName = String(item?.originalFilename ?? `${id}.stl`);
|
||||
const stlName = originalName.toLowerCase().endsWith('.stl')
|
||||
? originalName
|
||||
: `${originalName}.stl`;
|
||||
const previewFile = new File([blob], stlName, { type: 'model/stl' });
|
||||
this.previewFiles.update((prev) => ({ ...prev, [id]: previewFile }));
|
||||
this.previewLoading.update((prev) => ({ ...prev, [id]: false }));
|
||||
},
|
||||
error: () => {
|
||||
this.previewErrors.update((prev) => ({ ...prev, [id]: true }));
|
||||
this.previewLoading.update((prev) => ({ ...prev, [id]: false }));
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private isCadSessionData(session: any): boolean {
|
||||
return session?.session?.status === 'CAD_ACTIVE';
|
||||
}
|
||||
|
||||
private resetPreviewState(): void {
|
||||
this.previewFiles.set({});
|
||||
this.previewLoading.set({});
|
||||
this.previewErrors.set({});
|
||||
this.closePreview();
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
if (this.checkoutForm.invalid) {
|
||||
return;
|
||||
|
||||
@@ -5,5 +5,10 @@ export const CONTACT_ROUTES: Routes = [
|
||||
path: '',
|
||||
loadComponent: () =>
|
||||
import('./contact-page.component').then((m) => m.ContactPageComponent),
|
||||
data: {
|
||||
seoTitle: 'Contatti | 3D fab',
|
||||
seoDescription:
|
||||
'Richiedi informazioni, preventivi personalizzati o supporto per progetti di stampa 3D.',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -37,28 +37,40 @@
|
||||
<div class="cap-cards">
|
||||
<app-card>
|
||||
<div class="card-image-placeholder">
|
||||
<img src="assets/images/home/prototipi.jpg" alt="" />
|
||||
<img
|
||||
src="assets/images/home/prototipi.jpg"
|
||||
[attr.alt]="'HOME.CAP_1_TITLE' | translate"
|
||||
/>
|
||||
</div>
|
||||
<h3>{{ "HOME.CAP_1_TITLE" | translate }}</h3>
|
||||
<p class="text-muted">{{ "HOME.CAP_1_TEXT" | translate }}</p>
|
||||
</app-card>
|
||||
<app-card>
|
||||
<div class="card-image-placeholder">
|
||||
<img src="assets/images/home/original-vs-3dprinted.jpg" alt="" />
|
||||
<img
|
||||
src="assets/images/home/original-vs-3dprinted.jpg"
|
||||
[attr.alt]="'HOME.CAP_2_TITLE' | translate"
|
||||
/>
|
||||
</div>
|
||||
<h3>{{ "HOME.CAP_2_TITLE" | translate }}</h3>
|
||||
<p class="text-muted">{{ "HOME.CAP_2_TEXT" | translate }}</p>
|
||||
</app-card>
|
||||
<app-card>
|
||||
<div class="card-image-placeholder">
|
||||
<img src="assets/images/home/serie.jpg" alt="" />
|
||||
<img
|
||||
src="assets/images/home/serie.jpg"
|
||||
[attr.alt]="'HOME.CAP_3_TITLE' | translate"
|
||||
/>
|
||||
</div>
|
||||
<h3>{{ "HOME.CAP_3_TITLE" | translate }}</h3>
|
||||
<p class="text-muted">{{ "HOME.CAP_3_TEXT" | translate }}</p>
|
||||
</app-card>
|
||||
<app-card>
|
||||
<div class="card-image-placeholder">
|
||||
<img src="assets/images/home/cad.jpg" alt="" />
|
||||
<img
|
||||
src="assets/images/home/cad.jpg"
|
||||
[attr.alt]="'HOME.CAP_4_TITLE' | translate"
|
||||
/>
|
||||
</div>
|
||||
<h3>{{ "HOME.CAP_4_TITLE" | translate }}</h3>
|
||||
<p class="text-muted">{{ "HOME.CAP_4_TEXT" | translate }}</p>
|
||||
|
||||
@@ -5,10 +5,20 @@ export const LEGAL_ROUTES: Routes = [
|
||||
path: 'privacy',
|
||||
loadComponent: () =>
|
||||
import('./privacy/privacy.component').then((m) => m.PrivacyComponent),
|
||||
data: {
|
||||
seoTitle: 'Privacy Policy | 3D fab',
|
||||
seoDescription:
|
||||
'Informativa privacy di 3D fab: trattamento dati, finalita e contatti.',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'terms',
|
||||
loadComponent: () =>
|
||||
import('./terms/terms.component').then((m) => m.TermsComponent),
|
||||
data: {
|
||||
seoTitle: 'Termini e condizioni | 3D fab',
|
||||
seoDescription:
|
||||
'Termini e condizioni del servizio di stampa 3D e del calcolatore preventivi.',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -3,6 +3,22 @@ import { ShopPageComponent } from './shop-page.component';
|
||||
import { ProductDetailComponent } from './product-detail.component';
|
||||
|
||||
export const SHOP_ROUTES: Routes = [
|
||||
{ path: '', component: ShopPageComponent },
|
||||
{ path: ':id', component: ProductDetailComponent },
|
||||
{
|
||||
path: '',
|
||||
component: ShopPageComponent,
|
||||
data: {
|
||||
seoTitle: 'Shop 3D fab',
|
||||
seoDescription:
|
||||
'Lo shop 3D fab e in allestimento. Intanto puoi usare il calcolatore per ottenere un preventivo.',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: ':id',
|
||||
component: ProductDetailComponent,
|
||||
data: {
|
||||
seoTitle: 'Prodotto | 3D fab',
|
||||
seoRobots: 'noindex, nofollow',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
<div class="viewer-container" #rendererContainer>
|
||||
<div
|
||||
class="viewer-container"
|
||||
#rendererContainer
|
||||
[style.height.px]="height"
|
||||
[style.border-radius]="borderRadius"
|
||||
>
|
||||
@if (loading) {
|
||||
<div class="loading-overlay">
|
||||
<div class="spinner"></div>
|
||||
|
||||
@@ -26,6 +26,8 @@ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
|
||||
export class StlViewerComponent implements OnInit, OnDestroy, OnChanges {
|
||||
@Input() file: File | null = null;
|
||||
@Input() color: string = '#facf0a'; // Default Brand Color
|
||||
@Input() height = 300;
|
||||
@Input() borderRadius = 'var(--radius-lg)';
|
||||
|
||||
@ViewChild('rendererContainer', { static: true })
|
||||
rendererContainer!: ElementRef;
|
||||
@@ -176,7 +178,7 @@ export class StlViewerComponent implements OnInit, OnDestroy, OnChanges {
|
||||
|
||||
this.scene.add(this.currentMesh);
|
||||
|
||||
// Adjust camera to fit object
|
||||
// Adjust camera to fit object and keep it visually centered
|
||||
const maxDim = Math.max(size.x, size.y, size.z);
|
||||
const fov = this.camera.fov * (Math.PI / 180);
|
||||
|
||||
@@ -184,7 +186,12 @@ export class StlViewerComponent implements OnInit, OnDestroy, OnChanges {
|
||||
let cameraZ = Math.abs(maxDim / 2 / Math.tan(fov / 2));
|
||||
cameraZ *= 1.72;
|
||||
|
||||
this.camera.position.set(cameraZ * 0.65, cameraZ * 0.95, cameraZ * 1.1);
|
||||
this.camera.position.set(
|
||||
cameraZ * 0.68,
|
||||
cameraZ * 0.62,
|
||||
cameraZ * 1.08,
|
||||
);
|
||||
this.controls.target.set(0, 0, 0);
|
||||
this.camera.lookAt(0, 0, 0);
|
||||
this.camera.updateProjectionMatrix();
|
||||
this.controls.update();
|
||||
|
||||
@@ -402,8 +402,14 @@
|
||||
"SETUP_FEE": "Einrichtungskosten",
|
||||
"TOTAL": "Gesamt",
|
||||
"QTY": "Menge",
|
||||
"MATERIAL": "Material",
|
||||
"PER_PIECE": "pro Stück",
|
||||
"SHIPPING": "Versand (CH)",
|
||||
"PREVIEW_LOADING": "3D-Vorschau wird geladen...",
|
||||
"PREVIEW_UNAVAILABLE": "Vorschau nicht verfügbar",
|
||||
"PREVIEW_OPEN": "3D öffnen",
|
||||
"PREVIEW_ENLARGE": "Klicken Sie auf die Vorschau, um das 3D-Modell zu öffnen",
|
||||
"PREVIEW_CLOSE": "Vorschau schließen",
|
||||
"ERR_NO_SESSION_START": "Keine aktive Sitzung gefunden. Bitte starten Sie ein neues Angebot.",
|
||||
"ERR_LOAD_SESSION": "Sitzungsdetails konnten nicht geladen werden. Bitte erneut versuchen.",
|
||||
"ERR_NO_SESSION_CREATE_ORDER": "Keine aktive Sitzung gefunden. Bestellung kann nicht erstellt werden.",
|
||||
|
||||
@@ -402,8 +402,14 @@
|
||||
"SETUP_FEE": "Setup Fee",
|
||||
"TOTAL": "Total",
|
||||
"QTY": "Qty",
|
||||
"MATERIAL": "Material",
|
||||
"PER_PIECE": "per piece",
|
||||
"SHIPPING": "Shipping",
|
||||
"PREVIEW_LOADING": "Loading 3D preview...",
|
||||
"PREVIEW_UNAVAILABLE": "Preview not available",
|
||||
"PREVIEW_OPEN": "Open 3D",
|
||||
"PREVIEW_ENLARGE": "Click the preview to open the 3D model",
|
||||
"PREVIEW_CLOSE": "Close preview",
|
||||
"ERR_NO_SESSION_START": "No active session found. Please start a new quote.",
|
||||
"ERR_LOAD_SESSION": "Failed to load session details. Please try again.",
|
||||
"ERR_NO_SESSION_CREATE_ORDER": "No active session found. Cannot create order.",
|
||||
|
||||
@@ -459,8 +459,14 @@
|
||||
"SETUP_FEE": "Coût de setup",
|
||||
"TOTAL": "Total",
|
||||
"QTY": "Qté",
|
||||
"MATERIAL": "Matériau",
|
||||
"PER_PIECE": "par pièce",
|
||||
"SHIPPING": "Expédition (CH)",
|
||||
"PREVIEW_LOADING": "Chargement de l'aperçu 3D...",
|
||||
"PREVIEW_UNAVAILABLE": "Aperçu non disponible",
|
||||
"PREVIEW_OPEN": "Ouvrir 3D",
|
||||
"PREVIEW_ENLARGE": "Cliquez sur l'aperçu pour ouvrir le modèle 3D",
|
||||
"PREVIEW_CLOSE": "Fermer l'aperçu",
|
||||
"INVALID_EMAIL": "E-mail invalide",
|
||||
"COMPANY_OPTIONAL": "Nom de l'entreprise (Optionnel)",
|
||||
"REF_PERSON_OPTIONAL": "Personne de référence (Optionnel)",
|
||||
|
||||
@@ -459,8 +459,14 @@
|
||||
"SETUP_FEE": "Costo di Avvio",
|
||||
"TOTAL": "Totale",
|
||||
"QTY": "Qtà",
|
||||
"MATERIAL": "Materiale",
|
||||
"PER_PIECE": "al pezzo",
|
||||
"SHIPPING": "Spedizione (CH)",
|
||||
"PREVIEW_LOADING": "Caricamento anteprima 3D...",
|
||||
"PREVIEW_UNAVAILABLE": "Anteprima non disponibile",
|
||||
"PREVIEW_OPEN": "Apri 3D",
|
||||
"PREVIEW_ENLARGE": "Clicca sull'anteprima per aprire il modello 3D",
|
||||
"PREVIEW_CLOSE": "Chiudi anteprima",
|
||||
"INVALID_EMAIL": "Email non valida",
|
||||
"COMPANY_OPTIONAL": "Nome Azienda (Opzionale)",
|
||||
"REF_PERSON_OPTIONAL": "Persona di Riferimento (Opzionale)",
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="robots" content="noindex, nofollow" />
|
||||
<title>3D fab</title>
|
||||
<title>3D fab | Stampa 3D su misura</title>
|
||||
<meta
|
||||
name="description"
|
||||
content="Stampa 3D su misura con preventivo online immediato. Carica il file, scegli materiale e qualità, ricevi prezzo e tempi in pochi secondi."
|
||||
/>
|
||||
<meta name="robots" content="index, follow" />
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/png" href="assets/images/Fav-icon.png" />
|
||||
|
||||
123
security_best_practices_report.md
Normal file
123
security_best_practices_report.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# Security Best Practices Report
|
||||
|
||||
## Executive summary
|
||||
|
||||
Revisione sicurezza del progetto `print-calculator` (backend Spring Boot Java + frontend Angular/TypeScript) con focus su autenticazione/autorizzazione, esposizione dati, upload file, hardening e resilienza.
|
||||
|
||||
Risultato: **6 finding** totali.
|
||||
|
||||
- **Critical**: 1
|
||||
- **High**: 3
|
||||
- **Medium**: 2
|
||||
|
||||
Rischio principale: API pubbliche basate su UUID senza controllo di ownership/token, che consentono lettura PII e azioni di business su ordini.
|
||||
|
||||
## Scope e metodo
|
||||
|
||||
- Codice analizzato: backend (`backend/src/main/java`, `backend/src/main/resources`), frontend (`frontend/src/app`), config deploy (`deploy/`, `docker-compose*.yml`).
|
||||
- Riferimenti skill usati: `javascript-general-web-frontend-security.md`.
|
||||
- Nota: non è presente un riferimento specifico Java/Spring nel set dello skill; per il backend sono state applicate best practice consolidate Spring/security engineering.
|
||||
|
||||
## Critical findings
|
||||
|
||||
### SBP-001 - Broken access control su API ordine pubbliche (lettura PII + azioni stato)
|
||||
|
||||
- **Severity**: Critical
|
||||
- **Impatto**: Chiunque ottenga un `orderId` può leggere dati personali ordine e invocare operazioni di business senza autenticazione.
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/java/com/printcalculator/config/SecurityConfig.java:36` (`.anyRequest().permitAll()`).
|
||||
- `backend/src/main/java/com/printcalculator/controller/OrderController.java:131` (`GET /api/orders/{orderId}`).
|
||||
- `backend/src/main/java/com/printcalculator/controller/OrderController.java:141` (`POST /api/orders/{orderId}/payments/report`).
|
||||
- `backend/src/main/java/com/printcalculator/controller/OrderController.java:93` (`POST /api/orders/{orderId}/items/{orderItemId}/file`).
|
||||
- `backend/src/main/java/com/printcalculator/controller/OrderController.java:295`-`337` (PII completa nel DTO: email, telefono, indirizzi billing/shipping).
|
||||
- `backend/src/main/java/com/printcalculator/service/PaymentService.java:53`-`75` (cambio stato pagamento a `REPORTED`).
|
||||
- **Rischio tecnico**:
|
||||
- Assenza di autenticazione/authorization applicativa su endpoint ordine.
|
||||
- Modello “capability by UUID” senza token secondario, expiry o binding utente.
|
||||
- **Fix raccomandato**:
|
||||
- Introdurre un `order_access_token` random ad alta entropia (>=128 bit), memorizzato hashato e richiesto sugli endpoint pubblici ordine.
|
||||
- Separare endpoint pubblici (minimo set dati) da endpoint interni/admin.
|
||||
- Rimuovere `orderItemId` e dettagli sensibili dal DTO pubblico, o usare URL firmate a scadenza per upload/download.
|
||||
- Valutare auth customer leggera (magic link OTP) per consultazione/modifica ordine.
|
||||
|
||||
## High findings
|
||||
|
||||
### SBP-002 - Esposizione PII su endpoint pubblico custom quote request
|
||||
|
||||
- **Severity**: High
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/java/com/printcalculator/controller/CustomQuoteRequestController.java:188`-`193` (`GET /api/custom-quote-requests/{id}` senza auth).
|
||||
- `backend/src/main/java/com/printcalculator/entity/CustomQuoteRequest.java:24`-`40` (campi PII e messaggio cliente).
|
||||
- `backend/src/main/java/com/printcalculator/config/SecurityConfig.java:36` (`.anyRequest().permitAll()`).
|
||||
- **Rischio tecnico**:
|
||||
- Endpoint “lookup by UUID” ritorna oggetto completo con dati personali.
|
||||
- **Fix raccomandato**:
|
||||
- Proteggere endpoint con token di accesso separato per richiesta (non solo UUID).
|
||||
- Restituire una vista redatta/minimale per endpoint pubblici.
|
||||
- Se endpoint non usato dal frontend, rimuoverlo.
|
||||
|
||||
### SBP-003 - Antivirus in fail-open + default scanner disattivato
|
||||
|
||||
- **Severity**: High
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/resources/application.properties:27` (`clamav.enabled=${CLAMAV_ENABLED:false}`).
|
||||
- `backend/src/main/java/com/printcalculator/service/ClamAVService.java:42`-`43` (scanner disabilitato => ritorna `true`).
|
||||
- `backend/src/main/java/com/printcalculator/service/ClamAVService.java:54`-`61` (errori scanner => `FAIL-OPEN`).
|
||||
- `backend/src/main/java/com/printcalculator/service/FileSystemStorageService.java:59`-`60` (eccezioni scanner ignorate, file mantenuto).
|
||||
- **Rischio tecnico**:
|
||||
- File malevoli possono essere accettati quando scanner è down/non configurato.
|
||||
- **Fix raccomandato**:
|
||||
- Policy fail-closed in ambienti non-dev (`reject on scan error`).
|
||||
- Rendere `CLAMAV_ENABLED=true` default in deploy runtime e bloccare startup se scanner richiesto ma non raggiungibile.
|
||||
- Telemetria/alerting su scan bypass e failure rate.
|
||||
|
||||
### SBP-004 - Endpoint costosi esposti senza throttling/rate limit (DoS applicativo)
|
||||
|
||||
- **Severity**: High
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/java/com/printcalculator/config/SecurityConfig.java:36` (endpoint pubblici permessi globalmente).
|
||||
- `backend/src/main/java/com/printcalculator/controller/QuoteController.java:38`-`39` (`POST /api/quote` pubblico).
|
||||
- `backend/src/main/java/com/printcalculator/controller/QuoteSessionController.java:114`-`120` (`POST /api/quote-sessions/{id}/line-items` pubblico).
|
||||
- `backend/src/main/java/com/printcalculator/controller/QuoteSessionController.java:228`-`235` (invocazione slicing).
|
||||
- `backend/src/main/java/com/printcalculator/service/SlicerService.java:156`-`163` (job fino a 5 minuti).
|
||||
- **Rischio tecnico**:
|
||||
- Upload/slicing massivo può saturare CPU, I/O e worker thread.
|
||||
- **Fix raccomandato**:
|
||||
- Rate limiting per IP/fingerprint/session (anche lato reverse proxy).
|
||||
- Coda asincrona con limiti di concorrenza e timeout più stretti.
|
||||
- Quote per utente/sessione e limite richieste per finestra temporale.
|
||||
- CAPTCHA o proof-of-work per endpoint anonimi ad alto costo.
|
||||
|
||||
## Medium findings
|
||||
|
||||
### SBP-005 - Secret/default credenziali deboli nel codice di configurazione
|
||||
|
||||
- **Severity**: Medium
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/resources/application.properties:7` (`DB_PASSWORD` fallback `printcalc_secret`).
|
||||
- `backend/src/main/resources/application-local.properties:7`-`8` (admin password/secret hardcoded per profilo local).
|
||||
- **Rischio tecnico**:
|
||||
- In caso di misconfigurazione ambientale o uso improprio profilo, vengono usati valori prevedibili.
|
||||
- **Fix raccomandato**:
|
||||
- Rimuovere fallback sensibili e rendere obbligatori i secret a startup.
|
||||
- Spostare credenziali locali in file non versionato (`.env.local`, `.gitignore`) con template placeholder.
|
||||
- Policy di secret rotation periodica.
|
||||
|
||||
### SBP-006 - CSRF disabilitato globalmente con autenticazione admin basata su cookie
|
||||
|
||||
- **Severity**: Medium
|
||||
- **Evidenze**:
|
||||
- `backend/src/main/java/com/printcalculator/config/SecurityConfig.java:24` (CSRF disabilitato globalmente).
|
||||
- `backend/src/main/java/com/printcalculator/security/AdminSessionService.java:129`-`136` (cookie sessione admin).
|
||||
- `backend/src/main/java/com/printcalculator/security/AdminSessionService.java:133`-`134` (`Secure` + `SameSite=Strict` presenti, mitigazione parziale).
|
||||
- **Rischio tecnico**:
|
||||
- Con auth cookie-based, la protezione CSRF andrebbe mantenuta sugli endpoint state-changing admin; `SameSite=Strict` riduce ma non elimina tutti i vettori.
|
||||
- **Fix raccomandato**:
|
||||
- Riabilitare CSRF almeno su `/api/admin/**` e usare token CSRF (double-submit o synchronizer token).
|
||||
- Mantenere `SameSite=Strict` come difesa aggiuntiva.
|
||||
|
||||
## Note e assunzioni
|
||||
|
||||
- Alcuni endpoint pubblici sembrano progettati come flusso anonimo customer; il finding resta valido perché manca una seconda prova di possesso oltre all’UUID.
|
||||
- Non è stata eseguita una DAST esterna o pentest black-box; analisi effettuata su codice statico e configurazioni nel repository.
|
||||
|
||||
Reference in New Issue
Block a user