Exemplo n.º 1
0
        private void Obsluga_AcceptNewOrder()
        {
            var orderDetails = Obsluga_GenerateOrderDetailsFromGridView(obslugaDataGridView.Rows);
            Order newOrder = null;
            try
            {

                AsyncTaskRunner task = new AsyncTaskRunner(
                    new Task(
                        () =>
                        {
                            foreach (var orderDetail in orderDetails)
                            {
                                orderDetail.Product.OnStock -= orderDetail.Quantity;
                            }
                            var employee = UnitOfWork.EmployeeRepository.GetEmployeeById(AptekaGlobals.ActiveEmployeeId);
                            var pharmacy = employee.Pharmacy;// UnitOfWork.PharmacyRepository.GetPharmacyById(AptekaGlobals.ActivePharmacyId);

                            newOrder = new Order(orderDetails)
                                {
                                    Date = DateTime.Now,
                                    Employee = employee,
                                    Pharmacy = pharmacy
                                };
                            UnitOfWork.OrderRepository.Create(newOrder);
                            UnitOfWork.SaveChanges();
                             }),
                        () =>
                        {
                            MessageBox.Show("Zrealizowano zamówienie na kwotę: " + String.Format("{0:N2}", newOrder.CalculatePrice() + " zł"), "Sukces", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            OrderAcceptedEvent.Invoke(this, null);
                            Obsluga_ResetOrder();
                        }
                    );
                task.StartInTransaction();

            }
            catch (Exception)
            {
                MessageBox.Show("Błąd - nie udało się zrealizować zamówienia");
                throw;
            }
        }
Exemplo n.º 2
0
        public OrderResponse PlaceOrder(OrderRequest request)
        {
            if (!_userSignedIn)
                return new OrderResponse(request.Id,false)
                    {
                        Message = "Użytkownik niezalogowany"
                    };
            if (request.ProductIdQuantityDictionary.Keys.Count==0)
                return new OrderResponse(request.Id, false)
                {
                    Message = "Zamówienie nie może być puste"
                };

            try
            {
                Debug.WriteLine("{0} - User {1}: Place Order - begin", DateTime.Now, _signedCustomerId);
                lock (_placeOrderSynchronizationObject)
                {
                    using (var unitOfWork = new AptekaNetUnitOfWork())
                    {
                        var customer = unitOfWork.CustomerRepository.GetCustomerById(_signedCustomerId);
                        var orderDetails = new List<OrderDetail>();
                        Pharmacy pharmacy = null;

                        //Pobranie odpowiednich produktow z bazy danych na podstawie podanych id + sprawdzenie stanu produktu w aptece
                        foreach (var productId in request.ProductIdQuantityDictionary.Keys)
                        {
                            var prod = unitOfWork.ProductRepository.GetProductById(productId);
                            var quantity = request.ProductIdQuantityDictionary[productId];
                            if (prod.OnStock < quantity)
                                return new OrderResponse(request.Id, false)
                                {
                                    Message = String.Format("W aptece nie ma wystarczającej ilości leku: {0}", prod.Medicine.CommercialName)
                                };
                            else
                            {
                                prod.OnStock -= quantity;
                            }

                            orderDetails.Add(new OrderDetail
                            {
                                Product = prod,
                                Quantity = quantity
                            });

                            if (pharmacy != null && prod.Pharmacy != pharmacy)
                                return new OrderResponse(request.Id, false)
                                {
                                    Message = "Produkty nie mogą pochodzić z różnych aptek"
                                };

                            pharmacy = prod.Pharmacy;
                        }

                        var order = new Order
                        {
                            Customer = customer,
                            Date = DateTime.Now,
                            EmployeeId = unitOfWork.EmployeeRepository.Find(e => e.Pharmacy.Id==pharmacy.Id).Id
                        };
                        foreach (var orderDetail in orderDetails)
                        {
                            order.OrderedProducts.Add(orderDetail);
                        }

                        pharmacy.Orders.Add(order);

                        unitOfWork.SaveChanges();

                        Debug.WriteLine("Zamówienie ID: {0}",order.Id);
                    }
                }
                Debug.WriteLine("{0} - User {1}: Place Order - end", DateTime.Now, _signedCustomerId);
                return new OrderResponse(request.Id, true)
                {
                    Message = "Zamówienie dodane pomyślnie"
                };
            }
            catch (Exception e)
            {
                return new OrderResponse(request.Id, false)
                {
                    Message = String.Format("Błąd - {0}",e.Message)
                };
            }
        }