Пример #1
0
        private static void ProcessValidation(PXGraph graph, Contact record, ValidationFilter Filter)
        {
            Type    itemType = record.GetType();
            PXCache cache    = PXGraph.CreateInstance <PXGraph>().Caches[itemType];
            Type    graphType;
            object  copy = cache.CreateCopy(record);

            PXPrimaryGraphAttribute.FindPrimaryGraph(cache, ref copy, out graphType);

            if (graphType == null)
            {
                throw new PXException(Messages.UnableToFindGraph);
            }

            graph = PXGraph.CreateInstance(graphType);


            graph.Views[graph.PrimaryView].Cache.Current = copy;

            CRDuplicateEntities <PXGraph, Contact> .RunActionWithAppliedAutomation(
                graph,
                copy,
                nameof(CRDuplicateEntities <PXGraph, Contact> .CheckForDuplicates));

            DuplicateDocument document = graph.Caches[typeof(DuplicateDocument)].Current as DuplicateDocument;

            record.DuplicateStatus = document?.DuplicateStatus;
            record.DuplicateFound  = document?.DuplicateFound;

            var maxScore = graph
                           .Caches[typeof(CRDuplicateRecord)]
                           .Cached
                           .Cast <CRDuplicateRecord>()
                           .Max(_ => _?.Score) ?? 0;

            if (record.DuplicateStatus == DuplicateStatusAttribute.PossibleDuplicated &&
                record.ContactType == ContactTypesAttribute.Lead
                //&& record.Status == LeadStatusesAttribute.New
                && Filter.CloseNoActivityLeads == true &&
                maxScore > Filter.CloseThreshold)
            {
                CRActivity activity = PXSelect <CRActivity,
                                                Where <CRActivity.refNoteID, Equal <Required <Contact.noteID> > > > .SelectWindowed(graph, 0, 1, record.NoteID);

                if (activity == null)
                {
                    CRDuplicateEntities <PXGraph, Contact> .RunActionWithAppliedAutomation(
                        graph,
                        copy,
                        nameof(CRDuplicateEntities <PXGraph, Contact> .CloseAsDuplicate));
                }
            }
        }
Пример #2
0
            public bool PersistGrams(Contact contact, bool requireRecreate = false)
            {
                try
                {
                    if (track == null)
                    {
                        track = DateTime.Now;
                    }
                    if (graph.Caches[contact.GetType()].GetStatus(contact) == PXEntryStatus.Deleted)
                    {
                        PXDatabase.Delete <CRGrams>(new PXDataFieldRestrict("EntityID", PXDbType.Int, 4, contact.ContactID, PXComp.EQ));
                        return(false);
                    }

                    if (!requireRecreate && GrammSourceUpdated(contact))
                    {
                        return(false);
                    }

                    PXDatabase.Delete <CRGrams>(new PXDataFieldRestrict("EntityID", PXDbType.Int, 4, contact.ContactID, PXComp.EQ));
                    foreach (CRGrams gram in DoCreateGramms(contact))
                    {
                        PXDatabase.Insert <CRGrams>(
                            new PXDataFieldAssign(typeof(CRGrams.entityID).Name, PXDbType.Int, 4, contact.ContactID),
                            new PXDataFieldAssign(typeof(CRGrams.fieldName).Name, PXDbType.NVarChar, 60, gram.FieldName),
                            new PXDataFieldAssign(typeof(CRGrams.fieldValue).Name, PXDbType.NVarChar, 60, gram.FieldValue),
                            new PXDataFieldAssign(typeof(CRGrams.score).Name, PXDbType.Decimal, 8, gram.Score),
                            new PXDataFieldAssign(typeof(CRGrams.validationType).Name, PXDbType.NVarChar, 2, gram.ValidationType)
                            );
                        //CRGrams row = (CRGrams)graph.Caches[typeof(CRGrams)].Insert(gram);
                        //graph.Caches[typeof(CRGrams)].PersistInserted(row);
                    }
                    contact.GrammValidationDateTime = PXTimeZoneInfo.Now;
                    PXDatabase.Update <Contact>
                    (
                        new PXDataFieldAssign(typeof(Contact.grammValidationDateTime).Name, PXTimeZoneInfo.ConvertTimeToUtc(contact.GrammValidationDateTime.Value, LocaleInfo.GetTimeZone())),
                        new PXDataFieldRestrict(typeof(Contact.contactID).Name, contact.ContactID)
                    );
                    processedItems += 1;
                    return(true);
                }
                finally
                {
                    if (processedItems % 100 == 0)
                    {
                        TimeSpan taken = DateTime.Now - (DateTime)track;
                        System.Diagnostics.Debug.WriteLine("Items count:{0}, increment taken {1}", processedItems, taken);
                        track = DateTime.Now;
                    }
                }
            }
Пример #3
0
            public bool GrammSourceUpdated(Contact contact)
            {
                PXCache cache = graph.Caches[contact.GetType()];

                if (cache.GetStatus(contact) == PXEntryStatus.Inserted ||
                    cache.GetStatus(contact) == PXEntryStatus.Notchanged)
                {
                    return(false);
                }
                if (Definition.ContactRules(contact.ContactType).Any(rule => !object.Equals(cache.GetValue(contact, rule.MatchingField),
                                                                                            cache.GetValueOriginal(contact, rule.MatchingField))))
                {
                    return(false);
                }
                return(true);
            }
        private static void ProcessValidation(BusinessAccountMaint graph, Contact record, ValidationFilter Filter)
        {
            Type    itemType = record.GetType();
            PXCache cache    = PXGraph.CreateInstance <PXGraph>().Caches[itemType];
            Type    graphType;
            object  copy = cache.CreateCopy(record);

            PXPrimaryGraphAttribute.FindPrimaryGraph(cache, ref copy, out graphType);

            graph.Views[graph.PrimaryView].Cache.Current = copy as BAccount;

            CRDuplicateEntities <PXGraph, BAccount> .RunActionWithAppliedAutomation(
                graph,
                copy,
                nameof(CRDuplicateEntities <PXGraph, BAccount> .CheckForDuplicates));
        }
Пример #5
0
            private IEnumerable DoCreateGramms(Contact contact)
            {
                string[] types = contact.ContactType == ContactTypesAttribute.BAccountProperty ? AccountTypes : ContactTypes;

                foreach (string validationType in types)
                {
                    PXCache cache  = graph.Caches[contact.GetType()];
                    PXCache lCache = graph.Caches[typeof(Location)];

                    Location defLocation = null;
                    if (contact.BAccountID != null)
                    {
                        BAccount bAccount = PXSelect <BAccount, Where <BAccount.bAccountID, Equal <Required <Contact.bAccountID> > > > .Select(graph, contact.BAccountID);

                        if (bAccount != null && bAccount.DefLocationID != null)
                        {
                            defLocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <BAccount.defLocationID> > > > > .Select(graph, bAccount.BAccountID, bAccount.DefLocationID);
                        }
                    }


                    string type = validationType;                     // TODO: remove duplicate?
                    if (!Definition.TypeRules.ContainsKey(validationType))
                    {
                        continue;
                    }

                    decimal total = 0, totalZero = 0;
                    foreach (var rule in Definition.TypeRules[type])
                    {
                        if (type == ValidationTypesAttribute.Account && defLocation != null)
                        {
                            if (lCache.GetValue(defLocation, rule.MatchingField) == null && cache.GetValue(contact, rule.MatchingField) == null)
                            {
                                totalZero += rule.ScoreWeight.GetValueOrDefault();
                            }
                            else
                            {
                                total += rule.ScoreWeight.GetValueOrDefault();
                            }
                        }
                        else
                        {
                            if (cache.GetValue(contact, rule.MatchingField) == null)
                            {
                                totalZero += rule.ScoreWeight.GetValueOrDefault();
                            }
                            else
                            {
                                total += rule.ScoreWeight.GetValueOrDefault();
                            }
                        }
                    }

                    if (total == 0)
                    {
                        continue;
                    }

                    foreach (CRValidationRules rule in Definition.TypeRules[type])
                    {
                        string  fieldName     = rule.MatchingField;
                        string  transformRule = rule.TransformationRule;
                        Decimal sw            = rule.ScoreWeight ?? 0;
                        if (sw == 0)
                        {
                            continue;
                        }

                        if (sw > 0 && totalZero > 0)
                        {
                            sw += totalZero * (sw / total);
                        }

                        var value = cache.GetValue(contact, fieldName);
                        if (type == ValidationTypesAttribute.Account && value == null)
                        {
                            value = lCache.GetValue(defLocation, fieldName);
                        }

                        if (value == null)
                        {
                            continue;
                        }
                        string stringValue = value.ToString().ToLower();

                        if (transformRule.Equals(TransformationRulesAttribute.SplitWords))
                        {
                            int?     id    = contact.ContactID;
                            string[] words = stringValue.Split();

                            foreach (string word in words)
                            {
                                Decimal score = Decimal.Round(sw / words.Length, 4);
                                if (score <= 0)
                                {
                                    continue;
                                }
                                CRGrams gram = new CRGrams();
                                gram.EntityID       = id;
                                gram.ValidationType = validationType;
                                gram.FieldName      = fieldName;
                                gram.FieldValue     = word;
                                gram.Score          = score;
                                yield return(gram);
                            }
                        }
                        else
                        {
                            if (transformRule.Equals(TransformationRulesAttribute.DomainName))
                            {
                                if (stringValue.Contains('@'))
                                {
                                    stringValue = stringValue.Segment('@', 1);
                                }
                                else
                                {
                                    try
                                    {
                                        stringValue = new UriBuilder(stringValue).Host;
                                        int    index        = stringValue.IndexOf('.');
                                        string firstSegment = index < 0 ? stringValue : stringValue.Substring(0, index);
                                        if (firstSegment.Equals("www"))
                                        {
                                            stringValue = stringValue.Substring(index + 1);
                                        }
                                    }
                                    catch (UriFormatException)
                                    {
                                        //Do nothing
                                    }
                                }
                            }

                            CRGrams gram = new CRGrams();
                            gram.FieldValue     = stringValue;
                            gram.EntityID       = contact.ContactID;
                            gram.ValidationType = validationType;
                            gram.FieldName      = fieldName;
                            gram.Score          = Decimal.Round(sw, 4);

                            yield return(gram);
                        }
                    }
                }
            }