Ejemplo n.º 1
0
        public OrderModel Create(Order entity)
        {
            var model = _modelMapper.Map <OrderModel>(entity);

            model.User = _modelMapper.Map <UserModel>(entity.User);
            if (entity.BillingAddressSerialized != null)
            {
                var billingAddress = _dataSerializer.DeserializeAs <Address>(entity.BillingAddressSerialized);
                model.BillingAddress                   = _modelMapper.Map <AddressModel>(billingAddress);
                model.BillingAddress.CountryName       = billingAddress.Country.Name;
                model.BillingAddress.StateProvinceName = billingAddress.StateOrProvince?.Name ?? billingAddress.StateProvinceName;
            }

            if (entity.ShippingAddressSerialized != null)
            {
                var shippingAddress = _dataSerializer.DeserializeAs <Address>(entity.ShippingAddressSerialized);
                model.ShippingAddress                   = _modelMapper.Map <AddressModel>(shippingAddress);
                model.ShippingAddress.CountryName       = shippingAddress.Country.Name;
                model.ShippingAddress.StateProvinceName = shippingAddress.StateOrProvince?.Name ?? shippingAddress.StateProvinceName;
            }
            model.OrderItems = entity.OrderItems?.Select(Create).ToList();

            //is this subscription model?
            if (model.IsSubscription)
            {
                //get the latest payment info
                var paymentTransaction = _paymentTransactionService
                                         .Get(x => x.OrderGuid == entity.Guid && x.PaymentStatus == PaymentStatus.Complete)
                                         .OrderByDescending(x => x.Id).FirstOrDefault();
                model.LastInvoiceDate = paymentTransaction?.CreatedOn;
                if (paymentTransaction != null && model.OrderItems != null)
                {
                    var minCycleDays = model.OrderItems.Min(x => (int)x.SubscriptionCycle);
                    model.NextInvoiceDate = model.LastInvoiceDate.Value.AddDays(minCycleDays);
                }
            }

            if (!entity.SelectedShippingOption.IsNullEmptyOrWhiteSpace())
            {
                var selectedOptions =
                    _dataSerializer.DeserializeAs <IList <ShippingOption> >(entity.SelectedShippingOption);
                model.SelectedShippingOptions = selectedOptions.Select(x => new ShippingOptionModel()
                {
                    Id           = x.Id,
                    Name         = x.Name,
                    Description  = x.Description,
                    Rate         = x.Rate,
                    DeliveryTime = x.DeliveryTime
                }).ToList();
            }
            return(model);
        }