protected override void Process() { using (var unit = GetUnitOfWork()) { Int32 connectorID = Int32.Parse(GetConfiguration().AppSettings.Settings["connectorID"].Value); Int32 vendorID = Int32.Parse(GetConfiguration().AppSettings.Settings["ccVendorID"].Value); String path = GetConfiguration().AppSettings.Settings["OrderPath"].Value; var productRepo = unit.Scope.Repository <Product>(); var stockRepo = unit.Scope.Repository <VendorStock>(); #if DEBUG path = @"D:\Concentrator\OrderImport"; #endif var vendor = unit.Scope.Repository <Vendor>().GetSingle(c => c.VendorID == vendorID); var setting = vendor.VendorSettings.Where(c => c.SettingKey == "ShipOrderNumber").FirstOrDefault(); var maxOrderNumber = int.Parse(setting.Value); if (maxOrderNumber == 9999) { maxOrderNumber = 5000; } StoreOrderHelper helper = new StoreOrderHelper(); //check sold to customer - always the same so add and retrieve here var customerRepo = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Customer>(); var soldToCustomer = customerRepo.GetSingle(c => c.CustomerName == "Coolcat Fashion"); if (soldToCustomer == null) { soldToCustomer = new Concentrator.Objects.Models.Orders.Customer() { CustomerAddressLine1 = "Hoofdveste 10", CustomerAddressLine2 = string.Empty, City = "Houten", Country = "NL", CustomerName = "Coolcat Fashion", HouseNumber = "10", PostCode = "3992 DG" }; unit.Scope.Repository <Concentrator.Objects.Models.Orders.Customer>().Add(soldToCustomer); unit.Save(); } WMSDispatcher dispatcher = new WMSDispatcher(); foreach (String file in Directory.GetFiles(path, "*.xlsx")) { String fullPath = Path.Combine(path, file); try { var repo = new StoreOrderRepository(fullPath); var orders = repo.GetOrders(); //validate orders foreach (var order in orders) { Order or = new Order() { OrderLines = new List <OrderLine>(), ConnectorID = 5, CustomerOrderReference = string.Empty, HoldOrder = false, WebSiteOrderNumber = "W_" + maxOrderNumber, SoldToCustomer = soldToCustomer, PaymentTermsCode = "SHOP", PaymentInstrument = string.Empty }; foreach (var orderLineModel in order.OrderLines) { string vendorItemNumber = string.Format("{0} {1} {2}", orderLineModel.Art_Number, orderLineModel.Color_Code, orderLineModel.Size_Code); var product = productRepo.GetSingle(c => c.VendorItemNumber == vendorItemNumber); if (product == null) { continue; } var vendorStock = stockRepo.GetSingle(c => c.VendorID == vendorID && c.ProductID == product.ProductID && c.VendorStockTypeID == 3); var vendorStockWeb = stockRepo.GetSingle(c => c.VendorID == vendorID && c.ProductID == product.ProductID && c.VendorStockTypeID == 1); var vendorPrice = product.VendorAssortments.FirstOrDefault(c => c.VendorID == vendorID).VendorPrices.FirstOrDefault(); //validate and get order line var orderLine = helper.GetOrderLine(orderLineModel, vendorStock.QuantityOnHand, product.ProductID, vendorPrice.Price.Value, vendorPrice.SpecialPrice, product); if (orderLine != null) { vendorStock.QuantityOnHand = vendorStock.QuantityOnHand - orderLine.Quantity; //update Concentrator qty vendorStockWeb.QuantityOnHand = vendorStockWeb.QuantityOnHand - orderLine.Quantity; //update web qty or.OrderLines.Add(orderLine); } } //ship to customer details var shipTo = customerRepo.GetSingle(c => c.CustomerName == order.Store_Number); if (shipTo == null) { shipTo = new Concentrator.Objects.Models.Orders.Customer() { CustomerAddressLine1 = order.Address1, CustomerAddressLine2 = order.Address2, CompanyName = order.Store_Number, CustomerName = order.Store_Number, Country = order.Country, City = order.City, Street = order.Address1, PostCode = order.Postcode, }; customerRepo.Add(shipTo); } shipTo.Street = order.Address1; or.ShippedToCustomer = shipTo; dispatcher.DispatchOrders(new Dictionary <Order, List <OrderLine> >() { { or, or.OrderLines.ToList() } }, vendor, log, unit); if (maxOrderNumber < 9999) { maxOrderNumber++; } else { maxOrderNumber = 5000; } unit.Save(); } String pPath = Path.Combine(path, "Processed"); if (!Directory.Exists(pPath)) { Directory.CreateDirectory(pPath); } FileInfo inf = new FileInfo(file); String nPath = Path.Combine(pPath, String.Format("{0} {1}.xlsx", Path.GetFileNameWithoutExtension(inf.Name), DateTime.Now.ToString("dd-MM-yy hh-mm"))); inf.MoveTo(nPath); } catch (Exception e) { File.Move(fullPath, Path.ChangeExtension(fullPath, ".xlsx.err")); File.Create(Path.Combine(path, Path.GetFileNameWithoutExtension(fullPath) + ".txt.err")); using (FileStream logFile = File.Create(Path.Combine(Path.ChangeExtension(path, ".log")))) { using (StreamWriter writer = new StreamWriter(logFile, Encoding.UTF8)) { writer.WriteLine("Error processing orderlines from file : " + fullPath); writer.WriteLine(String.Empty); writer.WriteLine(e.ToString()); } } log.Error(e); } } setting.Value = maxOrderNumber.ToString(); unit.Save(); } }
public List <Objects.Models.EDI.Order.EdiOrder> ProcessOrder(string type, string document, int?connectorID) { XDocument xdocument = XDocument.Parse(document); using (var unit = ServiceLocator.Current.GetInstance <IUnitOfWork>()) { #region ParseXML var header = xdocument.Element("WebOrderRequest").Element("WebOrderHeader"); var details = xdocument.Element("WebOrderRequest").Element("WebOrderDetails").Elements("WebOrderDetail"); var customer = xdocument.Element("WebOrderRequest").Element("WebCustomer"); var ord = new { OrderHeader = new { isDropShipment = header.Element("CustomerOverride").Element("Dropshipment").Try( c => bool.Parse(c.Value), false), CustomerOrderReference = header.Element("CustomerOrderReference").Value, EdiVersion = header.Element("EdiVersion").Value, BSKIdentifier = header.Element("BSKIdentifier").Value, WebSiteOrderNumber = header.Element("WebSiteOrderNumber").Value, PaymentTermsCode = header.Element("PaymentTermsCode").Value, PaymentInstrument = header.Element("PaymentInstrument").Value, BackOrdersAllowed = header.Try(c => bool.Parse(c.Element("BackOrdersAllowed").Value), false), RouteCode = header.Try(c => c.Element("RouteCode").Value, string.Empty), EANIdentifier = header.Element("ShipToCustomer").Try( c => c.Element("EanIdentifier").Value, string.Empty).Trim(), HoldCode = header.Try(c => c.Element("HoldCode").Value, string.Empty), NonDropDetails = new { ShipToShopID = header.Try(c => c.Element("ShipToShopID").Value, string.Empty) } }, Customer = new { Name = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("Name").Value, string.Empty), AddressLine1 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine1").Value, string.Empty), AddressLine2 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine2").Value, string.Empty), AddressLine3 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine3").Value, string.Empty), HouseNumber = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("HouseNumber").Value, string.Empty), PostCode = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("ZipCode").Value, string.Empty), City = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("City").Value, string.Empty), Country = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("Country").Value, string.Empty), Email = header.Try(dc => dc.Element("CustomerOverride").Element("CustomerContact").Element("Email").Value, string.Empty) }, OrderLines = (from o in details let custReference = o.Element("CustomerReference") let product = o.Element("ProductIdentifier") select new { CustomerReference = custReference == null ? null : new { CustomerOrder = custReference.Try(c => c.Element("CustomerOrder").Value, string.Empty), CustomerOrderLine = custReference.Try(c => c.Element("CustomerOrderLine").Value, string.Empty), CustomerItemNumber = custReference.Try(c => c.Element("CustomerItemNumber").Value, string.Empty) }, Product = product == null ? null : new { ID = product.Element("ProductNumber").Value, EAN = product.Try(c => c.Element("EANIdentifier").Value, string.Empty), ManufacturerItemID = product.Try(c => c.Element("ManufacturerItemID").Value, string.Empty), Qty = o.Try(c => int.Parse(c.Element("Quantity").Value), 1), VendorItemNumber = o.Try(c => c.Element("VendorItemNumber").Value, string.Empty), WarehouseCode = o.Try(c => c.Element("WareHouseCode").Value, string.Empty), UnitPrice = o.Try(c => double.Parse(c.Element("UnitPrice").Value) / 10000, 0), PriceOverride = o.Try(c => bool.Parse(c.Element("PriceOverride").Value), false) } }).ToList(), CustomerContactInfo = customer == null ? null : new { Name = customer.Try(c => c.Element("CustomerAddress").Element("Name").Value, string.Empty), AddressLine1 = customer.Try(c => c.Element("CustomerAddress").Element("AddressLine1").Value, string.Empty), AddressLine2 = customer.Try(c => c.Element("CustomerAddress").Element("AddressLine2").Value, string.Empty), AddressLine3 = customer.Try(c => c.Element("CustomerAddress").Element("AddressLine3").Value, string.Empty), Number = customer.Try(c => c.Element("CustomerAddress").Element("Number").Value, string.Empty), ZipCode = customer.Try(c => c.Element("CustomerAddress").Element("ZipCode").Value, string.Empty), City = customer.Try(c => c.Element("CustomerAddress").Element("City").Value, string.Empty), Country = customer.Try(c => c.Element("CustomerAddress").Element("Country").Value, string.Empty), Email = customer.Try(c => c.Element("CustomerContact").Element("Email").Value, string.Empty), CompanyName = customer.Try(c => c.Element("CustomerContact").Element("Name").Value, string.Empty), CoCNumber = customer.Try(c => c.Element("CustomerContact").Element("CoCNumber").Value, string.Empty) } }; #endregion #region Dropshipment/Non-Drop //Customer customerE = (from c in context.Customers // where c.EANIdentifier == ord.OrderHeader.EANIdentifier // select c).FirstOrDefault(); //if (customerE == null || ord.OrderHeader.EANIdentifier == "0") //{ Concentrator.Objects.Models.Orders.Customer customerE = new Concentrator.Objects.Models.Orders.Customer { EANIdentifier = ord.OrderHeader.EANIdentifier }; unit.Scope.Repository <Concentrator.Objects.Models.Orders.Customer>().Add(customerE); //} if (ord.Customer != null) { customerE.CustomerAddressLine1 = ord.Customer.AddressLine1; customerE.CustomerAddressLine2 = ord.Customer.AddressLine2; customerE.CustomerAddressLine3 = ord.Customer.AddressLine3; customerE.HouseNumber = ord.Customer.HouseNumber; customerE.City = ord.Customer.City; customerE.Country = ord.Customer.Country; customerE.PostCode = ord.Customer.PostCode; customerE.CustomerName = ord.Customer.Name; customerE.CustomerEmail = ord.Customer.Email;//ord.Try(c => c.CustomerContactInfo.Email, string.Empty); } Concentrator.Objects.Models.Orders.Customer customerS = null; if (ord.CustomerContactInfo != null) { customerS = new Concentrator.Objects.Models.Orders.Customer { EANIdentifier = null }; customerS.CustomerAddressLine1 = ord.CustomerContactInfo.AddressLine1; customerS.CustomerAddressLine2 = ord.CustomerContactInfo.AddressLine2; customerS.CustomerAddressLine3 = ord.CustomerContactInfo.AddressLine3; customerS.HouseNumber = ord.CustomerContactInfo.Number; customerS.City = ord.CustomerContactInfo.City; customerS.Country = ord.CustomerContactInfo.Country; customerS.PostCode = ord.CustomerContactInfo.ZipCode; customerS.CustomerName = ord.CustomerContactInfo.Name; customerS.CustomerEmail = ord.CustomerContactInfo.Email; customerS.CoCNumber = ord.CustomerContactInfo.CoCNumber; unit.Scope.Repository <Concentrator.Objects.Models.Orders.Customer>().Add(customerS); } if (!connectorID.HasValue) { ConnectorRelation rel = unit.Scope.Repository <ConnectorRelation>().GetSingle(x => x.CustomerID == customerE.EANIdentifier); if (rel == null || !rel.ConnectorID.HasValue) { throw new Exception("No Connector Relation found for numer" + customerE.EANIdentifier); } else { connectorID = rel.ConnectorID.Value; } } EdiOrder newOrd = new EdiOrder() { ShippedToCustomer = customerE, SoldToCustomer = customerS, Document = document, ConnectorID = connectorID.Value, isDropShipment = ord.OrderHeader.isDropShipment, ReceivedDate = DateTime.Now, BackOrdersAllowed = ord.OrderHeader.BackOrdersAllowed, CustomerOrderReference = ord.OrderHeader.CustomerOrderReference, EdiVersion = ord.OrderHeader.EdiVersion, BSKIdentifier = int.Parse(ord.OrderHeader.BSKIdentifier), WebSiteOrderNumber = ord.OrderHeader.WebSiteOrderNumber, PaymentInstrument = ord.OrderHeader.PaymentInstrument, PaymentTermsCode = ord.OrderHeader.PaymentTermsCode, RouteCode = ord.OrderHeader.RouteCode, HoldCode = ord.OrderHeader.HoldCode }; #endregion var orderRepo = unit.Scope.Repository <EdiOrder>(); var duplicateOrder = orderRepo.GetSingle(d => d.ConnectorID == newOrd.ConnectorID && d.WebSiteOrderNumber == newOrd.WebSiteOrderNumber); if (duplicateOrder != null) { newOrd.HoldOrder = true; } else { newOrd.HoldOrder = false; } orderRepo.Add(newOrd); #region Order Lines foreach (var line in ord.OrderLines) { int?productid = null; var product = unit.Scope.Repository <Concentrator.Objects.Models.Products.Product>().GetSingle(p => p.ProductID == int.Parse(line.Product.ID)); if (product != null) { productid = product.ProductID; } EdiOrderLine ol = new EdiOrderLine { CustomerEdiOrderLineNr = line.CustomerReference.CustomerOrderLine, CustomerOrderNr = line.CustomerReference.CustomerOrder, CustomerItemNumber = line.CustomerReference.CustomerItemNumber, EdiOrder = newOrd, Quantity = line.Product.Qty, ProductID = productid, Price = line.Product.UnitPrice, WareHouseCode = line.Product.WarehouseCode, PriceOverride = line.Product.PriceOverride, EdiProductID = line.Product.ID }; unit.Scope.Repository <EdiOrderLine>().Add(ol); } #endregion unit.Save(); return(new List <EdiOrder>() { newOrd }); } }