예제 #1
0
        public IActionResult Create([FromBody] OrderLinesDTO orderlineDTO)
        {
            OrderLines orderline = new OrderLines();

            orderline.Price       = orderlineDTO.Price;
            orderline.OrderIdFk   = orderlineDTO.OrderIdFk;
            orderline.Quantity    = orderlineDTO.Quantity;
            orderline.ProductIdFk = orderlineDTO.ProductIdFk;
            orderline.TotalPrice  = orderlineDTO.TotalPrice;

            var orderlineEntity = _orderlineService.Create(orderline);
            var orderlines      = _mapper.Map <OrderLinesDTO>(orderlineEntity);

            return(Ok(orderline));
        }
        public async Task <ActionResult> Confirm()
        {
            var userId    = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var userEmail = User.FindFirstValue(ClaimTypes.Email);

            ShoppingCartVM cart;

            cart = HttpContext.Session.GetObject <ShoppingCartVM>(KEY_SHOPPINGCART);
            if (cart == null)
            {
                return(NotFound());
            }

            var newOrder = new Order
            {
                Customer = Guid.Parse(userId)
            };
            var newOrderLines = new List <NewOrderLineVM>();

            foreach (var item in cart.Cart)
            {
                if (item.MatchTicket.HasValue)
                {
                    // New ticket
                    var msl = await _mslService.GetById(item.MatchTicket.Value);

                    if (DateTime.Now >= msl.Match.Start || DateTime.Now.AddMonths(1) < msl.Match.Start.Date)
                    {
                        TempData["CartError"] = "Probleem met winkelmandje!";
                        cart.Cart.Remove(item);
                        return(RedirectToAction(nameof(Cart)));
                    }

                    var previousMatchTickets = await _ticketService.GetByCustomerAndMatch(Guid.Parse(userId), msl.MatchId);

                    int prevMatchTicketCount = 0;
                    if (previousMatchTickets != null)
                    {
                        prevMatchTicketCount = previousMatchTickets.Count();
                    }

                    var totalTicketCount = cart.Cart.Where(x => x.ForMatchId == item.ForMatchId).Select(x => x.Count).Sum() + prevMatchTicketCount;

                    if (item.Count <= msl.RemainingSeats && totalTicketCount <= 10)
                    {
                        for (int i = 0; i < item.Count; i++)
                        {
                            newOrderLines.Add(new NewOrderLineVM
                            {
                                MSL   = item.MatchTicket,
                                Match = msl.MatchId
                            });
                        }
                    }
                    else
                    {
                        TempData["CartError"] = $"Maximum 10 tickets per match toegelaten! U bestelde al {prevMatchTicketCount} tickets voor {msl.Match.TeamHomeNavigation.Name} - {msl.Match.TeamAwayNavigation.Name} op {msl.Match.Start.ToShortDateString()}! Gelieve uw winkelmandje aan te passen";
                        return(RedirectToAction(nameof(Cart)));
                    }
                }
                else if (item.TeamSubscription.HasValue)
                {
                    // New subscription
                    newOrderLines.Add(new NewOrderLineVM
                    {
                        TSL = item.TeamSubscription
                    });
                }
            }

            if (newOrderLines.Count < 1)
            {
                TempData["CartError"] = "Probleem met winkelmandje!";
                return(RedirectToAction(nameof(Cart)));
            }

            await _orderService.Create(newOrder);

            List <string> orderLineEmail = new List <string>();

            foreach (var newOL in newOrderLines)
            {
                if (newOL.MSL.HasValue && newOL.Match.HasValue)
                {
                    // Is ticket
                    var msl = await _mslService.GetById(newOL.MSL.Value);

                    msl.RemainingSeats--;
                    await _mslService.Update(msl);

                    var newTicket = new Ticket
                    {
                        MatchStadiumLocationId = newOL.MSL.Value,
                        MatchId  = newOL.Match.Value,
                        Customer = Guid.Parse(userId)
                    };
                    await _ticketService.Create(newTicket);

                    await _orderLineService.Create(new OrderLine
                    {
                        TicketId = newTicket.Id,
                        OrderId  = newOrder.Id,
                        Price    = msl.Price
                    });

                    orderLineEmail.Add($"<p>Ticket: {newTicket.Code}</p> <p>Match: {msl.Match.TeamHomeNavigation.Name} - {msl.Match.TeamAwayNavigation.Name}</p> <p>Start: {msl.Match.Start.ToShortDateString()} {msl.Match.Start.ToShortTimeString()}</p> <p>Plaats:  {msl.StadiumLocation.Location.Location}</p>");
                }
                else if (newOL.TSL.HasValue)
                {
                    // Is subscription

                    var tsl = await _tclService.GetById(newOL.TSL.Value);

                    var matches = await _matchService.GetByCompetitionTeamId(tsl.CompetitionId, tsl.TeamId);

                    var msls = await _mslService.GetMslByMatches(matches);

                    foreach (var msl in msls.Where(x => x.LocationId == tsl.LocationId).ToList())
                    {
                        if (msl.RemainingSeats < 1)
                        {
                            TempData["CartError"] = $"Kies een andere zitplaats aub. De zitplaatsen voor {tsl.Location.Location} zijn uitverkocht";
                            return(RedirectToAction(nameof(Cart)));
                        }
                    }

                    var newSubscription = new Subscription
                    {
                        TeamCompetitionLocation = tsl,
                        Customer = Guid.Parse(userId)
                    };

                    await _subscriptionService.Create(newSubscription);

                    await _orderLineService.Create(new OrderLine
                    {
                        SubscriptionId = newSubscription.Id,
                        OrderId        = newOrder.Id,
                        Price          = tsl.Price
                    });

                    foreach (var msl in msls.Where(x => x.LocationId == tsl.LocationId).ToList())
                    {
                        msl.RemainingSeats--;
                        await _mslService.Update(msl);
                    }

                    orderLineEmail.Add($"<p>Ticket: {newSubscription.Code}</p> <p>Subscrition: {tsl.Team.Name} from {tsl.Competition.StartDate} until {tsl.Competition.EndDate}</p> <p>Plaats:  {tsl.Location.Location}</p>");
                }
            }

            await _emailSender.SendEmailOrderAsync(userEmail, $"Ticketverkoop: order confirmation {newOrder.Id}", $"<h1> Order confirmation Order Id: {newOrder.Id} </h1>", orderLineEmail.ToArray());

            TempData["LatestOrderId"] = newOrder.Id;
            cart.Cart = new List <CartVM>();
            HttpContext.Session.SetObject(KEY_SHOPPINGCART, cart);
            return(RedirectToAction("Details", "Orders", new { id = newOrder.Id }));
        }