Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.IO;
- using System.Web.Security;
- using System.Web.Configuration;
- using Hotel;
- using Hotel.Web.Core.Models;
- using Hotel.Data;
- using Hotel.Service.Rents;
- using Hotel.Web.ViewModels;
- using JQueryDataTableParamModel = Hotel.Web.Core.Models.JQueryDataTableParamModel;
- namespace HotelManagement.Controllers
- {
- //[Authorize(Roles = CustomAuthorization.Invoice)]
- public class AdminController : Controller
- {
- IRentService rentService;
- public AdminController(IRentService rentService)
- {
- this.rentService = rentService;
- }
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult Index2()
- {
- if(CultureInfo.CurrentCulture.Name == "vi")
- {
- return HttpNotFound();
- }
- return View();
- }
- public ActionResult SearchRentByStatus(int status, int pageIndex, int pageSize)
- {
- var count = rentService.GetTotalRentByStatus(status);
- int totalPage = (count / pageSize) + (count % pageSize == 0 ? 0 : 1);
- var model = new SearchRentTableViewModel();
- IEnumerable<Rent> rents = rentService.GetRentByStatus((pageIndex - 1) * pageSize, pageSize, status);
- model.Rents = rents;
- model.TotalPage = totalPage;
- model.CurrentPageIndex = pageIndex;
- model.Status = status;
- // model.IsAdmin = System.Web.Security.Roles.GetRolesForUser().Contains("admin");
- model.IsAdmin = IdentityManager.IsCurrentUserHasRole("admin");
- return PartialView("_SearchRentTable", model);
- }
- public ActionResult LoadRentByStatusDatatables(JQueryDataTableParamModel param, int status, string fromDate, string toDate)
- {
- IEnumerable<Rent> allRents;
- if(!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
- {
- DateTime from = DateTime.Parse(fromDate);
- DateTime to = DateTime.Parse(toDate);
- allRents = rentService.GetAllRentByStatusAndDate(status, from, to);
- }
- else
- {
- allRents = rentService.GetAllRentsByStatus(status);
- }
- IEnumerable<Rent> filteredListItems;
- //Search
- if (!string.IsNullOrEmpty(param.sSearch))
- {
- filteredListItems = allRents.Where(d => d.Room.RoomName.Contains(param.sSearch));
- }
- else
- {
- filteredListItems = allRents;
- }
- //Sort
- var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
- var sortDirection = Request["sSortDir_0"]; //asc or //desc
- //Sort
- if (sortColumnIndex == 6 || sortColumnIndex == 9 ||sortColumnIndex == 10
- || sortColumnIndex == 11 || sortColumnIndex == 12)
- {
- Func<Rent, int> orderingIntFunction = (c => sortColumnIndex == 6 ? c.RentType :
- sortColumnIndex == 9 ? (c.OrderFee + c.RentFee) :
- sortColumnIndex == 10 ? c.TotalPayment : (c.OrderFee + c.RentFee - c.TotalPayment));
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- else if (sortColumnIndex == 7 || sortColumnIndex == 8)
- {
- Func<Rent, DateTime?> orderingIntFunction = (c => sortColumnIndex == 7 ? c.CheckInDate :
- c.CheckOutDate);
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- else if (sortColumnIndex == 4 || sortColumnIndex == 5)
- {
- Func<Rent, string> orderingIntFunction = (c => sortColumnIndex == 4 ? c.InvoiceID :
- c.Room.RoomName);
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- //Paging
- var displayedList = filteredListItems.Skip(param.iDisplayStart).Take(param.iDisplayLength);
- //Get the final result after filter + paging
- //PhuongLHK:Check current user is admin by identity
- bool isAdmin = IdentityManager.IsCurrentUserHasRole("admin"); ;
- int count = 1;
- var result = from c in displayedList
- select new IConvertible[]
- {
- c.RentID,
- c.RentID,
- //System.Web.Security.Roles.GetRolesForUser().Contains("admin"),
- isAdmin,
- count++,
- c.InvoiceID,
- c.Room.RoomName,
- c.RentType,
- c.CheckInDate.ToString("dd-MM-yyyy HH:mm"),
- c.CheckOutDate == null ? DateTime.Now.ToString("dd-MM-yyyy HH:mm") : ((DateTime)c.CheckOutDate).ToString("dd-MM-yyyy HH:mm"),
- (c.OrderFee + c.RentFee).ToString("N0"),
- c.TotalPayment.ToString("N0"),
- (c.OrderFee + c.RentFee - c.TotalPayment).ToString("N0"),
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- };
- //Return json to DBTB
- return Json(new
- {
- sEcho = param.sEcho,
- iTotalRecords = allRents.Count(),
- iTotalDisplayRecords = filteredListItems.Count(),
- aaData = result
- }, JsonRequestBehavior.AllowGet);
- }
- public ActionResult LoadCafeByStatusDatatables(JQueryDataTableParamModel param, int status, string fromDate, string toDate)
- {
- IEnumerable<Rent> allRents;
- if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
- {
- DateTime from = DateTime.Parse(fromDate);
- DateTime to = DateTime.Parse(toDate);
- allRents = rentService.GetAllCafeRentByStatusAndDate(status, from, to);
- }
- else
- {
- allRents = rentService.GetAllCafeRentsByStatus(status);
- }
- IEnumerable<Rent> filteredListItems;
- //Search
- if (!string.IsNullOrEmpty(param.sSearch))
- {
- filteredListItems = allRents.Where(d => d.Room.RoomName.Contains(param.sSearch));
- }
- else
- {
- filteredListItems = allRents;
- }
- //Sort
- var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
- var sortDirection = Request["sSortDir_0"]; //asc or //desc
- //Sort
- if (sortColumnIndex == 6 || sortColumnIndex == 9 || sortColumnIndex == 10
- || sortColumnIndex == 11 || sortColumnIndex == 12)
- {
- Func<Rent, int> orderingIntFunction = (c => sortColumnIndex == 6 ? c.RentType :
- sortColumnIndex == 9 ? (c.OrderFee + c.RentFee) :
- sortColumnIndex == 10 ? c.TotalPayment : (c.OrderFee + c.RentFee - c.TotalPayment));
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- else if (sortColumnIndex == 7 || sortColumnIndex == 8)
- {
- Func<Rent, DateTime?> orderingIntFunction = (c => sortColumnIndex == 7 ? c.CheckInDate :
- c.CheckOutDate);
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- else if (sortColumnIndex == 4 || sortColumnIndex == 5)
- {
- Func<Rent, string> orderingIntFunction = (c => sortColumnIndex == 4 ? c.InvoiceID :
- c.Room.RoomName);
- filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
- }
- //Paging
- var displayedList = filteredListItems.Skip(param.iDisplayStart).Take(param.iDisplayLength);
- //Get the final result after filter + paging
- //PhuongLHK:Check current user is admin by identity
- bool isAdmin = IdentityManager.IsCurrentUserHasRole( "admin");
- int count = 1;
- var result = from c in displayedList
- select new IConvertible[]
- {
- c.RentID,
- c.RentID,
- //System.Web.Security.Roles.GetRolesForUser().Contains("admin"),
- true,
- count++,
- c.InvoiceID,
- c.Room.RoomName,
- c.RentType,
- c.CheckInDate.ToString("dd-MM-yyyy HH:mm"),
- c.CheckOutDate == null ? DateTime.Now.ToString("dd-MM-yyyy HH:mm") : ((DateTime)c.CheckOutDate).ToString("dd-MM-yyyy HH:mm"),
- (c.OrderFee + c.RentFee).ToString("N0"),
- c.TotalPayment.ToString("N0"),
- (c.OrderFee + c.RentFee - c.TotalPayment).ToString("N0"),
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- c.RentStatus,
- };
- //Return json to DBTB
- return Json(new
- {
- sEcho = param.sEcho,
- iTotalRecords = allRents.Count(),
- iTotalDisplayRecords = filteredListItems.Count(),
- aaData = result
- }, JsonRequestBehavior.AllowGet);
- }
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult DeleteRent(int rentId)
- {
- try {
- rentService.DeleteRent(rentId);
- return Content("1");
- }
- catch (Exception) {
- return Content("0");
- }
- }
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult ApproveRent(int rentId)
- {
- bool rs = rentService.ApproveRent(rentId);
- return Content(rs ? "1" : "0");
- }
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult ApproveAllRents(int type, List<int> ids)
- {
- bool rs = false;
- rs = rentService.ApproveAllChosenRents(ids);
- return Content(rs ? "1" : "0");
- }
- //Hủy tất cả hóa đơn
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult DisabledAllRents(int type, List<int> ids)
- {
- bool rs = false;
- rs = rentService.DisabledAllChosenRents(ids);
- return Content(rs ? "1" : "0");
- }
- //Xóa tất cả hóa đơn
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult DeleteAllRents(int type, List<int> ids)
- {
- bool rs = false;
- rs = rentService.DeleteAllChosenRents(ids);
- return Content(rs ? "1" : "0");
- }
- public ActionResult CancelRent(int rentId, string reason)
- {
- bool rs = rentService.CancelRent(rentId, reason);
- return Content(rs ? "1" : "0");
- }
- //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
- public ActionResult AcceptCancelRentRequest(int rentId)
- {
- bool rs = rentService.AcceptCancelRentRequest(rentId);
- return Content(rs ? "1" : "0");
- }
- //public ActionResult ChangeRentStatus(int RentID, int fromStatus, int toStatus, int index)
- //{
- // var item = _db.Rents.Where(r => r.RentID == RentID && r.RentStatus == fromStatus).FirstOrDefault();
- // if ((item != null)&& CheckValidTransition((RentStatusEnum)fromStatus,(RentStatusEnum)toStatus))
- // {
- // if (toStatus == (int)RentStatusEnum.DeletePermanent)
- // {
- // _db.Rents.DeleteOnSubmit(item);
- // }
- // else
- // {
- // item.RentStatus = toStatus;
- // }
- // // Neu dang o trang thai InStay chuyển qua trang thai khac se phai cap nhat lai trang thai cua phong
- // if (fromStatus == (int)RentStatusEnum.InStay)
- // {
- // item.Room.RoomStatus = (int) RoomStatusEnum.Prepare;
- // }
- // _db.SubmitChanges();
- // }
- // if (toStatus != (int)RentStatusEnum.DeletePermanent)
- // {
- // RentItemViewModel model = new RentItemViewModel();
- // model.Index = index;
- // model.Rent = item;
- // return PartialView("_RentItem", model);
- // }
- // return Content("xx");
- //}
- //private bool CheckValidTransition(RentStatusEnum fromStatus, RentStatusEnum toStatus)
- //{
- // if (fromStatus == toStatus)
- // return false;
- // bool isAdmin = Roles.GetRolesForUser().Contains("admin");
- // switch (toStatus)
- // {
- // case RentStatusEnum.InStay:
- // return false;
- // case RentStatusEnum.Paid:
- // if (fromStatus == RentStatusEnum.InStay)
- // return true;
- // else
- // return false;
- // case RentStatusEnum.Approved:
- // if (isAdmin && fromStatus == RentStatusEnum.Paid)
- // return true;
- // else
- // return false;
- // case RentStatusEnum.WaitDisabled:
- // if (fromStatus == RentStatusEnum.InStay || fromStatus == RentStatusEnum.Paid || (fromStatus == RentStatusEnum.Approved && isAdmin))
- // return true;
- // else
- // return false;
- // case RentStatusEnum.Disabled:
- // if (isAdmin && fromStatus == RentStatusEnum.WaitDisabled)
- // return true;
- // else
- // return false;
- // case RentStatusEnum.DeletePermanent:
- // if (isAdmin && fromStatus == RentStatusEnum.Disabled)
- // return true;
- // else
- // return false;
- // }
- // return false;
- //}
- //public ActionResult ExportToExcel(DateTime StartTime, DateTime EndTime)
- //{
- // List<string> headerStrings = new List<string> { "Tên phòng", "Loại thuê", "Thời điểm vào", "Thời điểm trả", "Tổng tiền", "Tổng thu", "Còn lại", "Trạng thái" };
- // var item = from r in _db.Rents
- // where ((r.RentStatus != ((int)RentStatusEnum.Disabled)) &&
- // ((r.RentStatus == (int)(RentStatusEnum.InStay)) ||
- // ((r.CheckOutDate >= StartTime) && (r.CheckOutDate <= EndTime))))
- // select new RentItem
- // {
- // RoomName = r.Room.RoomName,
- // CheckInDate = r.CheckInDate,
- // CheckOutDate = r.CheckOutDate,
- // TotalMoney = r.RentFee + r.OrderFee,
- // TotalPayment = r.TotalPayment,
- // ContainMoney = r.RentFee + r.OrderFee - r.TotalPayment,
- // RentStatus = r.RentStatus == (int)RentStatusEnum.Approved ? "Đã duyệt" :
- // r.RentStatus == (int)RentStatusEnum.InStay ? "Đang ở" :
- // r.RentStatus == (int)RentStatusEnum.Paid ? "Chờ duyệt" : "Hủy"
- // ,
- // RentType = r.RentType == (int)RentTypeEnum.Day ? "Thuê ngày" :
- // r.RentType == (int)RentTypeEnum.Hour ? "Thuê giờ" : "Qua đêm"
- // };
- // string fileName = string.Format("BaoCao_{0}_{1}.xlsx", StartTime.ToShortDateString(), EndTime.ToShortDateString());
- // ExcelFacade excelFacade = new ExcelFacade();
- // MemoryStream stream = excelFacade.CreateExcelStream<RentItem>(fileName, item.ToList(), "Sheet1", headerStrings);
- // //byte[] content = new byte[stream.Length];
- // //stream.Read(content, 0, (int)stream.Length);
- // byte[] content = stream.ToArray();
- // return new BinaryFileResult()
- // {
- // FileName = fileName,
- // Content = content
- // };
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement