Exemplo n.º 1
0
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != string.Empty && txtDescription.Text != string.Empty && txtPurchaseLocation.Text != string.Empty &&
                    txtJustification.Text != string.Empty && txtPrice.Text != string.Empty && IsNumeric(txtPrice.Text) &&
                    txtQuantity.Text != string.Empty && Convert.ToInt32(txtQuantity.Text) > 0 && IsNumeric(txtQuantity.Text))
                {
                    Item tmpItem = ItemFactory.Create();
                    tmpItem.Name             = txtName.Text;
                    tmpItem.Description      = txtDescription.Text;
                    tmpItem.PurchaseLocation = txtPurchaseLocation.Text;
                    tmpItem.Justification    = txtJustification.Text;
                    tmpItem.Price            = Convert.ToDouble(txtPrice.Text);
                    tmpItem.Quantity         = Convert.ToInt32(txtQuantity.Text);
                    tmpItem.Subtotal         = tmpItem.Price * tmpItem.Quantity;
                    tmpItem.OrderNumber      = Order.OrderNumber;

                    if (VAL.Validate.cleanItem(tmpItem))
                    {
                        if (ItemList.Any(Item => Item.Name == tmpItem.Name) && ItemList.Any(Item => Item.Description == tmpItem.Description) &&
                            ItemList.Any(Item => Item.Price == tmpItem.Price) && ItemList.Any(Item => Item.PurchaseLocation == tmpItem.PurchaseLocation) &&
                            ItemList.Any(Item => Item.Justification == tmpItem.Justification))
                        {
                            foreach (Item item in ItemList)
                            {
                                if (tmpItem.Name.Equals(item.Name) && tmpItem.Description.Equals(item.Description) &&
                                    tmpItem.Price == item.Price && tmpItem.PurchaseLocation.Equals(item.PurchaseLocation) &&
                                    tmpItem.Justification.Equals(item.Justification))
                                {
                                    item.Quantity += tmpItem.Quantity;
                                    item.Subtotal += tmpItem.Price * tmpItem.Quantity;
                                    int rowIndex = ItemList.IndexOf(item);
                                    dgvItems.Rows[rowIndex].Cells["Quantity"].Value = item.Quantity;
                                    dgvItems.Rows[rowIndex].Cells["Subtotal"].Value = item.Subtotal;
                                    tmpItem = item;

                                    txtName.Focus();
                                }
                            }

                            if (!tmpItem.Status.Equals("Pending"))
                            {
                                MessageBox.Show("Cannot modify item if its status is not set to Pending.");
                            }
                            else
                            {
                                bool result = ItemFactory.EmployeeModifyItem(tmpItem);

                                if (result == true)
                                {
                                    CalculateTotals();

                                    double subtotal = Convert.ToDouble((txtSubtotal.Text as string).Trim('$'));
                                    double taxes    = Convert.ToDouble((txtTaxes.Text as string).Trim('$'));
                                    double total    = Convert.ToDouble((txtTotal.Text as string).Trim('$'));

                                    Order.Subtotal = subtotal;
                                    Order.Taxes    = taxes;
                                    Order.Total    = total;

                                    PurchaseOrderFactory.EmployeeUpdateTotals(Order);
                                    ItemList = ItemFactory.RetrieveByOrderNumber(tmpItem.OrderNumber);
                                    MessageBox.Show("Item #" + tmpItem.ItemId + " successfully updated. Purchase Order totals have also been updated.");

                                    dgvOrders.Rows[selectedOrderIndex].Cells["Subtotal"].Value = Order.Subtotal;
                                    dgvOrders.Rows[selectedOrderIndex].Cells["Taxes"].Value    = Order.Taxes;
                                    dgvOrders.Rows[selectedOrderIndex].Cells["Total"].Value    = Order.Total;

                                    dgvItems.DataSource = ItemList;

                                    if (!Order.Status.Equals("Closed"))
                                    {
                                        foreach (Item item in ItemList)
                                        {
                                            if (!item.Description.Equals("No longer needed"))
                                            {
                                                dgvItems.Rows[ItemList.IndexOf(item)].Cells["status"].Value = "Pending";
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            int itemId = ItemFactory.EmployeeAddItem(tmpItem, Order);

                            if (itemId != 0)
                            {
                                ItemList.Add(tmpItem);
                                CalculateTotals();

                                double subtotal = Convert.ToDouble((txtSubtotal.Text as string).Trim('$'));
                                double taxes    = Convert.ToDouble((txtTaxes.Text as string).Trim('$'));
                                double total    = Convert.ToDouble((txtTotal.Text as string).Trim('$'));

                                Order.Subtotal = subtotal;
                                Order.Taxes    = taxes;
                                Order.Total    = total;

                                PurchaseOrderFactory.EmployeeUpdateTotals(Order);
                                ItemList = ItemFactory.RetrieveByOrderNumber(tmpItem.OrderNumber);
                                MessageBox.Show("Item #" + itemId + " successfully added to Order #" + tmpItem.OrderNumber + ". Purchase Order totals have also been updated.");

                                dgvOrders.Rows[selectedOrderIndex].Cells["Subtotal"].Value = Order.Subtotal;
                                dgvOrders.Rows[selectedOrderIndex].Cells["Taxes"].Value    = Order.Taxes;
                                dgvOrders.Rows[selectedOrderIndex].Cells["Total"].Value    = Order.Total;

                                dgvItems.DataSource = ItemList;

                                if (!Order.Status.Equals("Closed"))
                                {
                                    foreach (Item item in ItemList)
                                    {
                                        if (!item.Description.Equals("No longer needed"))
                                        {
                                            dgvItems.Rows[ItemList.IndexOf(item)].Cells["status"].Value = "Pending";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please fill in all fields before adding a new item.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "An error has occurred");
            }
        }
Exemplo n.º 2
0
        private void btnModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != string.Empty && txtDescription.Text != string.Empty && txtPurchaseLocation.Text != string.Empty &&
                    txtJustification.Text != string.Empty && txtPrice.Text != string.Empty && IsNumeric(txtPrice.Text) &&
                    txtQuantity.Text != string.Empty && Convert.ToInt32(txtQuantity.Text) > 0 && IsNumeric(txtQuantity.Text))
                {
                    int  gridItemIndex = dgvItems.SelectedRows[0].Index;
                    Item listItem      = ItemList[gridItemIndex];
                    listItem.Name             = txtName.Text;
                    listItem.Description      = txtDescription.Text;
                    listItem.PurchaseLocation = txtPurchaseLocation.Text;
                    listItem.Justification    = txtJustification.Text;
                    listItem.Price            = Convert.ToDouble(txtPrice.Text);
                    listItem.Quantity         = Convert.ToInt32(txtQuantity.Text);
                    listItem.Subtotal         = listItem.Price * listItem.Quantity;

                    if (txtModReason.Text != string.Empty)
                    {
                        listItem.ModificationReason = txtModReason.Text;
                    }

                    if (VAL.Validate.cleanItem(listItem))
                    {
                        int itemIndex = ItemList.IndexOf(listItem);
                        ItemList[itemIndex] = listItem;

                        dgvItems.Rows[itemIndex].Cells["Name"].Value             = listItem.Name;
                        dgvItems.Rows[itemIndex].Cells["Description"].Value      = listItem.Description;
                        dgvItems.Rows[itemIndex].Cells["PurchaseLocation"].Value = listItem.PurchaseLocation;
                        dgvItems.Rows[itemIndex].Cells["Justification"].Value    = listItem.Justification;
                        dgvItems.Rows[itemIndex].Cells["Price"].Value            = listItem.Price;
                        dgvItems.Rows[itemIndex].Cells["Quantity"].Value         = listItem.Quantity;
                        dgvItems.Rows[itemIndex].Cells["Subtotal"].Value         = listItem.Subtotal;

                        bool result = false;

                        if (txtEmployee.Text != Settings.Default.EmployeeName.ToString())
                        {
                            if (txtModReason.Text == string.Empty)
                            {
                                MessageBox.Show("Supervisor must supply a reason for modifying an employee's item.");
                            }
                            else
                            {
                                result = ItemFactory.EmployeeModifyItem(listItem);
                            }
                        }
                        else
                        {
                            result = ItemFactory.EmployeeModifyItem(listItem);
                        }

                        if (result == true)
                        {
                            CalculateTotals();

                            double subtotal = Convert.ToDouble((txtSubtotal.Text as string).Trim('$'));
                            double taxes    = Convert.ToDouble((txtTaxes.Text as string).Trim('$'));
                            double total    = Convert.ToDouble((txtTotal.Text as string).Trim('$'));

                            Order.Subtotal = subtotal;
                            Order.Taxes    = taxes;
                            Order.Total    = total;

                            PurchaseOrderFactory.EmployeeUpdateTotals(Order);
                            ItemList = ItemFactory.RetrieveByOrderNumber(listItem.OrderNumber);
                            MessageBox.Show("Item #" + listItem.ItemId + " successfully updated. Purchase Order totals have also been updated.");

                            txtName.Text             = listItem.Name;
                            txtDescription.Text      = listItem.Description;
                            txtPurchaseLocation.Text = listItem.PurchaseLocation;
                            txtJustification.Text    = listItem.Justification;
                            txtPrice.Text            = listItem.Price.ToString();
                            txtQuantity.Text         = listItem.Quantity.ToString();

                            if (listItem.ModificationReason != null)
                            {
                                txtModReason.Text = listItem.ModificationReason;
                            }

                            dgvOrders.Rows[selectedOrderIndex].Cells["Subtotal"].Value = Order.Subtotal;
                            dgvOrders.Rows[selectedOrderIndex].Cells["Taxes"].Value    = Order.Taxes;
                            dgvOrders.Rows[selectedOrderIndex].Cells["Total"].Value    = Order.Total;

                            dgvItems.DataSource = ItemList;
                            dgvItems.Rows[selectedItemIndex].Selected = true;

                            if (!Order.Status.Equals("Closed"))
                            {
                                foreach (Item item in ItemList)
                                {
                                    if (!item.Description.Equals("No longer needed"))
                                    {
                                        dgvItems.Rows[ItemList.IndexOf(item)].Cells["status"].Value = "Pending";
                                    }
                                }
                            }
                        }

                        dgvItems.Rows[itemIndex].Selected = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "An error has occurred");
                btnCancelEdit.PerformClick();
                btnCancelItems.PerformClick();
                btnCancelSearch.PerformClick();
            }
        }