private void Update_Click(object sender, RoutedEventArgs e)
        {
            string newName = UpdateNewNameTB.Text.Trim();
            string oldName = UpdateOldNameTB.Text.Trim();

            if (commander.IsInputInvalid(newName, "New name") || commander.IsInputInvalid(oldName, "Old name"))
            {
                MessageBox.Show("Empty or default value is invalid for updating");
                return;
            }
            if (commander.ExistsInDatabaseByNameCaseInsensitive(OSHome.DbSources.Categories, newName, out string found))
            {
                MessageBox.Show($"Category named {newName} already exists in database.");
                return;
            }
            if (!commander.ExistsInDatabaseByNameCaseSensitive(OSHome.DbSources.Categories, oldName))
            {
                MessageBox.Show($"Category named {oldName} doesn't exists in database.");
                return;
            }
            if (!commander.FinalAcceptancePrompt())
            {
                return;
            }
            Categories categoryToUpdate = new Categories();

            categoryToUpdate = db.Categories.Single(x => x.category_name == oldName);
            categoryToUpdate.category_name = newName;
            db.SaveChanges();
            categoriesDataGrid.Items.Refresh();
            UpdateNewNameTB.Text = "New name";
            UpdateOldNameTB.Text = "Old name";
            MessageBox.Show($"Successfully updated: {oldName} into: {newName}.");
        }
        private void Update_Click(object sender, RoutedEventArgs e)
        {
            string stringId    = UpdateIDTB.Text.Trim();
            string newLastName = UpdateLastNameTB.Text.Trim();

            if (commander.IsInputInvalid(stringId, "ID") || !int.TryParse(stringId, out int id) || !commander.LettersInputOnly(newLastName))
            {
                MessageBox.Show($"{stringId} is not valid id.");
                return;
            }
            if (!commander.ExistsInDatabaseByID(OSHome.DbSources.Employees, id))
            {
                MessageBox.Show($"Employee doesn't exist in database.");
                return;
            }
            var    emp         = db.Employees.Single(x => x.employee_id == id);
            string oldLastName = emp.employee_name;

            if (oldLastName == newLastName)
            {
                MessageBox.Show($"New last name is same as existing last name.");
                return;
            }
            if (!commander.FinalAcceptancePrompt())
            {
                return;
            }
            emp.employee_name = newLastName;
            db.SaveChanges();
            employeesDataGrid.Items.Refresh();
            UpdateIDTB.Text       = "ID";
            UpdateLastNameTB.Text = "New last name";
            MessageBox.Show($"Successfully updated employee: {emp.employee_first_name} {oldLastName} into: {emp.employee_first_name} {newLastName}");
        }
Пример #3
0
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            string firstName = AddFirstNameTB.Text.Trim();
            string lastName  = AddLastNameTB.Text.Trim();
            string fen       = AddFENTB.Text.Trim();

            if (commander.IsInputInvalid(firstName, "First name") || commander.IsInputInvalid(lastName, "Last name"))
            {
                MessageBox.Show($"Invalid name value.");
                return;
            }
            if (!commander.LettersInputOnly(firstName))
            {
                MessageBox.Show($"First name must consist of letter only.");
                return;
            }
            if (!commander.LettersInputOnly(lastName))
            {
                MessageBox.Show($"Last name must consist of letter only.");
                return;
            }
            if (fen.Length > 0 && fen.Length < 8)
            {
                MessageBox.Show($"FEN must consist of 8-10 letters or digits or must be empty.");
                return;
            }
            if (!commander.LettersAndDigitsInputOnly(fen))
            {
                MessageBox.Show($"FEN must consist of letters or digits only, {fen} is not valid FEN number");
                return;
            }
            var duplicatedClient = db.Clients.SingleOrDefault(x =>
                                                              x.client_first_name.ToLower() == firstName.ToLower() &&
                                                              x.client_name.ToLower() == lastName.ToLower() &&
                                                              x.firm_evidence_number.ToLower() == fen.ToLower());

            if (duplicatedClient != null)
            {
                MessageBox.Show($"Client already exists.\nID: {duplicatedClient.client_id}, {duplicatedClient.client_first_name} {duplicatedClient.client_name}, FEN: {duplicatedClient.firm_evidence_number}");
                return;
            }
            Clients newClient = new Clients();

            newClient.client_first_name    = firstName;
            newClient.client_name          = lastName;
            newClient.firm_evidence_number = fen;
            db.Clients.Add(newClient);
            db.SaveChanges();
            MessageBox.Show($"Successfully created client:\nID: {newClient.client_id}, {newClient.client_first_name} {newClient.client_name}, FEN: {newClient.firm_evidence_number}");
            AddFirstNameTB.Text = "First name";
            AddLastNameTB.Text  = "Last name";
            AddFENTB.Text       = "FEN";
        }
Пример #4
0
        void CreateNewOrder_Click(object sender, RoutedEventArgs e)
        {
            var clientDGSelection = ClientsDG.SelectedItem;

            if (clientDGSelection == null)
            {
                MessageBox.Show($"Select client for order.");
                return;
            }
            var employeeDGSelection = EmployeesDG.SelectedItem;

            if (employeeDGSelection == null)
            {
                MessageBox.Show($"Select employee for order.");
                return;
            }
            if (productsInOrder.Count() == 0)
            {
                MessageBox.Show($"Order must contain at least one product.");
                return;
            }

            Orders newOrder = new Orders();

            newOrder.client_id      = ((Clients)clientDGSelection).client_id;
            newOrder.order_employee = ((Employees)employeeDGSelection).employee_id;
            var      date = DateTime.Now;
            TimeSpan ts   = new TimeSpan(0, 1, 0);

            date -= ts;
            ///one minute is substracted - when DateTime is converted to sql ShortDateTime the value is rounded up, it may breake sql constraint <= GETDATE()///

            newOrder.order_date = date;
            foreach (var p in productsInOrder)
            {
                Ord_Prod newOrderProduct = new Ord_Prod();
                newOrderProduct.product_id = p.productId;
                newOrderProduct.quantity   = (short)p.quantity;
                newOrder.Ord_Prod.Add(newOrderProduct);
            }
            try
            {
                db.Orders.Add(newOrder);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("New order successfully created.");
            ResetAllValues();
        }
Пример #5
0
        internal void RemoveFromDb(DbSources sourceCat, int index)
        {
            switch (sourceCat)
            {
            case DbSources.Producers:
            {
                Producers producerToRemove = db.Producers.Single(x => x.producer_id == index);
                db.Producers.Remove(producerToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Categories:
            {
                Categories categoryToRemove = db.Categories.Single(x => x.category_id == index);
                db.Categories.Remove(categoryToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Products:
            {
                Products productToRemove = db.Products.Single(x => x.product_id == index);
                db.Products.Remove(productToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Employees:
            {
                Employees employeeToRemove = db.Employees.Single(x => x.employee_id == index);
                db.Employees.Remove(employeeToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Clients:
                break;
            }
        }
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            string value = AddNameTB.Text.Trim();

            if (commander.IsInputInvalid(value, "Producer name"))
            {
                MessageBox.Show($"Can't add empty or default value to database.");
                return;
            }
            if (commander.ExistsInDatabaseByNameCaseInsensitive(OSHome.DbSources.Producers, value, out string found))
            {
                MessageBox.Show($"Producer with name {found} already exists in database.");
                return;
            }
            Producers newProducer = new Producers();

            newProducer.producer_name = value;
            db.Producers.Add(newProducer);
            db.SaveChanges();
            MessageBox.Show($"Successfully added new producer to database: {value}.");
            AddNameTB.Text = "Producer name";
        }
        private void DeleteOrder_Click(object sender, RoutedEventArgs e)
        {
            if (OrdersDG.SelectedItem == null)
            {
                return;
            }
            if (MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.No)
            {
                return;
            }
            int    selectedOrderId = ((Order)OrdersDG.SelectedItem).orderId;
            Orders selectedOrder   = db.Orders.Single(x => x.order_id == selectedOrderId);

            db.Orders.Remove(selectedOrder);
            db.SaveChanges();
            RefreshOrders();
            MessageBox.Show($"Order successfully deleted.");
        }
Пример #8
0
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            var cat  = db.Categories.SingleOrDefault(x => x.category_name == AddCategoriesCB.Text);
            var prod = db.Producers.SingleOrDefault(x => x.producer_name == AddProducersCB.Text);

            if (cat == null || prod == null)
            {
                MessageBox.Show("Choose correct category and producer");
                return;
            }
            if (commander.IsInputInvalid(AddNameTB.Text, "Product name"))
            {
                MessageBox.Show($"Product name must not be empty");
                return;
            }
            string integerPartS = AddPriceIntTB.Text.Trim();
            string decimalPartS = AddatePriceDecimalTB.Text.Trim();

            if (!int.TryParse(integerPartS, out int integerPart) || !int.TryParse(decimalPartS, out int decimalPart))
            {
                MessageBox.Show($"Invalid value of price.");
                return;
            }
            string concatPrice = integerPartS + "." + decimalPartS;

            if (!decimal.TryParse(concatPrice, out decimal newPrice))
            {
                MessageBox.Show($"Invalid price");
                return;
            }
            string productName = AddNameTB.Text;
            int    categoryId  = cat.category_id;
            int    producerId  = prod.producer_id;

            if (commander.ExistsInDatabaseByNameCaseSensitive(OSHome.DbSources.Products, productName))
            {
                Products existingProduct = db.Products.Single(x => x.product_name == productName);
                if (existingProduct.price == newPrice && existingProduct.category == categoryId && existingProduct.producer == producerId)
                {
                    MessageBox.Show($"Product with values:\nName: {productName} already exists. (ID = {existingProduct.product_id})");
                    return;
                }
            }
            Products newProduct = new Products();

            newProduct.product_name = productName;
            newProduct.category     = (short)categoryId;
            newProduct.producer     = (short)producerId;
            newProduct.price        = newPrice;
            db.Products.Add(newProduct);
            db.SaveChanges();
            if (category_nameComboBox.Text != string.Empty && newProduct.category == (category_nameComboBox.SelectedItem as Categories).category_id)
            {
                RefillProductsByCatList();
            }
            MessageBox.Show($"Successfully created new product:\nID: {newProduct.product_name}");
            AddCategoriesCB.SelectedIndex = 0;
            AddProducersCB.SelectedIndex  = 0;
            AddNameTB.Text            = "Product name";
            AddPriceIntTB.Text        = "Integer part";
            AddatePriceDecimalTB.Text = "Decimal part";
        }