public string AddUpdateReservationRoomMapping(ReservationRoomMappingVM model)
        {
            string id = string.Empty;

            SqlParameter[] parameters =
            {
                new SqlParameter {
                    ParameterName = "@ReservationId", Value = model.ReservationId
                },
                new SqlParameter {
                    ParameterName = "@RoomId", Value = model.RoomId
                },
                new SqlParameter {
                    ParameterName = "@CreatedBy", Value = model.CreatedBy
                },
                new SqlParameter {
                    ParameterName = "@UpdatedBy", Value = model.UpdatedBy
                }
            };

            id = Convert.ToString(DALHelper.ExecuteScalar("AddUpdateReservationRoomMapping", parameters));

            return(id);
        }
        public ActionResult CheckIn(CheckInPaymentMethodVM model)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(model.RoomIds))
                {
                    string strETAText = "10:00";
                    if (!string.IsNullOrWhiteSpace(model.CheckInTimeText))
                    {
                        strETAText = model.CheckInTimeText;
                    }

                    var checkReservationOrNot = reservationRepository.GetPreviousReservationOrNot(Guid.Parse(model.RoomIds), model.CheckInDate, TimeSpan.Parse(strETAText), null, LogInManager.LoggedInUserId);
                    if (checkReservationOrNot != null && checkReservationOrNot.Count > 0)
                    {
                        return(Json(new
                        {
                            IsSuccess = false,
                            IsReservation = true,
                            errorMessage = "Selected room already checked in by another user. please select check in (ETA) time after " + checkReservationOrNot.Select(m => m.DepartureDate != null ? m.DepartureDate.Value.ToString("dd MMM yyyy") : null).FirstOrDefault() + " " + checkReservationOrNot.Select(m => m.CheckOutTime != null ? m.CheckOutTime : null).FirstOrDefault() + ""
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
                CheckInCheckOutVM   checkIn           = new CheckInCheckOutVM();
                ReservationChargeVM reservationCharge = new ReservationChargeVM();

                //Get Reservation detail.
                var reservation = reservationRepository.GetReservationById(model.ReservationId, LogInManager.LoggedInUserId).FirstOrDefault();

                if (reservation != null)
                {
                    reservation.PaymentMethodId = model.PaymentMethodId;

                    //Encrypt Credit Card#
                    reservation.CreditCardNo = Utility.Utility.Encrypt(model.CreditCardNo, Utility.Utility.EncryptionKey);

                    reservation.CardExpiryDate = model.CardExpiryDate;
                    reservation.ArrivalDate    = model.CheckInDate;
                    if (model.RoomTypeId.HasValue)
                    {
                        reservation.RoomTypeId = model.RoomTypeId.Value;
                    }


                    DateTime dtDepartureDate;
                    dtDepartureDate           = model.CheckInDate.Value.AddDays(reservation.NoOfNight);
                    reservation.DepartureDate = dtDepartureDate;

                    string   CheckInTimeText = model.CheckInTimeText;
                    TimeSpan checkInTime     = new TimeSpan();
                    if (!string.IsNullOrWhiteSpace(CheckInTimeText))
                    {
                        string   todayDate = DateTime.Now.ToString("dd/MM/yyyy");
                        string   date      = (todayDate + " " + CheckInTimeText);
                        DateTime time      = Convert.ToDateTime(date);
                        checkInTime = time.TimeOfDay;

                        reservation.ETA = checkInTime;
                    }

                    if (reservation.ETA.HasValue)
                    {
                        checkInTime = reservation.ETA.Value;
                    }

                    reservation.UpdatedBy = LogInManager.LoggedInUserId;
                    reservationRepository.UpdateReservation(reservation);

                    #region Save Room Rent Charges

                    double totalPrice = Utility.Utility.CalculateReservationTotalPrice(reservation.Id);

                    var roomRentCharge = additionalChargeRepository.GetAdditionalChargesByCode(AdditionalChargeCode.ROOM_RENT).FirstOrDefault();

                    reservationCharge = new ReservationChargeVM();
                    reservationCharge.ReservationId          = reservation.Id;
                    reservationCharge.AdditionalChargeId     = roomRentCharge.Id;
                    reservationCharge.AdditionalChargeSource = AdditionalChargeSource.ADDITIONAL_CHARGE;
                    reservationCharge.Code            = roomRentCharge.Code;
                    reservationCharge.Description     = roomRentCharge.Description;
                    reservationCharge.TransactionDate = reservation.ArrivalDate;
                    reservationCharge.Amount          = totalPrice;
                    reservationCharge.Qty             = 1;
                    reservationCharge.IsActive        = true;
                    reservationCharge.CreatedBy       = LogInManager.LoggedInUserId;

                    reservationChargeRepository.AddReservationCharges(reservationCharge);

                    #endregion

                    #region Save Reservation Room Mapping & Update Room Occupied Flag

                    var roomIds = model.RoomIds;

                    string[] roomIdsArr = null;
                    if (!string.IsNullOrWhiteSpace(roomIds))
                    {
                        roomIdsArr = roomIds.Split(',');
                        if (roomIdsArr != null)
                        {
                            //Remove Duplication.
                            roomIdsArr = roomIdsArr.Distinct().ToArray();
                        }
                    }

                    //Delete Existing Reservation Room Mapping.
                    //roomRepository.DeleteReservationRoomMappingByReservation(reservation.Id, LogInManager.LoggedInUserId);

                    #region Delete Room Mapping

                    var roomMappings = roomRepository.GetReservationRoomMapping(reservation.Id, null, LogInManager.LoggedInUserId);

                    if (roomMappings != null && roomMappings.Count > 0)
                    {
                        List <Guid> roomMappingIds = new List <Guid>();

                        foreach (var roomMapping in roomMappings)
                        {
                            if (roomMapping.RoomId.HasValue)
                            {
                                if (roomIdsArr == null || !roomIdsArr.Contains(roomMapping.RoomId.Value.ToString()))
                                {
                                    roomMappingIds.Add(roomMapping.Id);
                                }
                            }
                        }

                        //Delete Room Mapping
                        if (roomMappingIds != null && roomMappingIds.Count > 0)
                        {
                            foreach (var mappingId in roomMappingIds)
                            {
                                roomRepository.DeleteReservationRoomMapping(mappingId, LogInManager.LoggedInUserId, LogInManager.LoggedInUserId);
                            }
                        }
                    }

                    #endregion


                    if (roomIdsArr != null)
                    {
                        foreach (var item in roomIdsArr)
                        {
                            //Save Reservation Room Mapping.
                            ReservationRoomMappingVM reservationRoomMapping = new ReservationRoomMappingVM();
                            reservationRoomMapping.RoomId        = Guid.Parse(item.Trim());
                            reservationRoomMapping.ReservationId = reservation.Id;
                            reservationRoomMapping.CreatedBy     = LogInManager.LoggedInUserId;
                            reservationRoomMapping.UpdatedBy     = LogInManager.LoggedInUserId;

                            roomRepository.AddUpdateReservationRoomMapping(reservationRoomMapping);

                            ////Update Room Occupied Flag.
                            //roomRepository.UpdateRoomOccupiedFlag(Guid.Parse(item.Trim()), true, LogInManager.LoggedInUserId);

                            ////Update Room Status CLEAN to DIRTY.
                            //roomRepository.UpdateRoomCheckInStatus(Guid.Parse(item.Trim()), Guid.Parse(RoomStatusType.DIRTY), true, LogInManager.LoggedInUserId);

                            //Commented by Raju: 22-JUNE-2018 (Due to functionality change)
                            //#region Remove Existing Reservation & Room Mapping (Who selected this Room# but not checked in yet.)
                            //reservationRepository.DeleteReservationAndRoomMappingByRoom(Guid.Parse(item.Trim()), reservation.Id, LogInManager.LoggedInUserId, LogInManager.LoggedInUserId);
                            //#endregion
                            //Commented by Raju: 22-JUNE-2018 (Due to functionality change)

                            //#region Remove Existing Reservation who did just booking but not checked-in yet.

                            //var reservationRoomMappingDetails = roomRepository.GetReservationRoomMappingByRoom(Guid.Parse(item.Trim()), reservation.Id, reservation.ArrivalDate, reservation.DepartureDate, LogInManager.LoggedInUserId);

                            //if (reservationRoomMappingDetails != null && reservationRoomMappingDetails.Count > 0)
                            //{
                            //    foreach(var reservationRoomMappingDetail in reservationRoomMappingDetails)
                            //    {
                            //        //Delete Reservation.
                            //        reservationRepository.DeleteReservation(reservationRoomMappingDetail.ReservationId.Value, LogInManager.LoggedInUserId, LogInManager.LoggedInUserId);

                            //        //Delete Reservation Room Mapping.
                            //        roomRepository.DeleteReservationRoomMappingByReservation(reservationRoomMappingDetail.ReservationId.Value, LogInManager.LoggedInUserId, LogInManager.LoggedInUserId);

                            //        //Delete Reservation Log.
                            //        reservationLogRepository.DeleteReservationLog(reservationRoomMappingDetail.Id, LogInManager.LoggedInUserId, LogInManager.LoggedInUserId);
                            //    }
                            //}

                            //#endregion

                            #region Add Reservation Log

                            var lstReservationLog = reservationLogRepository.GetReservationLogDetails(model.ReservationId, Guid.Parse(item.Trim()), null, LogInManager.LoggedInUserId).FirstOrDefault();

                            if (lstReservationLog != null)
                            {
                                lstReservationLog.ReservationId = model.ReservationId;
                                lstReservationLog.ProfileId     = model.ProfileId;
                                lstReservationLog.RoomId        = Guid.Parse(item.Trim());
                                lstReservationLog.CheckInDate   = model.CheckInDate.Value;
                                lstReservationLog.CheckInTime   = checkInTime;
                                lstReservationLog.CheckOutDate  = reservation.DepartureDate;
                                lstReservationLog.RoomStatusId  = Guid.Parse(RoomStatusType.DIRTY);
                                lstReservationLog.IsActive      = true;
                                lstReservationLog.UpdatedBy     = LogInManager.LoggedInUserId;

                                reservationLogRepository.UpdateReservationLog(lstReservationLog);
                            }
                            else
                            {
                                ReservationLogVM reservationLog = new ReservationLogVM();
                                reservationLog.ReservationId = model.ReservationId;
                                reservationLog.ProfileId     = model.ProfileId;
                                reservationLog.RoomId        = Guid.Parse(item.Trim());
                                reservationLog.CheckInDate   = model.CheckInDate.Value;
                                reservationLog.CheckInTime   = checkInTime;
                                reservationLog.CheckOutDate  = reservation.DepartureDate;
                                reservationLog.RoomStatusId  = Guid.Parse(RoomStatusType.DIRTY);
                                reservationLog.IsActive      = true;
                                reservationLog.CreatedBy     = LogInManager.LoggedInUserId;

                                reservationLogRepository.AddReservationLog(reservationLog);
                            }

                            #endregion
                        }
                    }

                    #endregion

                    #region Save CheckIn Details

                    checkIn.ReservationId = model.ReservationId;
                    checkIn.ProfileId     = model.ProfileId;
                    checkIn.CheckInDate   = model.CheckInDate.Value;
                    checkIn.CheckInTime   = checkInTime;
                    checkIn.IsActive      = true;
                    checkIn.CreatedBy     = LogInManager.LoggedInUserId;

                    var checkInId = checkInCheckOutRepository.AddCheckInDetail(checkIn);

                    #endregion

                    #region Update Reservation Check In Flag

                    reservationRepository.UpdateReservationCheckInFlag(model.ReservationId, true, LogInManager.LoggedInUserId);

                    #endregion

                    #region Update Reservation Status

                    reservationRepository.UpdateReservationStatus(model.ReservationId, Guid.Parse(ReservationStatusName.CHECKEDIN), LogInManager.LoggedInUserId);

                    #endregion

                    #region Update Reservation Total Balance.

                    double totalGuestBalance = Utility.Utility.CalculateTotalBalance(reservation.Id);

                    //Update Total Balance.
                    reservationRepository.UpdateReservationTotalBalance(reservation.Id, totalGuestBalance, LogInManager.LoggedInUserId);

                    #endregion

                    #region Update TrackReservationLog Status

                    reservationRepository.UpdateTrackReservationLogStatusByReservationId(reservation.Id, true);

                    #endregion

                    #region Record Activity Log
                    RecordActivityLog.RecordActivity(Pages.CHECKIN, string.Format("Checked in profile successfully. Name: {0} {1}, Comfirmation #: {2} ", reservation.LastName, reservation.FirstName, reservation.ConfirmationNumber));
                    #endregion

                    return(Json(new
                    {
                        IsSuccess = true,
                        data = new
                        {
                            CheckInId = checkInId,
                            Name = model.Name
                        }
                    }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new
                    {
                        IsSuccess = false,
                        errorMessage = "Check In details not saved successfully."
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception e)
            {
                Utility.Utility.LogError(e, "CheckIn");
                return(Json(new { IsSuccess = false, errorMessage = e.Message }));
            }
        }