Ejemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            int instructionId = Convert.ToInt32(Request.QueryString["iID"]);

            if (!Page.IsPostBack)
            {
                using (Facade.Instruction facInstruction = new Facade.Instruction())
                {
                    Entities.Instruction affectedInstruction = null;
                    Entities.Job         job = null;
                    int jobId = 0;
                    Entities.InstructionCollection instructions = null;

                    if (int.TryParse(Request.QueryString["jobId"], out jobId))
                    {
                        if (instructionId != 0)
                        {
                            affectedInstruction = facInstruction.GetInstruction(instructionId);

                            // Get the job.
                            job = (Entities.Job)Cache.Get("JobEntityForJobId" + jobId.ToString());

                            // Try and get the job from the cache
                            if (job == null)
                            {
                                Facade.IJob facJob = new Facade.Job();
                                job = facJob.GetJob(jobId);
                            }
                        }
                        else
                        {
                            // We have no instruction id - this means that ascx is being used for adding
                            // multiple destinations. We should use the last instruction on the job.

                            // Get the job.
                            job = (Entities.Job)Cache.Get("JobEntityForJobId" + jobId.ToString());

                            // Try and get the job from the cache
                            if (job == null)
                            {
                                Facade.IJob facJob = new Facade.Job();
                                job = facJob.GetJob(jobId);

                                // Job was not in the cache thus get the instruction collection from the db
                                instructions = new Facade.Instruction().GetForJobId(jobId);

                                // Get the end instruction
                                Entities.Instruction endInstruction = null;
                                endInstruction = instructions.Find(instruc => instruc.InstructionOrder == instructions.Count - 1);

                                if (endInstruction == null)
                                {
                                    throw new ApplicationException("Cannot find last instruction.");
                                }

                                affectedInstruction = endInstruction;
                            }
                            else
                            {
                                // Job is in the cache, check for instructions

                                if (job.Instructions != null)
                                {
                                    // We have instructions
                                    instructions = job.Instructions;
                                }
                                else
                                {
                                    // otherwise get a fresh instruction collection
                                    instructions = new Facade.Instruction().GetForJobId(jobId);
                                }

                                // Get the end instruction
                                Entities.Instruction endInstruction = null;
                                endInstruction = instructions.Find(instruc => instruc.InstructionOrder == instructions.Count - 1);

                                if (endInstruction == null)
                                {
                                    throw new ApplicationException("Cannot find last instruction.");
                                }

                                affectedInstruction = endInstruction;
                            }
                        }
                    }

                    if (job.JobState != eJobState.Invoiced && job.JobState != eJobState.Cancelled)
                    {
                        if (affectedInstruction.Driver != null)
                        {
                            chkDriver.Text = affectedInstruction.Driver.Individual.FullName;
                        }
                        else
                        {
                            chkDriver.Enabled = false;
                        }

                        if (affectedInstruction.Vehicle != null)
                        {
                            chkVehicle.Text = affectedInstruction.Vehicle.RegNo;
                        }
                        else
                        {
                            chkVehicle.Enabled = false;
                        }
                    }
                }
            }
        }