Exemple #1
0
        protected virtual bool PersistGrams(DuplicateDocument document)
        {
            if (document != null && document.ContactID > 1)
            {
                return(new CRGramProcessor(Base).PersistGrams(document));
            }

            return(false);
        }
Exemple #2
0
        protected virtual bool CheckIsActive()
        {
            DuplicateDocument duplicateDocument = DuplicateDocuments.Current ?? DuplicateDocuments.Select();

            if (duplicateDocument == null)
            {
                return(false);
            }

            return(duplicateDocument.IsActive == true);
        }
Exemple #3
0
        public virtual void Persist(Action del)
        {
            del();

            DuplicateDocument duplicateDocument = DuplicateDocuments.Current;

            if (Setup.Current?.ValidateContactDuplicatesOnEntry == true && duplicateDocument?.IsActive == true)
            {
                CheckForDuplicates.Press();
            }
        }
Exemple #4
0
        protected virtual void _(Events.RowPersisted <DuplicateDocument> e)
        {
            DuplicateDocument row = e.Row as DuplicateDocument;

            if (row == null || e.TranStatus != PXTranStatus.Open)
            {
                return;
            }

            if (PersistGrams(row))
            {
                DuplicateDocuments.Cache.SetValue <DuplicateDocument.duplicateStatus>(row, row.DuplicateStatus);
                DuplicateDocuments.Cache.SetValue <DuplicateDocument.grammValidationDateTime>(row, row.GrammValidationDateTime);
            }
        }
Exemple #5
0
        public virtual IEnumerable closeAsDuplicate(PXAdapter adapter)
        {
            DuplicateDocument duplicateDocument = DuplicateDocuments.Current;

            if (duplicateDocument == null)
            {
                return(adapter.Get());
            }

            duplicateDocument.DuplicateStatus = DuplicateStatusAttribute.Duplicated;

            DuplicateDocuments.Update(duplicateDocument);

            return(adapter.Get());
        }
Exemple #6
0
        public virtual void duplicateAttach()
        {
            DuplicateDocument duplicateDocument = DuplicateDocuments.Current;

            if (duplicateDocument == null)
            {
                return;
            }

            DoDuplicateAttach(duplicateDocument);

            if (Base.IsContractBasedAPI)
            {
                Base.Actions.PressSave();
            }
        }
Exemple #7
0
        public virtual IEnumerable markAsValidated(PXAdapter adapter)
        {
            DuplicateDocument duplicateDocument = DuplicateDocuments.Current;

            if (duplicateDocument == null)
            {
                return(adapter.Get());
            }

            duplicateDocument.DuplicateStatus = DuplicateStatusAttribute.Validated;
            duplicateDocument.DuplicateFound  = false;

            DuplicateDocuments.Update(duplicateDocument);

            Base.Actions.PressSave();

            return(adapter.Get());
        }
Exemple #8
0
        public bool GrammSourceUpdated(DuplicateDocument document)
        {
            var main = graph.Caches[typeof(DuplicateDocument)].GetMain(document);

            PXCache cache = graph.Caches[main.GetType()];

            if (cache.GetStatus(main) == PXEntryStatus.Inserted || cache.GetStatus(main) == PXEntryStatus.Notchanged)
            {
                return(false);
            }

            if (Definition.ContactRules(document.ContactType)
                .Any(rule => !String.Equals(
                         cache.GetValue(main, rule.MatchingField)?.ToString(),
                         cache.GetValueOriginal(main, rule.MatchingField)?.ToString(),
                         StringComparison.InvariantCultureIgnoreCase)))
            {
                return(false);
            }

            return(true);
        }
Exemple #9
0
        public bool PersistGrams(DuplicateDocument document, bool requireRecreate = false)
        {
            if (graph.Caches[document.GetType()].GetStatus(document) == PXEntryStatus.Deleted)
            {
                PXDatabase.Delete <CRGrams>(new PXDataFieldRestrict <CRGrams.entityID>(PXDbType.Int, 4, document.ContactID, PXComp.EQ));

                return(false);
            }

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

            PXDatabase.Delete <CRGrams>(new PXDataFieldRestrict <CRGrams.entityID>(PXDbType.Int, 4, document.ContactID, PXComp.EQ));

            foreach (CRGrams gram in DoCreateGramms(document))
            {
                PXDatabase.Insert <CRGrams>(
                    new PXDataFieldAssign(typeof(CRGrams.entityID).Name, PXDbType.Int, 4, document.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)
                    );
            }

            document.DuplicateStatus         = DuplicateStatusAttribute.NotValidated;
            document.GrammValidationDateTime = PXTimeZoneInfo.Now;

            PXDatabase.Update <Contact>(
                new PXDataFieldAssign <Contact.duplicateStatus>(PXDbType.NVarChar, document.DuplicateStatus),
                new PXDataFieldAssign <Contact.grammValidationDateTime>(PXDbType.DateTime, PXTimeZoneInfo.ConvertTimeToUtc(document.GrammValidationDateTime.Value, LocaleInfo.GetTimeZone())),
                new PXDataFieldRestrict <Contact.contactID>(PXDbType.Int, document.ContactID)
                );

            return(true);
        }
Exemple #10
0
        // copy-paste from base
        internal IEnumerable DoCreateGramms(DuplicateDocument document)
        {
            var main = graph.Caches[typeof(DuplicateDocument)].GetMain(document);

            string[] types = document.ContactType == ContactTypesAttribute.BAccountProperty ? AccountTypes : ContactTypes;

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

                Location defLocation = null;
                if (document.BAccountID != null)
                {
                    BAccount bAccount = PXSelect <BAccount, Where <BAccount.bAccountID, Equal <Required <Contact.bAccountID> > > > .Select(graph, document.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;

                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(main, rule.MatchingField) == null)
                        {
                            totalZero += rule.ScoreWeight.GetValueOrDefault();
                        }
                        else
                        {
                            total += rule.ScoreWeight.GetValueOrDefault();
                        }
                    }
                    else
                    {
                        if (cache.GetValue(main, 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(main, fieldName);
                    if (type == ValidationTypesAttribute.Account && value == null)
                    {
                        value = lCache.GetValue(defLocation, fieldName);
                    }

                    if (value == null)
                    {
                        continue;
                    }

                    if (transformRule.Equals(TransformationRulesAttribute.SplitWords))
                    {
                        int?id = document.ContactID;

                        value = value.ToString().ToLower();
                        string[] words = value.ToString().Split();

                        foreach (string word in words)
                        {
                            Decimal score = Decimal.Round(sw / words.Length, 4);
                            if (score <= 0)
                            {
                                continue;
                            }

                            CRGrams gram = new CRGrams
                            {
                                EntityID       = id,
                                ValidationType = validationType,
                                FieldName      = fieldName,
                                FieldValue     = word,
                                Score          = score
                            };
                            yield return(gram);
                        }
                    }
                    else
                    {
                        value = value.ToString().ToLower();
                        if (transformRule.Equals(TransformationRulesAttribute.DomainName))
                        {
                            if (value.ToString().Contains('@'))
                            {
                                value = value.ToString().Split('@')[1];
                            }
                            else
                            {
                                try
                                {
                                    value = new UriBuilder(value.ToString()).Host;
                                    if (value.ToString().Split('.')[0].Equals("www"))
                                    {
                                        value = value.ToString().Substring(value.ToString().IndexOf('.') + 1);
                                    }
                                }
                                catch (UriFormatException)
                                {
                                    //Do nothing
                                }
                            }
                        }

                        CRGrams gram = new CRGrams
                        {
                            FieldValue     = value.ToString(),
                            EntityID       = document.ContactID,
                            ValidationType = validationType,
                            FieldName      = fieldName,
                            Score          = Decimal.Round(sw, 4)
                        };

                        yield return(gram);
                    }
                }
            }
        }
Exemple #11
0
 protected abstract void DoDuplicateAttach(DuplicateDocument duplicateDocument);
Exemple #12
0
        public virtual IEnumerable checkForDuplicates(PXAdapter adapter)
        {
            DuplicateDocument duplicateDocument = DuplicateDocuments.Current ?? DuplicateDocuments.Select();

            if (duplicateDocument == null)
            {
                return(adapter.Get());
            }

            var prevStatus  = duplicateDocument.DuplicateStatus;
            var prevIsDirty = Base.Caches[DuplicateDocuments.Cache.GetMain(duplicateDocument).GetType()].IsDirty;

            if (CheckIsActive())
            {
                duplicateDocument.DuplicateFound = true;
                duplicateDocument = DuplicateDocuments.Update(duplicateDocument);

                Duplicates.View.Clear();
                var result = Duplicates.Select().ToList();

                bool anyFound = result != null && result.Count > 0;
                duplicateDocument.DuplicateFound  = anyFound;
                duplicateDocument.DuplicateStatus = anyFound
                                        ? DuplicateStatusAttribute.PossibleDuplicated
                                        : DuplicateStatusAttribute.Validated;

                duplicateDocument = DuplicateDocuments.Update(duplicateDocument);

                if (duplicateDocument.ContactType?.IsIn(ContactTypesAttribute.Lead, ContactTypesAttribute.Person) == true &&
                    Setup.Current?.ContactEmailUnique == true &&
                    !String.IsNullOrWhiteSpace(duplicateDocument.Email))
                {
                    try
                    {
                        var parsedEmail = EmailParser.ParseAddresses(duplicateDocument.Email).FirstOrDefault();

                        if (result
                            .Select(item => item.GetItem <DuplicateContact>())
                            .Where(item => !String.IsNullOrWhiteSpace(item.EMail) && item.ContactType.IsIn(ContactTypesAttribute.Lead, ContactTypesAttribute.Person))
                            .Any(item => item.EMail.Contains(parsedEmail.Address)))
                        {
                            DuplicateDocuments.Cache.RaiseExceptionHandling <DuplicateDocument.email>(
                                duplicateDocument,
                                duplicateDocument.Email,
                                new PXSetPropertyException(Messages.ContactEmailNotUnique, PXErrorLevel.Warning));
                        }
                    }
                    catch (Exception) { }
                }
            }

            if (duplicateDocument.DuplicateStatus != prevStatus)
            {
                Base.Actions.PressSave();
            }
            else
            {
                Base.Caches[DuplicateDocuments.Cache.GetMain(duplicateDocument).GetType()].IsDirty = prevIsDirty;
            }

            if (duplicateDocument.DuplicateStatus == DuplicateStatusAttribute.PossibleDuplicated || duplicateDocument.DuplicateFound == true)
            {
                DuplicateDocuments.Cache.RaiseExceptionHandling <DuplicateDocument.duplicateStatus>(duplicateDocument,
                                                                                                    duplicateDocument.DuplicateStatus,
                                                                                                    new PXSetPropertyException(WarningMessage, PXErrorLevel.Warning));
            }
            else
            {
                DuplicateDocuments.Cache.RaiseExceptionHandling <DuplicateDocument.duplicateStatus>(duplicateDocument,
                                                                                                    duplicateDocument.DuplicateStatus,
                                                                                                    null);
            }

            return(adapter.Get());
        }