DeliveryAllowedByInventoryOrderEventHandlerImpl extends ProcessAllocationOrderEventHandlerImpl implements OrderEventHandler { @Override public boolean handle(final OrderEvent orderEvent) throws OrderItemAllocationException { synchronized (OrderEventHandler.syncMonitor) { final CustomerOrderDelivery orderDelivery = orderEvent.getCustomerOrderDelivery(); if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(orderDelivery.getDeliveryGroup())) { final Map<String, Warehouse> warehouseByCode = getWarehouseService().getByShopIdMapped( orderEvent.getCustomerOrder().getShop().getShopId(), false); for (CustomerOrderDeliveryDet det : orderDelivery.getDetail()) { final Warehouse selected = warehouseByCode.get(det.getSupplierCode()); if (selected == null) { LOG.warn( Markers.alert(), "Warehouse is not found for delivery detail {}:{}", orderDelivery.getDeliveryNum(), det.getProductSkuCode() ); return false; } final SkuWarehouse stock = getInventoryResolver().findByWarehouseSku( selected, det.getProductSkuCode() ); if (stock == null || !stock.isAvailable(now()) || !stock.isAvailableToAllocate(det.getQty())) { LOG.info( Markers.alert(), "Not enough stock for delivery detail {}:{}", orderDelivery.getDeliveryNum(), det.getProductSkuCode() ); return false; } } } return super.handle(orderEvent); } } DeliveryAllowedByInventoryOrderEventHandlerImpl(final WarehouseService warehouseService,
                                                           final InventoryResolver inventoryResolver,
                                                           final ProductService productService); @Override boolean handle(final OrderEvent orderEvent);  }
@Test public void testHandleBackorderInventorySurplus() throws Exception { String label = assertPgFeatures("testPaymentGateway", false, true, true, true); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); CustomerOrder customerOrder = createTestOrder(TestOrderType.BACKORDER, label, false); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); CustomerOrderDelivery delivery = null; for (final CustomerOrderDelivery orderDelivery : customerOrder.getDelivery()) { if (CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT.equals(orderDelivery.getDeliveryStatus())) { assertNull(delivery); delivery = orderDelivery; } } creditInventoryAndAssert(WAREHOUSE_ID, "CC_TEST5-NOINV", "10.00", "10.00", "4.00"); assertTrue(handler.handle( new OrderEventImpl("", customerOrder, delivery, Collections.EMPTY_MAP))); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "6.00", "0.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_ALLOCATED); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); }
@Test public void testHandleBackorderInventorySufficient() throws Exception { String label = assertPgFeatures("testPaymentGateway", false, true, true, true); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); CustomerOrder customerOrder = createTestOrder(TestOrderType.BACKORDER, label, false); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); CustomerOrderDelivery delivery = null; for (final CustomerOrderDelivery orderDelivery : customerOrder.getDelivery()) { if (CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT.equals(orderDelivery.getDeliveryStatus())) { assertNull(delivery); delivery = orderDelivery; } } creditInventoryAndAssert(WAREHOUSE_ID, "CC_TEST5-NOINV", "4.00", "4.00", "4.00"); assertTrue(handler.handle( new OrderEventImpl("", customerOrder, delivery, Collections.EMPTY_MAP))); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_ALLOCATED); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); }
@Test public void testHandleBackorderInventoryEnoughForOneOrder() throws Exception { String label = assertPgFeatures("testPaymentGateway", false, true, true, true); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); CustomerOrder customerOrder = createTestOrder(TestOrderType.BACKORDER, label, false); createTestOrder(TestOrderType.BACKORDER, label, false); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "8.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); CustomerOrderDelivery delivery = null; for (final CustomerOrderDelivery orderDelivery : customerOrder.getDelivery()) { if (CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT.equals(orderDelivery.getDeliveryStatus())) { assertNull(delivery); delivery = orderDelivery; } } creditInventoryAndAssert(WAREHOUSE_ID, "CC_TEST5-NOINV", "4.00", "4.00", "8.00"); assertTrue(handler.handle( new OrderEventImpl("", customerOrder, delivery, Collections.EMPTY_MAP))); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_ALLOCATED); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); }
@Test public void testHandleBackorderInventoryInsufficient() throws Exception { String label = assertPgFeatures("testPaymentGateway", false, true, true, true); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); CustomerOrder customerOrder = createTestOrder(TestOrderType.BACKORDER, label, false); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); CustomerOrderDelivery delivery = null; for (final CustomerOrderDelivery orderDelivery : customerOrder.getDelivery()) { if (CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT.equals(orderDelivery.getDeliveryStatus())) { assertNull(delivery); delivery = orderDelivery; } } assertFalse(handler.handle( new OrderEventImpl("", customerOrder, delivery, Collections.EMPTY_MAP))); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "84.77", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("84.77", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); }
@Test public void testHandleMixedSingleInventorySurplus() throws Exception { String label = assertPgFeatures("testPaymentGateway", false, true, true, true); changeAvailabilityDatesAndAssert(WAREHOUSE_ID, "CC_TEST6", DateUtils.ldtFrom(0L), true); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "0.00"); CustomerOrder customerOrder = createTestOrder(TestOrderType.MIXED, label, true); assertInventory(WAREHOUSE_ID, "CC_TEST1", "9.00", "2.00"); assertInventory(WAREHOUSE_ID, "CC_TEST2", "1.00", "1.00"); assertInventory(WAREHOUSE_ID, "CC_TEST6", "500.00", "3.00"); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "0.00", "4.00"); assertTrue(customerOrder.getDelivery().size() == 1); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_DATE_WAIT); assertSinglePaymentEntry(customerOrder.getOrdernum(), "1000.71", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("1000.71", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); CustomerOrderDelivery delivery = null; for (final CustomerOrderDelivery orderDelivery : customerOrder.getDelivery()) { if (CustomerOrderDelivery.DELIVERY_STATUS_DATE_WAIT.equals(orderDelivery.getDeliveryStatus())) { assertNull(delivery); delivery = orderDelivery; } } creditInventoryAndAssert(WAREHOUSE_ID, "CC_TEST5-NOINV", "10.00", "10.00", "4.00"); assertTrue(handler.handle( new OrderEventImpl("", customerOrder, delivery, Collections.EMPTY_MAP))); assertInventory(WAREHOUSE_ID, "CC_TEST1", "7.00", "0.00"); assertInventory(WAREHOUSE_ID, "CC_TEST2", "0.00", "0.00"); assertInventory(WAREHOUSE_ID, "CC_TEST6", "497.00", "0.00"); assertInventory(WAREHOUSE_ID, "CC_TEST5-NOINV", "6.00", "0.00"); assertDeliveryStates(customerOrder.getDelivery(), CustomerOrderDelivery.DELIVERY_STATUS_INVENTORY_ALLOCATED); assertSinglePaymentEntry(customerOrder.getOrdernum(), "1000.71", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK, false); assertEquals("1000.71", customerOrder.getOrderTotal().toPlainString()); assertEquals("0.00", paymentService.getOrderAmount(customerOrder.getOrdernum()).toPlainString()); assertEquals(CustomerOrder.ORDER_STATUS_IN_PROGRESS, customerOrder.getOrderStatus()); }