Пример #1
0
        public async Task Handle(EntityDeleted <Shipment> notification, CancellationToken cancellationToken)
        {
            //reverse booked invetory
            foreach (var shipmentItem in notification.Entity.ShipmentItems)
            {
                await _inventoryManageService.ReverseBookedInventory(notification.Entity, shipmentItem);
            }


            var order = await _orderService.GetOrderById(notification.Entity.OrderId);

            if (order != null)
            {
                foreach (var item in notification.Entity.ShipmentItems)
                {
                    var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == item.OrderItemId);
                    if (orderItem != null)
                    {
                        orderItem.ShipQty -= item.Quantity;
                        orderItem.OpenQty += item.Quantity;
                        orderItem.Status   = orderItem.OpenQty <= 0 ? Domain.Orders.OrderItemStatus.Close : Domain.Orders.OrderItemStatus.Open;
                    }
                }

                if (!order.OrderItems.Where(x => x.ShipQty > 0).Any())
                {
                    order.ShippingStatusId = ShippingStatus.Pending;
                }
                else
                {
                    if (order.ShippingStatusId == ShippingStatus.Delivered)
                    {
                        order.ShippingStatusId = ShippingStatus.PartiallyShipped;
                    }
                    else
                    {
                        var shipments = await _shipmentService.GetShipmentsByOrder(order.Id);

                        if (shipments.Any())
                        {
                            if (shipments.Where(x => x.ShippedDateUtc == null).Any())
                            {
                                order.ShippingStatusId = ShippingStatus.PreparedToShipped;
                            }
                            if (shipments.Where(x => x.ShippedDateUtc != null).Any())
                            {
                                order.ShippingStatusId = ShippingStatus.PartiallyShipped;
                            }
                        }
                    }
                }
                await _orderService.UpdateOrder(order);
            }
        }
Пример #2
0
        public async Task <IActionResult> DeleteShipment(string id)
        {
            var shipment = await _shipmentService.GetShipmentById(id);

            if (shipment == null)
            {
                //No shipment found with the specified id
                return(RedirectToAction("List"));
            }

            if (_workContext.CurrentCustomer.IsStaff() && shipment.StoreId != _workContext.CurrentCustomer.StaffStoreId)
            {
                return(RedirectToAction("List"));
            }

            if (_workContext.CurrentVendor != null && _workContext.CurrentVendor.Id != shipment.VendorId)
            {
                ErrorNotification(_localizationService.GetResource("Admin.Orders.Shipments.VendorAccess"));
                return(RedirectToAction("ShipmentDetails", new { id = shipment.Id }));
            }

            var orderId = shipment.OrderId;
            var order   = await _orderService.GetOrderById(orderId);

            if (order == null)
            {
                //No order found with the specified id
                return(RedirectToAction("List"));
            }

            //a vendor should have access only to his products
            if (_workContext.CurrentVendor != null && !_workContext.HasAccessToShipment(order, shipment) && !_workContext.CurrentCustomer.IsStaff())
            {
                return(RedirectToAction("List"));
            }

            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var product = await _productService.GetProductById(shipmentItem.ProductId);

                shipmentItem.ShipmentId = shipment.Id;
                if (product != null)
                {
                    await _inventoryManageService.ReverseBookedInventory(product, shipment, shipmentItem);
                }
            }

            await _shipmentService.DeleteShipment(shipment);

            //add a note
            await _orderService.InsertOrderNote(new OrderNote {
                Note = $"A shipment #{shipment.ShipmentNumber} has been deleted",
                DisplayToCustomer = false,
                CreatedOnUtc      = DateTime.UtcNow,
                OrderId           = order.Id,
            });

            await _shipmentViewModelService.LogShipment(shipment.Id, $"A shipment #{shipment.ShipmentNumber} has been deleted");

            SuccessNotification(_localizationService.GetResource("Admin.Orders.Shipments.Deleted"));

            return(RedirectToAction("Edit", "Order", new { Id = order.Id }));
        }
        public async Task <bool> Handle(DeleteOrderCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null)
            {
                throw new ArgumentNullException("order");
            }

            //check whether the order wasn't cancelled before
            //if it already was cancelled, then there's no need to make the following adjustments
            //(such as reward points, inventory, recurring payments)
            //they already was done when cancelling the order
            if (request.Order.OrderStatus != OrderStatus.Cancelled)
            {
                //return (add) back redeemded reward points
                await _mediator.Send(new ReturnBackRedeemedRewardPointsCommand()
                {
                    Order = request.Order
                });

                //reduce (cancel) back reward points (previously awarded for this order)
                await _mediator.Send(new ReduceRewardPointsCommand()
                {
                    Order = request.Order
                });

                //cancel recurring payments
                var recurringPayments = await _orderService.SearchRecurringPayments(initialOrderId : request.Order.Id);

                foreach (var rp in recurringPayments)
                {
                    var errors = await _mediator.Send(new CancelRecurringPaymentCommand()
                    {
                        RecurringPayment = rp
                    });

                    //use "errors" variable?
                }

                //Adjust inventory for already shipped shipments
                //only products with "use multiple warehouses"
                foreach (var shipment in await _shipmentService.GetShipmentsByOrder(request.Order.Id))
                {
                    foreach (var shipmentItem in shipment.ShipmentItems)
                    {
                        var product = await _productService.GetProductById(shipmentItem.ProductId);

                        shipmentItem.ShipmentId = shipment.Id;
                        if (product != null)
                        {
                            await _inventoryManageService.ReverseBookedInventory(product, shipment, shipmentItem);
                        }
                    }
                }
                //Adjust inventory
                foreach (var orderItem in request.Order.OrderItems)
                {
                    var product = await _productService.GetProductById(orderItem.ProductId);

                    if (product != null)
                    {
                        await _inventoryManageService.AdjustInventory(product, orderItem.Quantity, orderItem.AttributesXml, orderItem.WarehouseId);
                    }
                }

                //cancel reservations
                await _productReservationService.CancelReservationsByOrderId(request.Order.Id);

                //cancel bid
                await _auctionService.CancelBidByOrder(request.Order.Id);
            }

            //deactivate gift cards
            if (_orderSettings.DeactivateGiftCardsAfterDeletingOrder)
            {
                await _mediator.Send(new ActivatedValueForPurchasedGiftCardsCommand()
                {
                    Order = request.Order, Activate = false
                });
            }

            request.Order.Deleted = true;
            //now delete an order
            await _orderService.UpdateOrder(request.Order);

            //cancel discounts
            await _discountService.CancelDiscount(request.Order.Id);

            return(true);
        }
        public async Task <bool> Handle(CancelOrderCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null)
            {
                throw new ArgumentNullException("order");
            }

            if (request.Order.OrderStatus == OrderStatus.Cancelled)
            {
                throw new Exception("Cannot do cancel for order.");
            }

            //Cancel order
            await _mediator.Send(new SetOrderStatusCommand()
            {
                Order            = request.Order,
                Os               = OrderStatus.Cancelled,
                NotifyCustomer   = request.NotifyCustomer,
                NotifyStoreOwner = request.NotifyStoreOwner
            });

            //add a note
            await _orderService.InsertOrderNote(new OrderNote {
                Note = "Order has been cancelled",
                DisplayToCustomer = false,
                CreatedOnUtc      = DateTime.UtcNow,
                OrderId           = request.Order.Id,
            });

            //return (add) back redeemded reward points
            await _mediator.Send(new ReturnBackRedeemedRewardPointsCommand()
            {
                Order = request.Order
            });

            //cancel recurring payments
            var recurringPayments = await _orderService.SearchRecurringPayments(initialOrderId : request.Order.Id);

            foreach (var rp in recurringPayments)
            {
                var errors = await _mediator.Send(new CancelRecurringPaymentCommand()
                {
                    RecurringPayment = rp
                });
            }

            //Adjust inventory for already shipped shipments
            //only products with "use multiple warehouses"
            var shipments = await _shipmentService.GetShipmentsByOrder(request.Order.Id);

            foreach (var shipment in shipments)
            {
                foreach (var shipmentItem in shipment.ShipmentItems)
                {
                    var product = await _productService.GetProductById(shipmentItem.ProductId);

                    shipmentItem.ShipmentId = shipment.Id;
                    await _inventoryManageService.ReverseBookedInventory(product, shipment, shipmentItem);
                }
            }
            //Adjust inventory
            foreach (var orderItem in request.Order.OrderItems)
            {
                var product = await _productService.GetProductById(orderItem.ProductId);

                await _inventoryManageService.AdjustInventory(product, orderItem.Quantity, orderItem.Attributes, orderItem.WarehouseId);
            }

            //cancel reservations
            await _productReservationService.CancelReservationsByOrderId(request.Order.Id);

            //cancel bid
            await _auctionService.CancelBidByOrder(request.Order.Id);

            //cancel discount
            await _discountService.CancelDiscount(request.Order.Id);

            //event notification
            await _mediator.Publish(new OrderCancelledEvent(request.Order));

            return(true);
        }