use of com.salesmanager.core.model.catalog.product.availability.ProductAvailability in project shopizer by shopizer-ecommerce.
the class OrderServiceImpl method process.
private Order process(Order order, Customer customer, List<ShoppingCartItem> items, OrderTotalSummary summary, Payment payment, Transaction transaction, MerchantStore store) throws ServiceException {
Validate.notNull(order, "Order cannot be null");
Validate.notNull(customer, "Customer cannot be null (even if anonymous order)");
Validate.notEmpty(items, "ShoppingCart items cannot be null");
Validate.notNull(payment, "Payment cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(summary, "Order total Summary cannot be null");
UserContext context = UserContext.getCurrentInstance();
if (context != null) {
String ipAddress = context.getIpAddress();
if (!StringUtils.isBlank(ipAddress)) {
order.setIpAddress(ipAddress);
}
}
// first process payment
Transaction processTransaction = paymentService.processPayment(customer, store, payment, items, order);
if (order.getOrderHistory() == null || order.getOrderHistory().size() == 0 || order.getStatus() == null) {
OrderStatus status = order.getStatus();
if (status == null) {
status = OrderStatus.ORDERED;
order.setStatus(status);
}
Set<OrderStatusHistory> statusHistorySet = new HashSet<OrderStatusHistory>();
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(status);
statusHistory.setDateAdded(new Date());
statusHistory.setOrder(order);
statusHistorySet.add(statusHistory);
order.setOrderHistory(statusHistorySet);
}
if (customer.getId() == null || customer.getId() == 0) {
customerService.create(customer);
}
order.setCustomerId(customer.getId());
this.create(order);
if (transaction != null) {
transaction.setOrder(order);
if (transaction.getId() == null || transaction.getId() == 0) {
transactionService.create(transaction);
} else {
transactionService.update(transaction);
}
}
if (processTransaction != null) {
processTransaction.setOrder(order);
if (processTransaction.getId() == null || processTransaction.getId() == 0) {
transactionService.create(processTransaction);
} else {
transactionService.update(processTransaction);
}
}
/**
* decrement inventory
*/
LOGGER.debug("Update inventory");
Set<OrderProduct> products = order.getOrderProducts();
for (OrderProduct orderProduct : products) {
orderProduct.getProductQuantity();
Product p = productService.getById(orderProduct.getId());
if (p == null)
throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
for (ProductAvailability availability : p.getAvailabilities()) {
int qty = availability.getProductQuantity();
if (qty < orderProduct.getProductQuantity()) {
// throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
LOGGER.error("APP-BACKEND [" + ServiceException.EXCEPTION_INVENTORY_MISMATCH + "]");
}
qty = qty - orderProduct.getProductQuantity();
availability.setProductQuantity(qty);
}
productService.update(p);
}
return order;
}
use of com.salesmanager.core.model.catalog.product.availability.ProductAvailability in project shopizer by shopizer-ecommerce.
the class ProductFacadeV2Impl method getProductPrice.
@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
Validate.notNull(id, "Product id cannot be null");
Validate.notNull(priceRequest, "Product price request cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
try {
Product model = productService.findOne(id, store);
List<ProductAttribute> attributes = null;
if (!CollectionUtils.isEmpty(priceRequest.getOptions())) {
List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
for (ProductAttribute attribute : attributes) {
if (attribute.getProduct().getId().longValue() != id.longValue()) {
// throw unauthorized
throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
}
}
}
if (!StringUtils.isBlank(priceRequest.getSku())) {
// change default availability with sku (instance availability)
List<ProductAvailability> availabilityList = productAvailabilityService.getBySku(priceRequest.getSku(), store);
if (CollectionUtils.isNotEmpty(availabilityList)) {
model.setAvailabilities(new HashSet(availabilityList));
}
}
FinalPrice price;
// attributes can be null;
price = pricingService.calculateProductPrice(model, attributes);
ReadableProductPrice readablePrice = new ReadableProductPrice();
ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
populator.setPricingService(pricingService);
return populator.populate(price, readablePrice, store, language);
} catch (Exception e) {
throw new ServiceRuntimeException("An error occured while getting product price", e);
}
}
use of com.salesmanager.core.model.catalog.product.availability.ProductAvailability in project shopizer by shopizer-ecommerce.
the class ProductInventoryFacadeImpl method getInventory.
@Override
public ReadableEntityList<ReadableInventory> getInventory(Long productId, MerchantStore store, String child, Language language, int page, int count) {
Product product = getProductById(productId);
validateProductHasSameStore(store, product);
Page<ProductAvailability> availabilities = productAvailabilityService.listByProduct(product, store, child, page, count);
List<ReadableInventory> inventories = availabilities.stream().map(pa -> readableInventoryMapper.convert(pa, store, language)).collect(Collectors.toList());
return createReadableList(availabilities, inventories);
}
use of com.salesmanager.core.model.catalog.product.availability.ProductAvailability in project shopizer by shopizer-ecommerce.
the class ProductInventoryFacadeImpl method get.
@Override
public ReadableInventory get(Long productId, String child, Language language) {
Product product = getProductById(productId);
MerchantStore store = getMerchantStore(child);
if (isStoreParentNotExist(store) || store.getParent().getId().equals(product.getMerchantStore().getId())) {
throw new ResourceNotFoundException("MerchantStore [" + child + "] is not a store of retailer [" + store.getCode() + "]");
}
ProductAvailability availability = productAvailabilityService.getByStore(product, store).orElseThrow(() -> new ResourceNotFoundException("Inventory with not found"));
return this.readableInventory(availability, store, language);
}
use of com.salesmanager.core.model.catalog.product.availability.ProductAvailability in project shopizer by shopizer-ecommerce.
the class ProductInventoryFacadeImpl method add.
@Override
public ReadableInventory add(Long productId, PersistableInventory inventory, MerchantStore store, Language language) {
Validate.notNull(store, "MerchantStore cannot be null");
ProductAvailability availability = getProductAvailabilityToSave(inventory, store, productId);
saveOrUpdate(availability);
return get(availability.getId(), store, language);
}
Aggregations