C Sharp - Generating XML data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using TylerToCalPers457.Parameters;
using TylerToCalPers457.Participants;
using TylerToCalPers457.PayPeriods;

namespace TylerToCalPers457.OutputFile
{
internal class OutputXMLGeneration
{

private readonly string _connectionString;
private readonly string _payPeriodConnectionString;

public OutputXMLGeneration(
string connectionString,
string payPeriodConnectionString
)
{
_connectionString = connectionString;
_payPeriodConnectionString = payPeriodConnectionString;
}

public void GenerateXmlFile(string outputFilePath)
{
Console.WriteLine();
Console.WriteLine($"-- GenerateXmlFile --");
Console.WriteLine();

try
{
// Retrieve current pay period
PayPeriod payPeriod = new PayPeriod(_payPeriodConnectionString);
payPeriod.GetLastPayPeriod();

// Create a new XmlWriterSettings object to customize XML output
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
Encoding = Encoding.UTF8
};

// Retrieve parameters, staff, and deductions data
var parameterService = new ParameterService(_connectionString);
var parametersData = parameterService.GetParametersData();

var participantService = new ParticipantService(_connectionString);
var staffAndDeductionsData = participantService.GetStaffAndDeductionsDataFromDb();

// Create XML file
using (XmlWriter writer = XmlWriter.Create(outputFilePath, settings))
{
// Start SOAP Envelope
writer.WriteStartDocument();
writer.WriteStartElement("soap", "Envelope", http://schemas.xmlsoap.org/soap/envelope/);
writer.WriteAttributeString("xmlns", "soap", null, http://schemas.xmlsoap.org/soap/envelope/);

writer.WriteStartElement("soap", "Header", null);

writer.WriteStartElement("cuns", "HeaderInfo", http://calpers.ca.gov/PSR/CommonUtilitiesV1);
writer.WriteAttributeString("xmlns", "cuns", null, http://calpers.ca.gov/PSR/CommonUtilitiesV1);


// Header Information
writer.WriteElementString("cuns", "InterfaceTypeId", null, parametersData["InterfaceTypeId"].Trim());
writer.WriteElementString("cuns", "BusinessPartnerId", null, parametersData["BusinessPartnerId"].Trim());
writer.WriteElementString("cuns", "SchemaVersion", null, parametersData["SchemaVersion"].Trim());
writer.WriteElementString("cuns", "DateTime", null, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss").Trim());

writer.WriteEndElement(); // End HeaderInfo
writer.WriteEndElement(); // End Header

// Body
writer.WriteStartElement("soap", "Body", null);
//writer.WriteStartElement("n1", "RetirementAndPayrollTransactions", "xmlns:cuns=\http://calpers.ca.gov/PSR/CommonUtilitiesV1\ xmlns:xsi=\http://www.w3.org/2001/XMLSchema-instance\ xmlns:rhtns=\http://calpers.ca.gov/PSR/RetirementHealthTransactionsV1\ xmlns:n1=\http://calpers.ca.gov/PSR/PayrollRetirementV1\ xsi:schemaLocation=\http://calpers.ca.gov/PSR/PayrollRetirementV1\r\nPayrollRetirementV1.xsd\");
writer.WriteStartElement("n1", "RetirementAndPayrollTransactions", http://calpers.ca.gov/PSR/PayrollRetirementV1);
writer.WriteAttributeString("xmlns", "cuns", null, http://calpers.ca.gov/PSR/CommonUtilitiesV1);
writer.WriteAttributeString("xmlns", "xsi", null, http://www.w3.org/2001/XMLSchema-instance);
writer.WriteAttributeString("xmlns", "rhtns", null, http://calpers.ca.gov/PSR/RetirementHealthTransactionsV1);
writer.WriteAttributeString("xsi", "schemaLocation", null, http://calpers.ca.gov/PSR/PayrollRetirementV1 PayrollRetirementV1.xsd);

writer.WriteStartElement("n1", "EmployerPayrollReport", null);
writer.WriteStartElement("n1", "Employer", null);
writer.WriteElementString("n1", "EmployersCalPERSId", null, parametersData["EmployersCalPERSId"].Trim());
writer.WriteStartElement("n1", "Report", null);
writer.WriteElementString("n1", "ReportPeriodBeginDate", null, payPeriod.BeginDate.ToString("yyyy-MM-dd").Trim());
writer.WriteElementString("n1", "ReportPeriodEndDate", null, payPeriod.EndDate.ToString("yyyy-MM-dd").Trim());
writer.WriteElementString("n1", "ProgramType", null, parametersData["ProgramType"].Trim());
writer.WriteElementString("n1", "TestReport", null, parametersData["TestReport"].Trim());
writer.WriteElementString("n1", "ReportType", null, parametersData["ReportType"].Trim());
// report counter
writer.WriteStartElement("n1", "ReportCounter", null);
writer.WriteElementString("n1", "RecordType", null, parametersData["RecordType"].Trim());
writer.WriteElementString("n1", "RecordTypeCount", null, parametersData["RecordTypeCount"].Trim());
//writer.WriteElementString("n1", "RecordTypeTotal", null, parametersData["RecordTypeTotal"].Trim());
writer.WriteElementString("n1", "RecordTypeTotal", null, staffAndDeductionsData.Count.ToString().Trim());
writer.WriteEndElement(); // End ReportCounter

// Process each participant
foreach (var participant in staffAndDeductionsData)
{
writer.WriteStartElement("n1", "Participant", null);

// Participant Info
writer.WriteStartElement("n1", "ParticipantInfo", null);
writer.WriteElementString("n1", "ParticipantsCalPERSId", null, participant.ParticipantsCalPERSId.Trim());
writer.WriteElementString("n1", "FirstName", null, participant.FirstName.Trim());
writer.WriteElementString("n1", "LastName", null, participant.LastName.Trim());
writer.WriteEndElement(); // End ParticipantInfo

// ParticipantRecordDetails
writer.WriteStartElement("n1", "ParticipantRecordDetails", null);

// Earned Period Details
writer.WriteStartElement("n1", "EarnedPeriodDetails", null);
writer.WriteElementString("n1", "RecordPeriodBeginDate", null, payPeriod.BeginDate.ToString("yyyy-MM-dd").Trim());
writer.WriteElementString("n1", "RecordPeriodEndDate", null, payPeriod.EndDate.ToString("yyyy-MM-dd").Trim());
writer.WriteEndElement(); // End EarnedPeriodDetails

// Supplemental Income Plan
writer.WriteStartElement("n1", "SupplementalIncomePlan", null);
writer.WriteElementString("n1", "PlanId", null, participant.PlanId.Trim());
writer.WriteElementString("n1", "TaxedMemberPaidContributionOrDeduction", null, participant.TaxedMemberPaidContributionOrDeduction.Trim());
writer.WriteElementString("n1", "TaxDeferredMemberPaidContributionOrDeduction", null, participant.TaxDeferredMemberPaidContributionOrDeduction.Trim());
writer.WriteElementString("n1", "TaxDeferredEmployerPaidMemberContribution", null, participant.TaxDeferredEmployerPaidMemberContribution.Trim());
writer.WriteElementString("n1", "AfterTaxLoanPayment", null, participant.AfterTaxLoanPayment.Trim());
writer.WriteEndElement(); // End SupplementalIncomePlan

writer.WriteEndElement(); // End ParticipantRecordDetails
writer.WriteEndElement(); // End Participant
}

// Close all open tags
writer.WriteEndElement(); // End EmployerPayrollReport
writer.WriteEndElement(); // End RetirementAndPayrollTransactions
writer.WriteEndElement(); // End Report
writer.WriteEndElement(); // End Employer
writer.WriteEndElement(); // End Body
writer.WriteEndElement(); // End Envelope
}

Console.WriteLine($"XML file generated successfully at {outputFilePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error generating XML file: {ex.Message}");
throw;
}
}


}
}