@@ -26,6 +26,7 @@ public class P615CreateMonthlyReportProvider(IOptionsSnapshot<D365AuthSettings>
26
26
private string _facId ;
27
27
private DateTime _previousMonth ;
28
28
private string _hrQuestionIdsXML ;
29
+ private Guid _reportTemplateId ;
29
30
30
31
public Int16 ProcessId => Setup . Process . FundingReports . CreateMonthlyReportId ;
31
32
public string ProcessName => Setup . Process . FundingReports . CreateMonthlyReportName ;
@@ -61,38 +62,75 @@ public string RequestUri
61
62
}
62
63
}
63
64
65
+ public string HRQuestionTemplateUri
66
+ {
67
+ get
68
+ {
69
+ // Note: Get the active funding record
70
+ //for reference only
71
+ var fetchXml = $ """
72
+ <fetch distinct="true">
73
+ <entity name="ofm_question">
74
+ <attribute name="ofm_question_id" />
75
+ <attribute name="ofm_header" />
76
+ <attribute name="ofm_questionid" />
77
+ <filter>
78
+ <condition attribute="ofm_question_id" operator="in">
79
+ { _hrQuestionIdsXML }
80
+ </condition>
81
+ </filter>
82
+ <link-entity name="ofm_section" from="ofm_sectionid" to="ofm_section">
83
+ <link-entity name="ofm_survey" from="ofm_surveyid" to="ofm_survey">
84
+ <filter>
85
+ <condition attribute="ofm_surveyid" operator="eq" value="{ _reportTemplateId } " />
86
+ </filter>
87
+ </link-entity>
88
+ </link-entity>
89
+ </entity>
90
+ </fetch>
91
+ """ ;
92
+
93
+ var hrQuestionTemplateUri = $ """
94
+ ofm_questions?fetchXml={ WebUtility . UrlEncode ( fetchXml ) }
95
+ """ . CleanCRLF ( ) ;
96
+ return hrQuestionTemplateUri ;
97
+ }
98
+ }
99
+
64
100
public string HRQuestionResponseUri
65
101
{
66
102
get
67
103
{
68
104
// Note: Get the active funding record
69
105
//for reference only
70
106
var fetchXml = $ """
71
- <fetch version="1.0" mapping="logical" distinct ="true" no-lock ="true">
107
+ <fetch version="1.0" mapping="logical" no-lock ="true" distinct ="true">
72
108
<entity name="ofm_question_response">
73
109
<attribute name="ofm_response_text" />
74
- <attribute name="ofm_row_id" />
75
- <attribute name="ofm_header" />
76
- <attribute name="ofm_question" />
77
- <link-entity name="ofm_survey_response" from="ofm_survey_responseid" to="ofm_survey_response">
78
- <attribute name="ofm_response_id" />
79
- <attribute name="ofm_facility" />
80
- <attribute name="ofm_start_date" />
81
- <attribute name="ofm_submitted_on" />
82
- <filter>
110
+ <link-entity name="ofm_survey_response" link-type="inner" from="ofm_survey_responseid" to="ofm_survey_response">
111
+ <filter type="and">
83
112
<condition attribute="ofm_facility" operator="eq" value="{ _facId } " />
84
113
<condition attribute="ofm_start_date" operator="on" value="{ _previousMonth } " />
85
- <condition attribute="ofm_submitted_on" operator="not-null" value="" />
114
+ <condition attribute="ofm_submitted_on" operator="not-null" />
86
115
</filter>
87
116
</link-entity>
88
- <link-entity name="ofm_question" from="ofm_questionid" to="ofm_question">
89
- <attribute name="ofm_questionid " />
117
+ <link-entity name="ofm_question" from="ofm_questionid" to="ofm_question" link-type="outer" alias="question" >
118
+ <attribute name="ofm_question_id " />
90
119
<filter>
91
120
<condition attribute="ofm_question_id" operator="in">
92
- { _hrQuestionIdsXML }
121
+ { _hrQuestionIdsXML }
93
122
</condition>
94
123
</filter>
124
+ <link-entity name="ofm_section" from="ofm_sectionid" to="ofm_section">
125
+ <filter>
126
+ <condition attribute="ofm_section_title" operator="begins-with" value="Human" />
127
+ </filter>
128
+ </link-entity>
95
129
</link-entity>
130
+ <link-entity name="ofm_question" from="ofm_questionid" to="ofm_header" link-type="outer" alias="header" visible="false">
131
+ <attribute name="ofm_question_id" />
132
+ </link-entity>
133
+ <attribute name="ofm_row_id" />
96
134
</entity>
97
135
</fetch>
98
136
""" ;
@@ -104,7 +142,6 @@ public string HRQuestionResponseUri
104
142
}
105
143
}
106
144
107
-
108
145
public async Task < ProcessData > GetDataAsync ( )
109
146
{
110
147
_logger . LogDebug ( CustomLogEvent . Process , "Calling GetData of {nameof}" , nameof ( P615CreateMonthlyReportProvider ) ) ;
@@ -153,7 +190,7 @@ public async Task<ProcessData> GetReportDataAsync(string uri)
153
190
if ( ! response . IsSuccessStatusCode )
154
191
{
155
192
var responseBody = await response . Content . ReadAsStringAsync ( ) ;
156
- _logger . LogError ( CustomLogEvent . Process , "Failed to query fiscal year with the server error {responseBody}" , responseBody . CleanLog ( ) ) ;
193
+ _logger . LogError ( CustomLogEvent . Process , "Failed to query result with the server error {responseBody}" , responseBody . CleanLog ( ) ) ;
157
194
158
195
return await Task . FromResult ( new ProcessData ( string . Empty ) ) ;
159
196
}
@@ -165,7 +202,7 @@ public async Task<ProcessData> GetReportDataAsync(string uri)
165
202
{
166
203
if ( currentValue ? . AsArray ( ) . Count == 0 )
167
204
{
168
- _logger . LogInformation ( CustomLogEvent . Process , "No fiscal year found with query {requestUri}" , uri . CleanLog ( ) ) ;
205
+ _logger . LogInformation ( CustomLogEvent . Process , "No result found with query {requestUri}" , uri . CleanLog ( ) ) ;
169
206
}
170
207
d365Result = currentValue ! ;
171
208
}
@@ -296,7 +333,7 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
296
333
_logger . LogInformation ( CustomLogEvent . Process , "Cannot find report template." ) ;
297
334
return ProcessResult . Completed ( ProcessId ) . SimpleProcessResult ;
298
335
}
299
- var reportTempateId = reportTemplate . Id ;
336
+ _reportTemplateId = reportTemplate . Id ;
300
337
301
338
302
339
//Convert the report Month
@@ -323,7 +360,7 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
323
360
var reportData = new JsonObject
324
361
{
325
362
{ "ofm_facility@odata.bind" , $ "/accounts({ facility } )"} ,
326
- { "ofm_survey@odata.bind" , $ "/ofm_surveies({ reportTempateId } )" } ,
363
+ { "ofm_survey@odata.bind" , $ "/ofm_surveies({ _reportTemplateId } )" } ,
327
364
{ "ofm_fiscal_year@odata.bind" , $ "/ofm_fiscal_years({ fiscalYear } )" } ,
328
365
{ "ofm_report_month" , reportMonth } ,
329
366
{ "ofm_duedate" , duedateInUTC } ,
@@ -355,6 +392,11 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
355
392
var hrQuestionIds = _processParams . FundingReport . HRQuestions . Split ( "," ) ;
356
393
_hrQuestionIdsXML = "<value>" + String . Join ( "</value>\r \n <value>" , hrQuestionIds ) + "</value>" ;
357
394
395
+ //Get question template for current report template
396
+
397
+ var hrQuestionTemplateData = await GetReportDataAsync ( HRQuestionTemplateUri ) ;
398
+ var serializedHRQuestionTemplateData = System . Text . Json . JsonSerializer . Deserialize < List < Question > > ( hrQuestionTemplateData . Data , Setup . s_writeOptionsForLogs ) ;
399
+
358
400
List < HttpRequestMessage > questionResponseRequests = [ ] ;
359
401
360
402
foreach ( var report in createdReports )
@@ -367,12 +409,16 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
367
409
368
410
foreach ( var questionResponse in serializedPreviousHRQuestionResponseData )
369
411
{
370
- var newQuestionResponse = new JsonObject
412
+
413
+ var questionTemplateId = $ "/ofm_questions({ serializedHRQuestionTemplateData ? . Where ( q => q . ofm_question_id == questionResponse . ofm_question_qid ) . FirstOrDefault ( ) . ofm_questionid } )";
414
+ var headerTemplateId = String . IsNullOrEmpty ( questionResponse . ofm_header_qid ) ? null : $ "/ofm_questions({ serializedHRQuestionTemplateData ? . Where ( q => q . ofm_question_id == questionResponse . ofm_header_qid ) . FirstOrDefault ( ) . ofm_questionid } )";
415
+
416
+ var newQuestionResponse = new JsonObject
371
417
{
372
418
{ "ofm_survey_response@odata.bind" , $ "/ofm_survey_responses({ uid } )"} ,
373
- { "ofm_question@odata.bind" , $ "/ofm_questions( { questionResponse . ofm_questionid } )" } ,
374
- { "ofm_header@odata.bind" , String . IsNullOrEmpty ( questionResponse . ofm_headerid ) ? null : $ "/ofm_questions( { questionResponse . ofm_headerid } )" } ,
375
- { "ofm_row_id" , questionResponse . ofm_row_id } ,
419
+ { "ofm_question@odata.bind" , questionTemplateId } ,
420
+ { "ofm_header@odata.bind" , headerTemplateId } ,
421
+ { "ofm_row_id" , questionResponse . ofm_row_id } ,
376
422
{ "ofm_response_text" , questionResponse . ofm_response_text }
377
423
} ;
378
424
0 commit comments