Esempio n. 1
0
 public bool CreateOrderDetail(Order_Details order_Details)
 {
     try
     {
         var product      = RepositoryContainer.GetInstance <ProductsRepository>().GetByProduct_ID(order_Details.Product_ID);
         var stock        = RepositoryContainer.GetInstance <StockRepository>();
         var productstock = stock.GetQuantityByPK(order_Details.Product_ID, order_Details.size, order_Details.Color);
         if (stock.CheckInventory(order_Details.Product_ID, order_Details.size, order_Details.Color, order_Details.Quantity))
         {
             stock.Update(new Stock
             {
                 Product_ID = order_Details.Product_ID,
                 Color      = order_Details.Color,
                 Quantity   = productstock - order_Details.Quantity,
                 Size       = order_Details.size
             });
         }
         else
         {
             throw new Exception();
         }
         order_Details.Price = product.UnitPrice * order_Details.Quantity;
         Order_DetailsRepository.Create(order_Details);
         var o = RepositoryContainer.GetInstance <OrdersRepository>();
         o.UpdateTotalMoney(order_Details.Order_ID);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 2
0
        public void Order_DetailsRepositoryTests_Create()
        {
            SqlConnection           connection = new SqlConnection();
            Order_DetailsRepository repository = new Order_DetailsRepository();
            var model = new Order_Details
            {
                Order_ID   = 28,
                Product_ID = 2,
                Quantity   = 1,
                Price      = 150,
                size       = "M"
            };

            repository.Create(model);
        }
        public string CreateOrder(string Account, string Pay, string Transport, decimal TranMoney)
        {
            var cart_R          = RepositoryContainer.GetInstance <ShoppingCartRepository>();
            var orders_R        = new OrdersRepository();
            var order_Details_R = new Order_DetailsRepository();
            var stock_R         = new StockRepository();
            var products_R      = new ProductsRepository();

            var           now_time   = DateTime.Now;
            SqlConnection connection = new SqlConnection(SqlConnect.str);

            connection.Open();
            var transaction = connection.BeginTransaction();

            try
            {
                orders_R.Create(connection, new Orders
                {
                    Account     = Account,
                    Order_Check = 0,
                    Order_Date  = now_time,
                    Pay         = Pay,
                    TranMoney   = TranMoney,
                    Transport   = Transport,
                }, transaction);

                string  errorMessage = "";
                var     order        = orders_R.GetLatestByAccount(connection, Account, transaction);
                var     items        = cart_R.GetByAccount(connection, Account, transaction);
                decimal totalmoney   = 0;
                foreach (var item in items)
                {
                    if (stock_R.CheckInventory(item.Product_ID, item.size, item.Color, item.Quantity) == false)
                    {
                        errorMessage += "產品 :" + products_R.GetByProduct_ID(item.Product_ID).Product_Name + " 庫存不足\n";
                    }
                    else
                    {
                        totalmoney += item.Quantity * products_R.GetByProduct_ID(item.Product_ID).UnitPrice;
                        order_Details_R.Create(connection, new Order_Details()
                        {
                            Order_ID   = order.Order_ID,
                            Product_ID = item.Product_ID,
                            Quantity   = (short)item.Quantity,
                            size       = item.size,
                            Price      = item.Quantity * products_R.GetByProduct_ID(item.Product_ID).UnitPrice,
                            Color      = item.Color
                        }, transaction);
                        stock_R.Update(connection, new Stock()
                        {
                            Product_ID = item.Product_ID,
                            Size       = item.size,
                            Quantity   = stock_R.GetByPK(item.Product_ID, item.size, item.Color).Quantity - item.Quantity,
                            Color      = item.Color
                        }, transaction);
                    }
                }
                if (errorMessage.Length <= 1)
                {
                    cart_R.DeleteByAccount(Account);
                    orders_R.Update(order.Order_ID, totalmoney + TranMoney, connection, transaction);
                    transaction.Commit();
                    connection.Close();
                    return("新訂單");
                }
                else
                {
                    throw new Exception(errorMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                transaction.Rollback();
                return(ex.Message);
            }
        }