Ejemplo n.º 1
0
        public Order ConfirmOrder(Entities.Order pOrder)

        //checks if the order is possible
        {
            using (TransactionScope lScope = new TransactionScope())
            {
                //LoadBookStocks(pOrder);
                //MarkAppropriateUnchangedAssociations(pOrder);

                using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer())
                {
                    try
                    {
                        pOrder.OrderNumber   = Guid.NewGuid();
                        pOrder.Store         = "OnLine";
                        pOrder.ProcessStatus = 0;

                        // Book objects in pOrder are missing the link to their Stock tuple (and the Stock GUID field)
                        // so fix up the 'books' in the order with well-formed 'books' with 1:1 links to Stock tuples
                        foreach (OrderItem lOrderItem in pOrder.OrderItems)
                        {
                            int bookId = lOrderItem.Book.Id;
                            lOrderItem.Book = lContainer.Books.Where(book => bookId == book.Id).First();
                            System.Guid stockId = lOrderItem.Book.Stock.Id;
                            lOrderItem.Book.Stock = lContainer.Stocks.Where(stock => stockId == stock.Id).First();
                        }

                        // confirm the order can be completed and from which warehouses
                        int[][] confirmedOrders = ConfirmOrderWarehouseLogic(pOrder);
                        Debug.WriteLine(pOrder.ProcessStatus);
                        // an error has occured when confirming the order
                        if (confirmedOrders[0][0] == -1)
                        {
                            SendOrderFailedConfirmation(pOrder);
                            pOrder.ProcessStatus = 1;
                            Debug.WriteLine(pOrder.ProcessStatus);
                            return(pOrder);
                        }
                        Debug.WriteLine(pOrder.ProcessStatus);
                        // and update the stock levels
                        try
                        {
                            pOrder.UpdateStockLevels();
                        }
                        catch (Exception)
                        {
                            pOrder.ProcessStatus = 1;
                            return(pOrder);
                        }

                        // add the modified Order tree to the Container (in Changed state)
                        lContainer.Orders.Add(pOrder);

                        // ask the Bank service to transfer fundss
                        lContainer.SaveChanges();

                        //TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0, pOrder.OrderNumber.ToString());
                        try
                        {
                            TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0, pOrder.Id.ToString(), pOrder.Customer.Email);
                        }
                        catch (EndpointNotFoundException)
                        {
                            pOrder.ProcessStatus = 2;
                            return(pOrder);
                        }

                        // and save the order
                        lContainer.SaveChanges();
                        lScope.Complete();
                    }
                    catch (Exception lException)
                    {
                        try
                        {
                            SendOrderErrorMessage(pOrder, lException);
                        }
                        catch
                        {
                            Debug.WriteLine("Email process is off. Switch on please in order to maintain communication with BookStore");
                        }
                    }
                }
            }
            return(pOrder);
        }