Exemple #1
0
        public bool CheckCustomerReference(string customerOrderReference, string soldToCustomer, string connection, string environment)
        {
            EDICommunicationLayer communication = new EDICommunicationLayer(EdiConnectionType.SQL, connection);

            var query = string.Format("SELECT Count(*) FROM {0}.F47011 WHERE SYVR01 = {1} and SYAN8 = {2}", environment, customerOrderReference, soldToCustomer);

            return(!communication.IsValid(query));
        }
        public Dictionary <int, string> ValidateOrder <T>(T obj, List <EdiValidate> validations, int entityID)
            where T : class
        {
            Type type = (typeof(T));

            Dictionary <int, string> errormessages = new Dictionary <int, string>();

            foreach (var validation in validations.Where(x => x.TableName == type.Name))
            {
                var value = obj.GetType().GetProperty(validation.FieldName).GetValue(obj, null).ToString().Trim();

                if (validation.MaxLength.HasValue && value.Length > validation.MaxLength.Value)
                {
                    errormessages.Add(entityID, string.Format("Max length for field {0} exceeded", validation.FieldName));
                }

                if (!string.IsNullOrEmpty(validation.Value))
                {
                    switch ((EdiValidationType)validation.EdiValidationType)
                    {
                    case EdiValidationType.ConstantValue:
                        if (validation.Value != value)
                        {
                            errormessages.Add(entityID, string.Format("Value for field {0} should be {1}", validation.FieldName, validation.Value));
                        }
                        break;

                    case EdiValidationType.Query:
                        string connection = validation.Connection;

                        if (ConfigurationManager.ConnectionStrings[validation.Connection] != null)
                        {
                            connection = ConfigurationManager.ConnectionStrings[validation.Connection].ConnectionString;
                        }

                        EDICommunicationLayer communication = new EDICommunicationLayer((EdiConnectionType)validation.EdiConnectionType, connection);
                        if (communication.IsValid(string.Format(validation.Value, value)))
                        {
                            errormessages.Add(entityID, string.Format("Value for field {0} not valid query returned false", validation.FieldName, validation.Value));
                        }
                        break;

                    case EdiValidationType.Formula:
                        //var calculateTotal = DynamicExpression.ParseLambda<T, string>("SubTotal*@0+Shipping", validation.Value).Compile();
                        if (!f(validation.Value, value))
                        {
                            errormessages.Add(entityID, string.Format("Value for field {0} not valid formula returned false", validation.FieldName, validation.Value));
                        }
                        break;

                    default:
                        break;
                    }
                }
            }

            return(errormessages);
        }
        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);
                }
            }
        }