// Update on selected site
        public ActionResult UpdateSelected(int id)
        {
            var _session = sessionService.GetSession(this.HttpContext);
            var _selecteditem = selecteditems.GetById(id);
            _selecteditem.checkInDate = _session.checkInDate.Value;
            _selecteditem.checkOutDate = _session.checkOutDate.Value;
            CalcSiteTotal calcResults = new CalcSiteTotal(
                _selecteditem.checkInDate,
                _selecteditem.checkOutDate,
                _selecteditem.weeklyRate,
                _selecteditem.dailyRate,
                true);
            _selecteditem.duration = calcResults.duration;
            _selecteditem.weeks = calcResults.weeks;
            _selecteditem.days = calcResults.days;
            _selecteditem.amount = calcResults.amount;
            _selecteditem.total = calcResults.total;
            _selecteditem.lastUpdate = DateTime.Now;

            selecteditems.Update(_selecteditem);
            selecteditems.Commit();

            return RedirectToAction("NewReservation");
        }
        public ActionResult GetSiteData(long idRVSite, DateTime checkInDate, DateTime checkOutDate)
        {
            decimal amount = 0;
            decimal weeklyRate = 0;
            decimal dailyRate = 0;

            var site = sites_description_rate.GetAll().Where(s => s.id == idRVSite).FirstOrDefault();

            if (site != null)
            {
                weeklyRate = site.weeklyRate.Value;
                dailyRate = site.dailyRate.Value;

                CalcSiteTotal calcResults = new CalcSiteTotal(
                    checkInDate,
                    checkOutDate,
                    weeklyRate,
                    dailyRate,
                    true);
                amount = calcResults.amount;
            }

            return Json(new
            {
                amount = amount.ToString("N2"),
                type = site.description,
                weeklyRate = weeklyRate.ToString("N2"),
                dailyRate = dailyRate.ToString("N2")
            });
        }
        public ActionResult SelectSite(long idRVSite, DateTime checkInDate, DateTime checkOutDate)
        {
            var _session = sessionService.GetSession(this.HttpContext);

            // Add selected item to the database
            var _selecteditem = new selecteditem();
            var type_rates = sites_description_rate.GetAll().
                Where(s => s.id == idRVSite).FirstOrDefault();

            _selecteditem.checkInDate = checkInDate;
            _selecteditem.checkOutDate = checkOutDate;
            _selecteditem.weeklyRate = type_rates.weeklyRate.Value;
            _selecteditem.dailyRate = type_rates.dailyRate.Value;
            _selecteditem.idRVSite = idRVSite;
            _selecteditem.idSession = _session.ID;
            _selecteditem.idIPMEvent = _session.idIPMEvent;
            _selecteditem.idStaff = _session.idStaff;
            _selecteditem.idCustomer = _session.idCustomer;
            _selecteditem.site = type_rates.RVSite;
            _selecteditem.siteType = type_rates.description;
            _selecteditem.isSiteChecked = true;
            CalcSiteTotal calcResults = new CalcSiteTotal(
                checkInDate,
                checkOutDate,
                type_rates.weeklyRate.Value,
                type_rates.dailyRate.Value,
                true);
            _selecteditem.duration = calcResults.duration;
            _selecteditem.weeks = calcResults.weeks;
            _selecteditem.days = calcResults.days;
            _selecteditem.amount = calcResults.amount;
            _selecteditem.total = calcResults.total;
            _selecteditem.createDate = DateTime.Now;
            _selecteditem.lastUpdate = DateTime.Now;

            selecteditems.Insert(_selecteditem);
            selecteditems.Commit();

            return Json(idRVSite);
        }
        public ActionResult EditReservation()
        {
            long sessionUserID = sessionService.GetSessionUserID(this.HttpContext);
            ViewBag.UserID = sessionUserID;

            long sessionID = sessionService.GetSessionID(this.HttpContext);
            long sessionCustomerID = sessionService.GetSessionCustomerID(sessionID);
            ViewBag.Customer = sessionService.GetSessionCustomerNamePhone(sessionID);

            long sessionIPMEventID = sessionService.GetSessionIPMEventID(sessionID);

            var _reserveditems = reservationitems.GetAll();
            if (sessionCustomerID != IDnotFound)
            {
                _reserveditems = _reserveditems.Where(q => q.idCustomer == sessionCustomerID).OrderByDescending(o => o.idRVSite);
            }

            foreach (var item in _reserveditems)
            {
                // If reserved item is not in the selected item table
                var _checkitem = selecteditems.GetAll().Where(s => s.idRVSite == item.idRVSite).FirstOrDefault();
                if (_checkitem == null)
                {
                    var _site_description_rate = sites_description_rate.GetByKey("id", item.idRVSite);
                    // Add reserved item as selected item
                    selecteditem _selecteditem = new selecteditem();
                    _selecteditem.idRVSite = item.idRVSite;
                    _selecteditem.idSession = sessionID;
                    _selecteditem.idIPMEvent = sessionIPMEventID;
                    _selecteditem.idStaff = sessionUserID;
                    _selecteditem.idCustomer = item.idCustomer;
                    _selecteditem.checkInDate = item.checkInDate;
                    _selecteditem.checkOutDate = item.checkOutDate;
                    _selecteditem.site = _site_description_rate.RVSite;
                    _selecteditem.siteType = _site_description_rate.description;
                    _selecteditem.duration = item.duration;
                    _selecteditem.weeks = item.weeks;
                    _selecteditem.weeklyRate = item.weeklyRate;
                    _selecteditem.days = item.days;
                    _selecteditem.dailyRate = item.dailyRate;
                    _selecteditem.amount = item.total;
                    _selecteditem.isSiteChecked = true;
                    CalcSiteTotal calcResults = new CalcSiteTotal(
                        item.checkInDate,
                        item.checkOutDate,
                        _site_description_rate.weeklyRate.Value,
                        _site_description_rate.dailyRate.Value,
                        true);
                    _selecteditem.duration = calcResults.duration;
                    _selecteditem.weeks = calcResults.weeks;
                    _selecteditem.days = calcResults.days;
                    _selecteditem.amount = calcResults.amount;
                    _selecteditem.total = calcResults.total;
                    _selecteditem.createDate = DateTime.Now;
                    _selecteditem.lastUpdate = DateTime.Now;
                    _selecteditem.idReservationItem = item.ID;
                    _selecteditem.reservationCheckInDate = item.checkInDate;
                    _selecteditem.reservationCheckOutDate = item.checkOutDate;
                    _selecteditem.reservationAmount = item.total;

                    selecteditems.Insert(_selecteditem);
                }
            }
            selecteditems.Commit();

            // Data to be presented on the view

            CreatePaymentViewBags(sessionID, sessionCustomerID);

            var _selecteditems = selecteditems.GetAll().
                Where(s => s.idSession == sessionID && s.idCustomer == sessionCustomerID);

            //payment _payment = paymentService.CalculateEditSelectedTotal(sessionID, sessionCustomerID);

            //// Value of previous reservation, just before edit reservation mode started
            //ViewBag.PrimaryTotal = _payment.primaryTotal.ToString("N2");
            //ViewBag.SelectionTotal = _payment.selectionTotal.ToString("N2");
            //ViewBag.CancellationFee = _payment.cancellationFee.ToString("N2");
            //// Suggested value for payment
            //if (_payment.amount >= 0)
            //{
            //    ViewBag.dueAmount = _payment.amount.ToString("N2");
            //    ViewBag.refundAmount = "0.00";
            //}
            //else
            //{
            //    ViewBag.refundAmount = (_payment.amount * -1).ToString("N2");
            //    ViewBag.dueAmount = "0.00";
            //}

            return View(_selecteditems);
        }
        public ActionResult EditReservation()
        {
            long sessionUserID = sessionService.GetSessionUserID(this.HttpContext, true, false);
            ViewBag.UserID = sessionUserID;

            long sessionID = sessionService.GetSessionID(this.HttpContext, false, false);
            long sessionCustomerID = sessionService.GetSessionCustomerID(sessionID);
            ViewBag.Customer = sessionService.GetSessionCustomerNamePhone(sessionID);

            long sessionIPMEventID = sessionService.GetSessionIPMEventID(sessionID);

            var _reserveditems = reservationitems.GetAll();
            if (sessionCustomerID != IDnotFound)
            {
                _reserveditems = reservationitems.GetAll().
                    Where(q => q.idCustomer == sessionCustomerID && q.isCancelled != true).
                    OrderByDescending(o => o.idRVSite);
            }

            // Discard reserved items from other years
            long IPMEventID = sessionService.GetSessionIPMEventID(sessionID);
            foreach (var _reserveditem in _reserveditems)
            {
                var _site = placesinmap.GetById(_reserveditem.idRVSite);
                if (_site.idIPMEvent != IPMEventID)
                {
                    _reserveditems = _reserveditems.Where(r => r.idRVSite != _site.ID);
                }
            }

            foreach (var item in _reserveditems)
            {
                // If reserved item is not in the selected item table
                var _checkitem = selecteditems.GetAll().Where(s => s.idRVSite == item.idRVSite && s.reservationAmount != 0).FirstOrDefault();
                if (_checkitem == null)
                {
                    var _site_description_rate = sites_description_rate.GetByKey("id", item.idRVSite);
                    // Add reserved item as selected item                
                    selecteditem _selecteditem = new selecteditem();
                    _selecteditem.idRVSite = item.idRVSite;
                    _selecteditem.idSession = sessionID;
                    _selecteditem.idIPMEvent = sessionIPMEventID;
                    _selecteditem.idStaff = sessionUserID;
                    _selecteditem.idCustomer = item.idCustomer;
                    _selecteditem.checkInDate = item.checkInDate;
                    _selecteditem.checkOutDate = item.checkOutDate;
                    _selecteditem.site = _site_description_rate.RVSite;
                    _selecteditem.siteType = _site_description_rate.description;
                    _selecteditem.duration = item.duration;
                    _selecteditem.weeks = item.weeks;
                    _selecteditem.weeklyRate = item.weeklyRate;
                    _selecteditem.days = item.days;
                    _selecteditem.dailyRate = item.dailyRate;
                    _selecteditem.amount = item.total;
                    _selecteditem.isSiteChecked = true;
                    CalcSiteTotal calcResults = new CalcSiteTotal(
                        item.checkInDate,
                        item.checkOutDate,
                        _site_description_rate.weeklyRate.Value,
                        _site_description_rate.dailyRate.Value,
                        true);
                    _selecteditem.duration = calcResults.duration;
                    _selecteditem.weeks = calcResults.weeks;
                    _selecteditem.days = calcResults.days;
                    _selecteditem.amount = calcResults.amount;
                    _selecteditem.total = calcResults.total;
                    _selecteditem.createDate = DateTime.Now;
                    _selecteditem.lastUpdate = DateTime.Now;
                    _selecteditem.timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
                    _selecteditem.idReservationItem = item.ID;
                    _selecteditem.reservationCheckInDate = item.checkInDate;
                    _selecteditem.reservationCheckOutDate = item.checkOutDate;
                    _selecteditem.reservationAmount = item.total;

                    selecteditems.Insert(_selecteditem);
                }
            }
            selecteditems.Commit();

            // Data to be presented on the view

            CreatePaymentViewBags(sessionID, IPMEventID, sessionCustomerID);

            var _selecteditems = selecteditems.GetAll().
                Where(si => si.idSession == sessionID && si.idCustomer == sessionCustomerID && si.reservationAmount != 0).
                OrderBy(order => order.site);

            return View(_selecteditems);
        }
        // Update Reserved Site
        public ActionResult UpdateReserved(int id)
        {
            var _session = sessionService.GetSession(this.HttpContext, true, false);
            var _selecteditem = selecteditems.GetById(id);
            _selecteditem.checkInDate = _session.checkInDate.Value;
            _selecteditem.checkOutDate = _session.checkOutDate.Value;
            CalcSiteTotal calcResults = new CalcSiteTotal(
                _selecteditem.checkInDate,
                _selecteditem.checkOutDate,
                _selecteditem.weeklyRate,
                _selecteditem.dailyRate,
                true);
            _selecteditem.duration = calcResults.duration;
            _selecteditem.weeks = calcResults.weeks;
            _selecteditem.days = calcResults.days;
            _selecteditem.amount = calcResults.amount;
            _selecteditem.total = calcResults.total;
            _selecteditem.lastUpdate = DateTime.Now;
            _selecteditem.timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");

            selecteditems.Update(_selecteditem);
            selecteditems.Commit();

            return RedirectToAction("EditReservation");
        }
        public ActionResult SelectSiteOnMap(long id)
        {
            var _session = sessionService.GetSession(this.HttpContext, true, false);

            // Check if site is really available
            bool isRVSiteAvailable = rvsites_available.GetAll().
                Where(s => s.idIPMEvent == _session.idIPMEvent && s.id == id).
                ToList().Count() > 0;

            if (isRVSiteAvailable)
            {
                ipmevent _IPMEvent = ipmevents.GetById(_session.idIPMEvent);

                // Read dates from IPM Event
                DateTime checkInDate = _IPMEvent.startDate.Value;
                DateTime checkOutDate = checkInDate.AddDays(7);
                // Read dates from session
                if (_session.checkInDate != null)
                {
                    checkInDate = _session.checkInDate.Value;
                };
                if (_session.checkOutDate != null)
                {
                    checkOutDate = _session.checkOutDate.Value;
                };

                // Add selected item to the database
                var _selecteditem = new selecteditem();
                var type_rates = sites_description_rate.GetAll().
                    Where(s => s.id == id).FirstOrDefault();

                _selecteditem.checkInDate = checkInDate;
                _selecteditem.checkOutDate = checkOutDate;
                _selecteditem.weeklyRate = type_rates.weeklyRate.Value;
                _selecteditem.dailyRate = type_rates.dailyRate.Value;
                _selecteditem.idRVSite = id;
                _selecteditem.idSession = _session.ID;
                _selecteditem.idIPMEvent = _session.idIPMEvent;
                _selecteditem.idStaff = _session.idStaff;
                _selecteditem.idCustomer = _session.idCustomer;
                _selecteditem.site = type_rates.RVSite;
                _selecteditem.siteType = type_rates.description;
                _selecteditem.isSiteChecked = true;
                CalcSiteTotal calcResults = new CalcSiteTotal(
                    checkInDate,
                    checkOutDate,
                    type_rates.weeklyRate.Value,
                    type_rates.dailyRate.Value,
                    true);
                _selecteditem.duration = calcResults.duration;
                _selecteditem.weeks = calcResults.weeks;
                _selecteditem.days = calcResults.days;
                _selecteditem.amount = calcResults.amount;
                _selecteditem.total = calcResults.total;
                _selecteditem.createDate = DateTime.Now;
                _selecteditem.lastUpdate = DateTime.Now;
                _selecteditem.timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");

                selecteditems.Insert(_selecteditem);
                selecteditems.Commit();
            }

            return Json(id);
        }
        public ActionResult SelectSite(long idRVSite, DateTime checkInDate, DateTime checkOutDate)
        {
            var _session = sessionService.GetSession(this.HttpContext, true, false);

            // Check if site is really available
            bool isRVSiteAvailable = rvsites_available.GetAll().
                Where(s => s.idIPMEvent == _session.idIPMEvent && s.id == idRVSite).
                ToList().Count() > 0;

            if (isRVSiteAvailable)
            {
                // Check if site is already in the list (it was previously cancelled)
                // If it is, remove it
                var old_selecteditems = selecteditems.GetAll().Where(osi => osi.idRVSite == idRVSite).ToList();
                foreach (var old_selecteditem in old_selecteditems)
                {
                    //selecteditems.Delete(old_selecteditem.ID);
                    //selecteditems.Commit();
                };

                // Add selected item to the database
                var _selecteditem = new selecteditem();
                var type_rates = sites_description_rate.GetAll().
                    Where(s => s.id == idRVSite).FirstOrDefault();

                _selecteditem.checkInDate = checkInDate;
                _selecteditem.checkOutDate = checkOutDate;
                _selecteditem.weeklyRate = type_rates.weeklyRate.Value;
                _selecteditem.dailyRate = type_rates.dailyRate.Value;
                _selecteditem.idRVSite = idRVSite;
                _selecteditem.idSession = _session.ID;
                _selecteditem.idIPMEvent = _session.idIPMEvent;
                _selecteditem.idStaff = _session.idStaff;
                _selecteditem.idCustomer = _session.idCustomer;
                _selecteditem.site = type_rates.RVSite;
                _selecteditem.siteType = type_rates.description;
                _selecteditem.isSiteChecked = true;
                CalcSiteTotal calcResults = new CalcSiteTotal(
                    checkInDate,
                    checkOutDate,
                    type_rates.weeklyRate.Value,
                    type_rates.dailyRate.Value,
                    true);
                _selecteditem.duration = calcResults.duration;
                _selecteditem.weeks = calcResults.weeks;
                _selecteditem.days = calcResults.days;
                _selecteditem.amount = calcResults.amount;
                _selecteditem.total = calcResults.total;
                _selecteditem.createDate = DateTime.Now;
                _selecteditem.lastUpdate = DateTime.Now;
                _selecteditem.timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");

                selecteditems.Insert(_selecteditem);
                selecteditems.Commit();

            }

            return Json(idRVSite);
        }