/// <summary> /// Adding (3) Rental Agreement Rate and (3) Time Records to rental agreement /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="rentalAgreement"></param> /// <param name="systemId"></param> static private void addingRate_Time_For_RentaAgreement(DbAppContext dbContext, HETSAPI.Import.EquipUsage oldObject, ref Models.RentalAgreement rentalAgreement, string workedDate, string systemId) { // Adding rental agreement rates and Time_Records: The two are added together becase Time Record reference rental agreement rate. Models.RentalAgreementRate rate = new RentalAgreementRate(); Models.TimeRecord tRec = new TimeRecord(); //Adding general properties for RentalAgreement Rate DateTime lastUpdateTimestamp = DateTime.UtcNow; if (oldObject.Entered_Dt != null) { lastUpdateTimestamp = DateTime.ParseExact(oldObject.Entered_Dt.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } string lastUpdateUserid = oldObject.Created_By == null ? systemId : ImportUtility.AddUserFromString(dbContext, oldObject.Created_By, systemId).SmUserId; //Adding general properties for Time Records DateTime workedDateTime; if (oldObject.Worked_Dt != null) { workedDateTime = DateTime.ParseExact(workedDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } else { workedDateTime = DateTime.UtcNow; } DateTime createdDate; if (oldObject.Created_Dt != null) { createdDate = DateTime.ParseExact(oldObject.Created_Dt.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } else { createdDate = DateTime.UtcNow; } //Adding three rates and hours using a Dictionary Dictionary <int, Pair> _f = new Dictionary <int, Pair>(); float Rate = (float)Decimal.Parse(oldObject.Rate == null ? "0" : oldObject.Rate, System.Globalization.NumberStyles.Any); float Rate2 = (float)Decimal.Parse(oldObject.Rate2 == null ? "0" : oldObject.Rate2, System.Globalization.NumberStyles.Any); float Rate3 = (float)Decimal.Parse(oldObject.Rate3 == null ? "0" : oldObject.Rate3, System.Globalization.NumberStyles.Any); float Hours = (float)Decimal.Parse(oldObject.Hours == null ? "0" : oldObject.Hours, System.Globalization.NumberStyles.Any); float Hours2 = (float)Decimal.Parse(oldObject.Hours2 == null ? "0" : oldObject.Hours2, System.Globalization.NumberStyles.Any); float Hours3 = (float)Decimal.Parse(oldObject.Hours3 == null ? "0" : oldObject.Hours3, System.Globalization.NumberStyles.Any); // Add items to dictionary. if (Hours != 0.0 || Rate != 0.0) { _f.Add(1, new Pair(Hours, Rate)); } if (Hours2 != 0.0 || Rate2 != 0.0) { _f.Add(2, new Pair(Hours2, Rate2)); } if (Hours3 != 0.0 || Rate3 != 0.0) { _f.Add(3, new Pair(Hours3, Rate3)); } // Use var in foreach loop. int ii = 0; Models.RentalAgreementRate [] rate_a = new RentalAgreementRate[3]; Models.TimeRecord [] tRec_a = new TimeRecord[3]; foreach (var pair in _f) { Models.RentalAgreementRate exitingRate = rentalAgreement.RentalAgreementRates.FirstOrDefault(x => x.Rate == pair.Value.Rate); if (exitingRate == null) //rate does not exist { // Adding the new rate rate_a[ii] = new RentalAgreementRate(); rate_a[ii].Comment = "Import from BCBid"; rate_a[ii].IsAttachment = false; rate_a[ii].LastUpdateTimestamp = lastUpdateTimestamp; rate_a[ii].LastUpdateUserid = lastUpdateUserid; rate_a[ii].CreateTimestamp = createdDate; rate_a[ii].CreateUserid = lastUpdateUserid; rate_a[ii].Rate = pair.Value.Rate; rentalAgreement.RentalAgreementRates.Add(rate_a[ii]); //Also add time Record tRec_a[ii] = new TimeRecord(); tRec_a[ii].EnteredDate = lastUpdateTimestamp; tRec_a[ii].LastUpdateUserid = lastUpdateUserid; tRec_a[ii].WorkedDate = workedDateTime; tRec_a[ii].Hours = pair.Value.Hours; tRec_a[ii].CreateTimestamp = createdDate; tRec_a[ii].CreateUserid = lastUpdateUserid; tRec_a[ii].RentalAgreementRate = rate_a[ii]; rentalAgreement.TimeRecords.Add(tRec_a[ii]); } else { //the rate already existed which is exitingRate, no need to add rate, just add Time Record Models.TimeRecord existingTimeRec = rentalAgreement.TimeRecords.FirstOrDefault(x => x.WorkedDate == workedDateTime); if (existingTimeRec == null) { //The new Time Record is added if it does not existm, otherwise, it's already existed. tRec_a[ii] = new TimeRecord(); tRec_a[ii].EnteredDate = lastUpdateTimestamp; tRec_a[ii].LastUpdateUserid = lastUpdateUserid; tRec_a[ii].WorkedDate = workedDateTime; tRec_a[ii].Hours = pair.Value.Hours; tRec_a[ii].CreateTimestamp = createdDate; tRec_a[ii].CreateUserid = lastUpdateUserid; tRec_a[ii].RentalAgreementRate = exitingRate; rentalAgreement.TimeRecords.Add(tRec_a[ii]); } } ii++; } // Ended adding Rental Agreement rates and time records. }
/// <summary> /// Copy xml item to instance (table entries) /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="rentalAgreement"></param> /// <param name="note"></param> /// <param name="workedDate"></param> /// <param name="systemId"></param> static private void CopyToTimeRecorded(PerformContext performContext, DbAppContext dbContext, HETSAPI.Import.EquipUsage oldObject, ref Models.RentalAgreement rentalAgreement, string note, string workedDate, List <Models.Equipment> equips, string systemId) { bool isNew = false; //Add the user specified in oldObject.Modified_By and oldObject.Created_By if not there in the database Models.User modifiedBy = ImportUtility.AddUserFromString(dbContext, "", systemId); Models.User createdBy = ImportUtility.AddUserFromString(dbContext, oldObject.Created_By, systemId); if (rentalAgreement == null) { isNew = true; rentalAgreement = new Models.RentalAgreement(); rentalAgreement.RentalAgreementRates = new List <RentalAgreementRate>(); rentalAgreement.TimeRecords = new List <TimeRecord>(); Models.Equipment equip = equips.FirstOrDefault(x => x.Id == oldObject.Equip_Id); //dbContext.Equipments.FirstOrDefault(x => x.Id == oldObject.Equip_Id); if (equip != null) { rentalAgreement.Equipment = equip; rentalAgreement.EquipmentId = equip.Id; } Models.Project proj = dbContext.Projects.FirstOrDefault(x => x.Id == oldObject.Project_Id); if (proj != null) { rentalAgreement.Project = proj; rentalAgreement.ProjectId = proj.Id; } // Adding rental agreement rates and Time_Records: The two are added together becase Time Record reference rental agreement rate. addingRate_Time_For_RentaAgreement(dbContext, oldObject, ref rentalAgreement, workedDate, systemId); rentalAgreement.Status = "Imported from BCBid"; rentalAgreement.Note = note; rentalAgreement.EquipmentRate = (float)Decimal.Parse(oldObject.Rate == null ? "0" : oldObject.Rate, System.Globalization.NumberStyles.Any); if (oldObject.Entered_Dt != null) { rentalAgreement.DatedOn = DateTime.ParseExact(oldObject.Entered_Dt.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } if (oldObject.Created_Dt != null) { try { rentalAgreement.CreateTimestamp = DateTime.ParseExact(oldObject.Created_Dt.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception e) { rentalAgreement.CreateTimestamp = DateTime.UtcNow; } } rentalAgreement.CreateUserid = createdBy.SmUserId; dbContext.RentalAgreements.Add(rentalAgreement); } else { rentalAgreement = dbContext.RentalAgreements.First(x => x.Note == note); if (rentalAgreement.RentalAgreementRates == null) { rentalAgreement.RentalAgreementRates = new List <RentalAgreementRate>(); } if (rentalAgreement.TimeRecords == null) { rentalAgreement.TimeRecords = new List <TimeRecord>(); } addingRate_Time_For_RentaAgreement(dbContext, oldObject, ref rentalAgreement, workedDate, systemId); rentalAgreement.LastUpdateUserid = modifiedBy.SmUserId; rentalAgreement.LastUpdateTimestamp = DateTime.UtcNow; dbContext.RentalAgreements.Update(rentalAgreement); } }