protected void lbUndo_Click(object sender, EventArgs e)
        {
            LinkButton lbUndo = sender as LinkButton;

            if (lbUndo.CommandArgument.Length > 1)
            {
                Entities.PalletDelivery pd = PalletDeliveries.SingleOrDefault(lpd => lpd.Identifier == lbUndo.CommandArgument);

                if (pd != null)
                {
                    Entities.PalletDelivery foundUpdatedPalletDelivery = UpdatedPalletDeliveries.FirstOrDefault(fupd => fupd.PalletOrder.OrderID == pd.PalletOrder.OrderID);

                    if (foundUpdatedPalletDelivery != null)
                    {
                        UpdatedPalletDeliveries.Remove(foundUpdatedPalletDelivery);
                    }

                    pd.UpdatedNoPallets = 0;
                    pd.IsDirty          = false;
                    pd.ToBeRemoved      = false;

                    Rebind(null);

                    btnGenerateUpdate.Visible = !PalletDeliveries.Exists(lpds => lpds.IsDirty || lpds.PalletOrder.OrderID < 1);
                }
            }
        }
        protected void lbRemove_Click(object sender, EventArgs e)
        {
            LinkButton lbRemove = sender as LinkButton;

            if (lbRemove.CommandArgument.Length > 1)
            {
                Entities.PalletDelivery pd = PalletDeliveries.SingleOrDefault(lpd => lpd.Identifier == lbRemove.CommandArgument);

                if (pd != null)
                {
                    if (pd.PalletOrder.OrderID > 0)
                    {
                        pd.ToBeRemoved = true;
                        pd.IsDirty     = true;
                    }
                    else
                    {
                        PalletDeliveries.Remove(pd);
                    }
                }
            }

            GetSelectedPallets();
            Rebind(null);
            ClearPalletHandling();

            btnGenerateUpdate.Visible = !PalletDeliveries.Exists(lpds => lpds.IsDirty || lpds.PalletOrder.OrderID < 1);
        }
        protected void lbSelect_Click(object sender, EventArgs e)
        {
            LinkButton lbUpdate = sender as LinkButton;

            if (lbUpdate.CommandArgument.Length > 1)
            {
                Entities.PalletDelivery pd = PalletDeliveries.SingleOrDefault(lpd => lpd.Identifier == lbUpdate.CommandArgument);

                if (pd != null)
                {
                    SelectedPalletDelivery = pd;
                    BindPalletHandling(pd);
                    Rebind(pd);
                    LoadSelectedPallets(pd, m_isUpdateable);

                    if (pd.PalletOrder.OrderID > 0)
                    {
                        btnGenerateUpdate.Style.Add("display", "none");
                    }

                    btnCancelUpdate.Style.Remove("display");
                    palletButtonBar.Style.Remove("display");
                }
            }
        }
        private void LoadExistingPalletHandling(Entities.PalletDelivery selectedPalletDelivery)
        {
            List <Entities.PalletDelivery> lpds = new List <Entities.PalletDelivery>();

            if (PalletDeliveries.Count > 0 && selectedPalletDelivery != null && PalletDeliveries.Exists(pd => pd.PalletOrder.OrderID == selectedPalletDelivery.PalletOrder.OrderID))
            {
                PalletDeliveries.Remove(selectedPalletDelivery);
            }

            if (UpdatedPalletDeliveries.Count > 0)
            {
                List <int> uppd = new List <int>();
                foreach (Entities.PalletDelivery pd in UpdatedPalletDeliveries)
                {
                    uppd.Add(pd.PalletOrder.OrderID);
                }

                lpds.AddRange(PalletDeliveries.Where(lpd => !uppd.Contains(lpd.PalletOrder.OrderID)));
                lpds.AddRange(UpdatedPalletDeliveries.Where(lpd => selectedPalletDelivery == null || lpd.PalletOrder.OrderID != selectedPalletDelivery.PalletOrder.OrderID));
            }
            else
            {
                lpds = PalletDeliveries;
            }

            lvExisingPalletHandling.DataSource = lpds;
            lvExisingPalletHandling.DataBind();
        }
        private void ConfigureDisplay()
        {
            // Bind the handle method
            rcbPalletHandlingAction.Items.Clear();
            rcbPalletHandlingAction.Items.Add(new RadComboBoxItem(Utilities.UnCamelCase(eInstructionType.LeavePallets.ToString()), ((int)eInstructionType.LeavePallets).ToString()));
            rcbPalletHandlingAction.Items.Add(new RadComboBoxItem(Utilities.UnCamelCase(eInstructionType.DeHirePallets.ToString()), ((int)eInstructionType.DeHirePallets).ToString()));
            rcbPalletHandlingAction.ClearSelection();

            // Setting currency culture
            CultureInfo systemCulture = new CultureInfo(Orchestrator.Globals.Configuration.NativeCulture);

            rntPalletDeliveryCharge.Culture = systemCulture;

            ClearPalletHandling();

            // Bind the job information.
            LoadJob();
            LoadEmptyPallets(null);

            if (m_emptyPalletCount == 0)
            {
                palletButtonBar.Style.Add("display", "none");
            }

            btnCancelUpdate.Style.Add("display", "none");
            LoadExistingPalletHandling(null);

            btnGenerateUpdate.Visible = !PalletDeliveries.Exists(lpds => lpds.IsDirty || lpds.PalletOrder.OrderID < 1);
        }
        void lvExisingPalletHandling_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Entities.PalletDelivery pd = ((ListViewDataItem)e.Item).DataItem as Entities.PalletDelivery;

                LinkButton lbRemove = e.Item.FindControl("lbRemove") as LinkButton;
                LinkButton lbSelect = e.Item.FindControl("lbSelect") as LinkButton;
                LinkButton lbUndo   = e.Item.FindControl("lbUndo") as LinkButton;

                //Only set to be visible before the order has been created.
                lbUndo.Visible   = pd.IsDirty;
                lbRemove.Visible = !lbUndo.Visible;

                if (m_job.Instructions.Count == 2 && this.PalletDeliveries.Count == 1) // If this is a simple load / drop, you cannot have less than 2 instructions on a run for Groupage Runs.
                {
                    lbRemove.Visible = false;
                }

                if (PalletDeliveries.Where(lpd => lpd.PalletOrder.OrderID < 1).Count() != 0)
                {
                    lbUndo.OnClientClick = string.Format("javascript:if(!pendingActionsWarning()) return false;");
                }

                //lblConfirmation.Visible = lbRemove.Visible = lbUndo.Visible = pd.PalletOrder.OrderID < 1;
            }
        }
        void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            btnCancelUpdate.Style.Add("display", "none");
            palletButtonBar.Style.Add("display", "none");
            btnGenerateUpdate.Style.Remove("display");
            btnGenerate.Text = "Generate";

            PalletDeliveries.Add(SelectedPalletDelivery);

            Rebind(null);
            ClearPalletHandling();

            btnGenerateUpdate.Visible = !PalletDeliveries.Exists(lpds => lpds.IsDirty || lpds.PalletOrder.OrderID < 1);
        }
        void btnGenerate_Click(object sender, EventArgs e)
        {
            btnCancelUpdate.Style.Add("display", "none");
            palletButtonBar.Style.Add("display", "none");
            btnGenerateUpdate.Style.Remove("display");

            Entities.PalletDelivery currentPalletDelivery = null;

            if (btnGenerate.Text == "Update" && SelectedPalletDelivery.PalletOrder.OrderID > 0)
            {
                currentPalletDelivery = SelectedPalletDelivery;
            }

            GetSelectedPallets();
            //LoadEmptyPallets(currentPalletDelivery);
            //LoadExistingPalletHandling(null);
            Rebind(null);

            btnGenerate.Text = "Generate";
            ClearPalletHandling();

            btnGenerateUpdate.Visible = !PalletDeliveries.Exists(lpds => lpds.IsDirty || lpds.PalletOrder.OrderID < 1);
        }
        private void GetSelectedPallets()
        {
            Entities.Instruction palletHandlingInstruction = new Entities.Instruction();

            eInstructionType selectedPalletAction = (eInstructionType)int.Parse(rcbPalletHandlingAction.SelectedValue);

            foreach (ListViewDataItem lvdi in lvEmptyPallets.Items)
            {
                CheckBox chkResourceSelected = lvdi.FindControl("chkResourceSelected") as CheckBox;

                if (chkResourceSelected != null && chkResourceSelected.Checked)
                {
                    HiddenField hdnResource    = lvdi.FindControl("hdnResource") as HiddenField;
                    HiddenField hidResourceID  = lvdi.FindControl("hidResourceID") as HiddenField;
                    HiddenField hidIsFixedUnit = lvdi.FindControl("hidIsFixedUnit") as HiddenField;
                    ListView    lvItems        = lvdi.FindControl("lvItems") as ListView;

                    int resourceID = 0;
                    int.TryParse(hidResourceID.Value, out resourceID);

                    bool isFixedUnit      = false;
                    int  isFixedUnitValue = 0;
                    int.TryParse(hidIsFixedUnit.Value, out isFixedUnitValue);
                    isFixedUnit = isFixedUnitValue > 0;

                    foreach (ListViewDataItem ilvdi in lvItems.Items)
                    {
                        #region Generate Pallet Orders

                        RadNumericTextBox rntNoOfPallets = ilvdi.FindControl("rntNoOfPallets") as RadNumericTextBox;

                        if (rntNoOfPallets != null && rntNoOfPallets.Value.Value > 0)
                        {
                            HiddenField hdnPalletTypeID = ilvdi.FindControl("hdnPalletTypeID") as HiddenField;
                            HiddenField hdnPalletType   = ilvdi.FindControl("hdnPalletType") as HiddenField;
                            HiddenField hdnIdentifier   = ilvdi.FindControl("hdnIdentifier") as HiddenField;

                            string                  destination    = string.Empty;
                            Entities.Order          palletDelivery = null;
                            Entities.PalletDelivery pd             = null;

                            if (hdnIdentifier != null && hdnIdentifier.Value != string.Empty)
                            {
                                #region Update Order
                                pd = SelectedPalletDelivery;

                                if (pd.PalletOrder.OrderID > 0)
                                {
                                    Entities.PalletDelivery updatedDelivery = null;

                                    if (UpdatedPalletDeliveries.Count > 0)
                                    {
                                        updatedDelivery = UpdatedPalletDeliveries.Find(upd => upd.PalletOrder.OrderID == pd.PalletOrder.OrderID);
                                    }

                                    if (updatedDelivery == null)
                                    {
                                        updatedDelivery = new Entities.PalletDelivery();
                                    }
                                    else
                                    {
                                        UpdatedPalletDeliveries.Remove(updatedDelivery);
                                    }

                                    if (updatedDelivery.PalletOrder == null || updatedDelivery.PalletOrder.OrderID < 1)
                                    {
                                        updatedDelivery.PalletOrder         = GeneratePalletOrder(int.Parse(hdnPalletTypeID.Value), (int)rntNoOfPallets.Value.Value, selectedPalletAction, ref destination);
                                        updatedDelivery.PalletOrder.OrderID = pd.PalletOrder.OrderID;
                                    }
                                    else
                                    {
                                        updatedDelivery.PalletOrder.NoPallets = (int)rntNoOfPallets.Value.Value;
                                    }

                                    updatedDelivery.PalletAction            = selectedPalletAction;
                                    updatedDelivery.PalletActionDescription = Utilities.UnCamelCase(selectedPalletAction.ToString());
                                    updatedDelivery.ResourceID  = resourceID;
                                    updatedDelivery.IsFixedUnit = isFixedUnit;

                                    updatedDelivery.PalletType  = hdnPalletType.Value;
                                    updatedDelivery.Resource    = hdnResource.Value;
                                    updatedDelivery.Destination = destination;

                                    updatedDelivery.UpdatedNoPallets = updatedDelivery.PalletOrder.NoPallets;
                                    //pd.UpdatedNoPallets = updatedDelivery.PalletOrder.NoPallets;
                                    updatedDelivery.IsDirty = true;

                                    UpdatedPalletDeliveries.Add(updatedDelivery);
                                }

                                //pd.PalletOrder.NoPallets = (int)rntNoOfPallets.Value.Value;
                                pd.UpdatedNoPallets = (int)rntNoOfPallets.Value.Value;
                                pd.IsDirty          = true;

                                #endregion
                            }
                            else
                            {
                                #region Generate Order

                                palletDelivery = GeneratePalletOrder(int.Parse(hdnPalletTypeID.Value), (int)rntNoOfPallets.Value.Value, selectedPalletAction, ref destination);

                                pd = new Entities.PalletDelivery();

                                pd.PalletOrder = palletDelivery;

                                pd.PalletAction            = selectedPalletAction;
                                pd.PalletActionDescription = Utilities.UnCamelCase(selectedPalletAction.ToString());
                                pd.ResourceID  = resourceID;
                                pd.IsFixedUnit = isFixedUnit;

                                pd.PalletType  = hdnPalletType.Value;
                                pd.Resource    = hdnResource.Value;
                                pd.Destination = destination;

                                #endregion
                            }

                            PalletDeliveries.Add(pd);
                        }

                        #endregion
                    }
                }
            }
        }
        private void LoadEmptyPallets(Entities.PalletDelivery selectedPalletDelivery)
        {
            Facade.IJob facJob = new Facade.Job();
            DataSet     ds     = facJob.GetUnhandledPalletsForJob(m_jobId, null);

            // If this is an update, then add the selected orders pallets to the unhandled pallets available - as this orders pallets will be updated.
            if (selectedPalletDelivery != null && selectedPalletDelivery.PalletOrder.OrderID > 0)
            {
                var     existingRows = ds.Tables[0].Rows.Cast <DataRow>().AsEnumerable();
                DataRow dr           = existingRows.FirstOrDefault(ldr => ldr.Field <int>("PalletTypeID") == selectedPalletDelivery.PalletTypeID && ldr.Field <int>("ResourceID") == selectedPalletDelivery.ResourceID);

                if (dr == null)
                {
                    #region Insert New Row

                    dr = ds.Tables[0].NewRow();
                    dr.SetField <string>("PalletType", selectedPalletDelivery.PalletType);
                    dr.SetField <int>("PalletTypeID", selectedPalletDelivery.PalletTypeID);

                    int currentPallets = selectedPalletDelivery.UpdatedNoPallets > 0 ? selectedPalletDelivery.UpdatedNoPallets : selectedPalletDelivery.NoOfPallets;

                    dr.SetField <int>("NoOfPallets", currentPallets);
                    dr.SetField <int>("TrailerPallets", selectedPalletDelivery.NoOfPallets);
                    dr.SetField <int>("HandledPallets", currentPallets);
                    dr.SetField <int>("CollectDropID", selectedPalletDelivery.CollectDropID);
                    dr.SetField <int>("ResourceID", selectedPalletDelivery.ResourceID);
                    dr.SetField <string>("Resource", selectedPalletDelivery.Resource);
                    dr.SetField <bool>("IsFixedUnit", selectedPalletDelivery.IsFixedUnit);
                    ds.Tables[0].Rows.Add(dr);

                    #endregion
                }
                else
                {
                    int currentPallets = dr.Field <int>("NoOfPallets");
                    currentPallets += selectedPalletDelivery.UpdatedNoPallets > 0 ? selectedPalletDelivery.NoOfPallets - selectedPalletDelivery.UpdatedNoPallets : 0;
                    dr.SetField <int>("NoOfPallets", currentPallets);
                }
            }

            // Check for any updated pallet handling instructions.
            foreach (Entities.PalletDelivery pd in PalletDeliveries.Where(cpd => cpd.IsDirty))//&& (selectedPalletDelivery == null || cpd.PalletOrder.OrderID != selectedPalletDelivery.PalletOrder.OrderID)))
            {
                var     existingRows = ds.Tables[0].Rows.Cast <DataRow>().AsEnumerable();
                DataRow dr           = existingRows.FirstOrDefault(ldr => ldr.Field <int>("PalletTypeID") == pd.PalletTypeID && ldr.Field <int>("ResourceID") == pd.ResourceID);

                if (dr == null)
                {
                    #region Insert New Row

                    dr = ds.Tables[0].NewRow();
                    dr.SetField <string>("PalletType", pd.PalletType);
                    dr.SetField <int>("PalletTypeID", pd.PalletTypeID);

                    int currentPallets = pd.UpdatedNoPallets > 0 ? pd.NoOfPallets - pd.UpdatedNoPallets : pd.NoOfPallets;

                    dr.SetField <int>("NoOfPallets", currentPallets);
                    dr.SetField <int>("TrailerPallets", currentPallets);
                    dr.SetField <int>("HandledPallets", currentPallets);
                    dr.SetField <int>("CollectDropID", pd.CollectDropID);
                    dr.SetField <int>("ResourceID", pd.ResourceID);
                    dr.SetField <string>("Resource", pd.Resource);
                    dr.SetField <bool>("IsFixedUnit", pd.IsFixedUnit);
                    ds.Tables[0].Rows.Add(dr);

                    #endregion
                }
                else
                {
                    int currentPallets = dr.Field <int>("NoOfPallets");
                    currentPallets += (pd.NoOfPallets - pd.UpdatedNoPallets); //pd.UpdatedNoPallets >= 0 ? pd.NoOfPallets - pd.UpdatedNoPallets : 0;
                    dr.SetField <int>("NoOfPallets", currentPallets);
                }
            }

            // Cast Datarows as Enumerated List
            var returndRows = ds.Tables[0].Rows.Cast <DataRow>().AsEnumerable();

            // If supplied with an existing pallet delivery, find this from the data rows provided.
            if (selectedPalletDelivery != null)
            {
                returndRows = returndRows.Where(rr => rr.Field <int>("ResourceID") == selectedPalletDelivery.ResourceID && rr.Field <int>("PalletTypeID") == selectedPalletDelivery.PalletTypeID);
            }

            // If there are still unhandled pallets and this is not a update.
            if (returndRows.Count() > 0 && selectedPalletDelivery == null)
            {
                var pendingPalletDeliveries = PalletDeliveries.Where(ppd => ppd.PalletOrder.OrderID < 1);

                if (pendingPalletDeliveries.Count() > 0)
                {
                    foreach (DataRow dr in returndRows)
                    {
                        int resourceID   = dr.Field <int>("ResourceID");
                        int palletTypeId = dr.Field <int>("PalletTypeID");

                        var foundPalletDeliveries = pendingPalletDeliveries.Where(ppd => ppd.ResourceID == resourceID && ppd.PalletTypeID == palletTypeId);

                        foreach (Entities.PalletDelivery pd in foundPalletDeliveries)
                        {
                            dr.SetField <int>("NoOfPallets", (dr.Field <int>("NoOfPallets") - pd.NoOfPallets));
                        }
                    }
                }
            }

            // Get all the rows that have pallets remaining.
            var retValRows = returndRows.Where(rr => rr.Field <int>("NoOfPallets") > 0);

            // If Any exist, run the queries and bind them up.
            if (retValRows.Count() > 0)
            {
                // If is an update, only bind rows with the same pallet type as the one on the selected order.
                var resourceEmptyPallets = from row in retValRows
                                           group row by
                                           new { ResourceID = row["ResourceID"], PalletContainer = row["Resource"], IsFixedUnit = row["IsFixedUnit"] } into g
                orderby g.Key.ResourceID, g.Key.PalletContainer, g.Key.IsFixedUnit
                    select new
                {
                    Resources = g.Key,
                    Items     = selectedPalletDelivery == null ? g : g.Where(ite => ite.Field <int>("PalletTypeID") == selectedPalletDelivery.PalletTypeID)
                };

                currentResources = (from row in retValRows
                                    where (int)row["ResourceID"] != 0
                                    select new KeyValuePair <int, string>
                                    (
                                        (int)row["ResourceID"],
                                        (string)row["Resource"]
                                    )).Distinct <KeyValuePair <int, string> >();

                // If is an update, only bind the resource associated with this order.
                lvEmptyPallets.DataSource = selectedPalletDelivery == null ? resourceEmptyPallets : resourceEmptyPallets.Where(rep => (int)rep.Resources.ResourceID == selectedPalletDelivery.ResourceID);
            }

            lvEmptyPallets.DataBind();
        }