protected static FR_L6TR_BT_2204 Execute(DbConnection Connection, DbTransaction Transaction, P_L6TR_BT_2204 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { #region UserCode var returnValue = new FR_L6TR_BT_2204(); returnValue.Result = new L6TR_BT_2204(); ORM_BIL_BillHeader header = new ORM_BIL_BillHeader(); long previousMaxPositionIndex = Parameter.previousMaxPositionIndex; #region persistHeader if (Parameter.isNewBilling) { header.Tenant_RefID = securityTicket.TenantID; header.BIL_BillHeaderID = Guid.NewGuid(); var tenantHeaders = ORM_BIL_BillHeader.Query.Search(Connection, Transaction, new ORM_BIL_BillHeader.Query() { Tenant_RefID = securityTicket.TenantID, IsDeleted = false }).ToArray(); tenantHeaders = tenantHeaders.OrderBy(t => t.Creation_Timestamp).ToArray(); var headersForThisYear = tenantHeaders.Where(h => h.Creation_Timestamp.Year == DateTime.Now.Year).ToArray(); int prevMaxHeaderNumber = 0; if (headersForThisYear.Length > 0) { ORM_BIL_BillHeader prevHeader = null; foreach (var headerTY in headersForThisYear) { int hn = 0; if (int.TryParse(headerTY.BillNumber, out hn) && prevMaxHeaderNumber <= hn) { prevMaxHeaderNumber = hn; prevHeader = headerTY; } } if (prevHeader != null) { var positionQuery = new ORM_BIL_BillPosition.Query(); positionQuery.Tenant_RefID = securityTicket.TenantID; positionQuery.BIL_BilHeader_RefID = prevHeader.BIL_BillHeaderID; var prevPositions = ORM_BIL_BillPosition.Query.Search(Connection, Transaction, positionQuery).ToArray(); if (prevPositions != null && prevPositions.Length > 0) { foreach (var prevPosition in prevPositions) { long number = 0; if (long.TryParse(prevPosition.External_PositionReferenceField, out number) && previousMaxPositionIndex < number) { previousMaxPositionIndex = number; } } } } } header.BillNumber = prevMaxHeaderNumber + 1 + ""; if (Parameter.billTreatments) { header.Save(Connection, Transaction); } } else { header.Tenant_RefID = securityTicket.TenantID; header.BIL_BillHeaderID = Parameter.ifOldBillingHeaderID; header.BillNumber = Parameter.ifOldBillingHeaderNumber; } //new edifact entry ORM_BIL_BillHeaderExtension_EDIFACT.Query edifactQuery = new ORM_BIL_BillHeaderExtension_EDIFACT.Query(); edifactQuery.Tenant_RefID = securityTicket.TenantID; var edifactRes = ORM_BIL_BillHeaderExtension_EDIFACT.Query.Search(Connection, Transaction, edifactQuery); edifactRes = edifactRes.Where(e => e.Creation_Timestamp.Year == DateTime.Now.Year).ToList(); ORM_BIL_BillHeaderExtension_EDIFACT edifact = new ORM_BIL_BillHeaderExtension_EDIFACT(); edifact.BIL_BillHeader_RefID = header.BIL_BillHeaderID; edifact.BIL_BillHeaderExtension_EDIFACTID = Guid.NewGuid(); edifact.EDIFACTCounter = edifactRes.Count + 1; edifact.Tenant_RefID = securityTicket.TenantID; if (Parameter.billTreatments) { edifact.Save(Connection, Transaction); } #endregion //positions List <L6TR_BT_2204_Position> positions = new List <L6TR_BT_2204_Position>(); List <Guid> patientsIDlist = new List <Guid>(); Dictionary <Guid, Guid> treatment2patient = new Dictionary <Guid, Guid>(); Dictionary <Guid, int> positionCountPerPatient = new Dictionary <Guid, int>(); foreach (var id in Parameter.TreatmentID_List) { var item = new ORM_HEC_Patient_Treatment(); item.Load(Connection, Transaction, id); if (item.IsTreatmentFollowup) { throw new Exception("This treatment, ID: " + id + ", is followup!"); } var patient2treatmentQuery = new ORM_HEC_Patient_2_PatientTreatment.Query(); patient2treatmentQuery.Tenant_RefID = securityTicket.TenantID; patient2treatmentQuery.HEC_Patient_Treatment_RefID = id; var patient2treatmentRes = ORM_HEC_Patient_2_PatientTreatment.Query.Search(Connection, Transaction, patient2treatmentQuery).First(); if (!patientsIDlist.Contains(patient2treatmentRes.HEC_Patient_RefID)) { patientsIDlist.Add(patient2treatmentRes.HEC_Patient_RefID); } treatment2patient.Add(id, patient2treatmentRes.HEC_Patient_RefID); if (!positionCountPerPatient.ContainsKey(patient2treatmentRes.HEC_Patient_RefID)) { positionCountPerPatient.Add(patient2treatmentRes.HEC_Patient_RefID, 0); } } //log.Debug("first loop finished after: " + sw.ElapsedMilliseconds); P_L6PA_GPBIfPID_1155 patParam = new P_L6PA_GPBIfPID_1155(); patParam.PatientID = patientsIDlist.ToArray(); var patients = cls_Get_PatientBillInfo_for_PatientID.Invoke(Connection, Transaction, patParam, securityTicket).Result; P_L6TR_GTaRDfBIbT_1204 tretParam = new P_L6TR_GTaRDfBIbT_1204(); tretParam.TreatmentID = Parameter.TreatmentID_List; var treatments = cls_Get_Treatment_and_RelevantDiagnosis_for_BillInfo_by_TreatmentID.Invoke(Connection, Transaction, tretParam, securityTicket).Result; List <Guid> followTretIDs = new List <Guid>(); if (treatments != null) { foreach (var t in treatments) { followTretIDs.Add(t.HEC_Patient_TreatmentID); } } P_L6TR_GFTfRbIDs_1646 followParam = new P_L6TR_GFTfRbIDs_1646(); followParam.TreatmentIDs = followTretIDs.ToArray(); var followups = cls_Get_Followups_for_Report_byIDs.Invoke(Connection, Transaction, followParam, securityTicket).Result; ///counter bill P_L5TR_GTCfPID_1152 tcParam = new P_L5TR_GTCfPID_1152(); tcParam.PatientID = patientsIDlist.ToArray(); var tretRes = cls_Get_TreatmentCount_for_PatientIDlist.Invoke(Connection, Transaction, tcParam, securityTicket).Result; int i = 1; foreach (var id in Parameter.TreatmentID_List) { #region collectPositionDataForReport var patient = patients.FirstOrDefault(p => p.HEC_PatientID == treatment2patient[id]); if (patient == null) { //throw new Exception("No patient for ID: " + treatment2patient[id]); } var treatment = treatments.FirstOrDefault(t => t.HEC_Patient_TreatmentID == id); if (treatment == null) { throw new Exception("No treatment for ID: " + id); } var followupsForThisThreatment = followups.Where(f => f.IfTreatmentFollowup_FromTreatment_RefID == treatment.HEC_Patient_TreatmentID).ToArray(); #region changeFlag if (Parameter.billTreatments) { var item = new ORM_HEC_Patient_Treatment(); item.Load(Connection, Transaction, id); item.IsTreatmentBilled = true; item.IfTreatmentBilled_Date = DateTime.Now; item.Save(Connection, Transaction); } #endregion #region persistPosition ORM_BIL_BillPosition position = new ORM_BIL_BillPosition(); position.Tenant_RefID = securityTicket.TenantID; position.PositionIndex = i; position.BIL_BilHeader_RefID = header.BIL_BillHeaderID; position.BIL_BillPositionID = Guid.NewGuid(); if (Parameter.billTreatments) { position.Save(Connection, Transaction); } ORM_BIL_BillPosition_2_PatientTreatment p2t = new ORM_BIL_BillPosition_2_PatientTreatment(); p2t.AssignmentID = Guid.NewGuid(); p2t.Tenant_RefID = securityTicket.TenantID; p2t.BIL_BillPosition_RefID = position.BIL_BillPositionID; p2t.HEC_Patient_Treatment_RefID = treatment.HEC_Patient_TreatmentID; if (Parameter.billTreatments) { p2t.Save(Connection, Transaction); } #endregion ORM_HEC_Doctor doctor = new ORM_HEC_Doctor(); if (treatment.IfTreatmentPerformed_ByDoctor_RefID != Guid.Empty) { var doctorQuery = new ORM_HEC_Doctor.Query(); doctorQuery.HEC_DoctorID = treatment.IfTreatmentPerformed_ByDoctor_RefID; doctor = ORM_HEC_Doctor.Query.Search(Connection, Transaction, doctorQuery).FirstOrDefault(); } var practiceQuery = new ORM_HEC_MedicalPractis.Query(); practiceQuery.HEC_MedicalPractiseID = treatment.TreatmentPractice_RefID; practiceQuery.Tenant_RefID = securityTicket.TenantID; var practice = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, practiceQuery).FirstOrDefault(); string BSNR = "x"; if (practice != null) { var practiceInfoQuery = new ORM_CMN_COM_CompanyInfo.Query(); practiceInfoQuery.CMN_COM_CompanyInfoID = practice.Ext_CompanyInfo_RefID; practiceInfoQuery.Tenant_RefID = securityTicket.TenantID; var practiceInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, practiceInfoQuery).First(); BSNR = practiceInfo.CompanyInfo_EstablishmentNumber; } L6TR_BT_2204_Position pos = new L6TR_BT_2204_Position(); pos.ORM_BIL_BillPositionID = position.BIL_BillPositionID; pos.strDoctorLANR = (doctor != null) ? doctor.DoctorIDNumber : "HEC_Doctor is null!!!!"; pos.TreatmentID = treatment.HEC_Patient_TreatmentID; pos.bTreatmentIsFollowup = treatment.IsTreatmentFollowup; pos.strPracticeBSNR = BSNR; pos.dtTreatment = treatment.IfTreatmentPerformed_Date; if (patient != null) { pos.iTreatmentNumber = tretRes.FirstOrDefault(t => t.HEC_PatientID == patient.HEC_PatientID).treatmentCount + positionCountPerPatient[treatment2patient[id]]; pos.iPatientSex = patient.Gender; pos.iPatientInsuranceState = (patient.InsuranceStateCode != null) ? patient.InsuranceStateCode : String.Empty; pos.dtPatientBirthDate = patient.Birthdate; pos.PatientFirstName = patient.FirstName; pos.PatientLastName = patient.LastName; pos.PatientInsuranceNumber = (patient.HealthInsurance_Number != null) ? patient.HealthInsurance_Number : String.Empty; } else { pos.iTreatmentNumber = -1; pos.iPatientSex = 0; pos.iPatientInsuranceState = "xxxxx"; pos.dtPatientBirthDate = DateTime.MinValue; pos.PatientFirstName = "x"; pos.PatientLastName = "x"; pos.PatientInsuranceNumber = "x"; } pos.cTreatmentLocalization = (treatment.IsTreatmentOfLeftEye) ? "L" : "R"; pos.strFollowupPractice = "-"; pos.strFollowupDoctor = "-"; pos.strFollowupStatus = "Keine Nachuntersuchung geplant."; if (followupsForThisThreatment != null && followupsForThisThreatment.Length > 0) { var performedF = followupsForThisThreatment.Where(f => f.IsTreatmentPerformed == true).ToArray(); if (performedF != null && performedF.Length > 0) { performedF = performedF.OrderBy(f => f.IfTreatmentPerformed_Date).ToArray(); var firstPF = performedF.First(); pos.strFollowupDoctor = firstPF.DoctorFirstName + " " + firstPF.DoctorLastname; pos.dtFollowup = firstPF.IfTreatmentPerformed_Date; pos.strFollowupPractice = firstPF.DisplayName; pos.strFollowupStatus = (firstPF.IsTreatmentPerformed) ? "durchgeführt" : "geplant"; } else { var scheduledF = followupsForThisThreatment.Where(f => f.IsScheduled == true).ToArray(); if (scheduledF != null && scheduledF.Length > 0) { scheduledF = scheduledF.OrderBy(f => f.IfSheduled_Date).ToArray(); var firstSF = scheduledF.First(); pos.strFollowupDoctor = firstSF.DoctorFirstName + " " + firstSF.DoctorLastname; pos.dtFollowup = firstSF.IfTreatmentPerformed_Date; pos.strFollowupPractice = firstSF.DisplayName; pos.strFollowupStatus = (firstSF.IsTreatmentPerformed) ? "durchgeführt" : "geplant"; } } } var articles = new List <L6TR_BT_2204_ArticleInfo>(); foreach (var art in treatment.Article) { var article = new L6TR_BT_2204_ArticleInfo(); article.ArticleID = art.CMN_PRO_ProductID; article.Name = art.Product_Name; article.PZN = art.Product_Number; article.Quantity = art.Quantity; articles.Add(article); } pos.ArticleInfo = articles.ToArray(); var diagnosies = new List <L6TR_BT_2204_DiagnosisInfo>(); foreach (var diag in treatment.RelevantDiagnosis) { var diagnose = new L6TR_BT_2204_DiagnosisInfo(); diagnose.cDiagnosisState = diag.DiagnosisState_Abbreviation; diagnose.DiagnosisID = diag.HEC_Patient_Treatment_RelevantDiagnosisID; diagnose.strDiagnosisICD10 = diag.ICD10_Code; diagnose.PatientInsuranceState = diag.DiagnosisState_Name; diagnosies.Add(diagnose); } pos.DiagnosisInfo = diagnosies.ToArray(); positions.Add(pos); #endregion positionCountPerPatient[treatment2patient[id]] = positionCountPerPatient[treatment2patient[id]] + 1; i++; } returnValue.Result.Positions = positions.ToArray(); returnValue.Result.EDIFACTCounter = edifact.EDIFACTCounter; returnValue.Result.HeaderNumber = header.BillNumber; returnValue.Result.HeaderID = header.BIL_BillHeaderID; returnValue.Result.previousMaxPositionIndex = previousMaxPositionIndex; return(returnValue); #endregion UserCode }
protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_L6TR_PBD_1706 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null) { #region UserCode var returnValue = new FR_Base(); if (Parameter.OutputData == null) { return(returnValue); } var treatmentIDs = Parameter.OutputData.Select(s => Guid.Parse(s.strTreatmentID)).Distinct().ToArray(); ORM_BIL_BillHeader header = ORM_BIL_BillHeader.Query.Search(Connection, Transaction, new ORM_BIL_BillHeader.Query() { Tenant_RefID = securityTicket.TenantID, BIL_BillHeaderID = Parameter.HeaderID }).Single(); ORM_BIL_BillHeaderExtension_EDIFACT edifact = new ORM_BIL_BillHeaderExtension_EDIFACT(); edifact.BIL_BillHeader_RefID = header.BIL_BillHeaderID; edifact.BIL_BillHeaderExtension_EDIFACTID = Guid.NewGuid(); edifact.EDIFACTCounter = Parameter.EdifactNumber; edifact.Tenant_RefID = securityTicket.TenantID; edifact.Modification_Timestamp = DateTime.Now; edifact.Save(Connection, Transaction); var ci = CultureInfo.InvariantCulture.Clone() as CultureInfo; ci.NumberFormat.NumberDecimalSeparator = ","; foreach (var treatmentID in treatmentIDs) { bool IsArticleAdded = false; var treatment = ORM_HEC_Patient_Treatment.Query.Search(Connection, Transaction, new ORM_HEC_Patient_Treatment.Query() { Tenant_RefID = securityTicket.TenantID, HEC_Patient_TreatmentID = treatmentID }).Single(); treatment.IsTreatmentBilled = true; treatment.IfTreatmentBilled_Date = DateTime.Now; treatment.Modification_Timestamp = DateTime.Now; treatment.Save(Connection, Transaction); foreach (var item in Parameter.OutputData.Where(i => i.strTreatmentID == treatmentID.ToString()).ToList()) { ORM_BIL_BillPosition billPosition = new ORM_BIL_BillPosition(); billPosition.Tenant_RefID = securityTicket.TenantID; //billPosition.PositionIndex = i; billPosition.BIL_BilHeader_RefID = header.BIL_BillHeaderID; billPosition.BIL_BillPositionID = Guid.NewGuid(); billPosition.External_PositionCode = item.strGpos; billPosition.External_PositionReferenceField = item.iProcessNumber.ToString(); billPosition.PositionValue_IncludingTax = decimal.Parse(item.strChargedValue, ci); billPosition.Modification_Timestamp = DateTime.Now; switch (item.iTypeOfBillingPosition) { case 1: billPosition.External_PositionType = "Behandlung"; break; case 2: billPosition.External_PositionType = "Nachsorge"; break; case 3: billPosition.External_PositionType = "Behandlung | Nachsorge"; break; case 4: billPosition.External_PositionType = "Zusatzposition Bevacuzimab"; break; case 5: billPosition.External_PositionType = "Wartezeitenmanagement"; break; default: throw new Exception("Wrong bill position type!"); } billPosition.Save(Connection, Transaction); ORM_BIL_BillPosition_2_PatientTreatment p2t = new ORM_BIL_BillPosition_2_PatientTreatment(); p2t.AssignmentID = Guid.NewGuid(); p2t.Tenant_RefID = securityTicket.TenantID; p2t.BIL_BillPosition_RefID = billPosition.BIL_BillPositionID; p2t.HEC_Patient_Treatment_RefID = treatment.HEC_Patient_TreatmentID; p2t.Save(Connection, Transaction); ORM_BIL_BillPosition_TransmitionStatus billPositionTransmitionStatus = new ORM_BIL_BillPosition_TransmitionStatus(); billPositionTransmitionStatus.BIL_BillPosition_TransmitionStatusID = Guid.NewGuid(); billPositionTransmitionStatus.BillPosition_RefID = billPosition.BIL_BillPositionID; billPositionTransmitionStatus.TransmitionCode = 2; billPositionTransmitionStatus.PrimaryComment = (String)HttpContext.GetGlobalResourceObject("Global", "TransferedToAOKStatus"); billPositionTransmitionStatus.IsActive = true; billPositionTransmitionStatus.Tenant_RefID = securityTicket.TenantID; billPositionTransmitionStatus.Creation_Timestamp = DateTime.Now; billPositionTransmitionStatus.TransmittedOnDate = DateTime.Now; billPositionTransmitionStatus.Save(Connection, Transaction); if (!treatment.IsTreatmentFollowup && item.aiRelevantArticle != null && !IsArticleAdded) { Guid articleID; if (Guid.TryParse(item.aiRelevantArticle.strArticleID, out articleID)) { IsArticleAdded = true; ORM_BIL_BillPosition_2_Product billToProduct1 = new ORM_BIL_BillPosition_2_Product(); billToProduct1.Tenant_RefID = securityTicket.TenantID; billToProduct1.Creation_Timestamp = DateTime.Now; billToProduct1.AssignmentID = Guid.NewGuid(); billToProduct1.BIL_BillPosition_RefID = billPosition.BIL_BillPositionID; billToProduct1.CMN_PRO_Product_RefID = articleID; billToProduct1.Quantity = item.aiRelevantArticle.iQuantity.ToString(); billToProduct1.Save(Connection, Transaction); } } } } return(returnValue); #endregion UserCode }