public EdiOrderResponse ValidateOrder(Objects.Models.EDI.Order.EdiOrder order, Objects.Models.EDI.Vendor.EdiVendor ediVendor, Objects.Models.Connectors.ConnectorRelation ediRelation, System.Configuration.Configuration config) { string connectionString = config.AppSettings.Settings["JDEconnectionString"].Value; string environment = config.AppSettings.Settings["Environment"].Value; EdiOrderResponse reponse = new EdiOrderResponse(); if (CheckCustomerReference(order.CustomerOrderReference, order.SoldToCustomer.EANIdentifier, connectionString, environment)) { reponse.ResponseErrors.Add(new ReturnError() { ErrorMessage = "Customer Orderreference already Exists for SoldToCustomer" }); } return(reponse); }
protected override void Process() { using (var unit = GetUnitOfWork()) { #region Communication var comms = (from o in unit.Scope.Repository <EdiCommunication>().GetAll() where !o.LastRun.HasValue || (o.NextRun.HasValue && DateTime.Now >= o.NextRun.Value) select o).ToList(); foreach (var comm in comms) { try { ediVendorID = comm.EdiFieldMappings.FirstOrDefault().EdiVendorID; string connection = comm.Connection; if (ConfigurationManager.ConnectionStrings[comm.Connection] != null) { connection = ConfigurationManager.ConnectionStrings[comm.Connection].ConnectionString; } EDICommunicationLayer eCom = new EDICommunicationLayer((EdiConnectionType)comm.EdiConnectionType, connection); var query = SetQueryParams(comm.Query, comm); var data = eCom.GetVendorData(query); var ediMapping = unit.Scope.Repository <EdiFieldMapping>().GetAll(x => x.EdiCommunicationID == comm.EdiCommunicationID).ToList(); var matchFields = (from m in ediMapping where m.MatchField group m by m.TableName into tableGroup select tableGroup).ToDictionary(x => x.Key, x => x); foreach (DataRow row in data.Tables[0].AsEnumerable()) { string tableName = matchFields.FirstOrDefault().Key; Type tableType = Type.GetType(tableName); var repoMethod = typeof(IScope).GetMethod("Repository").MakeGenericMethod(tableType); var repo = repoMethod.Invoke(unit.Scope, null); var table = repo.GetType().GetMethod("GetAllAsQueryable").Invoke(repo, new object[1] { null }); IQueryable result = null; var set = (table as IQueryable); var mField = matchFields[tableName].FirstOrDefault(); foreach (var matchField in matchFields[tableName]) { if (!String.IsNullOrEmpty(matchField.VendorFieldName)) { var value = row[matchField.VendorFieldName]; var propertyType = tableType.GetProperty(matchField.FieldName); LambdaExpression expression = null; if (propertyType.PropertyType == typeof(String)) { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, value)))); } else { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, Convert.ChangeType(value, (TypeCode)matchField.VendorFieldType)))); } if (result == null) { result = set.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } else { result = result.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } } var count = ((IQueryable <EdiOrderResponse>)result.AsQueryable()).Count(); if (count == 1) { EdiOrderResponse resp = ((IQueryable <EdiOrderResponse>)result.AsQueryable()).FirstOrDefault(); var newRecord = GenerateResponse(unit.Scope, resp, data.Tables[0].AsEnumerable().Where(x => Convert.ChangeType(x.Field <object>(mField.VendorFieldName), (TypeCode)mField.VendorFieldType) == row[mField.VendorFieldName]), ediMapping); newRecord.EdiOrderResponseLines.ForEach((x, idx) => { x.EdiOrderLine.SetStatus(EdiOrderStatus.ReceiveShipmentNotificaiton, unit); x.EdiOrderLine.SetStatus(EdiOrderStatus.WaitForInvoiceNotification, unit); }); unit.Save(); } } } comm.LastRun = DateTime.Now; CronExpression exp = new CronExpression(comm.Schedule); comm.NextRun = exp.GetTimeAfter(DateTime.UtcNow); unit.Save(); } catch (Exception ex) { comm.Remark = string.Format("Communication rule failed: {0}", ex.Message); log.AuditError(string.Format("Communication rule failed (rule: {0})", comm.EdiCommunicationID), ex, "Communication EDI orders"); } } #endregion try { ediProcessor.GetCustomOrderResponses(unit, Config); unit.Save(); } catch (Exception ex) { log.AuditCritical("CustomOrderresponse failed processing", ex); } } }
private EdiOrderResponse GenerateResponse(IScope scope, EdiOrderResponse response, EnumerableRowCollection rows, List <EdiFieldMapping> mapping) { var grouped = (from m in mapping group m by m.TableName into tableGroup select tableGroup).ToDictionary(x => x.Key, x => x); EdiOrderResponse newResponse = new EdiOrderResponse(); newResponse.EdiOrderID = response.EdiOrderID; newResponse.ReceiveDate = DateTime.Now; newResponse.ResponseType = (int)Enum.Parse(typeof(OrderResponseTypes), mapping.FirstOrDefault().EdiCommunication.ResponseType); Type t = Type.GetType("Concentrator.Objects.Models.EDI.Response.EdiOrderResponse, Concentrator.Objects"); var ediOrderResponseFields = grouped["Concentrator.Objects.Models.EDI.Response.EdiOrderResponse, Concentrator.Objects"]; foreach (var m in ediOrderResponseFields) { if (!string.IsNullOrEmpty(m.VendorFieldName)) { foreach (DataRow row in rows) { var value = row[m.VendorFieldName]; t.GetProperty(m.FieldName).SetValue(newResponse, Convert.ChangeType(value, (TypeCode)m.VendorFieldType), null); if (newResponse.GetType() == newResponse.GetType()) { break; } } } else { object value = null; { if (m.VendorDefaultValue == "[Document]") { List <string> strList = new List <string>(); foreach (DataRow r in rows) { strList.Add(string.Join(",", r.ItemArray.ToArray())); } value = string.Join(",", strList); } else if (m.VendorDefaultValue == "[VendorID]") { value = ediVendorID; } t.GetProperty(m.FieldName).SetValue(newResponse, Convert.ChangeType(value, (TypeCode)m.VendorFieldType), null); } } } scope.Repository <EdiOrderResponse>().Add(newResponse); foreach (DataRow row in rows) { EdiOrderResponseLine newResponseLine = new EdiOrderResponseLine(); newResponseLine.EdiOrderResponse = newResponse; var set = response.EdiOrderResponseLines.AsQueryable(); IQueryable result = null; var assambly = "Concentrator.Objects.Models.EDI.Response.EdiOrderResponseLine, Concentrator.Objects"; t = Type.GetType(assambly); foreach (var matchField in grouped[assambly].Where(x => x.MatchField)) { var value = row[matchField.VendorFieldName]; var propertyType = newResponseLine.GetType().GetProperty(matchField.FieldName); LambdaExpression expression = null; if (propertyType.PropertyType == typeof(String)) { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, value)))); } else { expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(set.ElementType, typeof(bool), string.Format(string.Format("({0}) == \"{1}\"", matchField.FieldName, Convert.ChangeType(value, (TypeCode)matchField.VendorFieldType)))); } if (result == null) { result = set.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } else { result = result.Provider.CreateQuery ( Expression.Call(typeof(Queryable), "Where", new Type[] { set.ElementType }, set.Expression, Expression.Quote(expression))); } } var ediOrderline = ((IQueryable <EdiOrderResponseLine>)result.AsQueryable()).FirstOrDefault(); if (ediOrderline != null) { newResponseLine.EdiOrderLine = ediOrderline.EdiOrderLine; } foreach (var m in grouped[assambly]) { if (!string.IsNullOrEmpty(m.VendorFieldName)) { var value = row[m.VendorFieldName]; t.GetProperty(m.FieldName).SetValue(newResponseLine, Convert.ChangeType(value, (TypeCode)m.VendorFieldType), null); } else { object value = null; { if (m.VendorDefaultValue == "[Document]") { List <string> strList = new List <string>(); foreach (DataRow r in rows) { strList.Add(string.Join(",", r.ItemArray.Cast <string>().ToArray())); } value = string.Join(",", strList); } else if (m.VendorDefaultValue == "[VendorID]") { value = ediVendorID; } t.GetProperty(m.FieldName).SetValue(newResponseLine, Convert.ChangeType(value, (TypeCode)m.VendorFieldType), null); } } } scope.Repository <EdiOrderResponseLine>().Add(newResponseLine); } return(newResponse); }
public InvoiceNotification GenerateInvoiceNotification(EdiOrderResponse response, IEdiProcessor processor, System.Configuration.Configuration config) { List <EdiOrderResponseLine> responseLines = response.EdiOrderResponseLines.ToList(); EdiOrder order = response.EdiOrder; if (order == null) { order = processor.GetOrderInformation(response, config); } List <InvoiceOrderDetail> replyItems = new List <InvoiceOrderDetail>(); foreach (var line in responseLines) { InvoiceOrderDetail lineItem = new InvoiceOrderDetail(); if (line.EdiOrderLineID.HasValue) { lineItem.ProductIdentifier = new Concentrator.Web.Objects.EDI.ProductIdentifier(); lineItem.ProductIdentifier.ProductNumber = line.EdiOrderLine.ProductID.HasValue ? line.EdiOrderLine.ProductID.Value.ToString() : line.EdiOrderLine.CustomerItemNumber; //lineItem.ProductIdentifier.ManufacturerItemID = line.EdiOrderLine.ProductID.HasValue ? line.EdiOrderLine.Product.VendorItemNumber : string.Empty; //if (line.EdiOrderLine.ProductID.HasValue && line.EdiOrderLine.Product.ProductBarcodes.Count > 0) // lineItem.ProductIdentifier.EANIdentifier = line.EdiOrderLine.Product.ProductBarcodes.FirstOrDefault().Barcode; lineItem.CustomerReference = new Concentrator.Web.Objects.EDI.CustomerReference(); lineItem.CustomerReference.CustomerItemNumber = line.EdiOrderLine.CustomerItemNumber; lineItem.CustomerReference.CustomerOrder = line.EdiOrderLine.CustomerOrderNr; lineItem.CustomerReference.CustomerOrderLine = line.EdiOrderLine.CustomerEdiOrderLineNr; lineItem.LineNumber = line.EdiOrderLineID.ToString(); } else { lineItem.ProductIdentifier = new Concentrator.Web.Objects.EDI.ProductIdentifier(); lineItem.ProductIdentifier.ProductNumber = line.VendorItemNumber; //lineItem.ProductIdentifier.ManufacturerItemID = line.EdiOrderLine.ProductID.HasValue ? line.EdiOrderLine.Product.VendorItemNumber : string.Empty; //if (line.EdiOrderLine.ProductID.HasValue && line.EdiOrderLine.Product.ProductBarcodes.Count > 0) // lineItem.ProductIdentifier.EANIdentifier = line.EdiOrderLine.Product.ProductBarcodes.FirstOrDefault().Barcode; lineItem.CustomerReference = new Concentrator.Web.Objects.EDI.CustomerReference(); lineItem.CustomerReference.CustomerItemNumber = string.Empty; lineItem.CustomerReference.CustomerOrder = line.Remark; lineItem.CustomerReference.CustomerOrderLine = line.VendorLineNumber; } if (line.DeliveryDate.HasValue) { lineItem.PromissedDeliveryDate = line.DeliveryDate.Value; } if (line.RequestDate.HasValue) { lineItem.RequestedDate = line.RequestDate.Value; } else if (order.RequestDate.HasValue) { lineItem.RequestedDate = order.RequestDate.Value; } lineItem.Quantity = new Quantity(); lineItem.Quantity.QuantityBackordered = line.Backordered; lineItem.Quantity.QuantityBackorderedSpecified = true; lineItem.Quantity.QuantityCancelled = line.Cancelled; lineItem.Quantity.QuantityCancelledSpecified = true; lineItem.Quantity.QuantityOrdered = line.Ordered; lineItem.Quantity.QuantityShipped = line.Invoiced; lineItem.Quantity.QuantityShippedSpecified = true; if (string.IsNullOrEmpty(line.EdiOrderResponse.VendorDocumentNumber)) { lineItem.StatusCode = StatusCode.Reject; } else if (line.Cancelled == line.Ordered) { lineItem.StatusCode = StatusCode.Delete; } else if (line.Ordered != line.Shipped) { lineItem.StatusCode = StatusCode.Change; } else { lineItem.StatusCode = StatusCode.Accept; } if (line.VatAmount.HasValue) { lineItem.TaxAmount = line.VatAmount.Value; } lineItem.UnitOfMeasure = InvoiceOrderDetailUnitOfMeasure.EA; lineItem.UnitPrice = line.Price; lineItem.ShipmentInformation = new ShipmentInformation(); lineItem.ShipmentInformation.CarrierCode = line.CarrierCode; lineItem.ShipmentInformation.NumberOfColli = line.NumberOfUnits.HasValue ? line.NumberOfUnits.Value.ToString() : "1"; lineItem.ShipmentInformation.NumberOfPallet = line.NumberOfPallets.HasValue ? line.NumberOfPallets.Value.ToString() : "0"; lineItem.ShipmentInformation.TrackAndTraceNumber = line.TrackAndTrace; lineItem.ExtendedPrice = line.Price; lineItem.ExtendedPriceSpecified = false; lineItem.InvoiceNumber = response.InvoiceDocumentNumber; if (!string.IsNullOrEmpty(line.SerialNumbers)) { lineItem.SerialNumbers = line.SerialNumbers.Split(';'); } replyItems.Add(lineItem); } InvoiceOrderHeader header = new InvoiceOrderHeader() { BSKIdentifier = order.ConnectorRelationID.HasValue ? order.ConnectorRelationID.Value : 0, CustomerOrder = order.CustomerOrderReference, OrderNumber = response.VendorDocumentNumber, RequestedDate = response.ReqDeliveryDate.HasValue ? response.ReqDeliveryDate.Value : order.ReceivedDate, RequestedDateSpecified = response.ReqDeliveryDate.HasValue, WebSiteOrderNumber = order.WebSiteOrderNumber, }; header.PackingInformation = new PackingInformation(); header.PackingInformation.PackingDateTime = response.ReceiveDate; header.PackingInformation.PackingNumber = response.ShippingNumber; if (response.PartialDelivery.HasValue && response.PartialDelivery.Value) { header.FullfillmentCode = InvoiceOrderHeaderFullfillmentCode.Partial; } else { header.FullfillmentCode = InvoiceOrderHeaderFullfillmentCode.Complete; } if (response.ShippedToCustomer != null) { header.ShipToCustomer = FillShipToCustomer(response.ShippedToCustomer); header.CustomerOverride = FillCustomerOverride(response.ShippedToCustomer); } else { header.ShipToCustomer = FillShipToCustomer(order.ShippedToCustomer); header.CustomerOverride = FillCustomerOverride(order.ShippedToCustomer); } if (response.SoldToCustomer != null) { header.SoldToCustomer = FillShipToCustomer(response.SoldToCustomer); } else { header.SoldToCustomer = FillShipToCustomer(order.SoldToCustomer); } if (response.InvoiceDate.HasValue) { header.InvoiceDate = response.InvoiceDate.Value; } header.InvoiceNumber = response.InvoiceDocumentNumber; if (response.VatAmount.HasValue) { header.InvoiceTax = response.VatAmount.Value.ToString(); } if (response.TotalExVat.HasValue) { header.InvoiceTaxableAmount = response.TotalExVat.Value.ToString(); } if (response.TotalAmount.HasValue) { header.InvoiceTotalInc = response.TotalAmount.Value.ToString(); } if (!string.IsNullOrEmpty(response.PaymentConditionDiscount)) { header.DisountAmount = response.PaymentConditionDiscount; } InvoiceNotification invoiceNotification = new InvoiceNotification() { InvoiceOrderDetails = replyItems.ToArray(), InvoiceOrderHeader = header, Version = "1.0" }; return(invoiceNotification); }