Ejemplo n.º 1
0
        private void PopulateForm()
        {
            if (Request.QueryString["Driver"] != "")
            {
                m_driver       = Request.QueryString["Driver"];
                chkDriver.Text = m_driver;
            }
            else
            {
                chkDriver.Enabled = false;
            }

            if (Request.QueryString["RegNo"] != "")
            {
                m_vehicle       = Request.QueryString["RegNo"];
                chkVehicle.Text = m_vehicle;
            }
            else
            {
                chkVehicle.Enabled = false;
            }

            if (!string.IsNullOrEmpty(Request.QueryString["IsUpdate"]))
            {
                IsUpdate = bool.Parse(Request.QueryString["IsUpdate"]);
            }

            Entities.Instruction previousInstruction = null;
            using (Facade.IInstruction facInstruction = new Facade.Instruction())
            {
                Entities.Instruction instruction = facInstruction.GetInstruction(m_instructionId);
                Entities.Job         job         = null;

                // Try and get the job from the cache
                job = (Entities.Job)Cache.Get("JobEntityForJobId" + instruction.JobId.ToString());

                DateTime trunkStart = DateTime.Now;
                DateTime trunkEnd   = DateTime.Now;

                if (IsUpdate)
                {
                    lblTrunk.Text = "Update Trunk Leg";
                    btnTrunk.Text = "Update";

                    #region Update Trunk

                    this.ucPoint.SelectedPoint = instruction.Point;

                    rdiStartDate.SelectedDate = instruction.PlannedArrivalDateTime;
                    rdiStartTime.SelectedDate = instruction.PlannedArrivalDateTime;

                    rdiEndDate.SelectedDate   = instruction.PlannedDepartureDateTime;
                    rdiStartTime.SelectedDate = instruction.PlannedDepartureDateTime;

                    trResources.Visible  = false;
                    rdiStartDate.Enabled = rdiStartTime.Enabled = false;
                    rdiEndDate.Enabled   = rdiEndTime.Enabled = false;
                    #endregion
                }
                else
                {
                    #region New Trunk
                    if (job == null)
                    {
                        // Job was not in the cache thus get the instruction collection from the db
                        Entities.InstructionCollection instructions = new Facade.Instruction().GetForJobId(instruction.JobId, true);

                        // Get the previous instruction
                        previousInstruction = instructions.GetPreviousInstruction(instruction.InstructionID);
                    }
                    else
                    {
                        // Job is in the cache, check for instructions

                        if (job.Instructions != null)
                        {
                            // We have instructions

                            // use the instruction collection from the cached job
                            previousInstruction = job.Instructions.GetPreviousInstruction(instruction.InstructionID);
                        }
                        else
                        {
                            // otherwise get a fresh instruction collection
                            Entities.InstructionCollection instructions = new Facade.Instruction().GetForJobId(instruction.JobId, true);

                            // Get the previous instruction
                            previousInstruction = instructions.GetPreviousInstruction(instruction.InstructionID);
                        }
                    }

                    if (previousInstruction == null)
                    {
                        previousInstruction = instruction;
                    }

                    previousInstruction.PlannedDepartureDateTime = previousInstruction.PlannedDepartureDateTime.Subtract(new TimeSpan(0, 0, 0, previousInstruction.PlannedDepartureDateTime.Second, previousInstruction.PlannedDepartureDateTime.Millisecond));
                    instruction.PlannedArrivalDateTime           = instruction.PlannedArrivalDateTime.Subtract(new TimeSpan(0, 0, 0, instruction.PlannedArrivalDateTime.Second, instruction.PlannedDepartureDateTime.Millisecond));

                    if (previousInstruction == instruction)
                    {
                        trunkStart = instruction.PlannedArrivalDateTime.AddHours(2);
                        trunkEnd   = instruction.PlannedDepartureDateTime.Subtract(new TimeSpan(0, 2, 0, 0, 0));
                    }
                    else
                    {
                        trunkStart = previousInstruction.PlannedDepartureDateTime.AddHours(2);
                        trunkEnd   = instruction.PlannedArrivalDateTime.Subtract(new TimeSpan(0, 2, 0, 0, 0));
                    }

                    trunkStart = trunkStart.AddSeconds(-trunkStart.Second);
                    trunkEnd   = trunkEnd.AddSeconds(-trunkEnd.Second);

                    if (trunkStart >= trunkEnd)
                    {
                        if (previousInstruction == instruction)
                        {
                            trunkStart = instruction.PlannedArrivalDateTime.AddMinutes(15);
                            trunkEnd   = instruction.PlannedDepartureDateTime.AddMinutes(-15);
                        }
                        else
                        {
                            trunkStart = previousInstruction.PlannedDepartureDateTime.AddMinutes(15);
                            trunkEnd   = instruction.PlannedArrivalDateTime.AddMinutes(-15);
                        }

                        if (trunkStart >= trunkEnd)
                        {
                            if (previousInstruction == instruction)
                            {
                                trunkStart = instruction.PlannedArrivalDateTime.AddMinutes(1);
                                trunkEnd   = instruction.PlannedDepartureDateTime.AddMinutes(-1);
                            }
                            else
                            {
                                trunkStart = previousInstruction.PlannedDepartureDateTime.AddMinutes(1);
                                trunkEnd   = instruction.PlannedArrivalDateTime.AddMinutes(-1);
                            }
                        }
                    }

                    if (trunkStart == trunkEnd)
                    {
                        if (previousInstruction == instruction)
                        {
                            trunkStart = instruction.PlannedArrivalDateTime.AddMinutes(60);
                            trunkEnd   = instruction.PlannedDepartureDateTime.AddMinutes(-60);
                        }
                        else
                        {
                            trunkStart = previousInstruction.PlannedDepartureDateTime.AddMinutes(60);
                            trunkEnd   = instruction.PlannedArrivalDateTime.AddMinutes(-60);
                        }
                    }
                    #endregion
                }

                rdiStartDate.SelectedDate = trunkStart;
                rdiStartTime.SelectedDate = trunkStart;
                rdiEndDate.SelectedDate   = trunkEnd;
                rdiEndTime.SelectedDate   = trunkEnd;
            }
        }