public PurchaseRequest GetPurchaseOrder(Concentrator.Objects.Models.Orders.Order order, List <OrderLine> orderLines, Vendor administrativeVendor, Vendor vendor) { using (var unit = ServiceLocator.Current.GetInstance <IUnitOfWork>()) { var customer = order.ShippedToCustomer; Dictionary <int, string> customItemNumbers = new Dictionary <int, string>(); Dictionary <int, VendorAssortment> VendorAssortments = new Dictionary <int, VendorAssortment>(); Dictionary <string, string> additionalItems = new Dictionary <string, string>(); ContentLogic logic = new ContentLogic(unit.Scope); foreach (var l in orderLines) { if (l.Product != null) { //var customItemNumber = l.Product.VendorAssortment.Where(x => x.VendorID == administrativeVendor.VendorID && x.Product == l.Product).Select(x => x.CustomItemNumber).FirstOrDefault(); string customItemNumber = logic.GetVendorItemNumber(l.Product, l.CustomerItemNumber, administrativeVendor.VendorID); customItemNumbers.Add(l.ProductID.Value, customItemNumber); var vass = l.Product.VendorAssortments.Where(x => x.VendorID == vendor.VendorID).Select(x => x).FirstOrDefault(); VendorAssortments.Add(l.ProductID.Value, vass); } else { var additionalOrderProduct = (from aop in unit.Scope.Repository <AdditionalOrderProduct>().GetAllAsQueryable() where aop.ConnectorID == l.Order.ConnectorID && aop.ConnectorProductID == l.CustomerItemNumber && aop.VendorID == administrativeVendor.VendorID select aop).FirstOrDefault(); if (additionalOrderProduct != null) { additionalItems.Add(l.CustomerItemNumber, additionalOrderProduct.VendorProductID); } } } var administrativeVendorSettings = administrativeVendor.PreferredConnectorVendors.Where(x => x.ConnectorID == order.ConnectorID).FirstOrDefault(); var connectorVendorSettings = vendor.PreferredConnectorVendors.Where(x => x.ConnectorID == order.ConnectorID).FirstOrDefault(); PurchaseRequest purchaseRequest = new PurchaseRequest() { bskIdentifier = administrativeVendorSettings.VendorIdentifier, Version = "1.0", PurchaseCustomer = new PurchaseCustomer() { PurchaseCustomerAddress = new PurchaseCustomerAddress() { AddressLine1 = order.ShippedToCustomer.CustomerAddressLine1, AddressLine2 = order.ShippedToCustomer.CustomerAddressLine2, AddressLine3 = order.ShippedToCustomer.CustomerAddressLine3, City = order.ShippedToCustomer.City, Country = order.ShippedToCustomer.Country, Name = order.ShippedToCustomer.CustomerName, ZipCode = order.ShippedToCustomer.PostCode }, PurchaseCustomerContact = new PurchaseCustomerContact() { Email = order.ShippedToCustomer.CustomerEmail, Name = order.ShippedToCustomer.CustomerName } }, PurchaseLines = (from l in orderLines let customItemNumber = l.Product != null ? customItemNumbers[l.ProductID.Value] : additionalItems[l.CustomerItemNumber] let vendorAss = l.Product != null ? VendorAssortments[l.ProductID.Value] : null select new PurchaseLine { ItemNumber = customItemNumber, Price = l.Price.HasValue ? l.Price.Value.ToString() : vendorAss.VendorPrices.FirstOrDefault().Price.Value.ToString(), Product = new Concentrator.Web.Objects.EDI.Purchase.Product() { BrandCode = l.Product.Brand.BrandVendors.Where(x => x.VendorID == administrativeVendorSettings.Vendor.VendorID).Select(x => x.VendorBrandCode).FirstOrDefault(), Description = l.Product.ProductDescriptions.Where(x => x.LanguageID == (int)LanguageTypes.English).Select(x => x.ShortContentDescription).FirstOrDefault() != null ? l.Product.ProductDescriptions.Where(x => x.LanguageID == (int)LanguageTypes.English).Select(x => x.ShortContentDescription).FirstOrDefault() : l.Product.Contents.Where(x => x.ConnectorID == order.ConnectorID).Select(x => x.ShortDescription).FirstOrDefault(), Description2 = string.Empty, EAN = l.Product.ProductBarcodes.FirstOrDefault() != null ? (l.Product.ProductBarcodes.FirstOrDefault().Barcode.Length == 13 ? l.Product.ProductBarcodes.FirstOrDefault().Barcode : string.Empty) : string.Empty, UPC = l.Product.ProductBarcodes.FirstOrDefault() != null ? (l.Product.ProductBarcodes.FirstOrDefault().Barcode.Length == 12 ? l.Product.ProductBarcodes.FirstOrDefault().Barcode : string.Empty) : string.Empty, ModelNumber = string.Empty, VendorItemNumber = l.Product != null ? l.Product.VendorItemNumber : string.Empty, UnitCost = vendorAss != null ? (vendorAss.VendorPrices.FirstOrDefault().CostPrice.HasValue ? vendorAss.VendorPrices.FirstOrDefault().CostPrice.Value : vendorAss.VendorPrices.FirstOrDefault().Price.Value) : 0, UnitPrice = new Concentrator.Web.Objects.EDI.Purchase.ProductUnitPrice() { HighTaxRate = true, LowTaxRate = false, Text = new string[] { l.Price.HasValue?l.Price.Value.ToString() : vendorAss.VendorPrices.FirstOrDefault().Price.Value.ToString() } } }, Quantity = l.GetDispatchQuantity(), Reference = l.OrderLineID, RequestDate = order.ReceivedDate, ShipToNumber = order.ShippedToCustomer.EANIdentifier, SupplierNumber = (connectorVendorSettings != null && !string.IsNullOrEmpty(connectorVendorSettings.VendorIdentifier)) ? connectorVendorSettings.VendorIdentifier : string.Empty, SupplierSalesOrder = l.OrderID, Remark = l.Remarks //, //SupplierSalesOrder = order.OrderID }).ToArray() }; return(purchaseRequest); } }
public override Concentrator.Web.Objects.EDI.DirectShipment.DirectShipmentRequest GetDirectShipmentOrder(Concentrator.Objects.Models.Orders.Order order, List <OrderLine> orderLines, Vendor administrativeVendor, Vendor vendor) { throw new NotImplementedException(); }
public override DirectShipmentRequest GetDirectShipmentOrder(Concentrator.Objects.Models.Orders.Order order, List <Concentrator.Objects.Models.Orders.OrderLine> orderLines, Vendor administrativeVendor, Vendor vendor) { var customer = order.ShippedToCustomer; using (var unit = ServiceLocator.Current.GetInstance <IUnitOfWork>()) { Dictionary <int, string> customItemNumbers = new Dictionary <int, string>(); Dictionary <int, VendorAssortment> VendorAssortments = new Dictionary <int, VendorAssortment>(); Dictionary <string, string> additionalItems = new Dictionary <string, string>(); ContentLogic logic = new ContentLogic(unit.Scope); foreach (var l in orderLines) { if (l.Product != null) { string customItemNumber1 = logic.GetVendorItemNumber(l.Product, l.CustomerItemNumber, administrativeVendor.VendorID); var customItemNumber = l.Product.VendorAssortments.Where(x => x.VendorID == administrativeVendor.VendorID && x.Product == l.Product).Select(x => x.CustomItemNumber).FirstOrDefault(); //l.ProductID.HasValue ? ContentLogic.GetVendorProductID(l.ProductID.Value, administrativeVendor.VendorID, l.CustomerItemNumber) : l.CustomerItemNumber; customItemNumbers.Add(l.ProductID.Value, customItemNumber); var vass = l.Product.VendorAssortments.Where(x => x.VendorID == vendor.VendorID).Select(x => x).FirstOrDefault(); VendorAssortments.Add(l.ProductID.Value, vass); } else { var additionalOrderProduct = (from aop in unit.Scope.Repository <AdditionalOrderProduct>().GetAllAsQueryable() where aop.ConnectorID == l.Order.ConnectorID && aop.ConnectorProductID == l.CustomerItemNumber && aop.VendorID == administrativeVendor.VendorID select aop).FirstOrDefault(); if (additionalOrderProduct != null && !additionalItems.ContainsKey(l.CustomerItemNumber)) { additionalItems.Add(l.CustomerItemNumber, additionalOrderProduct.VendorProductID); } } } var administrativeVendorSettings = administrativeVendor.PreferredConnectorVendors.Where(x => x.ConnectorID == order.ConnectorID).FirstOrDefault(); var connectorVendorSettings = vendor.PreferredConnectorVendors.Where(x => x.ConnectorID == order.ConnectorID).FirstOrDefault(); DirectShipmentRequest directShipmentOrder = new DirectShipmentRequest() { bskIdentifier = !string.IsNullOrEmpty(administrativeVendorSettings.VendorIdentifier) ? administrativeVendorSettings.VendorIdentifier : order.BSKIdentifier.ToString(), Version = "1.0", DirectShipmentCustomer = new DirectShipmentCustomer() { DirectShipmentCustomerAddress = new DirectShipmentCustomerAddress() { AddressLine1 = order.ShippedToCustomer.CustomerAddressLine1, AddressLine2 = order.ShippedToCustomer.CustomerAddressLine2, AddressLine3 = order.ShippedToCustomer.CustomerAddressLine3, City = order.ShippedToCustomer.City, Country = order.ShippedToCustomer.Country, Name = order.ShippedToCustomer.CustomerName, ZipCode = order.ShippedToCustomer.PostCode, Number = order.ShippedToCustomer.HouseNumber }, DirectShipmentCustomerContact = new DirectShipmentCustomerContact() { Email = order.ShippedToCustomer.CustomerEmail, Name = order.ShippedToCustomer.CustomerName } }, DirectShipmentLines = (from l in orderLines let customItemNumber = l.Product != null ? customItemNumbers[l.ProductID.Value] : additionalItems[l.CustomerItemNumber] let vendorAss = l.Product != null ? VendorAssortments[l.ProductID.Value] : null let description = l.Product != null ? (l.Product.ProductDescriptions.Where(x => x.LanguageID == (int)LanguageTypes.English).Select(x => x.ShortContentDescription).FirstOrDefault() != null ? l.Product.ProductDescriptions.Where(x => x.LanguageID == (int)LanguageTypes.English).Select(x => x.ShortContentDescription).FirstOrDefault() : l.Product.Contents.Where(x => x.ConnectorID == order.ConnectorID).Select(x => x.ShortDescription).FirstOrDefault()) : "Additional Item" let unitCost = (l.ProductID.HasValue ? logic.CalculatePrice(l.ProductID.Value, l.GetDispatchQuantity(), l.Order.Connector, PriceRuleType.CostPrice) : (decimal)l.Price.Value) let unitPrice = (l.ProductID.HasValue ? logic.CalculatePrice(l.ProductID.Value, l.GetDispatchQuantity(), l.Order.Connector, PriceRuleType.UnitPrice) : (decimal)l.Price.Value) select new DirectShipmentLine { ItemNumber = customItemNumber, Price = l.Price.HasValue ? l.Price.Value.ToString() : vendorAss.VendorPrices.FirstOrDefault().Price.Value.ToString(), Product = (l.ProductID.HasValue ? new Concentrator.Web.Objects.EDI.DirectShipment.Product() { BrandCode = l.Product.Brand.BrandVendors.Where(x => x.VendorID == administrativeVendorSettings.Vendor.VendorID).Select(x => x.VendorBrandCode).FirstOrDefault(), Description = string.IsNullOrEmpty(description) ? vendorAss.ShortDescription : description, Description2 = string.Empty, EAN = l.Product.ProductBarcodes.FirstOrDefault() != null ? (l.Product.ProductBarcodes.FirstOrDefault().Barcode.Length == 13 ? l.Product.ProductBarcodes.FirstOrDefault().Barcode : string.Empty) : string.Empty, UPC = l.Product.ProductBarcodes.FirstOrDefault() != null ? (l.Product.ProductBarcodes.FirstOrDefault().Barcode.Length == 12 ? l.Product.ProductBarcodes.FirstOrDefault().Barcode : string.Empty) : string.Empty, ModelNumber = string.Empty, VendorItemNumber = l.Product != null ? l.Product.VendorItemNumber : string.Empty, UnitCost = unitCost > 0 ? unitCost : (vendorAss != null ? (vendorAss.VendorPrices.FirstOrDefault().CostPrice.HasValue ? vendorAss.VendorPrices.FirstOrDefault().CostPrice.Value : (vendorAss.VendorPrices.FirstOrDefault().Price.HasValue ? vendorAss.VendorPrices.FirstOrDefault().Price.Value : 0)) : 0), UnitPrice = new Concentrator.Web.Objects.EDI.DirectShipment.ProductUnitPrice() { HighTaxRate = true, LowTaxRate = false, Text = new string[] { unitPrice > 0 ? unitPrice.ToString() : (l.Price.HasValue ? l.Price.Value.ToString() : (vendorAss.VendorPrices.FirstOrDefault().Price.HasValue ? vendorAss.VendorPrices.FirstOrDefault().Price.Value.ToString() : "0")) } } } : new Concentrator.Web.Objects.EDI.DirectShipment.Product() { }), Quantity = l.GetDispatchQuantity(), Reference = !string.IsNullOrEmpty(order.ShippedToCustomer.CustomerName) ? order.ShippedToCustomer.CustomerName : string.Empty, RequestDate = order.ReceivedDate, ShipToNumber = order.ShippedToCustomer.EANIdentifier, SupplierNumber = (connectorVendorSettings != null && !string.IsNullOrEmpty(connectorVendorSettings.VendorIdentifier)) ? connectorVendorSettings.VendorIdentifier : string.Empty, SupplierSalesOrder = l.OrderID, WebsiteNumber = order.WebSiteOrderNumber, Remark = l.Remarks //, //SupplierSalesOrder = order.OrderID }).ToArray() }; return(directShipmentOrder); } }
public bool PurchaseOrders(Concentrator.Objects.Models.Orders.Order order, List <Concentrator.Objects.Models.Orders.OrderLine> orderLines, Vendor administrativeVendor, Vendor vendor, bool directShipment, IUnitOfWork unit, ILog logger) #region IPurchase Members { try { if (directShipment) { var directShipmentOrder = new BasOrderExporter().GetDirectShipmentOrder(order, orderLines, administrativeVendor, vendor); StringBuilder requestString = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; using (XmlWriter xw = XmlWriter.Create(requestString, settings)) { xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); XmlSerializer serializer = new XmlSerializer(typeof(DirectShipmentRequest)); XmlSerializerNamespaces nm = new XmlSerializerNamespaces(); nm.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); serializer.Serialize(xw, directShipmentOrder, nm); XmlDocument document = new XmlDocument(); document.LoadXml(requestString.ToString()); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(administrativeVendor.VendorSettings.GetValueByKey("EDIUrl", string.Empty)); request.Method = "POST"; HttpPurchasePostState state = new HttpPurchasePostState(orderLines, administrativeVendor, request, "DirectShipmentPost"); byte[] byteData = UTF8Encoding.UTF8.GetBytes(document.OuterXml); using (Stream s = request.GetRequestStream()) { s.Write(byteData, 0, byteData.Length); } IAsyncResult result = request.BeginGetResponse(HttpCallBack, state); result.AsyncWaitHandle.WaitOne(); } return(true); } else { var purchaseOrder = new BasOrderExporter().GetDirectShipmentOrder(order, orderLines, administrativeVendor, vendor); StringBuilder requestString = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; using (XmlWriter xw = XmlWriter.Create(requestString, settings)) { xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); XmlSerializer serializer = new XmlSerializer(typeof(PurchaseRequest)); XmlSerializerNamespaces nm = new XmlSerializerNamespaces(); nm.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); serializer.Serialize(xw, purchaseOrder, nm); XmlDocument document = new XmlDocument(); document.LoadXml(requestString.ToString()); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(administrativeVendor.VendorSettings.GetValueByKey("EDIUrl", string.Empty)); request.Method = "POST"; HttpPurchasePostState state = new HttpPurchasePostState(orderLines, administrativeVendor, request, "PurchasePost"); byte[] byteData = UTF8Encoding.UTF8.GetBytes(document.OuterXml); using (Stream s = request.GetRequestStream()) { s.Write(byteData, 0, byteData.Length); } IAsyncResult result = request.BeginGetResponse(HttpCallBack, state); result.AsyncWaitHandle.WaitOne(); } return(true); } } catch (Exception e) { throw new Exception("Bas dispatching failed", e); } }
public bool PurchaseOrders(Concentrator.Objects.Models.Orders.Order order, List <Concentrator.Objects.Models.Orders.OrderLine> orderLines, Concentrator.Objects.Models.Vendors.Vendor administrativeVendor, Concentrator.Objects.Models.Vendors.Vendor vendor, bool directShipment, IUnitOfWork unit, ILog logger) { var filiaal890StockType = unit.Scope.Repository <VendorStockType>().GetSingle(x => x.StockType == "Filiaal890"); if (filiaal890StockType == null) { throw new Exception("Stocklocation Filiaal890 does not exists, skip order process"); } var cmStockType = unit.Scope.Repository <VendorStockType>().GetSingle(x => x.StockType == "CM"); if (cmStockType == null) { throw new Exception("Stocklocation CM does not exists, skip order process"); } var transferStockType = unit.Scope.Repository <VendorStockType>().GetSingle(x => x.StockType == "Transfer"); if (transferStockType == null) { throw new Exception("Stocklocation Transfer does not exists, skip order process"); } var webShopStockType = unit.Scope.Repository <VendorStockType>().GetSingle(x => x.StockType == "Webshop"); if (webShopStockType == null) { throw new Exception("Stocklocation Webshop does not exists, skip order process"); } var fileLocation = vendor.VendorSettings.GetValueByKey("casmutLocation", string.Empty); if (string.IsNullOrEmpty(fileLocation)) { throw new Exception("No camutlocation vendorsetting for vendor" + vendor.VendorID); } #region File Network Location NetworkExportUtility util = new NetworkExportUtility(); var userName = vendor.VendorSettings.GetValueByKey("CasMutLocationUserName", string.Empty); if (string.IsNullOrEmpty(userName)) { throw new Exception("No Casmutlocation UserName"); } var password = vendor.VendorSettings.GetValueByKey("CasMutLocationPassword", string.Empty); if (string.IsNullOrEmpty(password)) { throw new Exception("No Casmutlocation Password"); } #if DEBUG fileLocation = @"D:\tmp\CC"; #else fileLocation = util.ConnectorNetworkPath(fileLocation, "Y:", userName, password); #endif #endregion var triggerFilePath = Path.Combine(fileLocation, "kasmut.oke"); if (File.Exists(triggerFilePath)) { try { File.Delete(triggerFilePath); } catch (Exception) { } //in case in use by PFA process } using (var engine = new MultiRecordEngine(typeof(Casmut), typeof(DatColNormalSales))) { var file = Path.Combine(fileLocation, "kasmut"); if (!File.Exists(file)) { File.Create(file).Dispose(); } engine.BeginAppendToFile(file); orderLines.Where(c => (!c.Product.IsNonAssortmentItem.HasValue || (c.Product.IsNonAssortmentItem.HasValue && !c.Product.IsNonAssortmentItem.Value))).ToList().ForEach(line => { var filiaal890Stock = line.Product.VendorStocks.FirstOrDefault(x => x.VendorStockTypeID == filiaal890StockType.VendorStockTypeID && x.VendorID == 1); if (filiaal890Stock == null) { throw new Exception(string.Format("Stocklocation Filiaal890 does not exists for product {0}, skip order process", line.ProductID)); } var cmStock = line.Product.VendorStocks.FirstOrDefault(x => x.VendorStockTypeID == cmStockType.VendorStockTypeID && x.VendorID == 1); if (cmStock == null) { throw new Exception(string.Format("Stocklocation CM does not exists for product {0}, skip order process", line.ProductID)); } var transferStock = line.Product.VendorStocks.FirstOrDefault(x => x.VendorStockTypeID == transferStockType.VendorStockTypeID && x.VendorID == 1); if (transferStock == null) { throw new Exception(string.Format("Stocklocation Transfer does not exists for product {0}, skip order process", line.ProductID)); } var webshopStock = line.Product.VendorStocks.FirstOrDefault(x => x.VendorStockTypeID == webShopStockType.VendorStockTypeID && x.VendorID == 1); if (webshopStock == null) { throw new Exception(string.Format("Stocklocation Webshop does not exists for product {0}, skip order process", line.ProductID)); } try { logger.Info("For order " + order.WebSiteOrderNumber + " and orderline " + line.OrderLineID); logger.Info("Transfer quantity is " + transferStock.QuantityOnHand); logger.Info("CM quantity is " + cmStock.QuantityOnHand); logger.Info("WMS quantity is " + filiaal890Stock.QuantityOnHand); logger.Info("Desired qty is " + line.Quantity); } catch (Exception) { } int transferQuantity = (filiaal890Stock.QuantityOnHand + transferStock.QuantityOnHand) - line.Quantity; if (transferQuantity < 0) { int transferPrePickQuanity = cmStock.QuantityOnHand - Math.Abs(transferQuantity); int quantityToDispatch = Math.Abs(transferQuantity); if (transferPrePickQuanity < 0) { quantityToDispatch = Math.Abs(transferQuantity) - Math.Abs(transferPrePickQuanity); } if (quantityToDispatch < Math.Abs(transferQuantity)) { int quantityCancelled = Math.Abs(transferQuantity) - quantityToDispatch; line.SetStatus(OrderLineStatus.Cancelled, unit.Scope.Repository <OrderLedger>(), quantityCancelled); CancelLine(line, unit, quantityCancelled, "Out of stock, no CM stock availible"); } if (quantityToDispatch > 0) { var barcode = line.Product.ProductBarcodes.FirstOrDefault(x => x.BarcodeType.HasValue && x.BarcodeType.Value == 0); Casmut casmut = new Casmut() { EAN = barcode != null ? barcode.Barcode : string.Empty, PickTicketNumber = line.OrderLineID, PosNumber = 100, TicketNumber = line.OrderLineID, SalesDate = DateTime.Now, ShopNumber = 890, SKUCount = quantityToDispatch }; line.SetStatus(OrderLineStatus.ProcessedKasmut, unit.Scope.Repository <OrderLedger>(), quantityToDispatch); // If complete quantity is send if (quantityToDispatch == line.Quantity) { line.SetStatus(OrderLineStatus.ProcessedExportNotification, unit.Scope.Repository <OrderLedger>()); } casmut.SalesValue = (int)Math.Round((double)(line.UnitPrice.HasValue ? (int)Math.Round((((line.UnitPrice.Value - (line.LineDiscount.HasValue ? line.LineDiscount.Value : 0)) * quantityToDispatch) * 100)) : 0)); engine.WriteNext(casmut); #if DEBUG engine.Flush(); #endif cmStock.QuantityOnHand = cmStock.QuantityOnHand - quantityToDispatch; transferStock.QuantityOnHand = transferStock.QuantityOnHand + quantityToDispatch; unit.Save(); } } filiaal890Stock.QuantityOnHand = filiaal890Stock.QuantityOnHand - line.GetDispatchQuantity(); webshopStock.QuantityOnHand = cmStock.QuantityOnHand + transferStock.QuantityOnHand + filiaal890Stock.QuantityOnHand; line.SetStatus(OrderLineStatus.ReadyToOrder, unit.Scope.Repository <OrderLedger>()); try { logger.Info("After processing for order " + order.WebSiteOrderNumber + " and orderline " + line.OrderLineID); logger.Info("Transfer quantity is " + transferStock.QuantityOnHand); logger.Info("CM quantity is " + cmStock.QuantityOnHand); logger.Info("WMS quantity is " + filiaal890Stock.QuantityOnHand); } catch (Exception e) { logger.Debug(e); } }); engine.Flush(); #if !DEBUG util.DisconnectNetworkPath(fileLocation); #endif } if (!File.Exists(triggerFilePath)) { try { File.Create(triggerFilePath); } catch (Exception) { } } unit.Save(); return(false); }
public abstract DirectShipmentRequest GetDirectShipmentOrder(Concentrator.Objects.Models.Orders.Order order, List <OrderLine> orderLines, Vendor administrativeVendor, Vendor vendor);
private void ProcessResponse(XmlDocument responseString, string prefix, string fileName) { using (var unit = GetUnitOfWork()) { object ediDocument = null; string websiteNumber; Concentrator.Objects.Models.Orders.Order order = null; int bskIdentifier = 0; Connector connector = null; switch (prefix) { case "OrderResponse": ediDocument = ExtractResponseMessage <Concentrator.Web.Objects.EDI.OrderResponse>(responseString); Concentrator.Web.Objects.EDI.OrderResponse ediOrderResponse = (Concentrator.Web.Objects.EDI.OrderResponse)ediDocument; bskIdentifier = ediOrderResponse.OrderHeader.BSKIdentifier; connector = unit.Scope.Repository <Connector>().GetSingle(x => x.BSKIdentifier == bskIdentifier); if (connector == null) { var bsk = bskIdentifier.ToString(); connector = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetAllAsQueryable(x => x.BSKIdentifier == bsk && x.WebSiteOrderNumber == ediOrderResponse.OrderHeader.WebSiteOrderNumber).Select(x => x.Connector).FirstOrDefault(); if (connector == null) { log.WarnFormat("Process response failed for {0} message", prefix); return; } } websiteNumber = ediOrderResponse.OrderHeader.WebSiteOrderNumber; order = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetSingle(x => x.ConnectorID == connector.ConnectorID && x.WebSiteOrderNumber == websiteNumber); if (order != null) { if (order.SoldToCustomerID.HasValue && order.SoldToCustomer.EANIdentifier == "0" && ediOrderResponse.OrderHeader.SoldToCustomer != null && !string.IsNullOrEmpty(ediOrderResponse.OrderHeader.SoldToCustomer.EanIdentifier)) { order.SoldToCustomer.EANIdentifier = ediOrderResponse.OrderHeader.SoldToCustomer.EanIdentifier; } Concentrator.Objects.Models.Orders.OrderResponse orderResponse = new Concentrator.Objects.Models.Orders.OrderResponse() { OrderID = order.OrderID, Currency = "EUR", ReceiveDate = DateTime.Now, ReqDeliveryDate = ediOrderResponse.OrderHeader.RequestedDate.Year >= DateTime.Now.Year ? ediOrderResponse.OrderHeader.RequestedDate : DateTime.Now, ResponseType = OrderResponseTypes.Acknowledgement.ToString(), VendorID = _vendorID, VendorDocumentNumber = ediOrderResponse.OrderHeader.OrderNumber, VendorDocumentDate = DateTime.Now, VendorDocument = responseString.OuterXml, DocumentName = fileName }; foreach (var line in ediOrderResponse.OrderDetails) { int orderLineID = 0; if (line.CustomerReference != null && !string.IsNullOrEmpty(line.CustomerReference.CustomerOrderLine)) { orderLineID = int.Parse(line.CustomerReference.CustomerOrderLine); } else if (order.OrderLines.Any(x => x.OrderLineID == line.LineNumber)) { orderLineID = line.LineNumber; } var orderLine = order.OrderLines.Where(x => x.OrderLineID == orderLineID).FirstOrDefault(); if (orderLine == null) { orderLineID = order.OrderLines.Where(x => x.CustomerItemNumber == line.ProductIdentifier.ProductNumber).Select(x => x.OrderLineID).FirstOrDefault(); } if (orderLineID > 0) { OrderResponseLine orderResponseLine = new OrderResponseLine() { Backordered = line.Quantity.QuantityBackordered, Cancelled = line.Quantity.QuantityCancelled, Ordered = line.Quantity.QuantityOrdered, Shipped = line.Quantity.QuantityShipped, Delivered = line.Quantity.QuantityShipped, Unit = line.UnitOfMeasure.ToString(), VendorItemNumber = line.ProductIdentifier.ProductNumber, VendorLineNumber = line.LineNumber.ToString(), Price = line.UnitPrice, VatAmount = line.TaxAmount, vatPercentage = TaxRates.ContainsKey(line.TaxRate.ToString()) ? TaxRates[line.TaxRate.ToString()] : 0, Processed = false, OrderLineID = orderLineID, OEMNumber = line.ProductIdentifier.ManufacturerItemID, Barcode = line.ProductIdentifier.EANIdentifier, DeliveryDate = line.PromisedDeliveryDate, RequestDate = line.RequestedDate, OrderResponse = orderResponse, }; unit.Scope.Repository <OrderResponseLine>().Add(orderResponseLine); } } unit.Scope.Repository <Concentrator.Objects.Models.Orders.OrderResponse>().Add(orderResponse); unit.Save(); } break; case "ShippingNotification": ediDocument = ExtractResponseMessage <ShippingNotification>(responseString); Concentrator.Web.Objects.EDI.ShippingNotification ediShipmentNotification = (Concentrator.Web.Objects.EDI.ShippingNotification)ediDocument; bskIdentifier = ediShipmentNotification.ShipmentOrderHeader.BSKIdentifier; connector = unit.Scope.Repository <Connector>().GetSingle(x => x.BSKIdentifier == bskIdentifier); if (connector == null) { var bsk = bskIdentifier.ToString(); connector = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetAllAsQueryable(x => x.BSKIdentifier == bsk && x.WebSiteOrderNumber == ediShipmentNotification.ShipmentOrderHeader.WebSiteOrderNumber).Select(x => x.Connector).FirstOrDefault(); if (connector == null) { log.WarnFormat("Process response failed for {0} message", prefix); return; } } websiteNumber = ediShipmentNotification.ShipmentOrderHeader.WebSiteOrderNumber; order = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetSingle(x => x.ConnectorID == connector.ConnectorID && x.WebSiteOrderNumber == websiteNumber); if (order != null) { if (order.SoldToCustomerID.HasValue && order.SoldToCustomer.EANIdentifier == "0" && ediShipmentNotification.ShipmentOrderHeader.SoldToCustomer != null && !string.IsNullOrEmpty(ediShipmentNotification.ShipmentOrderHeader.SoldToCustomer.EanIdentifier)) { order.SoldToCustomer.EANIdentifier = ediShipmentNotification.ShipmentOrderHeader.SoldToCustomer.EanIdentifier; } Concentrator.Objects.Models.Orders.OrderResponse shipOrderResponse = new Concentrator.Objects.Models.Orders.OrderResponse() { OrderID = order.OrderID, Currency = "EUR", ReceiveDate = DateTime.Now, ReqDeliveryDate = ediShipmentNotification.ShipmentOrderHeader.RequestedDate.Year >= DateTime.Now.Year ? ediShipmentNotification.ShipmentOrderHeader.RequestedDate : DateTime.Now, ResponseType = OrderResponseTypes.ShipmentNotification.ToString(), VendorID = _vendorID, VendorDocumentNumber = ediShipmentNotification.ShipmentOrderHeader.OrderNumber, ShippingNumber = ediShipmentNotification.ShipmentOrderHeader.PackingInformation.PackingNumber, TrackAndTrace = string.Join(",", ediShipmentNotification.ShipmentOrderDetails.Select(x => x.ShipmentInformation.TrackAndTraceNumber).ToArray()), VendorDocumentDate = DateTime.Now, VendorDocument = responseString.OuterXml, DocumentName = fileName }; foreach (var line in ediShipmentNotification.ShipmentOrderDetails) { int orderLineID = 0; if (line.CustomerReference != null && line.CustomerReference.CustomerOrderLine != null) { orderLineID = int.Parse(line.CustomerReference.CustomerOrderLine); } else if (!string.IsNullOrEmpty(line.LineNumber)) { if (order.OrderLines.Any(x => x.OrderLineID == int.Parse(line.LineNumber))) { orderLineID = int.Parse(line.LineNumber); } } var orderLine = order.OrderLines.Where(x => x.OrderLineID == orderLineID).FirstOrDefault(); if (orderLine == null) { orderLineID = order.OrderLines.Where(x => x.CustomerItemNumber == line.ProductIdentifier.ProductNumber).Select(x => x.OrderLineID).FirstOrDefault(); } if (orderLineID > 0) { OrderResponseLine orderResponseLine = new OrderResponseLine() { Backordered = line.Quantity.QuantityBackordered, Cancelled = line.Quantity.QuantityCancelled, Ordered = line.Quantity.QuantityOrdered, Shipped = line.Quantity.QuantityShipped, Delivered = line.Quantity.QuantityShipped, Unit = line.UnitOfMeasure.ToString(), VendorItemNumber = line.ProductIdentifier.ProductNumber, VendorLineNumber = line.LineNumber.ToString(), Price = line.UnitPrice, VatAmount = line.TaxAmount, Processed = false, OrderLineID = orderLineID, OEMNumber = line.ProductIdentifier.ManufacturerItemID, Barcode = line.ProductIdentifier.EANIdentifier, DeliveryDate = line.PromissedDeliveryDate, RequestDate = line.RequestedDate, OrderResponse = shipOrderResponse, NumberOfPallets = int.Parse(line.ShipmentInformation.NumberOfPallet), NumberOfUnits = int.Parse(line.ShipmentInformation.NumberOfColli), TrackAndTrace = line.ShipmentInformation.TrackAndTraceNumber, TrackAndTraceLink = string.IsNullOrEmpty(line.ShipmentInformation.TrackAndTraceNumber) ? string.Empty : BuildTrackAndTraceNumber(line.ShipmentInformation.TrackAndTraceNumber, order.ShippedToCustomer.PostCode) }; unit.Scope.Repository <OrderResponseLine>().Add(orderResponseLine); } } unit.Scope.Repository <Concentrator.Objects.Models.Orders.OrderResponse>().Add(shipOrderResponse); unit.Save(); } break; case "InvoiceNotification": ediDocument = ExtractResponseMessage <InvoiceNotification>(responseString); Concentrator.Web.Objects.EDI.InvoiceNotification ediInvoiceNotification = (Concentrator.Web.Objects.EDI.InvoiceNotification)ediDocument; bskIdentifier = ediInvoiceNotification.InvoiceOrderHeader.BSKIdentifier; connector = unit.Scope.Repository <Connector>().GetSingle(x => x.BSKIdentifier == bskIdentifier); if (connector == null) { var bsk = bskIdentifier.ToString(); connector = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetAll(x => x.BSKIdentifier == bsk && x.WebSiteOrderNumber == ediInvoiceNotification.InvoiceOrderHeader.WebSiteOrderNumber).Select(x => x.Connector).FirstOrDefault(); if (connector == null) { log.WarnFormat("Process response failed for {0} message", prefix); return; } } websiteNumber = ediInvoiceNotification.InvoiceOrderHeader.WebSiteOrderNumber; order = unit.Scope.Repository <Concentrator.Objects.Models.Orders.Order>().GetSingle(x => x.ConnectorID == connector.ConnectorID && x.WebSiteOrderNumber == websiteNumber); if (order != null) { Concentrator.Objects.Models.Orders.OrderResponse invoiceOrderResponse = new Concentrator.Objects.Models.Orders.OrderResponse() { OrderID = order.OrderID, Currency = "EUR", ReceiveDate = DateTime.Now, ReqDeliveryDate = ediInvoiceNotification.InvoiceOrderHeader.RequestedDate.Year >= DateTime.Now.Year ? ediInvoiceNotification.InvoiceOrderHeader.RequestedDate : DateTime.Now, ResponseType = OrderResponseTypes.InvoiceNotification.ToString(), VendorID = _vendorID, VendorDocumentNumber = ediInvoiceNotification.InvoiceOrderHeader.OrderNumber, ShippingNumber = ediInvoiceNotification.InvoiceOrderHeader.PackingInformation != null ? ediInvoiceNotification.InvoiceOrderHeader.PackingInformation.PackingNumber : null, TrackAndTrace = ediInvoiceNotification.InvoiceOrderHeader.ShipmentInformation != null ? ediInvoiceNotification.InvoiceOrderHeader.ShipmentInformation.TrackAndTraceNumber : null, VendorDocumentDate = DateTime.Now, VendorDocument = responseString.OuterXml, InvoiceDate = ediInvoiceNotification.InvoiceOrderHeader.InvoiceDate, InvoiceDocumentNumber = ediInvoiceNotification.InvoiceOrderHeader.InvoiceNumber, TotalAmount = decimal.Parse(ediInvoiceNotification.InvoiceOrderHeader.InvoiceTotalInc), TotalExVat = decimal.Parse(ediInvoiceNotification.InvoiceOrderHeader.InvoiceTotalInc) - decimal.Parse(ediInvoiceNotification.InvoiceOrderHeader.InvoiceTaxableAmount), VatAmount = decimal.Parse(ediInvoiceNotification.InvoiceOrderHeader.InvoiceTaxableAmount), VatPercentage = decimal.Parse(ediInvoiceNotification.InvoiceOrderHeader.InvoiceTax), DocumentName = fileName }; foreach (var line in ediInvoiceNotification.InvoiceOrderDetails) { int orderLineID = 0; if (line.CustomerReference != null && !string.IsNullOrEmpty(line.CustomerReference.CustomerOrderLine)) { orderLineID = int.Parse(line.CustomerReference.CustomerOrderLine); } else if (!string.IsNullOrEmpty(line.LineNumber)) { if (order.OrderLines.Any(x => x.OrderLineID == int.Parse(line.LineNumber))) { orderLineID = int.Parse(line.LineNumber); } } var orderLine = order.OrderLines.Where(x => x.OrderLineID == orderLineID).FirstOrDefault(); if (orderLine == null) { orderLineID = order.OrderLines.Where(x => x.CustomerItemNumber == line.ProductIdentifier.ProductNumber).Select(x => x.OrderLineID).FirstOrDefault(); } if (orderLineID > 0) { OrderResponseLine orderResponseLine = new OrderResponseLine() { Backordered = line.Quantity.QuantityBackordered, Cancelled = line.Quantity.QuantityCancelled, Ordered = line.Quantity.QuantityOrdered, Shipped = line.Quantity.QuantityShipped, Delivered = line.Quantity.QuantityShipped, Unit = line.UnitOfMeasure.ToString(), VendorItemNumber = line.ProductIdentifier.ProductNumber, VendorLineNumber = line.LineNumber.ToString(), Price = line.UnitPrice, VatAmount = line.TaxAmount, Processed = false, OrderLineID = orderLineID, OEMNumber = line.ProductIdentifier.ManufacturerItemID, Barcode = line.ProductIdentifier.EANIdentifier, DeliveryDate = line.PromissedDeliveryDate, RequestDate = line.RequestedDate.Year >= DateTime.Now.Year ? line.RequestedDate : DateTime.Now, OrderResponse = invoiceOrderResponse, Invoiced = line.Quantity.QuantityOrdered }; if (line.ShipmentInformation != null) { orderResponseLine.NumberOfPallets = int.Parse(line.ShipmentInformation.NumberOfPallet); } if (line.ShipmentInformation != null) { orderResponseLine.NumberOfUnits = int.Parse(line.ShipmentInformation.NumberOfColli); } unit.Scope.Repository <OrderResponseLine>().Add(orderResponseLine); } } unit.Scope.Repository <Concentrator.Objects.Models.Orders.OrderResponse>().Add(invoiceOrderResponse); unit.Save(); } break; case "PurchaseAcknowledgement": ediDocument = ExtractResponseMessage <PurchaseAcknowledgement>(responseString); PurchaseAcknowledgement purchaseAcknowledgement = (PurchaseAcknowledgement)ediDocument; bskIdentifier = int.Parse(purchaseAcknowledgement.bskIdentifier); int orderID = int.Parse(purchaseAcknowledgement.Reference); Concentrator.Objects.Models.Orders.OrderResponse pruchaseResponse = new Concentrator.Objects.Models.Orders.OrderResponse() { OrderID = orderID, Currency = "EUR", ReceiveDate = DateTime.Now, ResponseType = OrderResponseTypes.PurchaseAcknowledgement.ToString(), VendorID = _vendorID, VendorDocumentNumber = purchaseAcknowledgement.PurchaseOrderNumber, VendorDocumentDate = DateTime.Now, VendorDocument = responseString.OuterXml, DocumentName = fileName }; int counter = 0; foreach (var line in purchaseAcknowledgement.PurchaseAcknowledgementLine) { int orderLineID = unit.Scope.Repository <OrderLine>().GetAll(x => x.OrderID == orderID && x.Product.VendorItemNumber.Trim() == line.LineReference.Trim()).Select(x => x.OrderLineID).FirstOrDefault(); if (orderLineID > 0) { OrderResponseLine orderResponseLine = new OrderResponseLine() { VendorItemNumber = line.ItemNumber, VendorLineNumber = line.LineNumber, Ordered = int.Parse(line.Quantity), OEMNumber = line.LineReference, OrderResponse = pruchaseResponse, OrderLineID = orderLineID }; unit.Scope.Repository <OrderResponseLine>().Add(orderResponseLine); counter++; } } if (counter > 0) { unit.Scope.Repository <Concentrator.Objects.Models.Orders.OrderResponse>().Add(pruchaseResponse); unit.Save(); } else { log.DebugFormat("No lines to process for order {0}", orderID); } break; } } }