public string SaveJSON(string Token, [FromBody] DataList list) { int ClaimID = 0; ClaimModel model = new ClaimModel(); Connection con = new Connection(); var insertHistory = new List <PKInfo>(); if (list.contrlData == null) { return(JsonConvert.SerializeObject(new { Submitted = false })); } int userId; int clientId; var txCommitted = false; var archive = false; var claimRefNuValue = list .contrlData .Find(x => x.TableName == "Claims") .Columns .Find(c => c.ColumnName == "ClaimRefNu") .Value; list.contrlData .Find(x => x.TableName == "Claims") .Columns .Find(c => c.ColumnName == "DateCreation") .Value = DateTime.Now.ToString(); using (var context = new OrgSys2017DataContext()) { userId = context.GetUserIDSession(Token).SingleOrDefault().UserID; clientId = context.GetClientIDBySession(Token).SingleOrDefault().ClientID; } //done outside transaction to prevent escalation to MSDTC if (string.IsNullOrEmpty(claimRefNuValue)) { claimRefNuValue = model.UniqueClaimReference("1003"); list .contrlData .Find(x => x.TableName == "Claims") .Columns .Find(c => c.ColumnName == "ClaimRefNu") .Value = claimRefNuValue; } else { archive = true; } try { using (var tx = new TransactionScope()) { var conString = ConfigurationManager.ConnectionStrings["OrgSysConnectionString"].ToString(); using (var connection = new SqlConnection(conString)) { connection.Open(); foreach (var table in list.contrlData) { if (string.IsNullOrEmpty(table.TableName) | table.Columns.Count == 0 | table.Columns == null) { continue; } var tableRows = table.Columns.GroupBy(x => x.Row).OrderBy(rowNu => rowNu.Key); //may have multiple rows foreach (var row in tableRows) { var query = new PortalQuery(table.TableName); object columnDescription = ""; foreach (var col in row) { object val = String.IsNullOrEmpty(col.Value) ? DBNull.Value : (object)col.Value; query.AddParameter(col.ColumnName, val); columnDescription = col.ColumnType; } // if (table.TableName == "Claim_Documents") { query.AddParameter("ClaimRefNu", claimRefNuValue); query.AddParameter("UserId", User); query.AddParameter("Timestamp", DateTime.Now.ToString()); } if (table.TableName == "Claim_Dates") { query.AddParameter("DateDescription", columnDescription); } if (table.TableName == "Claim_Contacts") { query.AddParameter("ContactType", columnDescription); query.AddParameter("ClaimRefNu", claimRefNuValue); } if (table.TableName == "Claim_Emp_Schedule") { query.AddParameter("WeekNo", row.Key); } if (table.TableName == "Claim_Emp_OtherEarnings") { query.AddParameter("WeekNu", row.Key); } if (table.TableName == "Claim_Employee") { query.AddParameter("DemEmpID", 1); } if (table.TableName == "Claim_Emp_ContactTypeDetails") { query.AddParameter("ClaimReference", claimRefNuValue); } if (table.TableName == "Claim_ICDCM_Witness") { query.AddParameter("Witness", row.Key); query.AddParameter("ClaimRefNu", claimRefNuValue); } if (table.TableName == "Claim_Injury_Cause") { query.AddParameter("IsSafe", 1); } if (table.TableName == "Claim_Injury_BodyPart") { query.AddParameter("Partside", row.FirstOrDefault().Group); } if (table.TableName == "Claims") { query.AddParameter("Status", list.status); query.AddParameter("ClientID", clientId); query.AddParameter("UserSubmitted", userId); if (!archive) { query.AddParameter("Archived", 0); } } //find fk reference and add as parameter if present var res = insertHistory.Find(x => x.PkTable == table.PKTable); if (res != null) { query.AddParameter(table.FKName, res.PkValue); } var insertedId = query.ExecuteInsert(connection); insertHistory.Add(new PKInfo(table.TableName, insertedId)); if (table.TableName == "Claims") { ClaimID = insertedId; } } } } tx.Complete(); txCommitted = true; } if (txCommitted && archive) { //newClaimRefNu not set means it exists; archive //done outside transaction to prevent escalation to MSDTC model.ArchiveClaim(claimRefNuValue); } } catch (Exception e) { ExceptionLog.LogException(e); } return(JsonConvert.SerializeObject(new { Submitted = txCommitted, ClaimID = ClaimID })); }
public HttpResponseMessage Post([FromBody] string claimData) { try { ClaimModel model = new ClaimModel(); var archive = false; using (var context = new OrgSys2017DataContext()) { if (claimData.Length > 0) { //Deserialzing Claim Objects ClaimList list = JsonConvert.DeserializeObject <ClaimList>(claimData.ToString()); //Employee Schedule foreach (Claim_Emp_Schedule schedule in list.claimData.objSch) { list.claimData.objEmp.Claim_Emp_Schedules.Add(schedule); } //Need to fix it[FIXED] //Billing Details //foreach (Billable bill in list.claimData.objBill) //{ // if (bill.objBilling != null) // { // bill.objComm.Claim_BillingDetails.Add(bill.objBilling); // } // // list.claimData.objClaim.Claim_Comments.Add(bill.objComm); //} //Main Dates foreach (Claim_Date claimDate in list.claimData.objDates) { list.claimData.objClaim.Claim_Dates.Add(claimDate); } //Abs Dates foreach (Claim_AbsenceDate absDate in list.claimData.objAbs) { list.claimData.objClaim.Claim_AbsenceDates.Add(absDate); } //Doc Dates foreach (Claim_Date docDate in list.claimData.objDoc) { list.claimData.objClaim.Claim_Dates.Add(docDate); } //Contacts foreach (Claim_Contact claimCon in list.claimData.objCon) { list.claimData.objClaim.Claim_Contacts.Add(claimCon); } // Job Details list.claimData.objEmp.Claim_Emp_JobDetails.Add(list.claimData.objJob); // Pay Details list.claimData.objEmp.Claim_Emp_PayDetails.Add(list.claimData.objPay); //Payroll list.claimData.objEmp.Claim_Employee_PayRolls.Add(list.claimData.objPayroll); //Payroll Earnings foreach (Claim_Emp_Payroll_Earning ear in list.claimData.objEarn) { list.claimData.objPayroll.Claim_Emp_Payroll_Earnings.Add(ear); } //Payroll Offsets foreach (Claim_Emp_Payroll_Offset offset in list.claimData.objOffset) { list.claimData.objPayroll.Claim_Emp_Payroll_Offsets.Add(offset); } //Offset Insurance foreach (Claim_Emp_Payroll_Insurance ins in list.claimData.objIns) { list.claimData.objPayroll.Claim_Emp_Payroll_Insurances.Add(ins); } //Predisability foreach (Claim_Emp_Payroll_PreDi dis in list.claimData.objDisability) { list.claimData.objPayroll.Claim_Emp_Payroll_PreDis.Add(dis); } //Add Contct Type foreach (Claim_Emp_ContactTypeDetail conType in list.claimData.objContactType) { list.claimData.objEmp.Claim_Emp_ContactTypeDetails.Add(conType); } list.claimData.objClaim.Claim_Absences.AddRange(list.claimData.objAbsence); list.claimData.objClaim.Claim_CPP_Applications.AddRange(list.claimData.objCPP); list.claimData.objClaim.Claim_GRTW_Schedules.AddRange(list.claimData.objGRTW); list.claimData.objClaim.Claim_Rehabilitations.AddRange(list.claimData.objRehab); list.claimData.objClaim.Claim_AdditionalDetails.Add(list.claimData.objAdditionalDetails); //Employee list.claimData.objClaim.Claim_Employees.Add(list.claimData.objEmp); //All the incident saving information would go in here......... //Incidents foreach (Incident incident in list.claimData.objInc) { //Witness if (incident.objWitness != null) { incident.objIncident.Claim_ICDCM_Witnesses.AddRange(incident.objWitness); } //Limitations if (incident.objLimit != null) { incident.objIncident.Claim_ICDCM_Limitations.AddRange(incident.objLimit); } //Corrective Actions if (incident.objAct != null) { incident.objIncident.Claim_Injury_CorrActions.AddRange(incident.objAct); } //Damage foreach (Damage dam in incident.objDam) { if (dam.objProp.PropertyType != "") { Claim_ICDCM_Collateral_Property pro = dam.objProp; dam.objDamage.Claim_ICDCM_Collateral_Properties.Add(pro); } if (dam.objAni.AnimalType != "") { Claim_ICDCM_Collateral_Animal ani = dam.objAni; dam.objDamage.Claim_ICDCM_Collateral_Animals.Add(ani); } incident.objIncident.Claim_ICDCM_CollateralDamages.Add(dam.objDamage); } //Category foreach (Category cat in incident.objCat) { cat.objCategory.Claim_Injury_BodyParts.AddRange(cat.objPart); cat.objCategory.Claim_Injury_Medications.AddRange(cat.objMed); cat.objCategory.Claim_Injury_Activities.AddRange(cat.objActivities); //Cause //if (cat.objCause != null) //{ // Cause cause = cat.objCause; // cat.objCategory.Claim_Injury_Causes = cause.objCauseFields; // cat.objCategory.Claim_Injury_Causes.Claim_Injury_Cause_EnvironmentFactors.AddRange(cause.objEnvironmentFactors); // cat.objCategory.Claim_Injury_Causes.Claim_Injury_Cause_EquipmentInvolveds.AddRange(cause.objEquipmentInvolved); // cat.objCategory.Claim_Injury_Causes.Claim_Injury_Cause_HumanFactors.AddRange(cause.objHumanFactors); // cat.objCategory.Claim_Injury_Causes.Claim_Injury_Cause_InjuryLocations.AddRange(cause.objInjuryLocations); // cat.objCategory.Claim_Injury_Causes.Claim_Injury_Cause_WeatherConditions.AddRange(cause.objWeatherConditions); //} incident.objIncident.Claim_ICDCMCategories.Add(cat.objCategory); } //Commented it out so that I could do some of the testing-----Sam Khan -----08/13/2018 //I tried uncommenting , but it would give me an error which Aaron has to take a look. list.claimData.objClaim.Claim_ICDCMs.Add(incident.objIncident); } var claimReferenceNumber = list.claimData.objClaim.ClaimRefNu; if (claimReferenceNumber == null || claimReferenceNumber == string.Empty) { var claimRef = model.UniqueClaimReference(list.claimData.objClaim.ClientID.ToString()); //set claimrefnu for new claim objects list.claimData.objClaim.ClaimRefNu = claimRef; list.claimData.objContactType.ForEach(x => x.ClaimReference = claimRef); list.claimData.objCon.ForEach(x => x.ClaimRefNu = claimRef); list.claimData.objAbsence.ForEach(x => x.ClaimRefNu = claimRef); } else { archive = true; //archive table records that are linked to the claim by claimReferenceNumber /abovtenko context.Claim_Emp_ContactTypeDetails .Where(x => x.ClaimReference == claimReferenceNumber).ToList().ForEach(x => x.Archived = true); } list.claimData.objClaim.Archived = false; var RefDate = list.claimData.objClaim.ReferralDate; if (RefDate == null) { var now = DateTime.Now; if (now > new DateTime(now.Year, now.Month, now.Day, 15, 30, 00)) { list.claimData.objClaim.ReferralDate = new DateTime().AddDays(1); } else { list.claimData.objClaim.ReferralDate = new DateTime(); } } context.Claims.InsertOnSubmit(list.claimData.objClaim); context.SubmitChanges(); //Save changes in to data base //archive old claim after SubmitChanges() to ensure that a new version of this claim was added to db /abovtenko if (archive) { model.ArchiveClaim(list.claimData.objClaim.ClaimRefNu); } ClaimID = list.claimData.objClaim.ClaimID; } } } catch (Exception ex) { ExceptionLog.LogException(ex); return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(ClaimID.ToString()) }); }