private void LoadEmptyPallets() { Facade.IJob facJob = new Facade.Job(); DataSet ds = facJob.GetUnhandledPalletsForJob(m_jobId, null); lvTrailerPallets.DataSource = ds; lvTrailerPallets.DataBind(); if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { trEmptyPallets.Style.Remove("display"); } else { trEmptyPallets.Style.Add("display", "none"); } }
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(); }