public override AddRmaResult Execute(IUnitOfWork unitOfWork, AddRmaParameter parameter, AddRmaResult result) { CustomSettings customSettings = new CustomSettings(); dynamic emailModel = new ExpandoObject(); List <RmaLineDto> lstRmaLine = new List <RmaLineDto>(); dynamic lstProduct = new ExpandoObject(); emailModel.CustomerNumber = !string.IsNullOrEmpty(SiteContext.Current.ShipTo.CustomerSequence) ? SiteContext.Current.ShipTo.CustomerSequence : SiteContext.Current.BillTo.CustomerNumber; emailModel.BTEmail = SiteContext.Current.BillTo.Email; emailModel.OrderNumber = result.OrderHistory.WebOrderNumber; emailModel.OrderDate = result.OrderHistory.OrderDate; emailModel.PONumber = result.OrderHistory.CustomerPO; emailModel.BTDisplayName = result.OrderHistory.BTCompanyName; emailModel.Address = result.OrderHistory.BTAddress1 + Environment.NewLine + result.OrderHistory.BTAddress2 + Environment.NewLine + result.OrderHistory.BTCity + ", " + result.OrderHistory.BTState + ", " + result.OrderHistory.BTPostalCode + Environment.NewLine + result.OrderHistory.BTCountry; // adding the returned webordernumber in email var returnNumber = string.Empty; result.Properties.TryGetValue("ReturnNumber", out returnNumber); emailModel.ReturnNumber = returnNumber; // Getting the Invoice Number var invoiceNumber = string.Empty; result.Properties.TryGetValue("InvoiceNumber", out invoiceNumber); emailModel.InvoiceNumber = invoiceNumber; emailModel.shortDescription = new List <ExpandoObject>(); foreach (var orderHistoryLines in result.OrderHistory.OrderHistoryLines) { dynamic products = new ExpandoObject(); var description = unitOfWork.GetRepository <Product>().GetTable().Where(x => x.Name == orderHistoryLines.ProductErpNumber).FirstOrDefault(); if (description != null) // BUSA-1270 : RMA button does not function when there is a WEBDISCOUNT { products.Name = description.Name; products.ShortDescription = description.ShortDescription; emailModel.shortDescription.Add(products); } } emailModel.OrderLines = new List <ExpandoObject>(); foreach (OrderHistoryLine orderHistoryLine in result.OrderHistory.OrderHistoryLines) { RmaLineDto rmaLineDto = parameter.RmaLines.FirstOrDefault <RmaLineDto>((RmaLineDto r) => r.Line.Equals(orderHistoryLine.LineNumber)) ?? new RmaLineDto(); if (rmaLineDto.RmaQtyRequested != 0) { dynamic productErpNumber = new ExpandoObject(); productErpNumber.Name = orderHistoryLine.ProductErpNumber; productErpNumber.ShortDescription = orderHistoryLine.Description; productErpNumber.QtyOrdered = decimal.ToInt32(orderHistoryLine.QtyOrdered); productErpNumber.RMAQtyRequested = rmaLineDto.RmaQtyRequested; productErpNumber.RMAReturnReason = rmaLineDto.RmaReasonCode; productErpNumber.LineNumber = orderHistoryLine.LineNumber; emailModel.OrderLines.Add(productErpNumber); } } emailModel.RmaLines = new List <ExpandoObject>(); foreach (var rma in result.OrderHistory.OrderHistoryLines) { dynamic lines = new ExpandoObject(); var RmaLines = parameter.RmaLines.Where(x => x.Line == rma.LineNumber).FirstOrDefault(); if (RmaLines != null) { lines.Line = RmaLines.Line; emailModel.RmaLines.Add(lines); } } //add rma email flag to identify different email notification emailModel.IsRmaEmail = true; emailModel.Notes = parameter.Notes; result.EmailModel = (ExpandoObject)emailModel; return(base.NextHandler.Execute(unitOfWork, parameter, result)); }
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); }
public override AddRmaResult Execute(IUnitOfWork unitOfWork, AddRmaParameter parameter, AddRmaResult result) { if (result.OrderHistory.CustomerNumber.ElementAt(0) != '1') { result.OrderHistory.Status = "Return Requested"; unitOfWork.Save(); return(base.NextHandler.Execute(unitOfWork, parameter, result)); } using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(customSettings.UPS_Ship_Url); client.DefaultRequestHeaders.Accept.Clear(); ShipRequestModel rmaModel = PopulateRequestData(customSettings, result, unitOfWork); var requestJson = new JavaScriptSerializer().Serialize(rmaModel); HttpContent inputContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); LogHelper.For(this).Info(string.Format("{0}: Ship requestJson: {1}", string.IsNullOrEmpty(result.OrderHistory.WebOrderNumber)? result.OrderHistory.ErpOrderNumber: result.OrderHistory.WebOrderNumber, requestJson)); HttpResponseMessage response = client.PostAsync(client.BaseAddress.ToString(), inputContent).Result; if (response.IsSuccessStatusCode) { string responseData = response.Content.ReadAsStringAsync().Result; LogHelper.For(this).Info(string.Format("{0}: Ship responseDate: {1}", string.IsNullOrEmpty(result.OrderHistory.WebOrderNumber) ? result.OrderHistory.ErpOrderNumber : result.OrderHistory.WebOrderNumber, responseData)); var rmaResponse = new JavaScriptSerializer().Deserialize <ShipResponseModel>(responseData); if (rmaResponse.ShipmentResponse != null && rmaResponse.ShipmentResponse.Response.ResponseStatus.Description.EqualsIgnoreCase("success")) { RmaResponse rma = new RmaResponse(); rma.Id = new Guid(); rma.WebOrderNumber = result.OrderHistory.WebOrderNumber; rma.ErpOrderNumber = result.OrderHistory.ErpOrderNumber; rma.TrackingNumber = rmaResponse.ShipmentResponse.ShipmentResults.PackageResults.TrackingNumber; rma.GraphicImage = rmaResponse.ShipmentResponse.ShipmentResults.PackageResults.ShippingLabel.GraphicImage; rma.HtmlImage = rmaResponse.ShipmentResponse.ShipmentResults.PackageResults.ShippingLabel.HtmlImage; rma.JsonValue = rmaResponse.ToJson(); var returnRquestRepository = unitOfWork.GetRepository <ReturnRequest>(); foreach (Insite.Data.Entities.OrderHistoryLine orderHistoryLine in result.OrderHistory.OrderHistoryLines) { RmaLineDto rmaLineDto = parameter.RmaLines.FirstOrDefault <RmaLineDto>((RmaLineDto r) => r.Line.Equals(orderHistoryLine.LineNumber)) ?? new RmaLineDto(); if (rmaLineDto.RmaQtyRequested != 0) { ReturnRequest returnRequest = new ReturnRequest(); returnRequest.Id = new Guid(); returnRequest.WebOrderNumber = result.OrderHistory.WebOrderNumber; returnRequest.ErpOrderNumber = result.OrderHistory.ErpOrderNumber; returnRequest.ProductNumber = orderHistoryLine.ProductErpNumber; returnRequest.QtyToReturn = rmaLineDto.RmaQtyRequested; returnRequest.ReturnReason = rmaLineDto.RmaReasonCode; returnRequest.RmaNotes = parameter.Notes; returnRequest.ReturnDate = DateTime.Now.Date; returnRquestRepository.Insert(returnRequest); } } var rmaRepository = unitOfWork.GetRepository <RmaResponse>(); rmaRepository.Insert(rma); result.OrderHistory.Status = "Return Requested"; unitOfWork.Save(); } } } return(base.NextHandler.Execute(unitOfWork, parameter, result)); }