예제 #1
0
        private DataTable verifyUnits()
        {
            DataTable toRequest = new DataTable();

            toRequest.Clear();
            toRequest.Columns.Add("ResourceID", typeof(int));

            foreach (ListItem resource in units_list.Items)
            {
                ResourceItem ri = resource as ResourceItem;

                var container = units_list.ItemContainerGenerator.ContainerFromItem(resource) as FrameworkElement;
                ContentPresenter listBoxItemCP = Helpers.FindVisualChild <ContentPresenter>(container);
                if (listBoxItemCP == null)
                {
                    throw new Exception("Invalid item");
                }

                DataTemplate dataTemplate = listBoxItemCP.ContentTemplate;

                int units = int.Parse(((DecimalUpDown)units_list.ItemTemplate.FindName("equipment_units", listBoxItemCP)).Text);
                if (units > ri.Units.Count)
                {
                    throw new Exception("You cannot request more units than the available!");
                }

                // Save each unit on DB
                while (units > 0)
                {
                    ElectronicUnit unit = ri.requestUnit();
                    DataRow        row  = toRequest.NewRow();
                    row["ResourceID"] = unit.ResourceID;
                    toRequest.Rows.Add(row);
                    units--;
                }
            }

            if (toRequest.Rows.Count == 0)
            {
                throw new Exception("You need to select at least one unit!");
            }

            return(toRequest);
        }
예제 #2
0
        private void SubmitRequisitionResources()
        {
            int reqID = -1;

            if (SelectedProject == null)
            {
                throw new Exception("You have to select a project first!");
            }

            // List's to manage equipments for requisition
            List <Resources> toBeRequested = new List <Resources>();
            List <ListItem>  toBeRemoved   = new List <ListItem>();

            DataTable toRequest = new DataTable();

            toRequest.Clear();
            toRequest.Columns.Add("ResourceID", typeof(int));

            foreach (ListItem resource in equipment_list.Items)
            {
                var container = equipment_list.ItemContainerGenerator.ContainerFromItem(resource) as FrameworkElement;
                if (container == null)
                {
                    equipment_list.UpdateLayout();
                    equipment_list.ScrollIntoView(resource);
                    container = equipment_list.ItemContainerGenerator.ContainerFromItem(resource) as FrameworkElement;
                }
                ContentPresenter listBoxItemCP = Helpers.FindVisualChild <ContentPresenter>(container);
                if (listBoxItemCP == null)
                {
                    return;
                }

                DataTemplate dataTemplate = listBoxItemCP.ContentTemplate;

                DecimalUpDown unitsTextBox = equipment_list.ItemTemplate.FindName("equipment_units", listBoxItemCP) as DecimalUpDown;
                int           units        = int.Parse(unitsTextBox.Text);
                if (units == 0)
                {
                    continue;
                }

                unitsTextBox.Value = 0;

                // Add chosen unit's number
                while (units > 0)
                {
                    Resources unit = null;
                    if (resource is ResourceItem)
                    {
                        ResourceItem r = resource as ResourceItem;
                        if (units > r.Units.Count)
                        {
                            throw new Exception("You cannot request more units than available!");
                        }
                        unit = r.requestUnit();
                        if (r.Units.Count == 0)
                        {
                            toBeRemoved.Add(r);
                        }
                    }
                    else if (resource is KitItem)
                    {
                        KitItem k = resource as KitItem;
                        if (units > k.Units.Count)
                        {
                            throw new Exception("You cannot request more units than available!");
                        }
                        unit = k.requestUnit();
                        if (k.Units.Count == 0)
                        {
                            toBeRemoved.Add(k);
                        }
                    }
                    toBeRequested.Add(unit);
                    DataRow row = toRequest.NewRow();
                    row["ResourceID"] = unit.ResourceID;
                    toRequest.Rows.Add(row);
                    units--;
                }
            }

            if (toRequest.Rows.Count == 0)
            {
                throw new Exception("You can't request 0 resources!");
            }

            reqID = SubmitRequisition();
            if (reqID == -1)
            {
                throw new Exception("Error creating requisition");
            }

            cn = Helpers.getSGBDConnection();
            if (!Helpers.verifySGBDConnection(cn))
            {
                throw new Exception("Error connecting to database");
            }

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection  = cn;
            SqlParameter listParam = cmd.Parameters.AddWithValue("@UnitsList", toRequest);

            listParam.SqlDbType = SqlDbType.Structured;
            cmd.Parameters.AddWithValue("@reqID", reqID);
            cmd.CommandText = "DML.REQUEST_RESOURCES";

            try
            {
                cmd.ExecuteNonQuery();
                foreach (Resources resource in toBeRequested)
                {
                    ActiveRequisitionsData.Add(resource);
                }
                foreach (ListItem li in toBeRemoved)
                {
                    ResourcesListData.Remove(li);
                    if (typeof(ResourceItem).IsInstanceOfType(li))
                    {
                        ResourceItem ri = li as ResourceItem;
                        ResourceItems.Remove(ri);
                    }
                    else if (typeof(KitItem).IsInstanceOfType(li))
                    {
                        KitItem ki = li as KitItem;
                        KitItems.Remove(ki);
                    }
                }
                equipment_list.Items.Refresh();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cn.Close();
            }
        }