示例#1
0
        public CustomerOrder CreateNewCart(IUnitOfWork unitOfWork, OrderHistory customerOrder, AddRmaParameter parameter, AddRmaResult result)
        {
            var orderNumber = string.Empty;

            if (string.IsNullOrEmpty(customerOrder.WebOrderNumber) && !string.IsNullOrEmpty(customerOrder.ErpOrderNumber))
            {
                orderNumber = customerOrder.ErpOrderNumber.Split('-')[0];
            }
            else
            {
                orderNumber = customerOrder.WebOrderNumber.Substring(0, 1) == "W" ? customerOrder.WebOrderNumber.Substring(1) : customerOrder.WebOrderNumber.Split('-')[0];
            }

            Customer customer = new Customer();

            if (string.IsNullOrEmpty(customerOrder.CustomerSequence) || customerOrder.CustomerSequence.Equals(customerOrder.CustomerNumber))
            {
                customer = unitOfWork.GetRepository <Customer>().GetTable().Where(x => x.CustomerNumber == customerOrder.CustomerNumber).FirstOrDefault();
            }
            else
            {
                customer = unitOfWork.GetRepository <Customer>().GetTable().Where(x => x.CustomerNumber == customerOrder.CustomerNumber && x.CustomerSequence == customerOrder.CustomerSequence).FirstOrDefault();
            }

            SiteContextDto siteContextDto = new SiteContextDto(SiteContext.Current);
            var            websiteId      = siteContextDto.Website.Id;
            var            website        = siteContextDto.Website;

            Salesperson salesperson = new Salesperson();

            salesperson = unitOfWork.GetRepository <Salesperson>().GetTable().Where(x => x.Name.Equals(customerOrder.Salesperson)).FirstOrDefault();

            ShipVia shipVia = unitOfWork.GetRepository <ShipVia>().GetTable().Where(x => x.ShipCode.ToLower().Equals(customerOrder.ShipCode.ToLower())).FirstOrDefault();

            CustomerOrder cart = new CustomerOrder()
            {
                Id              = new Guid(),
                OrderNumber     = customSettings.RMA_OrderNumberPrefix + orderNumber,
                OrderDate       = DateTimeProvider.Current.Now,
                Customer        = customer,
                WebsiteId       = websiteId,
                Website         = website,
                Status          = "Return Requested",
                CustomerPO      = customerOrder.CustomerPO,
                Notes           = customerOrder.Notes,
                Type            = "Return Requested",
                Salesperson     = salesperson,
                SalespersonId   = salesperson.Id,
                ShippingCharges = customerOrder.ShippingCharges,
                ShipCode        = customerOrder.ShipCode,
                ShipVia         = shipVia
            };

            foreach (var promotion in customerOrder.OrderHistoryPromotions.ToList())
            {
                CustomerOrderPromotion orderPromotion = new CustomerOrderPromotion()
                {
                    Id = new Guid(),
                    CustomerOrderId  = cart.Id,
                    PromotionId      = promotion.PromotionId,
                    OrderLineId      = promotion.OrderHistoryLineId,
                    Amount           = promotion.Amount,
                    CustomProperties = promotion.CustomProperties
                };
                cart.CustomerOrderPromotions.Add(orderPromotion);
            }

            this.CartPipeline.SetBillTo(new SetBillToParameter()
            {
                Cart   = cart,
                BillTo = cart.Customer
            });

            this.CartPipeline.SetShipTo(new SetShipToParameter()
            {
                Cart   = cart,
                ShipTo = cart.ShipTo
            });

            // Add the Returned Sequence in the Order Number
            var returnedSeq = unitOfWork.GetRepository <CustomerOrder>().GetTable().Where(x => x.OrderNumber.StartsWith(cart.OrderNumber)).Count();

            if (returnedSeq != 0)
            {
                returnedSeq += 1;
            }
            else
            {
                returnedSeq = 1;
            }
            cart.OrderNumber = cart.OrderNumber + "/" + returnedSeq;

            // Getting the Invoice Number
            var invoiceNumber = string.Empty;

            if (!string.IsNullOrEmpty(result.OrderHistory.ErpOrderNumber))
            {
                var erpNumber    = result.OrderHistory.ErpOrderNumber.Split('-')[0];
                var invoiceQuery = (from ih in unitOfWork.GetRepository <InvoiceHistory>().GetTable()
                                    join ihl in unitOfWork.GetRepository <InvoiceHistoryLine>().GetTable()
                                    on ih.Id equals ihl.InvoiceHistoryId
                                    where ihl.ErpOrderNumber == erpNumber
                                    select ih.InvoiceNumber);
                if (!string.IsNullOrEmpty(invoiceQuery.FirstOrDefault()))
                {
                    invoiceNumber = invoiceQuery.FirstOrDefault().Split('-')[0].ToString();
                }
            }

            foreach (var customProperty in customerOrder.CustomProperties)
            {
                cart.SetProperty(customProperty.Name, customProperty.Value);
            }

            var chkReturnType = 0; // To check partial return or full return based on quantity return

            foreach (var ol in customerOrder.OrderHistoryLines.ToList())
            {
                RmaLineDto rmaLineDto = parameter.RmaLines.FirstOrDefault((RmaLineDto r) => r.Line.Equals(ol.LineNumber)) ?? new RmaLineDto();
                if (rmaLineDto.RmaQtyRequested != 0)
                {
                    AddCartLineParameter addCartLineParameter = new AddCartLineParameter();
                    addCartLineParameter.Cart             = cart;
                    addCartLineParameter.Product          = unitOfWork.GetRepository <Product>().GetTable().Where(x => x.Name.Equals(ol.ProductErpNumber)).FirstOrDefault();
                    addCartLineParameter.QtyOrdered       = rmaLineDto.RmaQtyRequested;
                    addCartLineParameter.UnitOfMeasure    = ol.UnitOfMeasure;
                    addCartLineParameter.Notes            = ol.Notes;
                    addCartLineParameter.CustomProperties = ol.CustomProperties.ToList();

                    Insite.Cart.Services.Pipelines.Results.AddCartLineResult addCartLineResult = CartPipeline.AddCartLine(addCartLineParameter);
                    addCartLineResult.CartLine.UnitCost          = ol.UnitCost;
                    addCartLineResult.CartLine.UnitListPrice     = addCartLineParameter.Product.BasicListPrice;
                    addCartLineResult.CartLine.UnitNetPrice      = ol.UnitNetPrice;
                    addCartLineResult.CartLine.UnitRegularPrice  = ol.UnitRegularPrice;
                    addCartLineResult.CartLine.TotalNetPrice     = ol.TotalNetPrice;
                    addCartLineResult.CartLine.TotalRegularPrice = ol.TotalRegularPrice;

                    if (ol.QtyOrdered > rmaLineDto.RmaQtyRequested)
                    {
                        chkReturnType += 1;
                    }
                }
            }

            if (chkReturnType != 0 || customerOrder.OrderHistoryLines.Count() > cart.OrderLines.Count())
            {
                cart.Notes = invoiceNumber + "/" + 1; // 1 indicates the partial return
            }
            else
            {
                cart.Notes = invoiceNumber + "/" + 0; // 0 indicates the full return
            }

            unitOfWork.GetRepository <CustomerOrder>().Insert(cart);
            unitOfWork.Save();

            return(cart);
        }
        protected CustomerOrder CreateNewCart(IUnitOfWork unitOfWork, CustomerOrder customerOrder, AddRmaParameter parameter, AddRmaResult result)
        {
            CustomSettings customSettings = new CustomSettings();
            CustomerOrder  cart           = new CustomerOrder()
            {
                Id                       = Guid.NewGuid(),
                OrderNumber              = customSettings.RMA_OrderNumberPrefix + customerOrder.OrderNumber.Substring(1),
                OrderDate                = DateTimeProvider.Current.Now,
                Customer                 = customerOrder.Customer,
                ShipTo                   = customerOrder.ShipTo,
                DropShipCustomer         = customerOrder.DropShipCustomer,
                WebsiteId                = customerOrder.WebsiteId,
                Website                  = customerOrder.Website,
                Affiliate                = customerOrder.Affiliate,
                ShipVia                  = customerOrder.ShipVia,
                InitiatedByUserProfile   = customerOrder.InitiatedByUserProfile,
                InitiatedByUserProfileId = customerOrder.InitiatedByUserProfileId,
                CurrencyId               = customerOrder.CurrencyId,
                PlacedByUserName         = customerOrder.PlacedByUserName,
                PlacedByUserProfile      = customerOrder.PlacedByUserProfile,
                Status                   = "Return Requested",
                CustomerPO               = customerOrder.CustomerPO,
                Notes                    = customerOrder.Notes,
                Type                     = "Return Requested",
                Salesperson              = customerOrder.Salesperson,
                SalespersonId            = customerOrder.SalespersonId,
                PlacedByUserProfileId    = customerOrder.PlacedByUserProfileId,
                ShippingCharges          = customerOrder.ShippingCharges
            };

            cart.OrderPromotionCodes           = customerOrder.OrderPromotionCodes.ToList();
            cart.TermsCode                     = customerOrder.TermsCode;
            cart.ShippingCalculationNeededAsOf = DateTimeOffset.Now;

            this.CartPipeline.SetBillTo(new SetBillToParameter()
            {
                Cart   = cart,
                BillTo = cart.Customer
            });

            this.CartPipeline.SetShipTo(new SetShipToParameter()
            {
                Cart   = cart,
                ShipTo = cart.ShipTo
            });

            // Add the Returned Sequence in the Order Number
            var returnedSeq = unitOfWork.GetRepository <CustomerOrder>().GetTable().Where(x => x.OrderNumber.StartsWith(cart.OrderNumber)).Count();

            if (returnedSeq != 0)
            {
                returnedSeq += 1;
            }
            else
            {
                returnedSeq = 1;
            }
            cart.OrderNumber = cart.OrderNumber + "/" + returnedSeq;
            result.Properties.Add("ReturnNumber", cart.OrderNumber);

            // Getting the Invoice Number
            var invoiceNumber = string.Empty;

            if (!string.IsNullOrEmpty(result.OrderHistory.ErpOrderNumber))
            {
                var erpNumber    = result.OrderHistory.ErpOrderNumber.Split('-')[0];
                var invoiceQuery = (from ih in unitOfWork.GetRepository <InvoiceHistory>().GetTable()
                                    join ihl in unitOfWork.GetRepository <InvoiceHistoryLine>().GetTable()
                                    on ih.Id equals ihl.InvoiceHistoryId
                                    where ihl.ErpOrderNumber == erpNumber
                                    select ih.InvoiceNumber);
                if (!string.IsNullOrEmpty(invoiceQuery.FirstOrDefault()))
                {
                    invoiceNumber = invoiceQuery.FirstOrDefault().Split('-')[0].ToString();
                    result.Properties.Add("InvoiceNumber", invoiceNumber);
                }
            }

            foreach (var customProperty in customerOrder.CustomProperties)
            {
                cart.SetProperty(customProperty.Name, customProperty.Value);
            }

            List <int> qtyReturn = new List <int>();

            List <int> line          = new List <int>();
            var        chkReturnType = 0; // To check partial return or full return based on quantity return

            foreach (var ol in customerOrder.OrderLines.ToList())
            {
                RmaLineDto rmaLineDto = parameter.RmaLines.FirstOrDefault((RmaLineDto r) => r.Line.Equals(ol.Line)) ?? new RmaLineDto();
                if (rmaLineDto.RmaQtyRequested != 0)
                {
                    if (ol.IsPromotionItem.Equals(true))
                    {
                        line.Add(ol.Line);
                    }

                    Insite.Cart.Services.Pipelines.Parameters.AddCartLineParameter addCartLineParameter = new Insite.Cart.Services.Pipelines.Parameters.AddCartLineParameter();
                    addCartLineParameter.Cart             = cart;
                    addCartLineParameter.Product          = ol.Product;
                    addCartLineParameter.QtyOrdered       = rmaLineDto.RmaQtyRequested;
                    addCartLineParameter.UnitOfMeasure    = ol.UnitOfMeasure;
                    addCartLineParameter.CostCode         = ol.CostCode;
                    addCartLineParameter.Notes            = ol.Notes;
                    addCartLineParameter.CustomProperties = ol.CustomProperties.ToList();
                    qtyReturn.Add(rmaLineDto.RmaQtyRequested);

                    Insite.Cart.Services.Pipelines.Results.AddCartLineResult addCartLineResult = CartPipeline.AddCartLine(addCartLineParameter);
                    addCartLineResult.CartLine.SmartPart         = ol.SmartPart;
                    addCartLineResult.CartLine.UnitCost          = ol.UnitCost;
                    addCartLineResult.CartLine.UnitListPrice     = ol.UnitListPrice;
                    addCartLineResult.CartLine.UnitNetPrice      = ol.UnitNetPrice;
                    addCartLineResult.CartLine.UnitRegularPrice  = ol.UnitRegularPrice;
                    addCartLineResult.CartLine.TotalNetPrice     = ol.TotalNetPrice;
                    addCartLineResult.CartLine.TotalRegularPrice = ol.TotalRegularPrice;

                    if (ol.QtyOrdered > rmaLineDto.RmaQtyRequested)
                    {
                        chkReturnType += 1;
                    }
                }
            }

            if (chkReturnType != 0)
            {
                cart.Notes = invoiceNumber + "/" + 1; // 1 indicates the partial return
            }
            else if (customerOrder.OrderLines.Where(x => x.UnitNetPrice > 0).Count() > cart.OrderLines.Count())
            {
                cart.Notes = invoiceNumber + "/" + 1; // 1 indicates the partial return
            }
            else
            {
                cart.Notes = invoiceNumber + "/" + 0; // 0 indicates the full return
            }

            List <OrderLine> orderLineList = cart.OrderLines.ToList();

            for (int i = 0; i < orderLineList.Count; i++)
            {
                OrderLine orderLine = orderLineList[i];
                if (line.Contains(orderLine.Line))
                {
                    orderLine.IsPromotionItem = true;
                }
            }

            var totalQty = result.OrderHistory.OrderHistoryLines.Where(x => x.ProductErpNumber != "WEBDISCOUNT" && !line.Contains(Convert.ToInt32(x.LineNumber)) && (x.UnitNetPrice > 0 || x.QtyShipped < x.QtyOrdered)).Select(qty => qty.QtyOrdered).Sum();

            foreach (var promotion in customerOrder.CustomerOrderPromotions.ToList())
            {
                CustomerOrderPromotion orderPromotion = new CustomerOrderPromotion();
                orderPromotion.Id = new Guid();
                orderPromotion.CustomerOrderId  = cart.Id;
                orderPromotion.PromotionId      = promotion.PromotionId;
                orderPromotion.OrderLineId      = promotion.OrderLineId;
                orderPromotion.CustomProperties = promotion.CustomProperties;
                cart.CustomerOrderPromotions.Add(orderPromotion);

                if (promotion.OrderLineId == null && cart.Notes.Split('/')[1] == "1" && !promotion.Promotion.Name.ToLower().Contains("shipping") && totalQty != 0 && qtyReturn.Count > 0)
                {
                    orderPromotion.Amount = ((decimal)promotion.Amount / totalQty) * qtyReturn.Sum();
                }
                else
                {
                    orderPromotion.Amount = promotion.Amount;
                }
            }

            unitOfWork.GetRepository <CustomerOrder>().Insert(cart);
            unitOfWork.Save();

            return(cart);
        }
示例#3
0
        protected void CreateSubscriptionOrderLine(IUnitOfWork unitOfWork, CustomerOrder cart, UpdateCartParameter parameter, UpdateCartResult result)
        {
            if (cart == null || cart.OrderLines.Count <= 0)
            {
                return;
            }

            CustomerOrder subscriptionOrder = this.CreateSubscriptionOrder(unitOfWork, cart);

            SubscriptionBrasseler subscriptionBrasseler = new SubscriptionBrasseler();

            subscriptionBrasseler.CustomerOrderId       = subscriptionOrder.Id;
            subscriptionBrasseler.ActivationDate        = subscriptionOrder.OrderDate;
            subscriptionBrasseler.DeActivationDate      = DateTimeOffset.Parse(subscriptionOrder.OrderDate.AddYears(5).ToString());
            subscriptionBrasseler.ParentCustomerOrderId = subscriptionOrder.Id;// BUSA-759 : SS- Unable to identify the parent order ID when user places multiple smart supply orders

            foreach (var ol in cart.OrderLines)
            {
                //BUSA-463 Subscription - Exclude subscription promotional product in susbcription order
                if (!ol.IsPromotionItem)
                {
                    var isSubscriptionOpted = ol.GetProperty("IsSubscriptionOpted", "false");

                    if (!isSubscriptionOpted.EqualsIgnoreCase("true"))
                    {
                        continue;
                    }

                    //save subscriptionOrder payment Method Starts
                    if (parameter.TermsCode.EqualsIgnoreCase("CK"))
                    {
                        subscriptionBrasseler.PaymentMethod = "CK";
                        subscriptionOrder.TermsCode         = "CK";
                    }
                    else if (parameter.IsPaymentProfile && !string.IsNullOrEmpty(parameter.PaymentProfileId))
                    {
                        UserPaymentProfile userPaymentProfile = unitOfWork.GetRepository <UserPaymentProfile>().GetTable().FirstOrDefault(p => p.CardIdentifier == parameter.PaymentProfileId);
                        if (userPaymentProfile != null)
                        {
                            subscriptionBrasseler.PaymentMethod = userPaymentProfile.Id.ToString();
                            subscriptionOrder.TermsCode         = "CC";
                        }
                    }
                    else if (parameter.StorePaymentProfile)
                    {
                        var CCResponse = result.GetCartResult.Cart.CreditCardTransactions.FirstOrDefault();
                        if (CCResponse != null)
                        {
                            var CCidentifier       = CCResponse.PNRef.ToString().Split('|').FirstOrDefault();
                            var userPaymentProfile = unitOfWork.GetRepository <UserPaymentProfile>().GetTable().FirstOrDefault(x => x.CardIdentifier == CCidentifier);
                            if (userPaymentProfile != null)
                            {
                                subscriptionBrasseler.PaymentMethod = userPaymentProfile.Id.ToString();
                            }
                        }
                        subscriptionOrder.TermsCode = cart.TermsCode;
                    }
                    //else
                    //{
                    //    subscriptionBrasseler.PaymentMethod = "CK";
                    //}
                    //save subscriptionOrder payment Method Ends

                    //this.CustomerOrderUtilities.AddOrderLine(subscriptionOrder, this.OrderLineUtilities.Copy(ol));
                    Insite.Cart.Services.Pipelines.Results.AddCartLineResult addCartLineResult = this.CartPipeline.AddCartLine(new Insite.Cart.Services.Pipelines.Parameters.AddCartLineParameter()
                    {
                        Cart             = subscriptionOrder,
                        Product          = ol.Product,
                        QtyOrdered       = ol.QtyOrdered,
                        UnitOfMeasure    = ol.UnitOfMeasure,
                        CostCode         = ol.CostCode,
                        Notes            = ol.Notes,
                        CustomProperties = ol.CustomProperties.ToList()
                    });
                    addCartLineResult.CartLine.SmartPart = ol.SmartPart;

                    for (int index = subscriptionOrder.OrderLines.Count - 1; index >= 0; --index)
                    {
                        OrderLine orderLine = subscriptionOrder.OrderLines.ElementAt <OrderLine>(index);

                        if (!this.ProductUtilities.Value.IsQuoteRequired(orderLine.Product) || cart.Status == "QuoteProposed")
                        {
                            this.ProcessInventory(orderLine);
                        }

                        orderLine.PromotionResultId = null;
                    }
                }
            }

            subscriptionOrder.ShippingCalculationNeededAsOf = (DateTimeOffset)DateTimeProvider.Current.Now;
            subscriptionOrder.RecalculateTax = true;
            this.PromotionEngine.Value.ClearPromotions(subscriptionOrder);
            subscriptionOrder.RecalculatePromotions = true;
            PricingPipeline.GetCartPricing(new GetCartPricingParameter(subscriptionOrder));
            //this.CartHelper.Value.RecalculateCart(unitOfWork, subscriptionOrder, (OrderLine)null, true); kkk

            if (subscriptionOrder.CustomProperties.Where(x => x.Name == "subscriptionFrequencyOpted").Count() > 0)
            {
                var Frequency = subscriptionOrder.CustomProperties.FirstOrDefault(x => x.Name == "subscriptionFrequencyOpted").Value;
                subscriptionBrasseler.Frequency = int.Parse(Frequency);
                //BUSA-871 : Shipping SS order on future date provided by user
                //subscriptionBrasseler.NextDelieveryDate = new DateTimeOffset(DateTimeProvider.Current.Now.AddDays(double.Parse(Frequency)), TimeSpan.Zero);
                subscriptionBrasseler.NextDelieveryDate = (DateTimeOffset.Now.AddDays(double.Parse(Frequency)));
            }
            if (subscriptionBrasseler != null)
            {
                unitOfWork.GetRepository <SubscriptionBrasseler>().Insert(subscriptionBrasseler);
            }

            SendSubscriptionCreationEmail(unitOfWork, subscriptionOrder);
        }