/// <summary>
        /// Update the job record when user clicks on save and close
        /// </summary>
        public void UpdateJobData()
        {
            try
            {
                if ((_view.JobInfo.InitialCallDate.Date + _view.JobInfo.InitialCallTime) > DateTime.Now)
                {
                    _view.DisplayMessage("Job Info: Initial Call Date can not be greater than today.", false);
                    return;
                }

                if (_view.JobId.HasValue)
                {
                    int jobId = _view.JobId.Value;
                    string username = _view.Username;

                    CS_Job job = _view.Job;
                    CS_CustomerInfo customerInfo = _view.CustomerInfo;
                    IList<CS_JobDivision> divisionList = _view.DivisionList;
                    CS_PresetInfo presetInfo = _view.PresetInfo;
                    CS_LostJobInfo lostJobInfo = _view.LostJobInfo;
                    CS_SpecialPricing specialPricing = _view.SpecialPricing;
                    CS_JobInfo jobInfo = _view.JobInfo;
                    CS_LocationInfo locationInfo = _view.LocationInfo;
                    CS_JobDescription jobDescription = _view.JobDescription;
                    IList<CS_ScopeOfWork> scopeOfWorkList = _view.ScopeOfWorkList;
                    IList<CS_JobPermit> permitInfoList = _view.PermitInfoList;
                    IList<CS_JobPhotoReport> photoReportList = _view.PhotoReportList;
                    IList<LocalEquipmentTypeVO> requestedEquipmentList = _view.RequestedEquipmentTypeList;

                    job.ID = jobId;
                    job.ModifiedBy = username;
                    job.ModificationDate = DateTime.Now;

                    customerInfo.JobId = jobId;
                    customerInfo.ModifiedBy = username;
                    customerInfo.ModificationDate = DateTime.Now;

                    foreach (CS_JobDivision jobDivision in divisionList)
                    {
                        jobDivision.JobID = jobId;
                        jobDivision.ModifiedBy = username;
                        jobDivision.ModificationDate = DateTime.Now;
                        if (null != jobDivision.CS_Division)
                        {
                            jobDivision.DivisionID = jobDivision.CS_Division.ID;
                            jobDivision.CS_Division = null;
                        }
                    }

                    if (null != presetInfo)
                    {
                        presetInfo.JobId = jobId;
                        presetInfo.ModifiedBy = username;
                        presetInfo.ModificationDate = DateTime.Now;
                    }

                    if (null != lostJobInfo)
                    {
                        lostJobInfo.JobId = jobId;
                        lostJobInfo.ModifiedBy = username;
                        lostJobInfo.ModificationDate = DateTime.Now;
                    }

                    if (null != specialPricing)
                    {
                        specialPricing.JobId = jobId;
                        specialPricing.ModifiedBy = username;
                        specialPricing.ModificationDate = DateTime.Now;
                    }

                    jobInfo.JobID = jobId;
                    jobInfo.ModifiedBy = username;
                    jobInfo.ModificationDate = DateTime.Now;

                    locationInfo.JobID = jobId;
                    locationInfo.ModifiedBy = username;
                    locationInfo.ModificationDate = DateTime.Now;

                    jobDescription.JobId = jobId;
                    jobDescription.ModifiedBy = username;
                    jobDescription.ModificationDate = DateTime.Now;

                    foreach (CS_ScopeOfWork scopeOfWork in scopeOfWorkList)
                    {
                        scopeOfWork.JobId = jobId;
                    }

                    foreach (CS_JobPermit jobPermit in permitInfoList)
                    {
                        jobPermit.JobID = jobId;
                    }

                    foreach (CS_JobPhotoReport photoReport in photoReportList)
                    {
                        photoReport.JobID = jobId;
                    }

                    CS_Job_JobStatus jobStatusHistory = new CS_Job_JobStatus()
                    {
                        Active = true,
                        CreationDate = DateTime.Now,
                        CreatedBy = _view.Username,
                        ModificationDate = DateTime.Now,
                        ModifiedBy = _view.Username,
                        JobID = _view.JobId.Value,
                        JobStatusId = _view.JobStatusId,
                        JobStartDate = _view.JobStartDate,
                        JobCloseDate = _view.JobCloseDate
                    };

                    if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Active)
                        job.BillingStatus = (int)Globals.JobRecord.BillingStatus.Working;
                    else if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Closed || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Cancelled || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Lost)
                        job.BillingStatus = (int)Globals.JobRecord.BillingStatus.Done;
                    else if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Potential || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Preset || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.PresetPurchase)
                        job.BillingStatus = (int)Globals.JobRecord.BillingStatus.Created;

                    this._model = new JobModel()
                    {
                        NewJob = job,
                        NewCustomer = customerInfo,
                        NewJobDivision = divisionList,
                        NewPresetInfo = presetInfo,
                        NewLostJobInfo = lostJobInfo,
                        NewSpecialPricing = specialPricing,
                        NewJobInfo = jobInfo,
                        NewLocationInfo = locationInfo,
                        NewJobDescription = jobDescription,
                        NewScopeOfWork = scopeOfWorkList,
                        NewPermit = permitInfoList,
                        NewPhotoReport = photoReportList,
                        JobStatusID = _view.JobStatusId,
                        NewJobStatusHistory = jobStatusHistory,
                        NewRequestedEquipment = requestedEquipmentList
                    };

                    IDictionary<string, object> output = _model.UpdateJobData(_view.CreateInitialAdvise, false);

                    if (output.ContainsKey("ResourceConversion"))
                        _view.ResourceConversion = bool.Parse(output["ResourceConversion"].ToString());

                    if (output.ContainsKey("HasResources"))
                    {
                        bool hasResources = bool.Parse(output["HasResources"].ToString());

                        if (hasResources)
                            _view.DisplayMessage(output["Message"].ToString(), _view.SaveAndClose);

                        _view.HasResources = hasResources;
                    }

                    if (output.ContainsKey("HasBillToContact"))
                    {
                        bool hasBillToContact = bool.Parse(output["HasBillToContact"].ToString());

                        if (!hasBillToContact)
                            _view.DisplayMessage(output["Message"].ToString(), false);
                    }

                    if (output.ContainsKey("HasAssignedResources"))
                    {
                        bool hasAssignedResources = bool.Parse(output["HasAssignedResources"].ToString());

                        if (hasAssignedResources)
                            _view.DisplayMessage(output["Message"].ToString(), _view.SaveAndClose);

                        _view.HasResources = hasAssignedResources;
                    }

                    if (output.ContainsKey("OperationNotAllowed"))
                    {
                        bool operationNotAllowed = bool.Parse(output["OperationNotAllowed"].ToString());

                        if (operationNotAllowed)
                            _view.DisplayMessage(output["Message"].ToString(), false);

                        return;
                    }

                    if (output.ContainsKey("HasTemporaryCustomer"))
                    {
                        bool hasTemporaryCustomer = bool.Parse(output["HasTemporaryCustomer"].ToString());

                        if (hasTemporaryCustomer)
                            _view.DisplayMessage(output["Message"].ToString(), _view.SaveAndClose);
                    }

                    if (_view.CreateInitialAdvise)
                    {
                        _view.SetCloseEnabled();
                        _view.CreateInitialAdvise = false;
                    }
                }

                bool saveInitialAdivise = _view.SaveInitialAdviseEmail;
                _view.CreateInitialAdvise = false;
                _view.JobId = _model.NewJob.ID;
                LoadJobEntity();
                UpdateTitle();
                _view.DisplayMessage("Job Record saved successfully", _view.SaveAndClose);
                //_view.OpenEmailInitialAdvise = saveInitialAdivise;
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format("There was an error while trying to save the Job Record!\n{0}\n{1}", ex.InnerException, ex.StackTrace));
                _view.DisplayMessage("There was an error while trying to save the Job Record.", false);
            }
        }
        public void SaveJobData()
        {
            if ((_view.InitialCallDate.Date + _view.InitialCallTime) > DateTime.Now)
            {
                _view.DisplayMessage("Initial Call Date can not be greater than today.", false);
                _view.SavedSuccessfuly = false;
                return;
            }

            // CS_Job
            CS_Job csJob = new CS_Job();

            csJob.CreatedBy = _view.Username;
            csJob.CreationDate = DateTime.Now;
            csJob.ModifiedBy = _view.Username;
            csJob.ModificationDate = DateTime.Now;
            csJob.Active = true;
            csJob.EmergencyResponse = _view.IsEmergencyResponse;

            _jobModel.NewJob = csJob;

            // Validations for Hulcher Contact and Division
            //int? calledInByContactId = null;
            //if (_view.PrimaryContactId != null && _view.HulcherContactId != null)
            //{
            //    //calledInByContactId = _view.PrimaryContactId;
            //    _view.PrimaryContactId = null;
            //}
            int? hulcherDivision = null;
            if (_view.HulcherContactId.HasValue)
                hulcherDivision = _view.PrimaryDivisionId;

            // CS_CustomerInfo
            CS_CustomerInfo customerInfo = new CS_CustomerInfo()
            {
                Active = true,
                InitialCustomerContactId = _view.PrimaryContactId,
                CustomerId = _view.CustomerId,
                PocEmployeeId = _view.HulcherContactId,
                DivisionId = hulcherDivision,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now
            };
            _jobModel.NewCustomer = customerInfo;

            // CS_JobDivision
            IList<CS_JobDivision> jobDivisionList = new List<CS_JobDivision>();
            jobDivisionList.Add(new CS_JobDivision()
            {
                Active = true,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                DivisionID = _view.PrimaryDivisionId,
                IsFromCustomerInfo = hulcherDivision.HasValue,
                ModificationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                PrimaryDivision = true
            });
            _jobModel.NewJobDivision = jobDivisionList;

            // CS_JobInfo
            CS_JobCategory jobCategory = _jobModel.GetJobCategoryByJobAction(_view.JobActionId);
            CS_JobType jobType = _jobModel.GetJobTypeByJobAction(_view.JobActionId);
            DateTime? startDate = null;
            DateTime? closedDate = null;
            if (_view.JobStatusId.Equals((int)Globals.JobRecord.JobStatus.Active))
                startDate = DateTime.Now;
            else if (_view.JobStatusId.Equals((int)Globals.JobRecord.JobStatus.Closed))
                closedDate = DateTime.Now;

            CS_JobInfo jobInfo = new CS_JobInfo()
            {
                Active = true,
                InitialCallDate = _view.InitialCallDate,
                InitialCallTime = _view.InitialCallTime,
                //JobStatusID = _view.JobStatusId,
                PriceTypeID = _view.PriceTypeId,
                JobActionID = _view.JobActionId,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now,
                JobCategoryID = jobCategory.ID,
                JobTypeID = jobType.ID,
            };
            _jobModel.NewJobInfo = jobInfo;

            CS_Job_JobStatus jobStatus = new CS_Job_JobStatus()
            {
                Active = true,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now,
                JobStatusId = _view.JobStatusId,
                JobStartDate = startDate,
                JobCloseDate = closedDate
            };

            _jobModel.NewJobStatusHistory = jobStatus;
            _jobModel.JobStatusID = _view.JobStatusId;

            if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Active)
                _jobModel.NewJob.BillingStatus = (int)Globals.JobRecord.BillingStatus.Working;
            else if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Closed || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Cancelled || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Lost)
                _jobModel.NewJob.BillingStatus = (int)Globals.JobRecord.BillingStatus.Done;
            else if (_view.JobStatusId == (int)Globals.JobRecord.JobStatus.Potential || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.Preset || _view.JobStatusId == (int)Globals.JobRecord.JobStatus.PresetPurchase)
                _jobModel.NewJob.BillingStatus = (int)Globals.JobRecord.BillingStatus.Created;

            // CS_LocationInfo
            int countryId = _locationModel.GetCountryByStateId(_view.StateId);
            CS_LocationInfo locationInfo = new CS_LocationInfo()
            {
                Active = true,
                CountryID = countryId,
                StateID = _view.StateId,
                CityID = _view.CityId,
                ZipCodeId = _view.ZipCode,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now
            };
            _jobModel.NewLocationInfo = locationInfo;

            // CS_ScopeOfWork
            IList<CS_ScopeOfWork> lstScopeOfWork = new List<CS_ScopeOfWork>();
            lstScopeOfWork.Add(new CS_ScopeOfWork()
            {
                Active = true,
                ScopeOfWork = _view.ScopeOfWork,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now
            });
            _jobModel.NewScopeOfWork = lstScopeOfWork;

            // CS_JobDescription
            CS_JobDescription jobDescription = new CS_JobDescription()
            {
                Active = true,
                CreatedBy = _view.Username,
                CreationDate = DateTime.Now,
                ModifiedBy = _view.Username,
                ModificationDate = DateTime.Now
            };
            _jobModel.NewJobDescription = jobDescription;
            _jobModel.SaveJobData(false, null, true);
            _view.JobId = _jobModel.NewJob.ID;

            _view.SavedSuccessfuly = true;
        }