public async Task <ActionResult> PropertyOrder(Model.Models.Order order)
        {
            var listing = await _listingService.FindAsync(order.ListingID);

            var ordersListing = await _orderService.Query(x => x.ListingID == order.ListingID).SelectAsync();

            if (order.FromDate == order.ToDate)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[You cant book just to one day, minimun two day.]]]";

                return(RedirectToAction("Listing", "Manage", new { id = order.ListingID }));
            }

            if (listing == null)
            {
                return(new HttpNotFoundResult());
            }

            // Redirect if not authenticated
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account", new { ReturnUrl = Url.Action("Listing", "Manage", new { id = order.ListingID }) }));
            }

            var userCurrent = User.Identity.User();

            //validar que los dias no esten reservados
            List <DateTime> FechasCocinadas = new List <DateTime>();

            for (DateTime date = order.FromDate.Value; date <= order.ToDate.Value; date = date.Date.AddDays(1))
            {
                FechasCocinadas.Add(date);
            }
            foreach (Model.Models.Order ordenesArrendadas in ordersListing.Where(x => x.Status != (int)Enum_OrderStatus.Cancelled))
            {
                for (DateTime date = ordenesArrendadas.FromDate.Value; date < ordenesArrendadas.ToDate.Value; date = date.Date.AddDays(1))
                {
                    if (FechasCocinadas.Contains(date))
                    {
                        TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                        TempData[TempDataKeys.UserMessage]           = "[[[You can not book with these selected dates!]]]";
                        return(RedirectToAction("Listing", "Manage", new { id = listing.ID }));
                    }
                }
            }

            // Check if payment method is setup on user or the platform
            var descriptors = _pluginFinder.GetPluginDescriptors <IHookPlugin>(LoadPluginsMode.InstalledOnly, "Payment").Where(x => x.Enabled);

            if (descriptors.Count() == 0)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The provider has not setup the payment option yet, please contact the provider.]]]";

                return(RedirectToAction("Listing", "Manage", new { id = order.ListingID }));
            }



            if (order.ID == 0)
            {
                order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                order.Created       = DateTime.Now;
                order.Modified      = DateTime.Now;
                order.Status        = (int)Enum_OrderStatus.Pending;
                order.UserProvider  = listing.UserID;
                order.UserReceiver  = userCurrent.Id;
                order.ListingTypeID = order.ListingTypeID;
                order.Currency      = listing.Currency;
                order.OrderType     = 2;
                if (order.ToDate.HasValue && order.FromDate.HasValue)
                {
                    order.Description = HttpContext.ParseAndTranslate(
                        string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                      listing.Title,
                                      listing.ID,
                                      order.FromDate.Value.ToShortDateString(),
                                      order.ToDate.Value.ToShortDateString()));

                    order.Quantity = order.ToDate.Value.Date.AddDays(1).Subtract(order.FromDate.Value.Date).Days;
                    order.Price    = 0;
                }
                else if (order.Quantity.HasValue)
                {
                    order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                    order.Quantity    = order.Quantity.Value;
                    order.Price       = 0;
                }
                else
                {
                    // Default
                    order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                    order.Quantity    = 1;
                    order.Price       = 0;
                }
                _orderService.Insert(order);
            }

            await _unitOfWorkAsync.SaveChangesAsync();

            //Envio de correo a propietario
            var emailTemplateQuery = await _emailTemplateService.Query(x => x.Slug.ToLower() == "bloqueopropietario").SelectAsync();

            var emailTemplate = emailTemplateQuery.Single();

            dynamic email = new Postal.Email("Email");

            email.To       = userCurrent.Email;
            email.From     = CacheHelper.Settings.EmailAddress;
            email.Subject  = emailTemplate.Subject;
            email.Body     = emailTemplate.Body;
            email.Id       = order.ListingID;
            email.Name     = userCurrent.FullName;
            email.FromDate = order.FromDate.Value.ToShortDateString();
            email.ToDate   = order.ToDate.Value.ToShortDateString();
            EmailHelper.SendEmail(email);

            ClearCache();
            TempData[TempDataKeys.UserMessage] = string.Format("[[[Has bloqueado correctamenter tu propiedad entre las fechas {0} y {1}]]]", order.FromDate.Value.ToString("dd-MM-yyyy"), order.ToDate.Value.ToString("dd-MM-yyyy"));
            return(RedirectToAction("Listing", "Manage", new { id = listing.ID }));
        }
        public async Task <ActionResult> Order(Model.Models.Order order)
        {
            var listing = await _listingService.FindAsync(order.ListingID);

            var ordersListing = await _orderService.Query(x => x.ListingID == order.ListingID).SelectAsync();

            if ((order.ToDate.Value - order.FromDate.Value).Days < 2)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The minium quantity for order is two nights]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }
            if (order.ToDate < order.FromDate)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The date of check out can't be less than the check in date]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }
            if ((order.Children + order.Adults) > listing.Max_Capacity)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = string.Format("La maxima capacidad de pasajeros es de  {0}", listing.Max_Capacity);

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }

            if (listing == null)
            {
                return(new HttpNotFoundResult());
            }

            // Redirect if not authenticated
            if (!User.Identity.IsAuthenticated)
            {
                if (Session["fechas"] != null)
                {
                    SearchListingModel fecha = (SearchListingModel)Session["fechas"];
                    fecha.FromDate = order.FromDate;
                    fecha.ToDate   = order.ToDate;

                    fecha.Niños      = order.Children;
                    fecha.Passengers = order.Adults;

                    Session["fechas"] = fecha;
                }
                return(RedirectToAction("Login", "Account", new { ReturnUrl = Url.Action("Listing", "Listing", new { id = order.ListingID }), FromDate = order.FromDate, ToDate = order.ToDate, Adults = order.Adults, Children = order.Children }));
            }
            var userCurrent = User.Identity.User();

            //validar que los dias no esten reservados
            List <DateTime> FechasCocinadas = new List <DateTime>();

            for (DateTime date = order.FromDate.Value; date < order.ToDate.Value; date = date.Date.AddDays(1))
            {
                FechasCocinadas.Add(date);
            }
            foreach (Model.Models.Order ordenesArrendadas in ordersListing.Where(x => x.Status != (int)Enum_OrderStatus.Cancelled))
            {
                for (DateTime date = ordenesArrendadas.FromDate.Value; date < ordenesArrendadas.ToDate.Value; date = date.Date.AddDays(1))
                {
                    if (FechasCocinadas.Contains(date))
                    {
                        TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                        TempData[TempDataKeys.UserMessage]           = "[[[You can not book with these selected dates!]]]";
                        return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
                    }
                }
            }

            // Check if payment method is setup on user or the platform
            var descriptors = _pluginFinder.GetPluginDescriptors <IHookPlugin>(LoadPluginsMode.InstalledOnly, "Payment").Where(x => x.Enabled);

            if (descriptors.Count() == 0)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The provider has not setup the payment option yet, please contact the provider.]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }

            if (!User.IsInRole("Administrator"))
            {
                if (listing.UserID != userCurrent.Id)
                {
                    //foreach (var descriptor in descriptors)
                    //{
                    //    var controllerType = descriptor.Instance<IHookPlugin>().GetControllerType();
                    //    var controller = ContainerManager.GetConfiguredContainer().Resolve(controllerType) as IPaymentController;

                    //    if (!controller.HasPaymentMethod(listing.UserID))
                    //    {
                    //        TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                    //        TempData[TempDataKeys.UserMessage] = string.Format("[[[The provider has not setup the payment option for {0} yet, please contact the provider.]]]", descriptor.FriendlyName);

                    //        return RedirectToAction("Listing", "Listing", new { id = order.ListingID });
                    //    }
                    //}
                    if (order.ID == 0)
                    {
                        order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                        order.Created       = DateTime.Now;
                        order.Modified      = DateTime.Now;
                        order.Status        = (int)Enum_OrderStatus.Created;
                        order.UserProvider  = listing.UserID;
                        order.UserReceiver  = userCurrent.Id;
                        order.ListingTypeID = order.ListingTypeID;
                        order.Currency      = listing.Currency;
                        order.OrderType     = 3;
                        order.Price         = 0;
                        order.Quantity      = 0;
                        var listingPrice = await _listingPriceService.Query(x => x.ListingID == order.ListingID).SelectAsync();

                        var precioEnero   = listingPrice.FirstOrDefault(x => x.FromDate.Month.Equals(01));
                        var precioFebrero = listingPrice.FirstOrDefault(x => x.FromDate.Month.Equals(02));

                        for (DateTime date = order.FromDate.Value; date <= order.ToDate.Value.AddDays(-1); date = date.Date.AddDays(1))
                        {
                            if (date >= precioEnero.FromDate && date <= precioEnero.ToDate)
                            {
                                order.Price    = order.Price + precioEnero.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                            else if (date >= precioFebrero.FromDate && date <= precioFebrero.ToDate)
                            {
                                order.Price    = order.Price + precioFebrero.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                            else
                            {
                                order.Price    = order.Price + listing.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                        }

                        var servicio = order.Price * 0.04;
                        order.Total = order.Price + listing.CleanlinessPrice + servicio;

                        order.Description = HttpContext.ParseAndTranslate(
                            string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                          listing.Title,
                                          listing.ID,
                                          order.FromDate.Value.ToShortDateString(),
                                          order.ToDate.Value.ToShortDateString()));

                        _orderService.Insert(order);

                        var provider = await _aspNetUserService.FindAsync(order.UserProvider);

                        //await EnviarCorreo(confirmacion, provider.Email);
                    }
                    await _unitOfWorkAsync.SaveChangesAsync();

                    ClearCache();
                    return(RedirectToAction("ConfirmOrder", "Payment", new { id = order.ID }));
                }
                else
                {
                    order.OrderType = 2;
                    if (order.ID == 0)
                    {
                        order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                        order.Created       = DateTime.Now;
                        order.Modified      = DateTime.Now;
                        order.Status        = (int)Enum_OrderStatus.Pending;
                        order.UserProvider  = listing.UserID;
                        order.UserReceiver  = userCurrent.Id;
                        order.ListingTypeID = order.ListingTypeID;
                        order.Currency      = listing.Currency;

                        if (order.ToDate.HasValue && order.FromDate.HasValue)
                        {
                            order.Description = HttpContext.ParseAndTranslate(
                                string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                              listing.Title,
                                              listing.ID,
                                              order.FromDate.Value.ToShortDateString(),
                                              order.ToDate.Value.ToShortDateString()));

                            order.Quantity = order.ToDate.Value.Subtract(order.FromDate.Value.Date).Days;
                            order.Price    = 0;
                        }
                        else if (order.Quantity.HasValue)
                        {
                            order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                            order.Quantity    = order.Quantity.Value - 1;
                            order.Price       = 0;
                        }
                        else
                        {
                            // Default
                            order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                            order.Quantity    = 1;
                            order.Price       = 0;
                        }

                        _orderService.Insert(order);
                    }

                    await _unitOfWorkAsync.SaveChangesAsync();

                    ClearCache();
                    TempData[TempDataKeys.UserMessage] = string.Format("[[[Has bloqueado correctamenter tu propiedad entre las fechas {0} y {1}]]]", order.FromDate.Value.ToString("dd-MM-yyyy"), order.ToDate.Value.ToString("dd-MM-yyyy"));
                    return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
                }
            }
            else
            {
                order.OrderType = 1;
                if (order.ID == 0)
                {
                    order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                    order.Created       = DateTime.Now;
                    order.Modified      = DateTime.Now;
                    order.Status        = (int)Enum_OrderStatus.Pending;
                    order.UserProvider  = listing.UserID;
                    order.UserReceiver  = userCurrent.Id;
                    order.ListingTypeID = order.ListingTypeID;
                    order.Currency      = listing.Currency;

                    if (order.ToDate.HasValue && order.FromDate.HasValue)
                    {
                        order.Description = HttpContext.ParseAndTranslate(
                            string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                          listing.Title,
                                          listing.ID,
                                          order.FromDate.Value.ToShortDateString(),
                                          order.ToDate.Value.ToShortDateString()));

                        order.Quantity = order.ToDate.Value.Subtract(order.FromDate.Value.Date).Days;
                        order.Price    = 0;
                    }
                    else if (order.Quantity.HasValue)
                    {
                        order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                        order.Quantity    = order.Quantity.Value - 1;
                        order.Price       = 0;
                    }
                    else
                    {
                        // Default
                        order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                        order.Quantity    = 1;
                        order.Price       = 0;
                    }

                    _orderService.Insert(order);
                }

                await _unitOfWorkAsync.SaveChangesAsync();

                ClearCache();
                TempData[TempDataKeys.UserMessage] = "[[[The maintenance was scheduled successfully!]]]";
                return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
            }
        }