private void PopulateEntities()
        {
            Entities.Job job = new Orchestrator.Entities.Job();
            job.Instructions           = new Orchestrator.Entities.InstructionCollection();
            job.JobType                = eJobType.Groupage;
            job.BusinessTypeID         = int.Parse(cboBusinessType.SelectedValue);
            job.IdentityId             = Globals.Configuration.IdentityId;
            job.LoadNumber             = "GRP" + Environment.TickCount.ToString().Substring(0, 3);
            job.Charge                 = new Orchestrator.Entities.JobCharge();
            job.Charge.JobChargeAmount = 0;
            job.Charge.JobChargeType   = eJobChargeType.FreeOfCharge;

            Entities.InstructionCollection collections = new Orchestrator.Entities.InstructionCollection();
            Entities.Instruction           iCollect    = null;
            Entities.CollectDrop           cd          = null;

            Entities.InstructionCollection drops = new Orchestrator.Entities.InstructionCollection();
            Entities.Instruction           iDrop = null;

            Facade.IPoint  facPoint = new Facade.Point();
            Facade.IOrder  facOrder = new Facade.Order();
            Entities.Point point    = null;

            bool newcollection = false;

            foreach (DataRow row in OrderData.Tables[0].Rows)
            {
                #region Collections
                newcollection = false;
                int      pointID             = (int)row["CollectFromPointID"];
                DateTime bookedDateTime      = (DateTime)row["CollectAtDateTime"];
                bool     collectionIsAnytime = (bool)row["CollectAtAnyTime"];

                // if this setting is true then we want to create a new instruction for the order.
                if (Globals.Configuration.OneDropAndLoadInstructionPerOrder)
                {
                    iCollect = null;
                }
                else
                {
                    iCollect = collections.GetForInstructionTypeAndPointID(eInstructionType.Load, pointID, bookedDateTime, collectionIsAnytime);
                }

                if (iCollect == null)
                {
                    iCollect = new Orchestrator.Entities.Instruction();
                    iCollect.InstructionTypeId = (int)eInstructionType.Load;
                    iCollect.BookedDateTime    = bookedDateTime;
                    if ((bool)row["CollectAtAnytime"])
                    {
                        iCollect.IsAnyTime = true;
                    }
                    point            = facPoint.GetPointForPointId(pointID);
                    iCollect.PointID = pointID;
                    iCollect.Point   = point;
                    iCollect.ClientsCustomerIdentityID = point.IdentityId; //Steve is this correct
                    iCollect.CollectDrops = new Orchestrator.Entities.CollectDropCollection();
                    newcollection         = true;
                }

                cd             = new Orchestrator.Entities.CollectDrop();
                cd.NoPallets   = (int)row["NoPallets"];
                cd.NoCases     = (int)row["Cases"];
                cd.GoodsTypeId = (int)row["GoodsTypeID"];
                cd.OrderID     = (int)row["OrderID"];
                cd.Weight      = (decimal)row["Weight"];
                cd.ClientsCustomerReference = row["DeliveryOrderNumber"].ToString();
                cd.Docket = row["OrderID"].ToString();

                iCollect.CollectDrops.Add(cd);
                if (newcollection)
                {
                    collections.Add(iCollect);
                }

                #endregion
            }

            // Add the Drops in the Order specified
            DataView dvDrops = OrderData.Tables[0].DefaultView;
            dvDrops.Sort = "DeliveryOrder ASC";
            DataTable dtDrops = dvDrops.ToTable();
            foreach (DataRow row in dtDrops.Rows)
            {
                #region Deliveries
                bool newdelivery = false;
                newdelivery = false;
                int      deliveryPointID   = (int)row["DeliveryPointID"];
                DateTime deliveryDateTime  = (DateTime)row["DeliveryDateTime"];
                bool     deliveryIsAnyTime = (bool)row["DeliveryIsAnyTime"];

                // if this setting is true then we want to create a new instruction for the order.
                if (Globals.Configuration.OneDropAndLoadInstructionPerOrder)
                {
                    iDrop = null;
                }
                else
                {
                    iDrop = drops.GetForInstructionTypeAndPointID(eInstructionType.Drop, deliveryPointID, deliveryDateTime, deliveryIsAnyTime);
                }

                if (iDrop == null)
                {
                    iDrop = new Orchestrator.Entities.Instruction();
                    iDrop.InstructionTypeId = (int)eInstructionType.Drop;
                    iDrop.BookedDateTime    = deliveryDateTime;
                    if ((bool)row["DeliveryIsAnytime"])
                    {
                        iDrop.IsAnyTime = true;
                    }
                    point = facPoint.GetPointForPointId(deliveryPointID);
                    iDrop.ClientsCustomerIdentityID = point.IdentityId;
                    iDrop.PointID = deliveryPointID;
                    iDrop.Point   = point;

                    iDrop.CollectDrops = new Orchestrator.Entities.CollectDropCollection();
                    newdelivery        = true;
                }

                cd             = new Orchestrator.Entities.CollectDrop();
                cd.NoPallets   = (int)row["NoPallets"];
                cd.NoCases     = (int)row["Cases"];
                cd.GoodsTypeId = (int)row["GoodsTypeID"];
                cd.OrderID     = (int)row["OrderID"];
                cd.Weight      = (decimal)row["Weight"];
                cd.ClientsCustomerReference = row["DeliveryOrderNumber"].ToString();
                cd.Docket = row["OrderID"].ToString();

                iDrop.CollectDrops.Add(cd);
                if (newdelivery)
                {
                    drops.Add(iDrop); //Stephen Newman 23/04/07 Changed to insert the drop to the front of the list as the sort processed later seems to swap objects if equal.
                }
                facOrder.UpdateForDeliveryRun(cd.OrderID, iCollect.PointID, iCollect.BookedDateTime, iCollect.IsAnyTime, Page.User.Identity.Name);

                #endregion
            }



            #region Add the Instructions to the job

            if (job.Instructions == null)
            {
                job.Instructions = new Entities.InstructionCollection();
            }

            foreach (Entities.Instruction instruction in collections)
            {
                job.Instructions.Add(instruction);
            }

            // removed by t.lunken 23/08/07 as this is now manually done.
            //drops.Sort(eInstructionSortType.DropDateTime);
            foreach (Entities.Instruction instruction in drops)
            {
                job.Instructions.Add(instruction);
            }
            #endregion

            Facade.IJob facjob = new Facade.Job();
            job.JobState = eJobState.Booked;
            int jobID = facjob.Create(job, Page.User.Identity.Name);
            _jobID   = jobID;
            _openJob = true;
        }
        private void PopulateEntities()
        {
            Entities.Job job = new Orchestrator.Entities.Job();
            job.Instructions           = new Orchestrator.Entities.InstructionCollection();
            job.JobType                = eJobType.Groupage;
            job.BusinessTypeID         = int.Parse(cboBusinessType.SelectedValue);
            job.IdentityId             = Globals.Configuration.IdentityId;
            job.LoadNumber             = "GRP" + Environment.TickCount.ToString().Substring(0, 3);
            job.Charge                 = new Orchestrator.Entities.JobCharge();
            job.Charge.JobChargeAmount = 0;
            job.Charge.JobChargeType   = eJobChargeType.FreeOfCharge;

            Entities.InstructionCollection collections = new Orchestrator.Entities.InstructionCollection();
            Entities.Instruction           iCollect    = null;
            Entities.CollectDrop           cd          = null;

            Entities.InstructionCollection drops = new Orchestrator.Entities.InstructionCollection();
            Entities.Instruction           iDrop = null;

            Facade.IPoint  facPoint = new Facade.Point();
            Facade.IOrder  facOrder = new Facade.Order();
            Entities.Point point    = null;

            int  collectSequence = 1;
            bool newcollection   = false;

            foreach (DataRow row in OrderData.Tables[0].Rows)
            {
                #region Collections
                newcollection = false;
                int      pointID             = (int)row["CollectionRunDeliveryPointID"];
                DateTime bookedDateTime      = (DateTime)row["CollectionRunDeliveryDateTime"];
                bool     collectionIsAnyTime = (bool)row["CollectionRunDeliveryIsAnyTime"];

                // if this setting is true then we want to create a new instruction for the order.
                if (Globals.Configuration.OneDropAndLoadInstructionPerOrder)
                {
                    iCollect = null;
                }
                else
                {
                    iCollect = collections.GetForInstructionTypeAndPointID(eInstructionType.Load, pointID, bookedDateTime, collectionIsAnyTime);
                }

                if (iCollect == null)
                {
                    iCollect = new Orchestrator.Entities.Instruction();
                    iCollect.InstructionTypeId = (int)eInstructionType.Load;
                    iCollect.BookedDateTime    = bookedDateTime;
                    if (collectionIsAnyTime)
                    {
                        iCollect.IsAnyTime = true;
                    }
                    point            = facPoint.GetPointForPointId(pointID);
                    iCollect.PointID = pointID;
                    iCollect.Point   = point;
                    iCollect.ClientsCustomerIdentityID = point.IdentityId; //Steve is this correct
                    iCollect.CollectDrops     = new Orchestrator.Entities.CollectDropCollection();
                    iCollect.InstructionOrder = collectSequence;
                    newcollection             = true;
                    collectSequence++;
                }

                cd             = new Orchestrator.Entities.CollectDrop();
                cd.NoPallets   = (int)row["NoPallets"];
                cd.NoCases     = (int)row["Cases"];
                cd.GoodsTypeId = (int)row["GoodsTypeID"];
                cd.OrderID     = (int)row["OrderID"];
                cd.Weight      = (decimal)row["Weight"];
                cd.ClientsCustomerReference = row["DeliveryOrderNumber"].ToString();
                cd.Docket = row["OrderID"].ToString();

                iCollect.CollectDrops.Add(cd);
                if (newcollection)
                {
                    collections.Add(iCollect);
                }

                #endregion

                #region Deliveries
                eOrderAction orderAction  = (eOrderAction)(int)row[C_OrderActionID];
                int          dropSequence = 1;
                bool         newdelivery  = false;
                newdelivery = false;
                int      deliveryPointID   = (int)row[C_DeliverToPointID];
                DateTime deliveryDateTime  = (DateTime)row[C_DeliverAtDateTime];
                bool     deliveryIsAnyTime = (bool)row[C_DeliverAtAnyTime];
                // If the user has selected Default (i.e. Deliver) a drop instruction will be created, otherwise a trunk instruction will be
                // created.
                eInstructionType instructionType = orderAction == eOrderAction.Default ? eInstructionType.Drop : eInstructionType.Trunk;

                // if this setting is true then we want to create a new instruction for the order.
                if (Globals.Configuration.OneDropAndLoadInstructionPerOrder)
                {
                    iDrop = null;
                }
                else
                {
                    iDrop = drops.GetForInstructionTypeAndPointID(instructionType, deliveryPointID, deliveryDateTime, deliveryIsAnyTime);
                }

                if (iDrop == null)
                {
                    iDrop = new Orchestrator.Entities.Instruction();
                    iDrop.InstructionTypeId = (int)instructionType;
                    iDrop.BookedDateTime    = deliveryDateTime;
                    if ((bool)row[C_DeliverAtAnyTime])
                    {
                        iDrop.IsAnyTime = true;
                    }
                    point = facPoint.GetPointForPointId(deliveryPointID);
                    iDrop.ClientsCustomerIdentityID = point.IdentityId;
                    iDrop.PointID = deliveryPointID;
                    iDrop.Point   = point;

                    iDrop.CollectDrops     = new Orchestrator.Entities.CollectDropCollection();
                    iDrop.InstructionOrder = dropSequence;
                    newdelivery            = true;
                    dropSequence++;
                }

                cd             = new Orchestrator.Entities.CollectDrop();
                cd.NoPallets   = (int)row["NoPallets"];
                cd.NoCases     = (int)row["Cases"];
                cd.GoodsTypeId = (int)row["GoodsTypeID"];
                cd.OrderID     = (int)row["OrderID"];
                cd.Weight      = (decimal)row["Weight"];
                cd.ClientsCustomerReference = row["DeliveryOrderNumber"].ToString();
                cd.Docket      = row["OrderID"].ToString();
                cd.OrderAction = orderAction;

                iDrop.CollectDrops.Add(cd);
                if (newdelivery)
                {
                    drops.Insert(0, iDrop);
                }
                //drops.Add(iDrop); Stephen Newman 23/04/07 Changed to insert the drop to the front of the list as the sort processed later seems to swap objects if equal.

                facOrder.UpdateForCollectionRun(cd.OrderID, iDrop.PointID, iDrop.BookedDateTime, iDrop.IsAnyTime, cd.OrderAction, Page.User.Identity.Name);
                #endregion
            }

            #region Add the Instructions to the job
            foreach (Entities.Instruction instruction in collections)
            {
                job.Instructions.Add(instruction);
            }

            drops.Sort(eInstructionSortType.DropDateTime);
            foreach (Entities.Instruction instruction in drops)
            {
                job.Instructions.Add(instruction);
            }
            #endregion

            job.JobState = eJobState.Booked;

            Facade.IJob facjob = new Facade.Job();
            _jobID   = facjob.Create(job, Page.User.Identity.Name);
            _openJob = true;
        }