Beispiel #1
0
        public async Task <IActionResult> DownloadInvoice(ShipmentDetailsModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageOrders))
            {
                return(AccessDeniedView());
            }

            var shipment = await _shipmentService.GetShipmentByIdAsync(model.Id);

            if (shipment is not null)
            {
                var((downloadUrl, contentType), error) = await _easyPostService.DownloadInvoiceAsync(shipment);

                if (!string.IsNullOrEmpty(error))
                {
                    var locale = await _localizationService.GetResourceAsync("Plugins.Shipping.EasyPost.Error");

                    _notificationService.ErrorNotification(string.Format(locale, error, Url.Action("List", "Log")), false);
                }
                else
                {
                    //return file
                    var filename = _nopFileProvider.GetFileName(downloadUrl);
                    var client   = _httpClientFactory.CreateClient(NopHttpDefaults.DefaultHttpClient);
                    var stream   = await client.GetStreamAsync(downloadUrl);

                    return(File(stream, contentType, filename, true));
                }
            }

            return(RedirectToAction("ShipmentDetails", "Order", new { id = shipment?.Id ?? 0 }));
        }
Beispiel #2
0
        public async Task <IActionResult> BuyLabel(ShipmentDetailsModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageOrders))
            {
                return(AccessDeniedView());
            }

            var shipment = await _shipmentService.GetShipmentByIdAsync(model.Id);

            if (shipment is not null)
            {
                var(_, error) = await _easyPostService.BuyLabelAsync(shipment, model.RateId, model.Insurance);

                if (!string.IsNullOrEmpty(error))
                {
                    var locale = await _localizationService.GetResourceAsync("Plugins.Shipping.EasyPost.Error");

                    _notificationService.ErrorNotification(string.Format(locale, error, Url.Action("List", "Log")), false);
                }
                else
                {
                    var locale = await _localizationService.GetResourceAsync("Plugins.Shipping.EasyPost.Success");

                    _notificationService.SuccessNotification(locale);
                }
            }

            return(RedirectToAction("ShipmentDetails", "Order", new { id = shipment?.Id ?? 0 }));
        }
Beispiel #3
0
        public async Task <IActionResult> PostShipmentDetails(ShipmentDetailsModel shipmentDetailsModel)
        {
            try
            {
                AutoGenerateNumber autoGenerateNumber = new AutoGenerateNumber();
                int orderId = (from s in _context.Orders
                               where s.UserId == Convert.ToInt32(shipmentDetailsModel.userId) &&
                               s.OrderStatusCode == Convert.ToInt32(shipmentDetailsModel.OrderStatusCode)
                               select s.OrderId).Single();
                int invoiceId = (from i in _context.Invoices
                                 where i.OrderId == orderId
                                 select i.InvoiceId).Single();
                string shipmentTrackNum = autoGenerateNumber.GenShipmentTrackNum();
                var    shipmentDetails  = new Shipment()
                {
                    ShipmentTrackNum = shipmentTrackNum,
                    FullName         = shipmentDetailsModel.FullName,
                    AddressLineOne   = shipmentDetailsModel.AddressLineOne,
                    AddressLineTwo   = shipmentDetailsModel.AddressLineTwo,
                    City             = shipmentDetailsModel.City,
                    State            = shipmentDetailsModel.StateProvinceRegion,
                    Zip          = shipmentDetailsModel.Zip,
                    Country      = shipmentDetailsModel.Country,
                    ShipmentDate = Convert.ToDateTime(shipmentDetailsModel.ShipmentDate),
                    OrderId      = orderId,
                    InvoiceId    = invoiceId
                };
                await _context.Shipments.AddAsync(shipmentDetails);

                await _context.SaveChangesAsync();

                return(Ok(shipmentDetails));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #4
0
        protected virtual ShipmentDetailsModel PrepareShipmentDetailsModel(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

            var order = shipment.Order;

            if (order == null)
            {
                throw new Exception("order cannot be loaded");
            }
            var model = new ShipmentDetailsModel();

            model.Id = shipment.Id;
            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }
            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            //tracking number and shipment information
            if (!String.IsNullOrEmpty(shipment.TrackingNumber))
            {
                model.TrackingNumber = shipment.TrackingNumber;
                var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);
                if (srcm != null &&
                    srcm.PluginDescriptor.Installed &&
                    srcm.IsShippingRateComputationMethodActive(_shippingSettings))
                {
                    var shipmentTracker = srcm.ShipmentTracker;
                    if (shipmentTracker != null)
                    {
                        model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                        if (_shippingSettings.DisplayShipmentEventsToCustomers)
                        {
                            var shipmentEvents = shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);
                            if (shipmentEvents != null)
                            {
                                foreach (var shipmentEvent in shipmentEvents)
                                {
                                    var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel();
                                    var shipmentEventCountry     = _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);
                                    shipmentStatusEventModel.Country = shipmentEventCountry != null
                                                                           ? shipmentEventCountry.GetLocalized(x => x.Name)
                                                                           : shipmentEvent.CountryCode;

                                    shipmentStatusEventModel.Date      = shipmentEvent.Date;
                                    shipmentStatusEventModel.EventName = shipmentEvent.EventName;
                                    shipmentStatusEventModel.Location  = shipmentEvent.Location;
                                    model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                                }
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = _catalogSettings.ShowProductSku;
            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = _orderService.GetOrderItemById(shipmentItem.OrderItemId);
                if (orderItem == null)
                {
                    continue;
                }

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = orderItem.Product.FormatSku(orderItem.AttributesXml, _productAttributeParser),
                    ProductId       = orderItem.Product.Id,
                    ProductName     = orderItem.Product.GetLocalized(x => x.Name),
                    ProductSeName   = orderItem.Product.GetSeName(),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity,
                };
                //rental info
                if (orderItem.Product.IsRental)
                {
                    var rentalStartDate = orderItem.RentalStartDateUtc.HasValue ? orderItem.Product.FormatRentalDate(orderItem.RentalStartDateUtc.Value) : "";
                    var rentalEndDate   = orderItem.RentalEndDateUtc.HasValue ? orderItem.Product.FormatRentalDate(orderItem.RentalEndDateUtc.Value) : "";
                    shipmentItemModel.RentalInfo = string.Format(_localizationService.GetResource("Order.Rental.FormattedDate"),
                                                                 rentalStartDate, rentalEndDate);
                }
                model.Items.Add(shipmentItemModel);
            }

            //order details model
            model.Order = PrepareOrderDetailsModel(order);

            return(model);
        }
Beispiel #5
0
        protected ShipmentDetailsModel PrepareShipmentDetailsModel(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

            var order = shipment.Order;

            if (order == null)
            {
                throw new Exception("order cannot be loaded");
            }
            var model = new ShipmentDetailsModel();

            model.Id          = shipment.Id;
            model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc, DateTimeKind.Utc);
            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            //tracking number and shipment information
            model.TrackingNumber = shipment.TrackingNumber;
            var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);

            if (srcm != null &&
                srcm.PluginDescriptor.Installed &&
                srcm.IsShippingRateComputationMethodActive(_shippingSettings))
            {
                var shipmentTracker = srcm.ShipmentTracker;
                if (shipmentTracker != null)
                {
                    model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                    if (_shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);
                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel();
                                var shipmentEventCountry     = _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);
                                shipmentStatusEventModel.Country = shipmentEventCountry != null
                                                                       ? shipmentEventCountry.GetLocalized(x => x.Name)
                                                                       : shipmentEvent.CountryCode;

                                shipmentStatusEventModel.Date      = shipmentEvent.Date;
                                shipmentStatusEventModel.EventName = shipmentEvent.EventName;
                                shipmentStatusEventModel.Location  = shipmentEvent.Location;
                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = _catalogSettings.ShowProductSku;
            foreach (var sopv in shipment.ShipmentOrderProductVariants)
            {
                var opv = _orderService.GetOrderProductVariantById(sopv.OrderProductVariantId);
                if (opv == null)
                {
                    continue;
                }

                var sopvModel = new ShipmentDetailsModel.ShipmentOrderProductVariantModel()
                {
                    Id              = sopv.Id,
                    Sku             = opv.ProductVariant.Sku,
                    ProductId       = opv.ProductVariant.ProductId,
                    ProductSeName   = opv.ProductVariant.Product.GetSeName(),
                    AttributeInfo   = opv.AttributeDescription,
                    QuantityOrdered = opv.Quantity,
                    QuantityShipped = sopv.Quantity,
                };

                //product name//product name
                if (!String.IsNullOrEmpty(opv.ProductVariant.GetLocalized(x => x.Name)))
                {
                    sopvModel.ProductName = string.Format("{0} ({1})", opv.ProductVariant.Product.GetLocalized(x => x.Name), opv.ProductVariant.GetLocalized(x => x.Name));
                }
                else
                {
                    sopvModel.ProductName = opv.ProductVariant.Product.GetLocalized(x => x.Name);
                }
                model.Items.Add(sopvModel);
            }

            //order details model
            model.Order = PrepareOrderDetailsModel(order);

            return(model);
        }
Beispiel #6
0
        protected ShipmentDetailsModel PrepareShipmentDetailsModel(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

            var order = shipment.Order;

            if (order == null)
            {
                throw new SmartException(T("Order.NotFound", shipment.OrderId));
            }

            var store            = Services.StoreService.GetStoreById(order.StoreId) ?? Services.StoreContext.CurrentStore;
            var catalogSettings  = Services.Settings.LoadSetting <CatalogSettings>(store.Id);
            var shippingSettings = Services.Settings.LoadSetting <ShippingSettings>(store.Id);

            var model = new ShipmentDetailsModel
            {
                Id                = shipment.Id,
                TrackingNumber    = shipment.TrackingNumber,
                TrackingNumberUrl = shipment.TrackingUrl
            };

            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }

            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);

            if (srcm != null && srcm.IsShippingRateComputationMethodActive(shippingSettings))
            {
                var shipmentTracker = srcm.Value.ShipmentTracker;
                if (shipmentTracker != null)
                {
                    // The URL entered by the merchant takes precedence over an automatically generated URL.
                    if (model.TrackingNumberUrl.IsEmpty())
                    {
                        model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                    }

                    if (shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);
                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentEventCountry = _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);

                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel
                                {
                                    Country                                 = shipmentEventCountry != null?shipmentEventCountry.GetLocalized(x => x.Name) : shipmentEvent.CountryCode,
                                                                  Date      = shipmentEvent.Date,
                                                                  EventName = shipmentEvent.EventName,
                                                                  Location  = shipmentEvent.Location
                                };

                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            // Products in this shipment.
            model.ShowSku = catalogSettings.ShowProductSku;

            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = _orderService.GetOrderItemById(shipmentItem.OrderItemId);
                if (orderItem == null)
                {
                    continue;
                }

                orderItem.Product.MergeWithCombination(orderItem.AttributesXml);

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = orderItem.Product.Sku,
                    ProductId       = orderItem.Product.Id,
                    ProductName     = orderItem.Product.GetLocalized(x => x.Name),
                    ProductSeName   = orderItem.Product.GetSeName(),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity
                };

                shipmentItemModel.ProductUrl = _productUrlHelper.GetProductUrl(shipmentItemModel.ProductSeName, orderItem);

                model.Items.Add(shipmentItemModel);
            }

            model.Order = _orderHelper.PrepareOrderDetailsModel(order);
            return(model);
        }
        public virtual async Task <ShipmentDetailsModel> PrepareShipmentDetails(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

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

            if (order == null)
            {
                throw new Exception("order cannot be loaded");
            }
            var model = new ShipmentDetailsModel();

            model.Id             = shipment.Id;
            model.ShipmentNumber = shipment.ShipmentNumber;
            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }
            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            //tracking number and shipment information
            if (!String.IsNullOrEmpty(shipment.TrackingNumber))
            {
                model.TrackingNumber = shipment.TrackingNumber;
                var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);
                if (srcm != null &&
                    srcm.PluginDescriptor.Installed &&
                    srcm.IsShippingRateComputationMethodActive(_shippingSettings))
                {
                    var shipmentTracker = srcm.ShipmentTracker;
                    if (shipmentTracker != null)
                    {
                        model.TrackingNumberUrl = await shipmentTracker.GetUrl(shipment.TrackingNumber);

                        if (_shippingSettings.DisplayShipmentEventsToCustomers)
                        {
                            var shipmentEvents = await shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);

                            if (shipmentEvents != null)
                            {
                                foreach (var shipmentEvent in shipmentEvents)
                                {
                                    var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel();
                                    var shipmentEventCountry     = await _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);

                                    shipmentStatusEventModel.Country = shipmentEventCountry != null
                                                                           ? shipmentEventCountry.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id)
                                                                           : shipmentEvent.CountryCode;

                                    shipmentStatusEventModel.Date      = shipmentEvent.Date;
                                    shipmentStatusEventModel.EventName = shipmentEvent.EventName;
                                    shipmentStatusEventModel.Location  = shipmentEvent.Location;
                                    model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                                }
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = _catalogSettings.ShowSkuOnProductDetailsPage;
            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = order.OrderItems.Where(x => x.Id == shipmentItem.OrderItemId).FirstOrDefault();
                if (orderItem == null)
                {
                    continue;
                }
                var product = await _productService.GetProductByIdIncludeArch(orderItem.ProductId);

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = product.FormatSku(orderItem.AttributesXml, _productAttributeParser),
                    ProductId       = orderItem.ProductId,
                    ProductName     = product.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
                    ProductSeName   = product.GetSeName(_workContext.WorkingLanguage.Id),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity,
                };

                model.Items.Add(shipmentItemModel);
            }

            //order details model
            model.Order = await PrepareOrderDetails(order);

            return(model);
        }
        /// <summary>
        /// Prepare the shipment details model
        /// </summary>
        /// <param name="shipment">Shipment</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the shipment details model
        /// </returns>
        public virtual async Task <ShipmentDetailsModel> PrepareShipmentDetailsModelAsync(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException(nameof(shipment));
            }

            var order = await _orderService.GetOrderByIdAsync(shipment.OrderId);

            if (order == null)
            {
                throw new Exception("order cannot be loaded");
            }
            var model = new ShipmentDetailsModel
            {
                Id = shipment.Id
            };

            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = await _dateTimeHelper.ConvertToUserTimeAsync(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }
            if (shipment.ReadyForPickupDateUtc.HasValue)
            {
                model.ReadyForPickupDate = await _dateTimeHelper.ConvertToUserTimeAsync(shipment.ReadyForPickupDateUtc.Value, DateTimeKind.Utc);
            }
            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = await _dateTimeHelper.ConvertToUserTimeAsync(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            //tracking number and shipment information
            if (!string.IsNullOrEmpty(shipment.TrackingNumber))
            {
                model.TrackingNumber = shipment.TrackingNumber;
                var shipmentTracker = await _shipmentService.GetShipmentTrackerAsync(shipment);

                if (shipmentTracker != null)
                {
                    model.TrackingNumberUrl = await shipmentTracker.GetUrlAsync(shipment.TrackingNumber, shipment);

                    if (_shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = await shipmentTracker.GetShipmentEventsAsync(shipment.TrackingNumber, shipment);

                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel();
                                var shipmentEventCountry     = await _countryService.GetCountryByTwoLetterIsoCodeAsync(shipmentEvent.CountryCode);

                                shipmentStatusEventModel.Country = shipmentEventCountry != null
                                    ? await _localizationService.GetLocalizedAsync(shipmentEventCountry, x => x.Name) : shipmentEvent.CountryCode;

                                shipmentStatusEventModel.Status    = shipmentEvent.Status;
                                shipmentStatusEventModel.Date      = shipmentEvent.Date;
                                shipmentStatusEventModel.EventName = shipmentEvent.EventName;
                                shipmentStatusEventModel.Location  = shipmentEvent.Location;
                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = _catalogSettings.ShowSkuOnProductDetailsPage;
            foreach (var shipmentItem in await _shipmentService.GetShipmentItemsByShipmentIdAsync(shipment.Id))
            {
                var orderItem = await _orderService.GetOrderItemByIdAsync(shipmentItem.OrderItemId);

                if (orderItem == null)
                {
                    continue;
                }

                var product = await _productService.GetProductByIdAsync(orderItem.ProductId);

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = await _productService.FormatSkuAsync(product, orderItem.AttributesXml),
                    ProductId       = product.Id,
                    ProductName     = await _localizationService.GetLocalizedAsync(product, x => x.Name),
                    ProductSeName   = await _urlRecordService.GetSeNameAsync(product),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity,
                };
                //rental info
                if (product.IsRental)
                {
                    var rentalStartDate = orderItem.RentalStartDateUtc.HasValue
                        ? _productService.FormatRentalDate(product, orderItem.RentalStartDateUtc.Value) : "";
                    var rentalEndDate = orderItem.RentalEndDateUtc.HasValue
                        ? _productService.FormatRentalDate(product, orderItem.RentalEndDateUtc.Value) : "";
                    shipmentItemModel.RentalInfo = string.Format(await _localizationService.GetResourceAsync("Order.Rental.FormattedDate"),
                                                                 rentalStartDate, rentalEndDate);
                }
                model.Items.Add(shipmentItemModel);
            }

            //order details model
            model.Order = await PrepareOrderDetailsModelAsync(order);

            return(model);
        }
Beispiel #9
0
        public async Task <IActionResult> UpdateShipment(ShipmentDetailsModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageOrders))
            {
                return(AccessDeniedView());
            }

            var shipment = await _shipmentService.GetShipmentByIdAsync(model.Id);

            if (shipment is not null)
            {
                string getAttributeValue(Enum enumValue) => enumValue.GetType().GetMember(enumValue.ToString()).FirstOrDefault()
                ?.GetCustomAttributes(false).OfType <EnumMemberAttribute>().FirstOrDefault()
                ?.Value is string value && value != "---" ? value : null;

                //prepare the details to update the shipment
                var request = new UpdateShipmentRequest
                {
                    OptionsDetails = new()
                    {
                        AdditionalHandling   = model.AdditionalHandling,
                        Alcohol              = model.Alcohol,
                        ByDrone              = model.ByDrone,
                        CarbonNeutral        = model.CarbonNeutral,
                        DeliveryConfirmation = getAttributeValue((DeliveryConfirmation)model.DeliveryConfirmation),
                        Endorsement          = getAttributeValue((Endorsement)model.Endorsement),
                        HandlingInstructions = model.HandlingInstructions,
                        Hazmat                  = getAttributeValue((HazmatType)model.Hazmat),
                        InvoiceNumber           = model.InvoiceNumber,
                        Machinable              = model.Machinable,
                        PrintCustom1            = model.PrintCustom1,
                        PrintCustomCode1        = getAttributeValue((CustomCode)model.PrintCustomCode1),
                        PrintCustom2            = model.PrintCustom2,
                        PrintCustomCode2        = getAttributeValue((CustomCode)model.PrintCustomCode2),
                        PrintCustom3            = model.PrintCustom3,
                        PrintCustomCode3        = getAttributeValue((CustomCode)model.PrintCustomCode3),
                        SpecialRatesEligibility = getAttributeValue((SpecialRate)model.SpecialRatesEligibility),
                        CertifiedMail           = model.CertifiedMail,
                        RegisteredMail          = model.RegisteredMail,
                        RegisteredMailAmount    = model.RegisteredMailAmount,
                        ReturnReceipt           = model.ReturnReceipt
                    },
                    CustomsInfoDetails = !model.UseCustomsInfo ? null : new()
                    {
                        ContentsType        = getAttributeValue((ContentsType)model.ContentsType),
                        RestrictionType     = getAttributeValue((RestrictionType)model.RestrictionType),
                        NonDeliveryOption   = getAttributeValue((NonDeliveryOption)model.NonDeliveryOption),
                        ContentsExplanation = model.ContentsExplanation,
                        RestrictionComments = model.RestrictionComments,
                        CustomsCertify      = model.CustomsCertify,
                        CustomsSigner       = model.CustomsSigner,
                        EelPfc = model.EelPfc
                    }
                };
                var(_, error) = await _easyPostService.UpdateShipmentAsync(shipment, request);

                if (!string.IsNullOrEmpty(error))
                {
                    var locale = await _localizationService.GetResourceAsync("Plugins.Shipping.EasyPost.Error");

                    _notificationService.ErrorNotification(string.Format(locale, error, Url.Action("List", "Log")), false);
                }
                else
                {
                    var locale = await _localizationService.GetResourceAsync("Plugins.Shipping.EasyPost.Success");

                    _notificationService.SuccessNotification(locale);
                }
            }

            return(RedirectToAction("ShipmentDetails", "Order", new { id = shipment?.Id ?? 0 }));
        }
        protected async Task <ShipmentDetailsModel> PrepareShipmentDetailsModelAsync(Shipment shipment)
        {
            Guard.NotNull(shipment, nameof(shipment));

            var order = shipment.Order;

            if (order == null)
            {
                throw new SmartException(T("Order.NotFound", shipment.OrderId));
            }

            var currentStore = Services.StoreContext.CurrentStore;
            var store        = currentStore.Id != order.StoreId
                ? (await _db.Stores.FindByIdAsync(order.StoreId, false) ?? currentStore)
                : currentStore;
            var settingFactory  = Services.SettingFactory;
            var catalogSettings = await settingFactory.LoadSettingsAsync <CatalogSettings>(store.Id);

            var shippingSettings = await settingFactory.LoadSettingsAsync <ShippingSettings>(store.Id);

            var model = new ShipmentDetailsModel
            {
                Id                = shipment.Id,
                TrackingNumber    = shipment.TrackingNumber,
                TrackingNumberUrl = shipment.TrackingUrl
            };

            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }

            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            var srcm = _providerManager.GetProvider <IShippingRateComputationMethod>(order.ShippingRateComputationMethodSystemName, store.Id);

            if (srcm != null && srcm.IsShippingRateComputationMethodActive(shippingSettings))
            {
                var shipmentTracker = srcm.Value.ShipmentTracker;
                if (shipmentTracker != null)
                {
                    // The URL entered by the merchant takes precedence over an automatically generated URL.
                    if (model.TrackingNumberUrl.IsEmpty())
                    {
                        model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                    }

                    if (shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = await shipmentTracker.GetShipmentEventsAsync(shipment.TrackingNumber);

                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentEventCountry = await _db.Countries
                                                           .AsNoTracking()
                                                           .ApplyIsoCodeFilter(shipmentEvent.CountryCode)
                                                           .FirstOrDefaultAsync();

                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel
                                {
                                    Country                                 = shipmentEventCountry != null?shipmentEventCountry.GetLocalized(x => x.Name) : shipmentEvent.CountryCode,
                                                                  Date      = shipmentEvent.Date,
                                                                  EventName = shipmentEvent.EventName,
                                                                  Location  = shipmentEvent.Location
                                };

                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            // Products in this shipment.
            model.ShowSku = catalogSettings.ShowProductSku;

            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = await _db.OrderItems
                                .Include(x => x.Product)
                                .FindByIdAsync(shipmentItem.OrderItemId, false);

                if (orderItem == null)
                {
                    continue;
                }

                var attributeCombination = await _productAttributeMaterializer.FindAttributeCombinationAsync(orderItem.Product.Id, orderItem.AttributeSelection);

                orderItem.Product.MergeWithCombination(attributeCombination);

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = orderItem.Product.Sku,
                    ProductId       = orderItem.Product.Id,
                    ProductName     = orderItem.Product.GetLocalized(x => x.Name),
                    ProductSeName   = await orderItem.Product.GetActiveSlugAsync(),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity
                };

                shipmentItemModel.ProductUrl = await _productUrlHelper.GetProductUrlAsync(shipmentItemModel.ProductSeName, orderItem);

                model.Items.Add(shipmentItemModel);
            }

            model.Order = await _orderHelper.PrepareOrderDetailsModelAsync(order);

            return(model);
        }
Beispiel #11
0
        protected ShipmentDetailsModel PrepareShipmentDetailsModel(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

            var order = shipment.Order;

            if (order == null)
            {
                throw new Exception("order cannot be loaded");
            }

            var store            = _storeService.GetStoreById(order.StoreId) ?? _services.StoreContext.CurrentStore;
            var catalogSettings  = _services.Settings.LoadSetting <CatalogSettings>(store.Id);
            var shippingSettings = _services.Settings.LoadSetting <ShippingSettings>(store.Id);

            var model = new ShipmentDetailsModel();

            model.Id = shipment.Id;
            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }
            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            //tracking number and shipment information
            model.TrackingNumber = shipment.TrackingNumber;
            var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);

            if (srcm != null && srcm.IsShippingRateComputationMethodActive(shippingSettings))
            {
                var shipmentTracker = srcm.Value.ShipmentTracker;
                if (shipmentTracker != null)
                {
                    model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                    if (shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);
                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel();
                                var shipmentEventCountry     = _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);
                                shipmentStatusEventModel.Country = shipmentEventCountry != null
                                                                       ? shipmentEventCountry.GetLocalized(x => x.Name)
                                                                       : shipmentEvent.CountryCode;

                                shipmentStatusEventModel.Date      = shipmentEvent.Date;
                                shipmentStatusEventModel.EventName = shipmentEvent.EventName;
                                shipmentStatusEventModel.Location  = shipmentEvent.Location;
                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = catalogSettings.ShowProductSku;
            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = _orderService.GetOrderItemById(shipmentItem.OrderItemId);
                if (orderItem == null)
                {
                    continue;
                }

                orderItem.Product.MergeWithCombination(orderItem.AttributesXml);
                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel()
                {
                    Id              = shipmentItem.Id,
                    Sku             = orderItem.Product.Sku,
                    ProductId       = orderItem.Product.Id,
                    ProductName     = orderItem.Product.GetLocalized(x => x.Name),
                    ProductSeName   = orderItem.Product.GetSeName(),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity,
                };
                model.Items.Add(shipmentItemModel);
            }

            //order details model
            model.Order = PrepareOrderDetailsModel(order);

            return(model);
        }
        protected ShipmentDetailsModel PrepareShipmentDetailsModel(Shipment shipment)
        {
            if (shipment == null)
            {
                throw new ArgumentNullException("shipment");
            }

            var order = shipment.Order;

            if (order == null)
            {
                throw new SmartException(T("Order.NotFound", shipment.OrderId));
            }

            var store            = _storeService.GetStoreById(order.StoreId) ?? _services.StoreContext.CurrentStore;
            var catalogSettings  = _services.Settings.LoadSetting <CatalogSettings>(store.Id);
            var shippingSettings = _services.Settings.LoadSetting <ShippingSettings>(store.Id);

            var model = new ShipmentDetailsModel
            {
                Id             = shipment.Id,
                TrackingNumber = shipment.TrackingNumber
            };

            if (shipment.ShippedDateUtc.HasValue)
            {
                model.ShippedDate = _dateTimeHelper.ConvertToUserTime(shipment.ShippedDateUtc.Value, DateTimeKind.Utc);
            }

            if (shipment.DeliveryDateUtc.HasValue)
            {
                model.DeliveryDate = _dateTimeHelper.ConvertToUserTime(shipment.DeliveryDateUtc.Value, DateTimeKind.Utc);
            }

            var srcm = _shippingService.LoadShippingRateComputationMethodBySystemName(order.ShippingRateComputationMethodSystemName);

            if (srcm != null && srcm.IsShippingRateComputationMethodActive(shippingSettings))
            {
                var shipmentTracker = srcm.Value.ShipmentTracker;
                if (shipmentTracker != null)
                {
                    model.TrackingNumberUrl = shipmentTracker.GetUrl(shipment.TrackingNumber);
                    if (shippingSettings.DisplayShipmentEventsToCustomers)
                    {
                        var shipmentEvents = shipmentTracker.GetShipmentEvents(shipment.TrackingNumber);
                        if (shipmentEvents != null)
                        {
                            foreach (var shipmentEvent in shipmentEvents)
                            {
                                var shipmentEventCountry = _countryService.GetCountryByTwoLetterIsoCode(shipmentEvent.CountryCode);

                                var shipmentStatusEventModel = new ShipmentDetailsModel.ShipmentStatusEventModel
                                {
                                    Country   = (shipmentEventCountry != null ? shipmentEventCountry.GetLocalized(x => x.Name) : shipmentEvent.CountryCode),
                                    Date      = shipmentEvent.Date,
                                    EventName = shipmentEvent.EventName,
                                    Location  = shipmentEvent.Location
                                };

                                model.ShipmentStatusEvents.Add(shipmentStatusEventModel);
                            }
                        }
                    }
                }
            }

            //products in this shipment
            model.ShowSku = catalogSettings.ShowProductSku;

            foreach (var shipmentItem in shipment.ShipmentItems)
            {
                var orderItem = _orderService.GetOrderItemById(shipmentItem.OrderItemId);
                if (orderItem == null)
                {
                    continue;
                }

                orderItem.Product.MergeWithCombination(orderItem.AttributesXml);

                var attributeQueryData = new List <List <int> >();

                var shipmentItemModel = new ShipmentDetailsModel.ShipmentItemModel
                {
                    Id              = shipmentItem.Id,
                    Sku             = orderItem.Product.Sku,
                    ProductId       = orderItem.Product.Id,
                    ProductName     = orderItem.Product.GetLocalized(x => x.Name),
                    ProductSeName   = orderItem.Product.GetSeName(),
                    AttributeInfo   = orderItem.AttributeDescription,
                    QuantityOrdered = orderItem.Quantity,
                    QuantityShipped = shipmentItem.Quantity
                };

                if (orderItem.Product.ProductType != ProductType.BundledProduct)
                {
                    _productAttributeParser.DeserializeQueryData(attributeQueryData, orderItem.AttributesXml, orderItem.ProductId);
                }
                else if (orderItem.Product.BundlePerItemPricing && orderItem.BundleData.HasValue())
                {
                    var bundleData = orderItem.GetBundleData();

                    bundleData.ForEach(x => _productAttributeParser.DeserializeQueryData(attributeQueryData, x.AttributesXml, x.ProductId, x.BundleItemId));
                }

                shipmentItemModel.ProductUrl = _productAttributeParser.GetProductUrlWithAttributes(attributeQueryData, shipmentItemModel.ProductSeName);

                model.Items.Add(shipmentItemModel);
            }

            model.Order = PrepareOrderDetailsModel(order);

            return(model);
        }