public void TestGenerateFirstAlertNumber()
 {
     //Arrange
     FirstAlertModel model = new FirstAlertModel(new FakeUnitOfWork());
     CS_FirstAlert firstAlert = new CS_FirstAlert();
     //Act
     model.GenerateFirstAlertNumber(firstAlert);
     //Assert
     Assert.AreEqual("0002", firstAlert.Number);
 }
 public void MustFillFirstAlertHeaderWithEntityObject()
 {
     //Arrange
     Mock<IFirstAlertView> mockView = new Mock<IFirstAlertView>();
     Mock<FirstAlertModel>  mockModel = new Mock<FirstAlertModel>();
     System.Data.Objects.DataClasses.EntityCollection<CS_FirstAlertDivision> divisions = new System.Data.Objects.DataClasses.EntityCollection<CS_FirstAlertDivision>();
     divisions.Add(new CS_FirstAlertDivision() { CS_Division = new CS_Division() {ID=1, Name="001"}});
     divisions.Add(new CS_FirstAlertDivision() { CS_Division = new CS_Division() {ID=1, Name="002"}});
     CS_FirstAlert firstAlertStub =  new CS_FirstAlert()
         {
             ID=1,
            CS_Job = new CS_Job() { ID=1, Number = "1234"},
             CS_Customer = new CS_Customer() {ID=2, Name = "Customer 1" },
             CS_Employee_InCharge = new CS_Contact() { ID = 2, Name = "Peter", LastName = "Parker", Active = true},
             CS_FirstAlertDivision = divisions,
             Date = new DateTime(2011, 7, 12, 5, 0, 0),
             CS_Country = new CS_Country() { ID = 1, Name = "USA" },
             CS_State = new CS_State() { ID = 1, Name = "Florida" },
             CS_City = new CS_City() { ID = 1, Name = "Miami" },
             ReportedBy = "danilo",
             CS_Employee_CompletedBy = new CS_Employee() { ID = 1, FirstName = "Danilo", Name = "Ruziska" },
             Details = "details",
             HasPoliceReport = true,
             PoliceAgency = "agency",
             PoliceReportNumber = "1234"
         } ;
     mockView.SetupProperty(e => e.FirstAlertID, 1);
     mockView.SetupProperty(e => e.FirstAlertEntity, null);
     mockModel.Setup(e => e.GetFirstAlertById(1)).Returns(firstAlertStub);
     FirstAlertPresenter presenter = new FirstAlertPresenter(mockView.Object, mockModel.Object);
     //Act
     presenter.FillFirstAlertHeaderFields();
     //Assert
     Assert.AreEqual(firstAlertStub.CS_Job.Number,  mockView.Object.FirstAlertEntity.CS_Job.Number);
     Assert.AreEqual(firstAlertStub.CS_Customer.ID, mockView.Object.FirstAlertEntity.CS_Customer.ID);
     Assert.AreEqual(firstAlertStub.CS_Employee_InCharge.ID,  mockView.Object.FirstAlertEntity.CS_Employee_InCharge.ID);
     Assert.AreEqual(firstAlertStub.CS_FirstAlertDivision.Count,  mockView.Object.FirstAlertEntity.CS_FirstAlertDivision.Count);
     Assert.AreEqual(firstAlertStub.Date, mockView.Object.FirstAlertEntity.Date);
     Assert.AreEqual(firstAlertStub.CS_Country.ID,  mockView.Object.FirstAlertEntity.CS_Country.ID);
     Assert.AreEqual(firstAlertStub.CS_City.ID, mockView.Object.FirstAlertEntity.CS_City.ID);
     Assert.AreEqual(firstAlertStub.CS_State.ID, mockView.Object.FirstAlertEntity.CS_State.ID);
     Assert.AreEqual(firstAlertStub.ReportedBy, mockView.Object.FirstAlertEntity.ReportedBy);
     Assert.AreEqual(firstAlertStub.CS_Employee_CompletedBy.ID, mockView.Object.FirstAlertEntity.CS_Employee_CompletedBy.ID);
     Assert.AreEqual(firstAlertStub.ReportedBy, mockView.Object.FirstAlertEntity.ReportedBy);
     Assert.AreEqual(firstAlertStub.Details, mockView.Object.FirstAlertEntity.Details);
     Assert.AreEqual(firstAlertStub.HasPoliceReport, mockView.Object.FirstAlertEntity.HasPoliceReport);
     Assert.AreEqual(firstAlertStub.PoliceAgency,  mockView.Object.FirstAlertEntity.PoliceAgency);
     Assert.AreEqual(firstAlertStub.PoliceReportNumber, mockView.Object.FirstAlertEntity.PoliceReportNumber);
 }
        /// <summary>
        /// Updates a list of CS_FirstAlertDivision
        /// </summary>
        /// <param name="_firstAlertDivisionList"></param>
        private void UpdateFirstAlertDivision(IList<CS_FirstAlertDivision> firstAlertDivisionList, IList<CS_FirstAlertDivision> oldDBList, CS_FirstAlert firstAlert)
        {
            try
            {
                //Finds Who needs to be saved
                List<CS_FirstAlertDivision> saveList = firstAlertDivisionList.Where(e => !oldDBList.Any(a => a.DivisionID == e.DivisionID)).ToList();
                //Finds Who needs to be deleted
                List<CS_FirstAlertDivision> toDeleteList = oldDBList.Where(e => !firstAlertDivisionList.Any(a => a.DivisionID == e.DivisionID)).ToList();
                List<CS_FirstAlertDivision> deleteList = new List<CS_FirstAlertDivision>();

                //Apply logical delete at deleteList (Active = false)
                for (int i = 0; i < toDeleteList.Count; i++)
                {
                    CS_FirstAlertDivision deleteItem = toDeleteList[i];

                    deleteList.Add(
                        new CS_FirstAlertDivision()
                        {
                            ID = deleteItem.ID,
                            DivisionID = deleteItem.DivisionID,
                            FirstAlertID = deleteItem.FirstAlertID,
                            CreationID = deleteItem.CreationID,
                            CreatedBy = deleteItem.CreatedBy,
                            CreationDate = deleteItem.CreationDate,
                            ModificationID = firstAlert.ModificationID,
                            ModifiedBy = firstAlert.ModifiedBy,
                            ModificationDate = firstAlert.ModificationDate,
                            Active = false
                        }
                    );
                }

                //Fill create and modified info
                for (int i = 0; i < saveList.Count; i++)
                {
                    CS_FirstAlertDivision division = saveList[i];
                    division.FirstAlertID = firstAlert.ID;
                    division.CreatedBy = firstAlert.ModifiedBy;
                    division.CreationDate = firstAlert.ModificationDate;
                    //division.CreationID;
                    division.ModifiedBy = firstAlert.ModifiedBy;
                    division.ModificationDate = firstAlert.ModificationDate;
                    //division.ModificationID;
                }

                //Saves new CS_FirstAlertDivision list
                SaveFirstAlertDivision(saveList);

                //Updates CS_FirstAlertDivision list
                if (deleteList != null && deleteList.Count > 0)
                    _firstAlertDivisionRepository.UpdateList(deleteList);
            }
            catch (Exception ex)
            {
                throw new Exception("There was an error updating the First Alert Division data. Please verify the content of the fields and try again.", ex);
            }
        }
        /// <summary>
        /// Updates CS_FirstAlert Entity and relations
        /// </summary>
        /// <param name="firstAlertEntity"></param>
        private void UpdateFirstAlert(CS_FirstAlert firstAlertEntity, IList<CS_FirstAlertPerson> personList, IList<CS_FirstAlertVehicle> vehicleList, IList<CS_FirstAlertDivision> divisionList, IList<CS_FirstAlertFirstAlertType> typeList, IList<CS_FirstAlertContactPersonal> contactPersonalList)
        {
            if (firstAlertEntity != null)
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    CS_FirstAlert oldfirstAlert = _firstAlertRepository.Get(e => e.ID == firstAlertEntity.ID, "CS_FirstAlertPerson", "CS_FirstAlertVehicle", "CS_FirstAlertDivision", "CS_FirstAlertFirstAlertType");
                    DateTime modificationDate = DateTime.Now;

                    try
                    {

                        firstAlertEntity.Number = oldfirstAlert.Number;
                        firstAlertEntity.CreatedBy = oldfirstAlert.CreatedBy;
                        //firstAlertEntity.CreationID = oldfirstAlert.CreationID;
                        firstAlertEntity.CreationDate = oldfirstAlert.CreationDate;
                        firstAlertEntity.ModifiedBy = this.UserName;
                        firstAlertEntity.ModificationDate = modificationDate;
                        //firstAlertEntity.ModificationID;

                        firstAlertEntity = _firstAlertRepository.Update(firstAlertEntity);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("There was an error updating the First Alert data. Please verify the content of the fields and try again.", ex);
                    }

                    if (firstAlertEntity != null)
                    {
                        vehicleList = UpdateFirstAlertVehicle(vehicleList, oldfirstAlert.CS_FirstAlertVehicle.Where(e => e.Active).ToList(), firstAlertEntity);
                        UpdateFirstAlertPerson(LinkVehiclesAndPersons(personList,vehicleList), oldfirstAlert.CS_FirstAlertPerson.Where(e => e.Active).ToList(), firstAlertEntity);
                        UpdateFirstAlertDivision(divisionList, oldfirstAlert.CS_FirstAlertDivision.Where(e => e.Active).ToList(), firstAlertEntity);
                        UpdateFirstAlertFirstAlertType(typeList, oldfirstAlert.CS_FirstAlertFirstAlertType.Where(e => e.Active).ToList(), firstAlertEntity);
                    }

                    try
                    {
                        if (null != contactPersonalList && contactPersonalList.Count > 0)
                            _firstAlertContactPersonalRepository.UpdateList(contactPersonalList);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("There was an error update the Contact Personal list.", ex);
                    }

                    scope.Complete();
                }
            }
        }
        private void SaveFirstAlertContactPersonal(CS_FirstAlert firstAlert, IList<EmailVO> callCriteriaList)
        {
            try
            {
                foreach (EmailVO item in callCriteriaList)
                {
                    CS_FirstAlertContactPersonal contactPersonal = new CS_FirstAlertContactPersonal();
                    contactPersonal.FirstAlertID = firstAlert.ID;
                    if (item.Type == (int)Globals.CallCriteria.EmailVOType.Employee)
                        contactPersonal.EmployeeID = item.PersonID;
                    else if (item.Type == (int)Globals.CallCriteria.EmailVOType.Contact)
                        contactPersonal.ContactID = item.PersonID;

                    contactPersonal.EmailAdviseDate = firstAlert.ModificationDate;
                    contactPersonal.EmailAdviseUser = firstAlert.ModifiedBy;

                    contactPersonal.CreatedBy = firstAlert.ModifiedBy;
                    contactPersonal.CreationDate = firstAlert.ModificationDate;
                    contactPersonal.ModifiedBy = firstAlert.ModifiedBy;
                    contactPersonal.ModificationDate = firstAlert.ModificationDate;
                    contactPersonal.Active = true;

                    _firstAlertContactPersonalRepository.Add(contactPersonal);
                }

            }
            catch (Exception ex)
            {
                throw new Exception("There was an error while trying to save Contact Personal information", ex);
            }
        }
        /// <summary>
        /// Create the Lapsed Preset Call Entry
        /// </summary>
        /// <param name="job"></param>
        private void SaveFirstAlertCallLog(CS_FirstAlert firstAlert, bool isGeneralLog)
        {
            try
            {
                CS_CallLog newCallEntry = new CS_CallLog();

                if (isGeneralLog)
                    newCallEntry.JobID = Globals.GeneralLog.ID;
                else
                    newCallEntry.JobID = firstAlert.JobID;
                newCallEntry.CallTypeID = (int)Globals.CallEntry.CallType.FirstAlert;
                if (isGeneralLog)
                    newCallEntry.PrimaryCallTypeId = (int)Globals.CallEntry.PrimaryCallType.NonJobUpdateNotification;
                else
                    newCallEntry.PrimaryCallTypeId = (int)Globals.CallEntry.PrimaryCallType.JobUpdateNotification;
                newCallEntry.CallDate = DateTime.Now;
                DateTimeOffset dateTimeOffset = DateTimeOffset.Now;
                newCallEntry.Xml = null;
                newCallEntry.Note = BuildFirstAlertNote(firstAlert);
                newCallEntry.CreatedBy = "System";
                newCallEntry.CreationDate = DateTime.Now;
                newCallEntry.ModifiedBy = "System";
                newCallEntry.ModificationDate = DateTime.Now;
                newCallEntry.Active = true;
                newCallEntry.UserCall = true;

                _callLogRepository.Add(newCallEntry);

                IList<EmailVO> callCriteriaList = SaveCallCriteriaFirstAlertResources(newCallEntry);

                if (!isGeneralLog)
                    SaveFirstAlertContactPersonal(firstAlert, callCriteriaList);
            }
            catch (Exception ex)
            {
                throw new Exception("There was an error while trying to save a new call entry of type First Alert.", ex);
            }
        }
        /// <summary>
        /// Saves CS_FirstAlert Entity and relations
        /// </summary>
        /// <param name="firstAlertEntity"></param>
        private void SaveFirstAlert(CS_FirstAlert firstAlertEntity, IList<CS_FirstAlertPerson> personList, IList<CS_FirstAlertVehicle> vehicleList, IList<CS_FirstAlertDivision> divisionList, IList<CS_FirstAlertFirstAlertType> typeList)
        {
            if (firstAlertEntity != null)
            {
                lock (locked)
                {
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted }))
                    {
                        DateTime creationDate = DateTime.Now;
                        try
                        {
                            firstAlertEntity.CreatedBy = this.UserName;
                            firstAlertEntity.CreationDate = creationDate;
                            //firstAlertEntity.CreationID;
                            firstAlertEntity.ModifiedBy = this.UserName;
                            firstAlertEntity.ModificationDate = creationDate;
                            //firstAlertEntity.ModificationID;
                            firstAlertEntity = GenerateFirstAlertNumber(firstAlertEntity);
                            firstAlertEntity = _firstAlertRepository.Add(firstAlertEntity);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("There was an error saving the Job data. Please verify the content of the fields and try again.", ex);
                        }

                        if (firstAlertEntity != null)
                        {
                            for (int i = 0; i < personList.Count; i++)
                            {
                                CS_FirstAlertPerson person = personList[i];
                                person.FirstAlertID = firstAlertEntity.ID;
                                person.CreatedBy = this.UserName;
                                person.CreationDate = creationDate;
                                //person.CreationID;
                                person.ModifiedBy = this.UserName;
                                person.ModificationDate = creationDate;
                                //person.ModificationID;
                            }

                            for (int i = 0; i < vehicleList.Count; i++)
                            {
                                CS_FirstAlertVehicle vehicle = vehicleList[i];
                                vehicle.FirstAlertID = firstAlertEntity.ID;
                                vehicle.CreatedBy = this.UserName;
                                vehicle.CreationDate = creationDate;
                                //vehicle.CreationID;
                                vehicle.ModifiedBy = this.UserName;
                                vehicle.ModificationDate = creationDate;
                                //vehicle.ModificationID;
                            }

                            for (int i = 0; i < divisionList.Count; i++)
                            {
                                CS_FirstAlertDivision division = divisionList[i];
                                division.FirstAlertID = firstAlertEntity.ID;
                                division.CreatedBy = this.UserName;
                                division.CreationDate = creationDate;
                                //division.CreationID;
                                division.ModifiedBy = this.UserName;
                                division.ModificationDate = creationDate;
                                //division.ModificationID;
                            }

                            for (int i = 0; i < typeList.Count; i++)
                            {
                                CS_FirstAlertFirstAlertType type = typeList[i];
                                type.FirstAlertID = firstAlertEntity.ID;
                                type.CreatedBy = this.UserName;
                                type.CreationDate = creationDate;
                                //type.CreationID;
                                type.ModifiedBy = this.UserName;
                                type.ModificationDate = creationDate;
                                //type.ModificationID;
                            }

                            vehicleList = SaveFirstAlertVehicle(vehicleList);
                            SaveFirstAlertPerson(LinkVehiclesAndPersons(personList,vehicleList));
                            SaveFirstAlertDivision(divisionList);
                            SaveFirstAlertFirstAlertType(typeList);
                            if (firstAlertEntity.JobID == Globals.GeneralLog.ID)
                                SaveFirstAlertCallLog(firstAlertEntity, true);
                            else
                            {
                                SaveFirstAlertCallLog(firstAlertEntity, false);
                                if (firstAlertEntity.CopyToGeneralLog)
                                    SaveFirstAlertCallLog(firstAlertEntity, true);
                            }
                            scope.Complete();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Method That defines if a First Alert is going to saved or updated
        /// </summary>
        /// <param name="firstAlertEntity"></param>
        public void SaveUpdateFirstAlert(CS_FirstAlert firstAlertEntity, IList<FirstAlertPersonVO> personList, IList<CS_FirstAlertVehicle> vehicleList, IList<CS_FirstAlertDivision> divisionList, IList<CS_FirstAlertFirstAlertType> typeList, IList<CS_FirstAlertContactPersonal> contactPersonalList)
        {
            List<CS_FirstAlertPerson> csPersonList = new List<CS_FirstAlertPerson>();

            for (int i = 0; i < personList.Count; i++)
            {
                csPersonList.Add(new CS_FirstAlertPerson());
                csPersonList[i].FirstAlertPersonVO = personList[i];
            }

            if (firstAlertEntity.ID.Equals(0))
                SaveFirstAlert(firstAlertEntity, csPersonList, vehicleList, divisionList, typeList);
            else
                UpdateFirstAlert(firstAlertEntity, csPersonList, vehicleList, divisionList, typeList, contactPersonalList);
        }
        /// <summary>
        /// Generates and set First Alert Number
        /// </summary>
        /// <param name="firstAlertEntity"></param>
        /// <returns></returns>
        public CS_FirstAlert GenerateFirstAlertNumber(CS_FirstAlert firstAlertEntity)
        {
            // Gets the latest First Alert Number generated
            int lastFirstAlertNumber = _settingsModel.GetLastFirstAlertNumber();

            // Increments value
            lastFirstAlertNumber++;

            // Updates Last First Alert Number
            _settingsModel.UpdateLastFirstAlertNumber(lastFirstAlertNumber);

            firstAlertEntity.Number = FormatFirstAlertNumber(lastFirstAlertNumber);

            return firstAlertEntity;
        }
        /// <summary>
        /// Build the Note for the Lapsed Preset based on Job
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public string BuildFirstAlertNote(CS_FirstAlert firstAlert)
        {
            StringBuilder firstAlertNote = new StringBuilder();

            string number = "";

            if (firstAlert.Number.ToString().Length > 4)
                number = firstAlert.Number.ToString().PadLeft(6, '0');
            else
                number = firstAlert.Number.ToString().PadLeft(4, '0');

            firstAlertNote.Append("First Alert ID#:<Text>" + number + "<BL>");
            firstAlertNote.Append("Job #:<Text>" + firstAlert.CS_Job.PrefixedNumber + "<BL>");
            firstAlertNote.Append("Creating User:<Text>" + firstAlert.CreatedBy + "<BL>");
            if (null != firstAlert.CS_Customer)
                firstAlertNote.Append("Company:<Text>" + firstAlert.CS_Customer.FullCustomerInformation + "<BL>");
            firstAlertNote.Append("Location:<Text>");
            if (null != firstAlert.CS_City)
                firstAlertNote.Append(firstAlert.CS_City.ExtendedName);
            if (null != firstAlert.CS_City && null != firstAlert.CS_State)
                firstAlertNote.Append(", ");
            if (null != firstAlert.CS_State)
                firstAlertNote.Append(firstAlert.CS_State.AcronymName);
            firstAlertNote.Append("<BL>");

            firstAlertNote.Append("First Alert Type:");
            if (firstAlert.CS_FirstAlertFirstAlertType.Count > 0)
            {
                foreach (CS_FirstAlertFirstAlertType firstAlertType in firstAlert.CS_FirstAlertFirstAlertType)
                    firstAlertNote.Append("<Text>" + firstAlertType.CS_FirstAlertType.Description + "<BL>");
            }
            else
                firstAlertNote.Append("<BL>");

            if (null != firstAlert.CS_FirstAlertPerson && firstAlert.CS_FirstAlertPerson.Count > 0)
            {
                firstAlertNote.Append("Employee Involved:");
                foreach (CS_FirstAlertPerson person in firstAlert.CS_FirstAlertPerson)
                {
                    if (person.IsHulcherEmployee)
                        firstAlertNote.Append("<Text>" + person.CS_Employee.DivisionAndFullName + "<BL>");
                }
            }

            firstAlertNote.Append("Reported By:<Text>" + firstAlert.ReportedBy + "<BL>");

            if (firstAlert.CompletedByEmployeeID.HasValue)
                firstAlertNote.Append("Completed By:<Text>" + firstAlert.CS_Employee_CompletedBy.DivisionAndFullName + "<BL>");

            if (!string.IsNullOrEmpty(firstAlert.Details))
                firstAlertNote.Append("Details:<Text>" + firstAlert.Details + "<BL>");

            return firstAlertNote.ToString();
        }
        /// <summary>
        /// Updates a list of CS_FirstAlertVehicle
        /// </summary>
        /// <param name="_firstAlertVehicleList"></param>
        private IList<CS_FirstAlertVehicle> UpdateFirstAlertVehicle(IList<CS_FirstAlertVehicle> firstAlertVehicleList, IList<CS_FirstAlertVehicle> oldDBList, CS_FirstAlert firstAlert)
        {
            try
            {
                //Finds Who needs to be saved
                List<CS_FirstAlertVehicle> saveList = firstAlertVehicleList.Where(e => e.ID == 0).ToList();
                //Finds Who needs to be updates
                List<CS_FirstAlertVehicle> updateList = firstAlertVehicleList.Where(e => oldDBList.Any(a => a.ID == e.ID)).ToList();
                //Finds Who needs to be deleted
                List<CS_FirstAlertVehicle> toDeleteList = oldDBList.Where(e => !firstAlertVehicleList.Any(a => a.ID == e.ID && a.ID != 0)).ToList();
                List<CS_FirstAlertVehicle> deleteList = new List<CS_FirstAlertVehicle>();

                //Apply logical delete at deleteList (Active = false)
                for (int i = 0; i < toDeleteList.Count; i++)
                {
                    CS_FirstAlertVehicle deleteItem = toDeleteList[i];

                    deleteList.Add
                        (
                            new CS_FirstAlertVehicle()
                            {
                                ID = deleteItem.ID,
                                FirstAlertID = deleteItem.FirstAlertID,
                                IsHulcherVehicle = deleteItem.IsHulcherVehicle,
                                EquipmentID = deleteItem.EquipmentID,
                                Make = deleteItem.Make,
                                Model = deleteItem.Model,
                                Year = deleteItem.Year,
                                Damage = deleteItem.Damage,
                                EstimatedCost = deleteItem.EstimatedCost,
                                CreationID = deleteItem.CreationID,
                                CreatedBy = deleteItem.CreatedBy,
                                CreationDate = deleteItem.CreationDate,
                                ModificationID = firstAlert.ModificationID,
                                ModifiedBy = firstAlert.ModifiedBy,
                                ModificationDate = firstAlert.ModificationDate,
                                Active = false

                            }
                        );
                }

                //Maintain cretedby and creationdate
                for (int i = 0; i < updateList.Count; i++)
                {
                    CS_FirstAlertVehicle newReg = updateList[i];
                    CS_FirstAlertVehicle oldReg = oldDBList.First(e => e.ID == updateList[i].ID);

                    newReg.CreatedBy = oldReg.CreatedBy;
                    newReg.CreationDate = oldReg.CreationDate;
                    newReg.ModificationID = firstAlert.ModificationID;
                    newReg.ModifiedBy = firstAlert.ModifiedBy;
                    newReg.ModificationDate = firstAlert.ModificationDate;
                }

                //Fill create and modified info
                for (int i = 0; i < saveList.Count; i++)
                {
                    CS_FirstAlertVehicle vehicle = saveList[i];
                    vehicle.FirstAlertID = firstAlert.ID;
                    vehicle.CreatedBy = firstAlert.ModifiedBy;
                    vehicle.CreationDate = firstAlert.ModificationDate;
                    //vehicle.CreationID;
                    vehicle.ModifiedBy = firstAlert.ModifiedBy;
                    vehicle.ModificationDate = firstAlert.ModificationDate;
                    //vehicle.ModificationID;
                }

                //Merges updateList and deleteList
                updateList.AddRange(deleteList);

                //Saves new CS_FirstAlertVehicle list
                saveList = SaveFirstAlertVehicle(saveList).ToList();

                //Updates CS_FirstAlertVehicle list
                if (updateList != null && updateList.Count > 0)
                    updateList = _firstAlertVehicleRepository.UpdateList(updateList).ToList();

                updateList.AddRange(saveList);

                return updateList.Where(e => e.Active).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception("There was an error updating the First Alert Vehicle data. Please verify the content of the fields and try again.", ex);
            }
        }
        /// <summary>
        /// Updates a list of CS_FirstAlertPerson
        /// </summary>
        /// <param name="firstAlertPersonList"></param>
        private void UpdateFirstAlertPerson(IList<CS_FirstAlertPerson> firstAlertPersonList, IList<CS_FirstAlertPerson> oldDBList, CS_FirstAlert firstAlert)
        {
            try
            {
                //Finds Who needs to be saved
                List<CS_FirstAlertPerson> saveList = firstAlertPersonList.Where(e => e.ID == 0).ToList();
                //Finds Who needs to be updates
                List<CS_FirstAlertPerson> updateList = firstAlertPersonList.Where(e => oldDBList.Any(a => a.ID == e.ID)).ToList();
                //Finds Who needs to be deleted
                List<CS_FirstAlertPerson> toDeleteList = oldDBList.Where(e => !firstAlertPersonList.Any(a => a.ID == e.ID && a.ID != 0)).ToList();
                List<CS_FirstAlertPerson> deleteList = new List<CS_FirstAlertPerson>();

                //Apply logical delete at deleteList
                for (int i = 0; i < toDeleteList.Count; i++)
                {
                    CS_FirstAlertPerson deleteItem = toDeleteList[i];

                    deleteList.Add
                        (
                            new CS_FirstAlertPerson()
                            {
                                ID = deleteItem.ID,
                                FirstAlertID = deleteItem.FirstAlertID,
                                IsHulcherEmployee = deleteItem.IsHulcherEmployee,
                                FirstAlertVehicleID = deleteItem.FirstAlertVehicleID,
                                VehiclePosition = deleteItem.VehiclePosition,
                                EmployeeID = deleteItem.EmployeeID,
                                LastName = deleteItem.LastName,
                                FirstName = deleteItem.FirstName,
                                CountryID = deleteItem.CountryID,
                                StateID = deleteItem.StateID,
                                CityID = deleteItem.CityID,
                                ZipcodeID = deleteItem.ZipcodeID,
                                Address = deleteItem.Address,
                                InjuryNature = deleteItem.InjuryNature,
                                InjuryBodyPart = deleteItem.InjuryBodyPart,
                                MedicalSeverity = deleteItem.MedicalSeverity,
                                Details = deleteItem.Details,
                                DoctorsName = deleteItem.DoctorsName,
                                DoctorsCountryID = deleteItem.DoctorsCountryID,
                                DoctorsStateID = deleteItem.DoctorsStateID,
                                DoctorsCityID = deleteItem.DoctorsCityID,
                                DoctorsZipcodeID = deleteItem.DoctorsZipcodeID,
                                DoctorsPhoneNumber = deleteItem.DoctorsPhoneNumber,
                                HospitalName = deleteItem.HospitalName,
                                HospitalCountryID = deleteItem.HospitalCountryID,
                                HospitalStateID = deleteItem.HospitalStateID,
                                HospitalCityID = deleteItem.HospitalCityID,
                                HospitalZipcodeID = deleteItem.HospitalZipcodeID,
                                HospitalPhoneNumber = deleteItem.HospitalPhoneNumber,
                                DriversLicenseNumber = deleteItem.DriversLicenseNumber,
                                DriversLicenseCountryID = deleteItem.DriversLicenseCountryID,
                                DriversLicenseStateID = deleteItem.DriversLicenseStateID,
                                DriversLicenseCityID = deleteItem.DriversLicenseCityID,
                                DriversLicenseZipcodeID = deleteItem.DriversLicenseZipcodeID,
                                DriversLicenseAddress = deleteItem.DriversLicenseAddress,
                                InsuranceCompany = deleteItem.InsuranceCompany,
                                PolicyNumber = deleteItem.PolicyNumber,
                                DrugScreenRequired = deleteItem.DrugScreenRequired,
                                CreationID = deleteItem.CreationID,
                                CreatedBy = deleteItem.CreatedBy,
                                CreationDate = deleteItem.CreationDate,
                                ModificationID = firstAlert.ModificationID,
                                ModifiedBy = firstAlert.ModifiedBy,
                                ModificationDate = firstAlert.ModificationDate,
                                Active = false

                            }
                        );
                }

                //Maintain cretedby and creationdate
                for (int i = 0; i < updateList.Count; i++)
                {
                    CS_FirstAlertPerson newReg = updateList[i];
                    CS_FirstAlertPerson oldReg = oldDBList.First(e => e.ID == updateList[i].ID);

                    newReg.CreatedBy = oldReg.CreatedBy;
                    newReg.CreationDate = oldReg.CreationDate;
                    newReg.ModificationID = firstAlert.ModificationID;
                    newReg.ModifiedBy = firstAlert.ModifiedBy;
                    newReg.ModificationDate = firstAlert.ModificationDate;
                }

                //Fill create and modified info
                for (int i = 0; i < saveList.Count; i++)
                {
                    CS_FirstAlertPerson person = saveList[i];
                    person.FirstAlertID = firstAlert.ID;
                    person.CreatedBy = firstAlert.ModifiedBy;
                    person.CreationDate = firstAlert.ModificationDate;
                    //person.CreationID;
                    person.ModifiedBy = firstAlert.ModifiedBy;
                    person.ModificationDate = firstAlert.ModificationDate;
                    //person.ModificationID;
                }

                //Merges updateList and deleteList
                updateList.AddRange(deleteList);

                //Saves new CS_FirstAlertPerson list
                SaveFirstAlertPerson(saveList);

                //Updates CS_FirstAlertPerson list
                if (updateList != null && updateList.Count > 0)
                    _firstAlertPersonRepository.UpdateList(updateList);
            }
            catch (Exception ex)
            {
                throw new Exception("There was an error updating the First Alert Person data. Please verify the content of the fields and try again.", ex);
            }
        }
        public void SetCallLogViewCallEntryRowData()
        {
            //Arrange
            FakeObjectSet<CS_FirstAlert> fakeFirstAlert = new FakeObjectSet<CS_FirstAlert>();

            CS_FirstAlertType csFirstAlertType = new CS_FirstAlertType()
            {
                Active = true,
                Description = "injury",
                CreatedBy = "dcecilia",
                CreationDate = new DateTime(10, 10, 10, 5, 0, 1),
                ModifiedBy = "dcecilia",
                ModificationDate = new DateTime(10, 10, 10, 5, 0, 1),

            };

            CS_FirstAlertFirstAlertType csFirstAlertFirstAlertType = new CS_FirstAlertFirstAlertType()
            {
                Active = true,
                FirstAlertID = 1,
                FirstAlertTypeID = 1,
                CreatedBy = "dcecilia",
                CreationDate =
                    new DateTime(10, 10, 10, 5, 0, 1),
                ModifiedBy = "dcecilia",
                ModificationDate =
                    new DateTime(10, 10, 10, 5, 0, 1),
                CS_FirstAlertType = csFirstAlertType
            };

            EntityCollection<CS_FirstAlertFirstAlertType> entityCollectionFirstAlertFirstAlertType = new EntityCollection<CS_FirstAlertFirstAlertType>();
            entityCollectionFirstAlertFirstAlertType.Add(csFirstAlertFirstAlertType);

            DateTime currentDate = DateTime.Now;

            CS_Customer csCustomer = new CS_Customer()
                                         {
                                             ID = 1,
                                             Active = true,
                                             Name = "Abcd",
                                             Country = "USA",
                                             CustomerNumber = "1000"
                                         };

            CS_Job csJob = new CS_Job()
                               {
                                   ID = 1,
                                   Active = true,
                                   CreatedBy = "dcecilia",
                                   CreationDate = currentDate,
                                   ModifiedBy = "dcecilia",
                                   ModificationDate = currentDate,
                                   Number = "123"
                               };

            CS_FirstAlert csFirstAlert = new CS_FirstAlert()
                                             {
                                                 ID = 1,
                                                 Active = true,
                                                 Number = "123",
                                                 JobID = 1,
                                                 CS_Job = csJob,
                                                 CustomerID = 1,
                                                 CS_Customer = csCustomer,

                                                 Details = "aaAaA",
                                                 Date = currentDate,
                                                 HasPoliceReport = true,
                                                 CreatedBy = "dcecilia",
                                                 CreationDate = currentDate,
                                                 ModifiedBy = "dcecilia",
                                                 ModificationDate = new DateTime(2011, 7, 12, 5, 0, 0),
                                                 CS_FirstAlertFirstAlertType = entityCollectionFirstAlertFirstAlertType,
                                             };

            CS_Division csDivision = new CS_Division()
            {
                Active = true,
                ID = 1,
                Name = "001"
            };

            CS_FirstAlertDivision csFirstAlertDivision = new CS_FirstAlertDivision()
            {
                Active = true,
                ID = 1,
                FirstAlertID = 1,
                DivisionID = 1,
                CS_Division = csDivision,
                CS_FirstAlert = csFirstAlert
            };

            Mock<IFirstAlertView> mock = new Mock<IFirstAlertView>();
            mock.SetupProperty(c => c.FirstAlertRowDataItem, csFirstAlert);
            mock.SetupProperty(c => c.FirstAlertRowAlertDateAndTime, "");
            mock.SetupProperty(c => c.FirstAlertRowAlertId, "");
            mock.SetupProperty(c => c.FirstAlertRowAlertNumber, "");
            mock.SetupProperty(c => c.FirstAlertRowCustomer, "");
            mock.SetupProperty(c => c.FirstAlertRowDivision, "");
            mock.SetupProperty(c => c.FirstAlertRowFirstAlertType, "");
            mock.SetupProperty(c => c.FirstAlertRowJobNumber, "");

               //Act
            FirstAlertViewModel viewModel = new FirstAlertViewModel(mock.Object);

            viewModel.SetDetailedFirstAlertRowData();

            // Assert
            Assert.AreEqual(currentDate.ToString("MM/dd/yyyy") + " " + currentDate.ToShortTimeString(), mock.Object.FirstAlertRowAlertDateAndTime, "Failed in FirstAlertRowAlertDateAndTime");
            Assert.AreEqual("1", mock.Object.FirstAlertRowAlertId, "Failed in FirstAlertRowAlertId");
            Assert.AreEqual("123", mock.Object.FirstAlertRowAlertNumber, "Failed in FirstAlertRowAlertNumber");
            Assert.AreEqual("Abcd - USA - 1000", mock.Object.FirstAlertRowCustomer, "Failed in FirstAlertRowCustomer");
            Assert.AreEqual("001", mock.Object.FirstAlertRowDivision, "Failed in FirstAlertRowDivision");
            Assert.AreEqual("injury", mock.Object.FirstAlertRowFirstAlertType, "Failed in FirstAlertRowFirstAlertType");
            Assert.AreEqual("123", mock.Object.FirstAlertRowJobNumber, "Failed in FirstAlertRowJobNumber");
        }