Пример #1
0
        private void UpdateSegment()
        {
            if (curSegment == null)
            {
                throw new Exception(SiteUtils.ExceptionMessageForCustomer("Current segment is null."));
            }

            DateTime startTime = DateTime.Parse(this.txtTimeStarted.Text);

            startTime = AdminUtils.UtcOfEmployeeDate(startTime, curEmployee);

            DateTime stopTime = DateTime.Parse(this.txtTimeStopped.Text);

            stopTime = AdminUtils.UtcOfEmployeeDate(stopTime, curEmployee);

            int minutesWorked = Convert.ToInt32(Convert.ToDecimal(this.txtHoursWorked.Text) * 60m);
            //(int) Math.Round(nowUtc.Subtract(curSegment.StartDate).TotalMinutes);

            // make new segment, so we can test for null -- necessary ??
            JobSegment newSegment = curSegment.UpdateJobSegment(startTime, stopTime, minutesWorked, this.txtSegmentNotesForStop.Text.Trim());

            if (newSegment == null)
            {
                throw new Exception(SiteUtils.ExceptionMessageForCustomer("Failed to update job segment."));
            }

            // assign successful result
            curSegment = newSegment;

            // update job status
            JobStatus newStatus = JobStatus.JobStatusFromId(Convert.ToInt32(this.ddStatuses.SelectedValue));

            bool statusChanged = !newStatus.Equals(curJob.JobStatus);

            if (statusChanged)
            {
                JobStatusChange change = JobStatusChange.InsertJobStatusChange(curJob, newStatus, DateTime.UtcNow);
                if (change == null)
                {
                    throw new Exception(SiteUtils.ExceptionMessageForCustomer("Failed to add job status change."));
                }

                if (newStatus.IsAClosedStatus())
                {
                    // update the job to insert completion date
                    Job newJob = curJob.ShallowCopy();

                    DateTime dateCompleted = Convert.ToDateTime(this.txtTimeStopped.Text);
                    dateCompleted = AdminUtils.UtcOfEmployeeDate(dateCompleted, curEmployee);

                    newJob.DateCompleted = dateCompleted;

                    bool ret = ClientData.Current.UpdateJob(newJob.BillingReference, newJob.JobType.JobTypeId, newJob.ToApplication, newJob.Formatted, newJob.Proofread, newJob.DateDue, newJob.DateCompleted, newJob.Instructions, newJob.Estimate, newJob.FinalCharge, newJob.Taxes, newJob.DeliveryNotes, newJob.IsArchived, newJob.JobId);

                    if (!ret)
                    {
                        throw new Exception(SiteUtils.ExceptionMessageForCustomer("Failed to update job in database."));
                    }

                    // update cached job
                    List <Job> jobsInCache = CacheLayer.RecentJobs();
                    int        index       = jobsInCache.IndexOf(curJob);
                    if (index != -1)
                    {
                        jobsInCache.Remove(curJob);
                        jobsInCache.Insert(index, newJob);
                    }

                    // replace global ref
                    curJob = newJob;
                }
            }
        }