Ejemplo n.º 1
0
        /// <summary>
        /// Map data
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="rentalAgreement"></param>
        /// <param name="note"></param>
        /// <param name="workedDate"></param>
        /// <param name="equips"></param>
        /// <param name="systemId"></param>
        private static void CopyToTimeRecorded(DbAppContext dbContext, EquipUsage oldObject,
                                               ref RentalAgreement rentalAgreement, string note, string workedDate, List <Equipment> equips, string systemId)
        {
            // add the user specified in oldObject.Modified_By and oldObject.Created_By if not there in the database
            User modifiedBy = ImportUtility.AddUserFromString(dbContext, "", systemId);
            User createdBy  = ImportUtility.AddUserFromString(dbContext, oldObject.Created_By, systemId);

            if (rentalAgreement == null)
            {
                rentalAgreement = new RentalAgreement
                {
                    RentalAgreementRates = new List <RentalAgreementRate>(),
                    TimeRecords          = new List <TimeRecord>()
                };

                Equipment equip = equips.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.
                AddingRateTimeForRentaAgreement(dbContext, oldObject, ref rentalAgreement, workedDate, systemId);

                rentalAgreement.Status        = "Imported from BCBid";
                rentalAgreement.Note          = note;
                rentalAgreement.EquipmentRate = (float)Decimal.Parse(oldObject.Rate ?? "0", 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
                    {
                        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>();
                }

                AddingRateTimeForRentaAgreement(dbContext, oldObject, ref rentalAgreement, workedDate, systemId);
                rentalAgreement.LastUpdateUserid    = modifiedBy.SmUserId;
                rentalAgreement.LastUpdateTimestamp = DateTime.UtcNow;
                dbContext.RentalAgreements.Update(rentalAgreement);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adding Rental Agreement Rate and Time Records to rental agreement
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="rentalAgreement"></param>
        /// <param name="workedDate"></param>
        /// <param name="systemId"></param>
        private static void AddingRateTimeForRentaAgreement(DbAppContext dbContext, EquipUsage oldObject,
                                                            ref RentalAgreement rentalAgreement, string workedDate, string systemId)
        {
            // adding rental agreement rates and time records:
            // the two are added together because time record reference rental agreement rate

            // adding general properties for Rental Agreement 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 Record
            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 ?? "0", System.Globalization.NumberStyles.Any);
            float rate2  = (float)Decimal.Parse(oldObject.Rate2 ?? "0", System.Globalization.NumberStyles.Any);
            float rate3  = (float)Decimal.Parse(oldObject.Rate3 ?? "0", System.Globalization.NumberStyles.Any);
            float hours  = (float)Decimal.Parse(oldObject.Hours ?? "0", System.Globalization.NumberStyles.Any);
            float hours2 = (float)Decimal.Parse(oldObject.Hours2 ?? "0", System.Globalization.NumberStyles.Any);
            float hours3 = (float)Decimal.Parse(oldObject.Hours3 ?? "0", 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;

            RentalAgreementRate [] rateA = new RentalAgreementRate[3];
            TimeRecord []          tRecA = new TimeRecord[3];

            foreach (var pair in f)
            {
                RentalAgreementRate exitingRate = rentalAgreement.RentalAgreementRates.FirstOrDefault(x => x.Rate == pair.Value.Rate);

                // rate does not exist
                if (exitingRate == null)
                {
                    // adding the new rate
                    rateA[ii] = new RentalAgreementRate
                    {
                        Comment             = "Import from BCBid",
                        IsAttachment        = false,
                        LastUpdateTimestamp = lastUpdateTimestamp,
                        LastUpdateUserid    = lastUpdateUserid,
                        CreateTimestamp     = createdDate,
                        CreateUserid        = lastUpdateUserid,
                        Rate = pair.Value.Rate
                    };

                    rentalAgreement.RentalAgreementRates.Add(rateA[ii]);

                    // add time Record
                    tRecA[ii] = new TimeRecord
                    {
                        EnteredDate         = lastUpdateTimestamp,
                        LastUpdateUserid    = lastUpdateUserid,
                        WorkedDate          = workedDateTime,
                        Hours               = pair.Value.Hours,
                        CreateTimestamp     = createdDate,
                        CreateUserid        = lastUpdateUserid,
                        RentalAgreementRate = rateA[ii]
                    };

                    rentalAgreement.TimeRecords.Add(tRecA[ii]);
                }
                else
                {
                    //the rate already existed which is exitingRate, no need to add rate, just add Time Record
                    TimeRecord existingTimeRec = rentalAgreement.TimeRecords.FirstOrDefault(x => x.WorkedDate == workedDateTime);

                    if (existingTimeRec == null)
                    {
                        // the new Time Record is added if it does not exist, otherwise, it already existed
                        tRecA[ii] = new TimeRecord
                        {
                            EnteredDate         = lastUpdateTimestamp,
                            LastUpdateUserid    = lastUpdateUserid,
                            WorkedDate          = workedDateTime,
                            Hours               = pair.Value.Hours,
                            CreateTimestamp     = createdDate,
                            CreateUserid        = lastUpdateUserid,
                            RentalAgreementRate = exitingRate
                        };

                        rentalAgreement.TimeRecords.Add(tRecA[ii]);
                    }
                }

                ii++;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Map data
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="timeRecord"></param>
        /// <param name="systemId"></param>
        /// <param name="maxTimesheetIndex"></param>
        private static void CopyToTimeRecorded(DbAppContext dbContext, EquipUsage oldObject, ref TimeRecord timeRecord,
                                               string systemId, ref int maxTimesheetIndex)
        {
            try
            {
                if (oldObject.Equip_Id <= 0)
                {
                    return;
                }

                if (oldObject.Project_Id <= 0)
                {
                    return;
                }

                // ***********************************************
                // we only need records from the current fiscal
                // so ignore all others
                // ***********************************************
                DateTime fiscalStart;
                DateTime fiscalEnd;

                if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3)
                {
                    fiscalEnd = new DateTime(DateTime.UtcNow.Year, 3, 31);
                }
                else
                {
                    fiscalEnd = new DateTime(DateTime.UtcNow.AddYears(1).Year, 3, 31);
                }

                if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3)
                {
                    fiscalStart = new DateTime(DateTime.UtcNow.AddYears(-1).Year, 4, 1);
                }
                else
                {
                    fiscalStart = new DateTime(DateTime.UtcNow.Year, 4, 1);
                }

                string tempRecordDate = oldObject.Worked_Dt;

                if (string.IsNullOrEmpty(tempRecordDate))
                {
                    return; // ignore if we don't have a created date
                }

                if (!string.IsNullOrEmpty(tempRecordDate))
                {
                    DateTime?recordDate = ImportUtility.CleanDateTime(tempRecordDate);

                    if (recordDate == null ||
                        recordDate < fiscalStart ||
                        recordDate > fiscalEnd)
                    {
                        return; // ignore this record - it is outside of the current fiscal year
                    }
                }


                // ************************************************
                // get the imported equipment record map
                // ************************************************
                string tempId = oldObject.Equip_Id.ToString();

                ImportMap mapEquip = dbContext.ImportMaps.AsNoTracking()
                                     .FirstOrDefault(x => x.OldKey == tempId &&
                                                     x.OldTable == ImportEquip.OldTable &&
                                                     x.NewTable == ImportEquip.NewTable);

                if (mapEquip == null)
                {
                    throw new DataException(string.Format("Cannot locate Equipment record (Timesheet Equip Id: {0}", tempId));
                }

                // ***********************************************
                // find the equipment record
                // ***********************************************
                Equipment equipment = dbContext.Equipments.AsNoTracking().FirstOrDefault(x => x.Id == mapEquip.NewKey);

                if (equipment == null)
                {
                    throw new ArgumentException(string.Format("Cannot locate Equipment record (Timesheet Equip Id: {0}", tempId));
                }

                // ************************************************
                // get the imported project record map
                // ************************************************
                string tempProjectId = oldObject.Project_Id.ToString();

                ImportMap mapProject = dbContext.ImportMaps.AsNoTracking()
                                       .FirstOrDefault(x => x.OldKey == tempProjectId &&
                                                       x.OldTable == ImportProject.OldTable &&
                                                       x.NewTable == ImportProject.NewTable);

                // ***********************************************
                // find the project record
                // (or create a project (inactive))
                // ***********************************************
                Models.Project project;

                if (mapProject != null)
                {
                    project = dbContext.Projects.AsNoTracking().FirstOrDefault(x => x.Id == mapProject.NewKey);

                    if (project == null)
                    {
                        throw new ArgumentException(string.Format("Cannot locate Project record (Timesheet Equip Id: {0}", tempId));
                    }
                }
                else
                {
                    // create new project
                    project = new Models.Project
                    {
                        Information            = "Created to support Time Record import from BCBid",
                        Status                 = "Complete",
                        Name                   = "Legacy BCBid Project",
                        AppCreateUserid        = systemId,
                        AppCreateTimestamp     = DateTime.UtcNow,
                        AppLastUpdateUserid    = systemId,
                        AppLastUpdateTimestamp = DateTime.UtcNow
                    };

                    dbContext.Projects.Add(project);

                    // save now so we can access it for other time records
                    dbContext.SaveChanges();

                    // add mapping record
                    ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.Id, ImportProject.NewTable);
                    dbContext.SaveChanges();
                }

                // ***********************************************
                // find or create the rental agreement
                // ***********************************************
                DateTime?enteredDate = ImportUtility.CleanDateTime(oldObject.Entered_Dt);  // use for the agreement

                RentalAgreement agreement = dbContext.RentalAgreements.AsNoTracking()
                                            .FirstOrDefault(x => x.EquipmentId == equipment.Id &&
                                                            x.ProjectId == project.Id);

                if (agreement == null)
                {
                    // create a new agreement record
                    agreement = new RentalAgreement
                    {
                        EquipmentId            = equipment.Id,
                        ProjectId              = project.Id,
                        Note                   = "Created to support Time Record import from BCBid",
                        Number                 = "Legacy BCBid Agreement",
                        DatedOn                = enteredDate,
                        AppCreateUserid        = systemId,
                        AppCreateTimestamp     = DateTime.UtcNow,
                        AppLastUpdateUserid    = systemId,
                        AppLastUpdateTimestamp = DateTime.UtcNow
                    };

                    if (project.RentalAgreements == null)
                    {
                        project.RentalAgreements = new List <RentalAgreement>();
                    }

                    project.RentalAgreements.Add(agreement);

                    // save now so we can access it for other time records
                    dbContext.SaveChangesForImport();
                }

                // ***********************************************
                // create time record
                // ***********************************************
                timeRecord = new TimeRecord {
                    Id = ++maxTimesheetIndex
                };

                // ***********************************************
                // set time record attributes
                // ***********************************************
                DateTime?workedDate = ImportUtility.CleanDateTime(oldObject.Worked_Dt);

                if (workedDate != null)
                {
                    timeRecord.WorkedDate = (DateTime)workedDate;
                }
                else
                {
                    throw new DataException(string.Format("Worked Date cannot be null (TimesheetIndex: {0}", maxTimesheetIndex));
                }

                // get hours worked
                float?tempHoursWorked = ImportUtility.GetFloatValue(oldObject.Hours);

                if (tempHoursWorked != null)
                {
                    timeRecord.Hours = tempHoursWorked;
                }
                else
                {
                    throw new DataException(string.Format("Hours cannot be null (TimesheetIndex: {0}", maxTimesheetIndex));
                }

                if (enteredDate != null)
                {
                    timeRecord.EnteredDate = (DateTime)enteredDate;
                }
                else
                {
                    throw new DataException(string.Format("Entered Date cannot be null (TimesheetIndex: {0}", maxTimesheetIndex));
                }

                // ***********************************************
                // create time record
                // ***********************************************
                timeRecord.AppCreateUserid        = systemId;
                timeRecord.AppCreateTimestamp     = DateTime.UtcNow;
                timeRecord.AppLastUpdateUserid    = systemId;
                timeRecord.AppLastUpdateTimestamp = DateTime.UtcNow;

                if (agreement.TimeRecords == null)
                {
                    agreement.TimeRecords = new List <TimeRecord>();
                }

                agreement.TimeRecords.Add(timeRecord);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("***Error*** - Worked Date: " + oldObject.Worked_Dt);
                Debug.WriteLine("***Error*** - Master TimeRecord Index: " + maxTimesheetIndex);
                Debug.WriteLine(ex.Message);
                throw;
            }
        }