예제 #1
0
        public async Task <IActionResult> Create(BookingViewModel model)
        {
            if (!HasPermission("CREATE_BOOKINGS"))
            {
                return(Unauthorized());
            }

            if (ModelState.IsValid)
            {
                var booking = model.ToEntity();

                booking.CreatedAt = DateTime.Now;

                int userId = GetCurrentUserId();

                booking.CreatedByUserId = userId;

                if (booking.Invoice != null)
                {
                    booking.Invoice.Status     = InvoiceStatus.Pending;
                    booking.Invoice.AmountDue  = booking.Invoice.Total;
                    booking.Invoice.AmountPaid = 0;
                }

                if (booking.Package != null)
                {
                    booking.Package.TrackingNumber = DateTime.Now.Ticks.ToString();

                    if (booking.PickupRequired)
                    {
                        booking.Package.Status = PackageStatus.PendingPickup;
                    }
                    else
                    {
                        booking.Package.Status = PackageStatus.ReceivedByLocation;
                    }
                }

                _context.Bookings.Add(booking);

                await _context.SaveChangesAsync();

                if (booking.Invoice.BillingMode == BillingMode.PayNow)
                {
                    return(RedirectToAction("Create", "Payments", new { id = booking.Id }));
                }

                return(RedirectToAction("Details", "Bookings", new { id = booking.Id }));
            }

            ViewData["LocationId"]    = new SelectList(_context.Locations, "Id", "Name");
            ViewData["PackageTypeId"] = new SelectList(_context.PackageTypes, "Id", "Name");
            ViewData["ServiceId"]     = new SelectList(_context.Services, "Id", "Name", model.ServiceId);

            return(View(model));
        }