public static void ProcessSurvey(List <SurveyCollector> recs, SurveyFilter filter) { var collGraph = CreateInstance <SurveyCollectorMaint>(); var action = filter.Action; if (string.IsNullOrEmpty(action)) { throw new PXException(Messages.SurveyActionNotRecognised); } SurveyProcess surveyProcessGraph = PXGraph.CreateInstance <SurveyProcess>(); surveyProcessGraph.Filter.Insert(filter); var collCache = collGraph.Collector.Cache; var errorOccurred = false; var docCount = 0; foreach (var rec in recs) { var row = (SurveyCollector)collCache.CreateCopy(rec); collGraph.Collector.Current = row; try { PXProcessing <SurveyCollector> .SetCurrentItem(rec); switch (action) { case SurveyAction.SendNew: collGraph.DoSendNewNotification(row); break; case SurveyAction.RemindOnly: collGraph.DoSendReminder(row, filter.DurationTimeSpan); break; case SurveyAction.ExpireOnly: row.Status = CollectorStatus.Expired; row.Message = null; collGraph.Collector.Update(row); break; } if (++docCount % 10 == 0) { surveyProcessGraph.Actions.PressSave(); } PXProcessing <SurveyCollector> .SetInfo(recs.IndexOf(rec), string.Format("The survey collector {0} has been updated", rec.CollectorID)); PXProcessing <SurveyCollector> .SetProcessed(); } catch (Exception ex) { row.Status = CollectorStatus.Error; row.Message = ex.Message; surveyProcessGraph.Documents.Update(row); surveyProcessGraph.Actions.PressSave(); PXTrace.WriteError(ex); string errorMessage = ex.Message + ": "; if (ex is PXOuterException pex && pex.InnerMessages != null) { foreach (string message in pex.InnerMessages) { errorMessage += message + ", "; } }
public virtual IEnumerable documents() { SurveyFilter filter = Filter.Current; var action = filter.Action; var cmd = new PXSelectJoin <SurveyCollector, LeftJoin <Survey, On <Survey.surveyID, Equal <SurveyCollector.surveyID> >, LeftJoin <SurveyUser, On <SurveyUser.surveyID, Equal <SurveyCollector.surveyID>, And <SurveyUser.lineNbr, Equal <SurveyCollector.userLineNbr> > > > >, Where <SurveyUser.anonymous, IsNull, Or <SurveyUser.anonymous, Equal <False> > > >(this); switch (action) { case SurveyAction.SendNew: cmd.WhereAnd <Where <SurveyCollector.sentOn, IsNull> >(); break; case SurveyAction.ExpireOnly: //cmd.WhereAnd<Where<SurveyCollector.status, SurveyCollector.notProcessed>>(); break; case SurveyAction.RemindOnly: cmd.WhereAnd <Where <SurveyCollector.sentOn, IsNotNull> >(); //cmd.WhereAnd<Where<SurveyCollector.status, Equal<CollectorStatus.sent>>>(); break; } /* Filter by SurveyID */ if (!string.IsNullOrEmpty(filter.SurveyID)) { cmd.WhereAnd <Where <SurveyCollector.surveyID, Equal <Current <SurveyFilter.surveyID> > > >(); } /* Filter by ShowClosed */ if (filter.ShowClosed != true) { cmd.WhereAnd <Where <Survey.status, NotEqual <SurveyStatus.closed> > >(); } var rows = cmd.Select(); return(rows); }
public static void ProcessSurvey(PXCache cache, SurveyFilter filter, List <SurveyUser> surveyUserList) { bool erroroccurred = false; SurveyCollectorMaint graph = PXGraph.CreateInstance <SurveyCollectorMaint>(); Survey surveyCurrent = (Survey)PXSelectorAttribute.Select <SurveyFilter.surveyID>(cache, filter); List <SurveyUser> dataToProceed = new List <SurveyUser>(surveyUserList); foreach (var surveyUser in dataToProceed) { try { string sCollectorStatus = (surveyUser.UsingMobileApp.GetValueOrDefault(false)) ? SurveyResponseStatus.CollectorSent : SurveyResponseStatus.CollectorNew; graph.Clear(); SurveyCollector surveyCollector = new SurveyCollector { CollectorName = $"{surveyCurrent.SurveyName} {PXTimeZoneInfo.Now:yyyy-MM-dd hh:mm:ss}", SurveyID = surveyUser.SurveyID, UserID = surveyUser.UserID, CollectedDate = null, ExpirationDate = null, CollectorStatus = sCollectorStatus }; surveyCollector = graph.SurveyQuestions.Insert(surveyCollector); graph.Persist(); string sScreenID = PXSiteMap.Provider.FindSiteMapNodeByGraphType(typeof(SurveyCollectorMaint).FullName).ScreenID; Guid noteID = surveyCollector.NoteID.Value; PXTrace.WriteInformation("UserID " + surveyUser.UserID.Value); PXTrace.WriteInformation("noteID " + noteID.ToString()); PXTrace.WriteInformation("ScreenID " + sScreenID); var pushNotificationSender = ServiceLocator.Current.GetInstance <IPushNotificationSender>(); List <Guid> userIds = new List <Guid>(); userIds.Add(surveyUser.UserID.Value); pushNotificationSender.SendNotificationAsync( userIds: userIds, title: Messages.PushNotificationTitleSurvey, text: $"{ Messages.PushNotificationMessageBodySurvey } # { surveyCollector.CollectorName }.", link: (sScreenID, noteID), cancellation: CancellationToken.None); if (sCollectorStatus == SurveyResponseStatus.CollectorSent) { PXProcessing <SurveyUser> .SetInfo(surveyUserList.IndexOf(surveyUser), Messages.SurveySent); } else { PXProcessing <SurveyUser> .SetWarning(surveyUserList.IndexOf(surveyUser), Messages.NoDeviceError); } } catch (AggregateException ex) { var message = string.Join(";", ex.InnerExceptions.Select(e => e.Message)); PXProcessing <SurveyUser> .SetError(surveyUserList.IndexOf(surveyUser), message); } catch (Exception e) { erroroccurred = true; PXProcessing <SurveyUser> .SetError(surveyUserList.IndexOf(surveyUser), e); } } if (erroroccurred) { throw new PXException(Messages.SurveyError); } }
protected virtual void _(Events.RowSelected <SurveyFilter> e) { SurveyFilter filter = Filter.Current; Records.SetProcessDelegate(list => ProcessSurvey(e.Cache, filter, list)); }