Ejemplo n.º 1
0
        public async Task <IActionResult> Create(int?id, [Bind("OrderId,OrderNumber,OrderDate,RealisationDate,InvoiceId,UserId,CustomerId,AddressId")] Orders orders)
        {
            if (HttpContext.Session.GetString("EditOrders") == "false")
            {
                return(RedirectToAction("Index", "Home"));
            }
            var orderPositions = await _context.OrderPositions
                                 .Include(op => op.Product)
                                 .ThenInclude(p => p.TaxRate)
                                 .Where(op => op.OrderId == orders.OrderId)
                                 .ToListAsync();

            if (orderPositions.Count == 0)
            {
                ViewBag.ErrorMessage = "Nie możesz zapisać zamówienia bez pozycji!";
            }
            if (orderPositions.Count > 0)
            {
                Orders orderToAdd    = new Orders();
                var    lastOrderInDB = await _context.Orders
                                       .Where(o => !o.OrderNumber.Contains("R"))
                                       .OrderByDescending(o => o.OrderId)
                                       .FirstOrDefaultAsync();

                string month = DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo);
                string year  = DateTime.Now.ToString("yy", DateTimeFormatInfo.InvariantInfo);
                string newOrderNumber;
                if (lastOrderInDB == null)
                {
                    newOrderNumber = "ZAM/0001/" + month + "/" + year;
                }
                else
                {
                    string[] lastOrderNumberSplit = lastOrderInDB.OrderNumber.Split('/');
                    int      numberInt;
                    DateTime lastOrderDate = lastOrderInDB.OrderDate;
                    DateTime todayDate     = DateTime.Now;
                    if (lastOrderDate.Month == todayDate.Month && lastOrderDate.Year == todayDate.Year)
                    {
                        numberInt = int.Parse(lastOrderNumberSplit[1]);
                    }
                    else
                    {
                        numberInt = 1;
                    }

                    string numberString = "" + (numberInt + 1);
                    while (numberString.Length < 4)
                    {
                        numberString = "0" + numberString;
                    }
                    newOrderNumber = "ZAM/" + numberString + "/" + month + "/" + year;
                }
                orderToAdd.OrderNumber = newOrderNumber;
                orderToAdd.OrderDate   = DateTime.Now;
                orderToAdd.UserId      = orders.UserId;
                orderToAdd.CustomerId  = orders.CustomerId;
                orderToAdd.AddressId   = orders.AddressId;

                _context.Add(orderToAdd);
                await _context.SaveChangesAsync();

                List <OrderPositions> newOrderPositions = new List <OrderPositions>();
                orderPositions.ForEach(op =>
                {
                    var newPosition       = new OrderPositions();
                    newPosition.OrderId   = orderToAdd.OrderId;
                    newPosition.ProductId = op.ProductId;
                    newPosition.Count     = op.Count;
                    newPosition.Discount  = op.Discount;
                    newOrderPositions.Add(newPosition);
                });
                _context.RemoveRange(orderPositions);
                await _context.SaveChangesAsync();

                _context.AddRange(newOrderPositions);
                await _context.SaveChangesAsync();

                _context.Remove(orders);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            Orders order;

            order = await _context.Orders
                    .Include(o => o.OrderPositions)
                    .ThenInclude(op => op.Product)
                    .ThenInclude(p => p.TaxRate)
                    .FirstOrDefaultAsync(o => o.OrderId == orders.OrderId);

            var customer = await _context.Customers.Include(c => c.Addresses).FirstOrDefaultAsync(c => c.CustomerId == order.CustomerId);

            if (customer == null)
            {
                return(NotFound());
            }

            ViewBag.Customer      = customer;
            ViewData["AddressId"] = new SelectList(customer.Addresses, "AddressId", "FullAddress");
            return(View(order));
        }