Vendor Order Stacking made functional.
This commit is contained in:
@@ -183,36 +183,74 @@ public class ProductController {
|
||||
public ResponseEntity<Product> publishProduct(@PathVariable Long id, @RequestBody Product productDetails) {
|
||||
return productRepository.findById(id)
|
||||
.map(product -> {
|
||||
// Generate new PRD ID
|
||||
String newProductId = "PRD-" + String.format("%04d", new java.util.Random().nextInt(10000));
|
||||
String finalProductId = productDetails.getProductId();
|
||||
|
||||
// Update all details first
|
||||
product.setName(productDetails.getName());
|
||||
product.setProductId(newProductId);
|
||||
product.setCategory(productDetails.getCategory());
|
||||
product.setDescription(productDetails.getDescription());
|
||||
product.setBasePrice(productDetails.getBasePrice());
|
||||
product.setPrice(productDetails.getPrice());
|
||||
product.setOfferPrice(productDetails.getOfferPrice());
|
||||
product.setCounter(productDetails.getCounter());
|
||||
product.setTag(productDetails.getTag());
|
||||
product.setBarcode(productDetails.getBarcode());
|
||||
product.setImageData(productDetails.getImageData());
|
||||
product.setStock(productDetails.getStock());
|
||||
|
||||
product.getSessions().clear();
|
||||
if (productDetails.getSessions() != null) {
|
||||
product.getSessions().addAll(productDetails.getSessions());
|
||||
// If it's a draft ID or blank, try to find an existing live product by name first
|
||||
if (finalProductId == null || finalProductId.startsWith("DRAFT-") || finalProductId.isEmpty()) {
|
||||
List<Product> nameMatches = productRepository.findByNameRobust(productDetails.getName());
|
||||
if (!nameMatches.isEmpty()) {
|
||||
finalProductId = nameMatches.get(0).getProductId();
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize
|
||||
product.setDraft(false);
|
||||
product.setActive(true);
|
||||
|
||||
Product updated = productRepository.save(product);
|
||||
updateStallAssociations(updated, productDetails.getStalls());
|
||||
|
||||
return ResponseEntity.ok(productRepository.findById(updated.getId()).orElse(updated));
|
||||
// If still a draft ID or blank, generate a new PRD ID
|
||||
if (finalProductId == null || finalProductId.startsWith("DRAFT-") || finalProductId.isEmpty()) {
|
||||
finalProductId = "PRD-" + String.format("%04d", new java.util.Random().nextInt(10000));
|
||||
}
|
||||
|
||||
// Check if a live product with this ID already exists (to merge)
|
||||
final String targetId = finalProductId;
|
||||
java.util.Optional<Product> existingLive = productRepository.findByProductIdRobust(targetId);
|
||||
|
||||
if (existingLive.isPresent()) {
|
||||
Product live = existingLive.get();
|
||||
// MERGE: Update stock and other details
|
||||
live.setStock((live.getStock() != null ? live.getStock() : 0) + (productDetails.getStock() != null ? productDetails.getStock() : 0));
|
||||
live.setName(productDetails.getName());
|
||||
live.setCategory(productDetails.getCategory());
|
||||
live.setDescription(productDetails.getDescription());
|
||||
live.setBasePrice(productDetails.getBasePrice());
|
||||
live.setPrice(productDetails.getPrice());
|
||||
live.setOfferPrice(productDetails.getOfferPrice());
|
||||
live.setCounter(productDetails.getCounter());
|
||||
live.setTag(productDetails.getTag());
|
||||
live.setImageData(productDetails.getImageData());
|
||||
live.setDraft(false);
|
||||
live.setActive(true);
|
||||
|
||||
Product savedLive = productRepository.save(live);
|
||||
productRepository.delete(product); // Delete the draft
|
||||
updateStallAssociations(savedLive, productDetails.getStalls());
|
||||
return ResponseEntity.ok(productRepository.findById(savedLive.getId()).orElse(savedLive));
|
||||
} else {
|
||||
// CREATE/UPDATE as new live product
|
||||
product.setName(productDetails.getName());
|
||||
product.setProductId(finalProductId);
|
||||
product.setCategory(productDetails.getCategory());
|
||||
product.setDescription(productDetails.getDescription());
|
||||
product.setBasePrice(productDetails.getBasePrice());
|
||||
product.setPrice(productDetails.getPrice());
|
||||
product.setOfferPrice(productDetails.getOfferPrice());
|
||||
product.setCounter(productDetails.getCounter());
|
||||
product.setTag(productDetails.getTag());
|
||||
product.setBarcode(productDetails.getBarcode());
|
||||
product.setImageData(productDetails.getImageData());
|
||||
product.setStock(productDetails.getStock());
|
||||
|
||||
product.getSessions().clear();
|
||||
if (productDetails.getSessions() != null) {
|
||||
product.getSessions().addAll(productDetails.getSessions());
|
||||
}
|
||||
|
||||
// Finalize
|
||||
product.setDraft(false);
|
||||
product.setActive(true);
|
||||
|
||||
Product updated = productRepository.save(product);
|
||||
updateStallAssociations(updated, productDetails.getStalls());
|
||||
|
||||
return ResponseEntity.ok(productRepository.findById(updated.getId()).orElse(updated));
|
||||
}
|
||||
})
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||
List<String> findDistinctCategories();
|
||||
|
||||
List<Product> findByIsDraftTrue();
|
||||
|
||||
@Query("SELECT p FROM Product p WHERE LOWER(TRIM(p.name)) = LOWER(TRIM(:name)) AND (p.isDraft = false OR p.isDraft IS NULL)")
|
||||
List<Product> findByNameRobust(@org.springframework.data.repository.query.Param("name") String name);
|
||||
|
||||
@Query("SELECT p FROM Product p WHERE p.productId = :productId AND (p.isDraft = false OR p.isDraft IS NULL)")
|
||||
java.util.Optional<Product> findByProductIdRobust(@org.springframework.data.repository.query.Param("productId") String productId);
|
||||
|
||||
boolean existsByNameAndCategory(String name, String category);
|
||||
@org.springframework.data.jpa.repository.Modifying
|
||||
|
||||
@@ -154,13 +154,31 @@ public class PurchaseService {
|
||||
for (PurchaseOrderItem item : order.getItems()) {
|
||||
Product draftProduct = new Product();
|
||||
draftProduct.setName(item.getProductName());
|
||||
|
||||
// Try to find an existing live product with the same name to copy metadata
|
||||
String itemName = item.getProductName();
|
||||
List<Product> existingProducts = productRepository.findByNameRobust(itemName);
|
||||
if (!existingProducts.isEmpty()) {
|
||||
Product existing = existingProducts.get(0);
|
||||
draftProduct.setCategory(existing.getCategory());
|
||||
draftProduct.setImageData(existing.getImageData());
|
||||
draftProduct.setDescription(existing.getDescription());
|
||||
draftProduct.setTag(existing.getTag());
|
||||
draftProduct.setCounter(existing.getCounter());
|
||||
draftProduct.setPrice(existing.getPrice());
|
||||
}
|
||||
|
||||
draftProduct.setStock(item.getQuantity() != null ? item.getQuantity().intValue() : 0);
|
||||
draftProduct.setBasePrice(item.getRate());
|
||||
draftProduct.setDraft(true);
|
||||
draftProduct.setActive(false); // Not live yet
|
||||
|
||||
// Set a unique product ID if possible
|
||||
draftProduct.setProductId("DRAFT-" + String.format("%04d", new java.util.Random().nextInt(10000)) + "-" + item.getId());
|
||||
if (!existingProducts.isEmpty()) {
|
||||
draftProduct.setProductId(existingProducts.get(0).getProductId());
|
||||
} else {
|
||||
draftProduct.setProductId("DRAFT-" + String.format("%04d", new java.util.Random().nextInt(10000)) + "-" + item.getId());
|
||||
}
|
||||
|
||||
productRepository.save(draftProduct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user