private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            services     = DatabaseAccess.loadServices();
            customers    = DatabaseAccess.loadCustomers();
            cars         = DatabaseAccess.loadCars();
            appointments = DatabaseAccess.loadAppointments();
            warehouse    = DatabaseAccess.loadWarehouse();
            orders       = DatabaseAccess.loadOrders();
            employees    = DatabaseAccess.loadEmployees();
            workplaces   = DatabaseAccess.loadWorkplace();

            string[] separator   = new string[] { ", " };
            string[] neededParts = neededPartsText.Text.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            WarehouseModel part;
            OrdersModel    pendingOrder;
            ServiceModel   service = services.Find(x => x.serviceName == appointmentTypeComboBox.Text);

            DateTime nearestDate      = AppointmentModel.RoundUp(DateTime.Now, TimeSpan.FromMinutes(15));
            bool     nearestDateFound = false;
            List <WarehouseModel>   confirmedNeededParts  = new List <WarehouseModel>();
            List <AppointmentModel> collidingAppointments = new List <AppointmentModel>();
            List <EmployeeModel>    availableEmployees    = new List <EmployeeModel>();
            List <WorkplaceModel>   availableWorkplaces   = new List <WorkplaceModel>();

            PartObject        chosenPart = new PartObject("", PartStatus.Unavailable, 0);
            MessageBoxButtons buttons    = MessageBoxButtons.YesNo;
            string            caption    = "Czy chcesz ją zamówić?";
            DialogResult      result;

            chosenPartsList.Clear();

            //reset employee and workplace
            chosenEmployee  = new EmployeeModel();
            chosenWorkplace = new WorkplaceModel();

            //make appointment after working hours
            if (!AppointmentModel.isWorkDay(nearestDate) || !AppointmentModel.isWorkHour(nearestDate))
            {
                nearestDate = AppointmentModel.changeDateToNextWorkDay(nearestDate);
            }

            if (neededParts.Length != 0)
            {
                foreach (var item in neededParts)
                {
                    part = warehouse.Find(x => x.partName == item); //by names
                    if (part == default)
                    {
                        System.Windows.Forms.MessageBox.Show("Część: " + item + " nie jest znana");
                        chosenPart = new PartObject(item, PartStatus.Unavailable, 0);
                        chosenPartsList.Add(chosenPart);
                        return;
                    }
                    else if (part.stockQuantity == 0)
                    {
                        pendingOrder = orders.Find(x => x.idParts == part.idParts && x.status != "zrealizowane" && x.amount > x.bookedAmount);
                        if (pendingOrder == default)
                        {
                            result = System.Windows.Forms.MessageBox.Show("Brak części: " + item + " w magazynie.", caption, buttons); //rozwinąć ew (przechodzenie do zamówienia części
                            if (result == System.Windows.Forms.DialogResult.Yes)
                            {
                                ManageOrdersWindow objManageOrdersWindow = new ManageOrdersWindow();
                                objManageOrdersWindow.Show();
                                this.Close();
                            }
                            else
                            {
                                chosenPart = new PartObject(item, PartStatus.Unavailable, 0);
                                chosenPartsList.Add(chosenPart);
                                return;
                            }
                        }
                        else
                        {
                            if (nearestDate < pendingOrder.realizationDate)
                            {
                                nearestDate = AppointmentModel.RoundUp(pendingOrder.realizationDate, TimeSpan.FromMinutes(15)); // If parts are ordered take new available nearestDate
                                if (!AppointmentModel.isWorkDay(nearestDate) || !AppointmentModel.isWorkHour(nearestDate))
                                {
                                    nearestDate = AppointmentModel.changeDateToNextWorkDay(nearestDate);
                                }
                                chosenPart = new PartObject(item, PartStatus.Ordered, pendingOrder.amount);
                                orders[orders.FindIndex(x => x.idOrder == pendingOrder.idOrder)].bookedAmount += 1;
                                chosenPartsList.Add(chosenPart);
                            }
                        }
                    }
                    else
                    {
                        chosenPart = new PartObject(item, PartStatus.Available, part.stockQuantity);
                        warehouse[warehouse.FindIndex(x => x.partName == item)].stockQuantity -= 1;
                        chosenPartsList.Add(chosenPart);
                    }

                    confirmedNeededParts.Add(part);
                } //all parts ready
            }

            if (!nearestDateCheckBox.IsChecked.Value)
            {
                if (AppointmentModel.isWorkDay(chosenDatePicker.SelectedDate.Value))
                {
                    if (nearestDate.Date < chosenDatePicker.SelectedDate)
                    {
                        nearestDate = chosenDatePicker.SelectedDate.Value;
                        nearestDate = new DateTime(nearestDate.Year, nearestDate.Month, nearestDate.Day, AppointmentModel.startHour, 0, 0);
                    }
                    else if (nearestDate.Date > chosenDatePicker.SelectedDate)
                    {
                        System.Windows.Forms.MessageBox.Show("Brak części lub zły termin");
                        return;
                    }
                    // if equal take nearestDate
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Warsztat nieczynny w weekendy");
                    return;
                }
            }

            while (!nearestDateFound) //found neares available date
            {
                collidingAppointments = appointments.FindAll(x => (x.date <= nearestDate && x.GetFinishDate() > nearestDate) ||
                                                             (x.date >= nearestDate && x.GetFinishDate() <= AppointmentModel.GetFinishDate(nearestDate, double.Parse(estimatedTimeText.Text))) ||
                                                             (x.date >= nearestDate && x.date <AppointmentModel.GetFinishDate(nearestDate, double.Parse(estimatedTimeText.Text)) && x.GetFinishDate()> AppointmentModel.GetFinishDate(nearestDate, double.Parse(estimatedTimeText.Text))));
                if (collidingAppointments.Count != 0)
                {
                    availableEmployees  = new List <EmployeeModel>(employees);
                    availableWorkplaces = new List <WorkplaceModel>(workplaces);
                    foreach (var item in collidingAppointments) // wolny pracownik i stanowisko
                    {
                        availableEmployees.Remove(availableEmployees.Find(x => x.idEmployee == item.idEmployee));
                        availableWorkplaces.Remove(availableWorkplaces.Find(x => x.idWorkplace == item.idWorkplace));
                    }

                    if (service.serviceCategory == "ogólny")
                    {
                        chosenEmployee  = availableEmployees[0];
                        chosenWorkplace = availableWorkplaces.Find(x => x.workplaceName == service.serviceCategory);
                    }
                    else
                    {
                        chosenEmployee  = availableEmployees.Find(x => x.specialization == service.serviceCategory);
                        chosenWorkplace = availableWorkplaces.Find(x => x.workplaceName == service.serviceCategory);
                    }

                    if (chosenWorkplace == default || chosenEmployee == default)  //sprawidzić działanie default
                    {
                        nearestDate = nearestDate.AddMinutes(15);
                        if (!AppointmentModel.isWorkHour(nearestDate))
                        {
                            if (!nearestDateCheckBox.IsChecked.Value)
                            {
                                System.Windows.Forms.MessageBox.Show("Brak wolnych godzin w wybranym dniu");
                                return;
                            }
                            else
                            {
                                nearestDate = AppointmentModel.changeDateToNextWorkDay(nearestDate);
                            }
                        }
                        //następna iteracja
                    }
                    else
                    {
                        if (nearestDate.Hour == 18)
                        {
                            nearestDate = AppointmentModel.changeDateToNextWorkDay(nearestDate);
                        }
                        dateTextBlock.Content = nearestDate + " - " + AppointmentModel.GetFinishDate(nearestDate, double.Parse(estimatedTimeText.Text));
                        nearestDateFound      = true;
                    }
                }
                else
                {
                    if (nearestDate.Hour == 18)
                    {
                        nearestDate = AppointmentModel.changeDateToNextWorkDay(nearestDate);
                    }
                    dateTextBlock.Content = nearestDate + " - " + AppointmentModel.GetFinishDate(nearestDate, double.Parse(estimatedTimeText.Text));
                    chosenEmployee        = employees.Find(x => x.specialization == service.serviceCategory);
                    chosenWorkplace       = workplaces.Find(x => x.workplaceName == service.serviceCategory);
                    nearestDateFound      = true;
                }
            }
            //sprawdzić czy któraś ścieżka nie przypisuje
            employeeTextBlock.Content  = chosenEmployee.firstName + " " + chosenEmployee.lastName;
            workplaceTextBlock.Content = chosenWorkplace.workplaceName + " stanowisko: " + chosenWorkplace.idWorkplace;
            //save chosen date
            chosenDateTime = nearestDate;

            //calculate price
            double price = 0;

            foreach (var item in confirmedNeededParts)
            {
                price += item.price;
            }
            price += service.price * double.Parse(estimatedTimeText.Text);
            priceTextBlock.Content = price;
        }