Ejemplo n.º 1
0
        public static IList <MessageString> CancelCurrentOrderLabels(
            ILogService log,
            IUnitOfWork db,
            ILabelService labelService,
            ITime time,
            long orderId,
            bool sampleMode)
        {
            var cancelTrackings = GetOrdersToCancel(db, orderId);

            var results = new List <MessageString>();

            foreach (var tracking in cancelTrackings)
            {
                MailLabelInfo     mailInfo     = null;
                OrderShippingInfo shippingInfo = null;
                if (tracking.LabelFromType == (int)LabelFromType.Mail)
                {
                    mailInfo = db.MailLabelInfos.Get(tracking.Id);
                    if (mailInfo != null)
                    {
                        mailInfo.CancelLabelRequested = true;
                    }
                }
                else
                {
                    shippingInfo = db.OrderShippingInfos.Get(tracking.Id);
                    if (shippingInfo != null)
                    {
                        shippingInfo.CancelLabelRequested = true;
                    }
                }

                try
                {
                    string shipmentIdentifier = tracking.StampsTxId;
                    if (tracking.ShipmentProviderType == (int)ShipmentProviderType.Stamps ||
                        tracking.ShipmentProviderType == (int)ShipmentProviderType.StampsPriority)
                    {
                        shipmentIdentifier = tracking.TrackingNumber;
                    }

                    var result = labelService.CancelLabel((ShipmentProviderType)tracking.ShipmentProviderType,
                                                          shipmentIdentifier,
                                                          sampleMode);

                    if (result.IsSuccess)
                    {
                        results.Add(MessageString.Success(tracking.TrackingNumber + " (cancelled)" + ", OrderId=" + orderId));
                        if (tracking.LabelFromType == (int)LabelFromType.Mail)
                        {
                            if (mailInfo != null)
                            {
                                mailInfo.LabelCanceled     = true;
                                mailInfo.LabelCanceledDate = time.GetAppNowTime();
                            }
                        }
                        else
                        {
                            if (shippingInfo != null)
                            {
                                shippingInfo.LabelCanceled     = true;
                                shippingInfo.LabelCanceledDate = time.GetAppNowTime();
                            }
                        }
                    }
                    else
                    {
                        results.Add(MessageString.Error(tracking.TrackingNumber + " (cancellation was failed with error: " + result.Message + ")"));
                    }
                }
                catch (Exception ex)
                {
                    results.Add(MessageString.Error(tracking.TrackingNumber + " (cancellation was failed with error: " + ExceptionHelper.GetMostDeeperException(ex) + ")"));
                }
            }
            db.Commit();

            if (!cancelTrackings.Any())
            {
                results.Add(MessageString.Success("No tracking numbers to cancel"));
            }

            return(results);
        }