public static MessageResult RemoveFromBatch(IUnitOfWork db,
                                                    ILogService log,
                                                    ISystemActionService systemAction,
                                                    IOrderHistoryService orderHistoryService,
                                                    IBatchManager batchManager,
                                                    long batchId,
                                                    long orderId,
                                                    long?by)
        {
            var batch = db.OrderBatches.GetAsDto(batchId);
            var order = db.Orders.GetAll().FirstOrDefault(o => o.Id == orderId);

            if (batch.IsClosed && !batchManager.CanBeRemovedFromBatch(order))
            {
                if (!AccessManager.IsAdmin)
                {
                    return(MessageResult.Error("Batch was closed"));
                }
            }
            if (batch.IsLocked)
            {
                if (!AccessManager.IsAdmin)
                {
                    return(MessageResult.Error("Batch was locked"));
                }
            }

            if (order != null)
            {
                var previoudBatchId = order.BatchId;

                order.BatchId = null;

                batchManager.CheckRemovedOrder(db, log, systemAction, order, null, by);

                db.Commit();

                orderHistoryService.AddRecord(order.Id, OrderHistoryHelper.AddToBatchKey, previoudBatchId, batch.Name, null, null, by);

                return(MessageResult.Success());
            }
            return(MessageResult.Error("No order"));
        }
        public static MessageResult RemoveMultipleFromBatch(IUnitOfWork db,
                                                            ILogService log,
                                                            ISystemActionService systemAction,
                                                            IOrderHistoryService orderHistoryService,
                                                            IBatchManager batchManager,
                                                            long batchId,
                                                            string orderIds,
                                                            long?toBatchId,
                                                            bool?removeOnHold)
        {
            var           by         = AccessManager.UserId;
            var           wasChanged = false;
            var           hasClosed  = false;
            var           fromBatch  = db.OrderBatches.GetAsDto(batchId);
            OrderBatchDTO toBatch    = null;

            if (toBatchId.HasValue)
            {
                toBatch = db.OrderBatches.GetAsDto(toBatchId.Value);
            }

            if (fromBatch.IsClosed || (toBatch != null && toBatch.IsClosed))
            {
                if (!AccessManager.CanEditSystemInfo() && !AccessManager.IsAdmin)
                {
                    hasClosed = true;
                }
            }
            if (fromBatch.IsLocked || (toBatch != null && toBatch.IsLocked))
            {
                if (!AccessManager.IsAdmin)
                {
                    return(MessageResult.Error("Source or distination batch was locked"));
                }
            }

            var orders = db.Orders.GetFiltered(o => o.BatchId == batchId);

            if (!string.IsNullOrEmpty(orderIds) && !removeOnHold.HasValue)
            {
                var stringOrderIdList = orderIds.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                var orderIdList       = stringOrderIdList.Select(long.Parse).ToList();
                foreach (var orderId in orderIdList)
                {
                    var order = orders.FirstOrDefault(o => o.Id == orderId);
                    if (order != null)
                    {
                        if (!hasClosed || batchManager.CanBeRemovedFromBatch(order) || AccessManager.IsAdmin)
                        {
                            order.BatchId = toBatchId;

                            batchManager.CheckRemovedOrder(db, log, systemAction, order, toBatchId, by);

                            orderHistoryService.AddRecord(order.Id, OrderHistoryHelper.AddToBatchKey, fromBatch.Id, fromBatch.Name, toBatch?.Id, toBatch?.Name, by);

                            wasChanged = true;
                        }
                    }
                }
            }
            else if (removeOnHold.HasValue && removeOnHold.Value)
            {
                var onHoldOrders = orders.Where(o => o.OnHold).ToList();
                foreach (var order in onHoldOrders)
                {
                    if (!hasClosed || batchManager.CanBeRemovedFromBatch(order))
                    {
                        order.BatchId = toBatchId;

                        batchManager.CheckRemovedOrder(db, log, systemAction, order, toBatchId, by);

                        orderHistoryService.AddRecord(order.Id, OrderHistoryHelper.AddToBatchKey, fromBatch.Id, fromBatch.Name, toBatch?.Id, toBatch?.Name, by);

                        wasChanged = true;
                    }
                }
            }

            db.Commit();

            if (!wasChanged && hasClosed)
            {
                return(MessageResult.Error("Source or distination batch was closed"));
            }
            if (!wasChanged)
            {
                return(MessageResult.Error("Order list is empty"));
            }
            return(MessageResult.Success());
        }
Пример #3
0
        private IList <long> RemoveOrdersWithIssue(IUnitOfWork db,
                                                   long[] orderIds,
                                                   IList <OrderShippingInfoDTO> shippingList,
                                                   PrintLabelResult printLabelResult)
        {
            var removedOrderIds = new List <long>();

            //Remove orders w/o shippings
            var orderIdsWoShippings = orderIds.Where(oId => shippingList.All(sh => sh.OrderId != oId)).ToList();

            if (orderIdsWoShippings.Any())
            {
                foreach (var orderId in orderIdsWoShippings)
                {
                    var dbOrder = db.Orders.GetById(orderId);
                    _batchManager.CheckRemovedOrder(db,
                                                    _log,
                                                    _actionService,
                                                    dbOrder,
                                                    null,
                                                    null);
                    dbOrder.BatchId = null;
                    _orderHistoryService.AddRecord(dbOrder.Id, OrderHistoryHelper.AddToBatchKey, dbOrder.BatchId, null, null, null, null);
                    removedOrderIds.Add(dbOrder.Id);
                    _log.Info("W/o shipping order was removed from batch, orderId=" + dbOrder.AmazonIdentifier);
                }
                printLabelResult.Messages.Add(
                    new Message(String.Format("{0} - orders w/o selected shipping was removed from batch",
                                              orderIdsWoShippings.Distinct().Count()), MessageTypes.Error));
            }

            //Remove OnHold orders from batch
            var onHoldShippingList = shippingList.Where(sh => sh.OnHold).ToList();

            if (onHoldShippingList.Any())
            {
                foreach (var shipping in onHoldShippingList)
                {
                    var dbOrder = db.Orders.GetById(shipping.OrderId);
                    _batchManager.CheckRemovedOrder(db,
                                                    _log,
                                                    _actionService,
                                                    dbOrder,
                                                    null,
                                                    null);
                    dbOrder.BatchId = null;
                    _orderHistoryService.AddRecord(dbOrder.Id, OrderHistoryHelper.AddToBatchKey, dbOrder.BatchId, null, null, null, null);
                    removedOrderIds.Add(dbOrder.Id);
                    _log.Info("OnHold order was removed from batch, orderId=" + dbOrder.AmazonIdentifier);
                }
                printLabelResult.Messages.Add(new Message(String.Format("{0} on hold orders was removed from batch",
                                                                        onHoldShippingList.Select(sh => sh.OrderId).Distinct().Count()), MessageTypes.Error));
            }

            //Removed with Mail label
            var alreadyMailedBatchOrderIds = db.MailLabelInfos.GetAllAsDto().Where(m => orderIds.Contains(m.Id) &&
                                                                                   !m.LabelCanceled &&
                                                                                   !m.CancelLabelRequested)
                                             .Select(m => m.OrderId).ToList();
            var alreadyMailedShippingList = shippingList.Where(sh => alreadyMailedBatchOrderIds.Contains(sh.OrderId)).ToList();

            if (alreadyMailedShippingList.Any())
            {
                foreach (var shipping in alreadyMailedShippingList)
                {
                    shipping.LabelPrintStatus = (int)LabelPrintStatus.AlreadyMailed;

                    var dbOrder = db.Orders.GetById(shipping.OrderId);
                    _orderHistoryService.AddRecord(dbOrder.Id, OrderHistoryHelper.AddToBatchKey, dbOrder.BatchId, null, null, null, null);
                    dbOrder.BatchId = null;
                    removedOrderIds.Add(dbOrder.Id);
                    _log.Info("Already mailed order was removed from batch, orderId=" + dbOrder.AmazonIdentifier);
                }
                printLabelResult.Messages.Add(new Message(String.Format("{0} previously processed and mailed orders was removed from batch",
                                                                        alreadyMailedShippingList.Select(sh => sh.OrderId).Distinct().Count()), MessageTypes.Error));
            }

            db.Commit();

            return(removedOrderIds);
        }