Ejemplo n.º 1
0
        private void RemoveClientButton_Click(object sender, EventArgs e)
        {
            int clientId = GetSelectedClientDbId();

            using (var db = new RentalEntities())
            {
                var loansEntities = from poz in db.PozycjeWypozyczenia
                                    join wyp in db.Wypozyczenia on poz.IDwypozyczenia equals wyp.IDwypozyczenia
                                    where wyp.IDklienta == clientId
                                    select poz;

                foreach (var le in loansEntities)
                {
                    db.PozycjeWypozyczenia.Remove(le);
                }

                var loans = db.Wypozyczenia.Where(l => l.IDklienta == clientId).Select(l => l);

                foreach (var l in loans)
                {
                    db.Wypozyczenia.Remove(l);
                }

                var client = db.Klienci.Where(c => c.IDklienta == clientId).FirstOrDefault();
                db.Klienci.Remove(client);

                db.SaveChanges();
            }

            UpdateClientsList();
            LoansGrid.Rows.Clear();
            ToolsList.Rows.Clear();
            LoansGrid.Enabled = ToolsList.Visible = false;
        }
Ejemplo n.º 2
0
 private void AddClientButton_Click(object sender, EventArgs e)
 {
     using (var db = new RentalEntities())
     {
         var client = new Klienci()
         {
             Imie     = FirstNameBox.Text,
             Nazwisko = LastNameBox.Text
         };
         db.Klienci.Add(client);
         db.SaveChanges();
         Close();
     }
 }
Ejemplo n.º 3
0
        private void LoansGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (LoansGrid.SelectedCells[0].Value.ToString() == "Podgląd" ||
                LoansGrid.SelectedCells[0].Value.ToString() == "Zwrot")
            {
                int    clientId       = GetSelectedClientDbId();
                string selectedLoanId = LoansGrid.SelectedCells[0].RowIndex.ToString();
                int    loanId         = GetLoanDbId();

                if (LoansGrid.SelectedCells[0].Value.ToString() == "Podgląd")
                {
                    ToolsList.Rows.Clear();

                    using (var db = new RentalEntities())
                    {
                        var tools = from wyp in db.Wypozyczenia
                                    join poz in db.PozycjeWypozyczenia on wyp.IDwypozyczenia equals poz.IDwypozyczenia
                                    join narz in db.Narzedzia on poz.IDnarzedzia equals narz.IDnarzedzia
                                    where wyp.IDklienta == clientId && wyp.IDwypozyczenia == loanId
                                    select narz.NazwaNarzedzia;

                        if (tools.Any())
                        {
                            foreach (var toolName in tools)
                            {
                                ToolsList.Rows.Add(toolName);
                            }
                            ToolsList.Visible = true;
                        }
                    }
                }
                if (LoansGrid.SelectedCells[0].Value.ToString() == "Zwrot")
                {
                    using (var db = new RentalEntities())
                    {
                        var loan = db.Wypozyczenia.Where(l => l.IDwypozyczenia == loanId)
                                   .Select(l => l).SingleOrDefault();
                        loan.DataZwrotu = DateTime.Now;
                        db.SaveChanges();
                        ClientsList_SelectedIndexChanged(sender, e);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void LoanButton_Click(object sender, EventArgs e)
        {
            double advanceAmount = double.Parse(AdvanceAmount.Value.ToString());
            double discount;
            bool   validDiscount = true;

            foreach (DataGridViewRow row in Cart.Rows)
            {
                if (!double.TryParse(row.Cells[2].Value.ToString(), out discount))
                {
                    validDiscount = false;
                    break;
                }
                else
                {
                    if (discount < 0 || discount > 99)
                    {
                        validDiscount = false;
                        break;
                    }
                }
            }
            double toPay = double.Parse(PriceSum.Text) - double.Parse(DiscountSum.Text);

            if (advanceAmount < 0 || advanceAmount >= toPay)
            {
                CartError.SetError(LoanButton, $"Wartość zaliczki jest wyższa lub równa wartości koszyka");
            }

            else if (!validDiscount)
            {
                CartError.SetError(LoanButton, $"Wartość rabatu jest " +
                                   $" nieprawidłowa, podaj wartość od 0 do 99.");
            }
            else
            {
                using (var db = new RentalEntities())
                {
                    var loan = new Wypozyczenia()
                    {
                        IDklienta        = selectedUserId,
                        DataWypozyczenia = DateTime.Now,
                        Zaliczka         = advanceAmount
                    };
                    db.Wypozyczenia.Add(loan);
                    db.SaveChanges();

                    int                      loanID         = GetLatestLoanId();
                    StringBuilder            loanNodeValues = new StringBuilder();
                    Dictionary <string, int> toolDuplicates = new Dictionary <string, int>();

                    foreach (DataGridViewRow row in Cart.Rows)
                    {
                        if (toolDuplicates.ContainsKey(row.Cells[0].Value.ToString()))
                        {
                            toolDuplicates[row.Cells[0].Value.ToString()] += 1;
                        }

                        else
                        {
                            toolDuplicates.Add(row.Cells[0].Value.ToString(), 1);
                        }
                    }

                    foreach (KeyValuePair <string, int> tool in toolDuplicates)
                    {
                        var toolsIds = db.Narzedzia.Where(t => t.Dostepnosc == true && t.NazwaNarzedzia
                                                          == tool.Key).Take(tool.Value).Select(t => t.IDnarzedzia);
                        var toolsIdsList = toolsIds.ToList();

                        int currentToolId = 0, cartRowsLength = Cart.Rows.Count;
                        for (int i = 0; i < cartRowsLength; i++)
                        {
                            if (Cart.Rows[i].Cells[0].Value.ToString() == tool.Key)
                            {
                                DataGridViewRow row        = Cart.Rows[i];
                                var             loanEntity = new PozycjeWypozyczenia()
                                {
                                    IDwypozyczenia = loanID,
                                    IDnarzedzia    = toolsIdsList[currentToolId],
                                    Rabat          = double.Parse(row.Cells[2].Value.ToString()) / 100
                                };
                                db.PozycjeWypozyczenia.Add(loanEntity);
                                currentToolId++;
                            }
                        }
                    }
                    db.SaveChanges();
                    Close();
                }
            }
        }