Advertisement
locnhse61312

Untitled

Nov 24th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.IO;
  8. using System.Web.Security;
  9. using System.Web.Configuration;
  10. using Hotel;
  11. using Hotel.Web.Core.Models;
  12. using Hotel.Data;
  13. using Hotel.Service.Rents;
  14. using Hotel.Web.ViewModels;
  15. using JQueryDataTableParamModel = Hotel.Web.Core.Models.JQueryDataTableParamModel;
  16.  
  17. namespace HotelManagement.Controllers
  18. {
  19.     //[Authorize(Roles = CustomAuthorization.Invoice)]
  20.     public class AdminController : Controller
  21.     {
  22.         IRentService rentService;
  23.  
  24.         public AdminController(IRentService rentService)
  25.         {
  26.             this.rentService = rentService;
  27.         }
  28.  
  29.         public ActionResult Index()
  30.         {
  31.             return View();
  32.         }
  33.  
  34.         public ActionResult Index2()
  35.         {
  36.             if(CultureInfo.CurrentCulture.Name == "vi")
  37.             {
  38.                 return HttpNotFound();
  39.             }
  40.             return View();
  41.         }
  42.  
  43.         public ActionResult SearchRentByStatus(int status, int pageIndex, int pageSize)
  44.         {
  45.             var count = rentService.GetTotalRentByStatus(status);
  46.             int totalPage = (count / pageSize) + (count % pageSize == 0 ? 0 : 1);
  47.             var model = new SearchRentTableViewModel();
  48.  
  49.             IEnumerable<Rent> rents = rentService.GetRentByStatus((pageIndex - 1) * pageSize, pageSize, status);
  50.             model.Rents = rents;
  51.             model.TotalPage = totalPage;
  52.             model.CurrentPageIndex = pageIndex;
  53.             model.Status = status;
  54.            // model.IsAdmin = System.Web.Security.Roles.GetRolesForUser().Contains("admin");
  55.             model.IsAdmin = IdentityManager.IsCurrentUserHasRole("admin");
  56.             return PartialView("_SearchRentTable", model);
  57.         }
  58.  
  59.         public ActionResult LoadRentByStatusDatatables(JQueryDataTableParamModel param, int status, string fromDate, string toDate)
  60.         {
  61.             IEnumerable<Rent> allRents;
  62.             if(!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
  63.             {
  64.                     DateTime from = DateTime.Parse(fromDate);
  65.                     DateTime to = DateTime.Parse(toDate);
  66.                     allRents = rentService.GetAllRentByStatusAndDate(status, from, to);
  67.             }
  68.             else
  69.             {
  70.                 allRents = rentService.GetAllRentsByStatus(status);
  71.             }  
  72.  
  73.             IEnumerable<Rent> filteredListItems;
  74.  
  75.             //Search
  76.             if (!string.IsNullOrEmpty(param.sSearch))
  77.             {
  78.  
  79.                 filteredListItems = allRents.Where(d => d.Room.RoomName.Contains(param.sSearch));
  80.             }
  81.             else
  82.             {
  83.                 filteredListItems = allRents;
  84.             }
  85.  
  86.             //Sort
  87.             var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
  88.  
  89.             var sortDirection = Request["sSortDir_0"]; //asc or //desc
  90.  
  91.             //Sort
  92.  
  93.             if (sortColumnIndex == 6 || sortColumnIndex == 9 ||sortColumnIndex == 10
  94.                 || sortColumnIndex == 11 || sortColumnIndex == 12)
  95.             {
  96.                 Func<Rent, int> orderingIntFunction = (c => sortColumnIndex == 6 ? c.RentType :
  97.                                                             sortColumnIndex == 9 ? (c.OrderFee + c.RentFee) :
  98.                                                             sortColumnIndex == 10 ? c.TotalPayment : (c.OrderFee + c.RentFee - c.TotalPayment));
  99.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  100.             }
  101.             else if (sortColumnIndex == 7 || sortColumnIndex == 8)
  102.             {
  103.                 Func<Rent, DateTime?> orderingIntFunction = (c => sortColumnIndex == 7 ? c.CheckInDate :
  104.                                                             c.CheckOutDate);
  105.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  106.             }
  107.             else if (sortColumnIndex == 4 || sortColumnIndex == 5)
  108.             {
  109.                 Func<Rent, string> orderingIntFunction = (c => sortColumnIndex == 4 ? c.InvoiceID :
  110.                                                            c.Room.RoomName);
  111.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  112.             }
  113.  
  114.  
  115.             //Paging
  116.             var displayedList = filteredListItems.Skip(param.iDisplayStart).Take(param.iDisplayLength);
  117.  
  118.             //Get the final result after filter + paging
  119.             //PhuongLHK:Check current user  is admin by identity
  120.             bool isAdmin = IdentityManager.IsCurrentUserHasRole("admin"); ;
  121.             int count = 1;
  122.             var result = from c in displayedList
  123.                          select new IConvertible[]
  124.                                     {                                  
  125.                                         c.RentID,
  126.                                         c.RentID,
  127.                                         //System.Web.Security.Roles.GetRolesForUser().Contains("admin"),
  128.                                         isAdmin,
  129.                                         count++,
  130.                                         c.InvoiceID,
  131.                                         c.Room.RoomName,
  132.                                         c.RentType,
  133.                                         c.CheckInDate.ToString("dd-MM-yyyy HH:mm"),
  134.                                         c.CheckOutDate == null ? DateTime.Now.ToString("dd-MM-yyyy HH:mm") : ((DateTime)c.CheckOutDate).ToString("dd-MM-yyyy HH:mm"),
  135.                                         (c.OrderFee + c.RentFee).ToString("N0"),
  136.                                         c.TotalPayment.ToString("N0"),
  137.                                         (c.OrderFee + c.RentFee - c.TotalPayment).ToString("N0"),
  138.                                         c.RentStatus,
  139.                                         c.RentStatus,
  140.                                         c.RentStatus,
  141.                                         c.RentStatus,
  142.                                         c.RentStatus,
  143.                                     };
  144.             //Return json to DBTB
  145.             return Json(new
  146.             {
  147.                 sEcho = param.sEcho,
  148.                 iTotalRecords = allRents.Count(),
  149.                 iTotalDisplayRecords = filteredListItems.Count(),
  150.                 aaData = result
  151.             }, JsonRequestBehavior.AllowGet);
  152.         }
  153.  
  154.         public ActionResult LoadCafeByStatusDatatables(JQueryDataTableParamModel param, int status, string fromDate, string toDate)
  155.         {
  156.             IEnumerable<Rent> allRents;
  157.             if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
  158.             {
  159.  
  160.                 DateTime from = DateTime.Parse(fromDate);
  161.                 DateTime to = DateTime.Parse(toDate);
  162.  
  163.                 allRents = rentService.GetAllCafeRentByStatusAndDate(status, from, to);
  164.  
  165.             }
  166.             else
  167.             {
  168.                 allRents = rentService.GetAllCafeRentsByStatus(status);
  169.             }
  170.  
  171.             IEnumerable<Rent> filteredListItems;
  172.  
  173.             //Search
  174.             if (!string.IsNullOrEmpty(param.sSearch))
  175.             {
  176.  
  177.                 filteredListItems = allRents.Where(d => d.Room.RoomName.Contains(param.sSearch));
  178.             }
  179.             else
  180.             {
  181.                 filteredListItems = allRents;
  182.             }
  183.  
  184.             //Sort
  185.             var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
  186.  
  187.             var sortDirection = Request["sSortDir_0"]; //asc or //desc
  188.  
  189.             //Sort
  190.  
  191.             if (sortColumnIndex == 6 || sortColumnIndex == 9 || sortColumnIndex == 10
  192.                 || sortColumnIndex == 11 || sortColumnIndex == 12)
  193.             {
  194.                 Func<Rent, int> orderingIntFunction = (c => sortColumnIndex == 6 ? c.RentType :
  195.                                                             sortColumnIndex == 9 ? (c.OrderFee + c.RentFee) :
  196.                                                             sortColumnIndex == 10 ? c.TotalPayment : (c.OrderFee + c.RentFee - c.TotalPayment));
  197.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  198.             }
  199.             else if (sortColumnIndex == 7 || sortColumnIndex == 8)
  200.             {
  201.                 Func<Rent, DateTime?> orderingIntFunction = (c => sortColumnIndex == 7 ? c.CheckInDate :
  202.                                                             c.CheckOutDate);
  203.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  204.             }
  205.             else if (sortColumnIndex == 4 || sortColumnIndex == 5)
  206.             {
  207.                 Func<Rent, string> orderingIntFunction = (c => sortColumnIndex == 4 ? c.InvoiceID :
  208.                                                            c.Room.RoomName);
  209.                 filteredListItems = sortDirection == "asc" ? filteredListItems.OrderBy(orderingIntFunction) : filteredListItems.OrderByDescending(orderingIntFunction);
  210.             }
  211.  
  212.  
  213.             //Paging
  214.             var displayedList = filteredListItems.Skip(param.iDisplayStart).Take(param.iDisplayLength);
  215.  
  216.             //Get the final result after filter + paging
  217.             //PhuongLHK:Check current user  is admin by identity
  218.             bool isAdmin = IdentityManager.IsCurrentUserHasRole( "admin");
  219.             int count = 1;
  220.             var result = from c in displayedList
  221.                          select new IConvertible[]
  222.                                     {                                  
  223.                                         c.RentID,
  224.                                         c.RentID,
  225.                                         //System.Web.Security.Roles.GetRolesForUser().Contains("admin"),
  226.                                         true,
  227.                                         count++,
  228.                                         c.InvoiceID,
  229.                                         c.Room.RoomName,
  230.                                         c.RentType,
  231.                                         c.CheckInDate.ToString("dd-MM-yyyy HH:mm"),
  232.                                         c.CheckOutDate == null ? DateTime.Now.ToString("dd-MM-yyyy HH:mm") : ((DateTime)c.CheckOutDate).ToString("dd-MM-yyyy HH:mm"),
  233.                                         (c.OrderFee + c.RentFee).ToString("N0"),
  234.                                         c.TotalPayment.ToString("N0"),
  235.                                         (c.OrderFee + c.RentFee - c.TotalPayment).ToString("N0"),
  236.                                         c.RentStatus,
  237.                                         c.RentStatus,
  238.                                         c.RentStatus,
  239.                                         c.RentStatus,
  240.                                         c.RentStatus,
  241.                                     };
  242.             //Return json to DBTB
  243.             return Json(new
  244.             {
  245.                 sEcho = param.sEcho,
  246.                 iTotalRecords = allRents.Count(),
  247.                 iTotalDisplayRecords = filteredListItems.Count(),
  248.                 aaData = result
  249.             }, JsonRequestBehavior.AllowGet);
  250.         }
  251.  
  252.  
  253.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  254.         public ActionResult DeleteRent(int rentId)
  255.         {
  256.             try {
  257.                 rentService.DeleteRent(rentId);
  258.                 return Content("1");
  259.             }
  260.             catch (Exception) {
  261.                 return Content("0");
  262.             }
  263.         }
  264.  
  265.  
  266.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  267.         public ActionResult ApproveRent(int rentId)
  268.         {
  269.             bool rs = rentService.ApproveRent(rentId);
  270.             return Content(rs ? "1" : "0");
  271.         }
  272.  
  273.  
  274.  
  275.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  276.         public ActionResult ApproveAllRents(int type, List<int> ids)
  277.         {
  278.             bool rs = false;
  279.             rs = rentService.ApproveAllChosenRents(ids);
  280.             return Content(rs ? "1" : "0");
  281.         }
  282.  
  283.         //Hủy tất cả hóa đơn
  284.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  285.         public ActionResult DisabledAllRents(int type, List<int> ids)
  286.         {
  287.             bool rs = false;
  288.             rs = rentService.DisabledAllChosenRents(ids);
  289.             return Content(rs ? "1" : "0");
  290.         }
  291.  
  292.         //Xóa tất cả hóa đơn
  293.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  294.         public ActionResult DeleteAllRents(int type, List<int>  ids)
  295.         {
  296.             bool rs = false;
  297.             rs = rentService.DeleteAllChosenRents(ids);
  298.             return Content(rs ? "1" : "0");
  299.         }
  300.  
  301.  
  302.  
  303.         public ActionResult CancelRent(int rentId, string reason)
  304.         {
  305.             bool rs = rentService.CancelRent(rentId, reason);
  306.             return Content(rs ? "1" : "0");
  307.         }
  308.  
  309.         //[AuthorizeUnlessOnlyUser(Roles = "Admin")]
  310.         public ActionResult AcceptCancelRentRequest(int rentId)
  311.         {
  312.             bool rs = rentService.AcceptCancelRentRequest(rentId);
  313.             return Content(rs ? "1" : "0");
  314.         }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.         //public ActionResult ChangeRentStatus(int RentID, int fromStatus, int toStatus, int index)
  321.         //{
  322.         //    var item = _db.Rents.Where(r => r.RentID == RentID && r.RentStatus == fromStatus).FirstOrDefault();
  323.         //    if ((item != null)&& CheckValidTransition((RentStatusEnum)fromStatus,(RentStatusEnum)toStatus))
  324.         //    {
  325.  
  326.         //        if (toStatus == (int)RentStatusEnum.DeletePermanent)
  327.         //        {
  328.         //            _db.Rents.DeleteOnSubmit(item);
  329.  
  330.         //        }
  331.         //        else
  332.         //        {
  333.         //             item.RentStatus = toStatus;
  334.         //        }
  335.  
  336.  
  337.         //        // Neu dang o trang thai InStay chuyển qua trang thai khac se phai cap nhat lai trang thai cua phong
  338.         //        if (fromStatus == (int)RentStatusEnum.InStay)
  339.         //        {
  340.         //            item.Room.RoomStatus = (int) RoomStatusEnum.Prepare;
  341.         //        }
  342.  
  343.         //        _db.SubmitChanges();
  344.         //    }
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.         //    if (toStatus != (int)RentStatusEnum.DeletePermanent)
  352.         //    {
  353.         //        RentItemViewModel model = new RentItemViewModel();
  354.         //        model.Index = index;
  355.         //        model.Rent = item;
  356.         //        return PartialView("_RentItem", model);
  357.         //    }
  358.  
  359.         //    return Content("xx");
  360.         //}
  361.  
  362.  
  363.         //private bool CheckValidTransition(RentStatusEnum fromStatus, RentStatusEnum toStatus)
  364.         //{
  365.         //    if (fromStatus == toStatus)
  366.         //        return false;
  367.  
  368.         //    bool isAdmin = Roles.GetRolesForUser().Contains("admin");
  369.  
  370.         //    switch (toStatus)
  371.         //    {
  372.         //        case RentStatusEnum.InStay:
  373.         //            return false;
  374.         //        case RentStatusEnum.Paid:
  375.         //            if (fromStatus == RentStatusEnum.InStay)
  376.         //                return true;
  377.         //            else
  378.         //                return false;
  379.         //        case RentStatusEnum.Approved:
  380.         //            if (isAdmin && fromStatus == RentStatusEnum.Paid)
  381.         //                return true;
  382.         //            else
  383.         //                return false;
  384.  
  385.         //        case RentStatusEnum.WaitDisabled:
  386.         //            if (fromStatus == RentStatusEnum.InStay || fromStatus == RentStatusEnum.Paid || (fromStatus == RentStatusEnum.Approved && isAdmin))
  387.         //                return true;
  388.         //            else
  389.         //                return false;
  390.         //        case RentStatusEnum.Disabled:
  391.         //            if (isAdmin && fromStatus == RentStatusEnum.WaitDisabled)
  392.         //                return true;
  393.         //            else
  394.         //                return false;
  395.         //        case RentStatusEnum.DeletePermanent:
  396.         //            if (isAdmin && fromStatus == RentStatusEnum.Disabled)
  397.         //                return true;
  398.         //            else
  399.         //                return false;
  400.  
  401.         //    }
  402.         //    return false;
  403.  
  404.         //}
  405.  
  406.         //public ActionResult ExportToExcel(DateTime StartTime, DateTime EndTime)
  407.         //{
  408.         //    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" };
  409.         //    var item = from r in _db.Rents
  410.         //               where ((r.RentStatus != ((int)RentStatusEnum.Disabled)) &&
  411.         //                      ((r.RentStatus == (int)(RentStatusEnum.InStay)) ||
  412.         //                       ((r.CheckOutDate >= StartTime) && (r.CheckOutDate <= EndTime))))
  413.         //               select new RentItem
  414.         //               {
  415.         //                   RoomName = r.Room.RoomName,
  416.         //                   CheckInDate = r.CheckInDate,
  417.         //                   CheckOutDate = r.CheckOutDate,
  418.         //                   TotalMoney = r.RentFee + r.OrderFee,
  419.         //                   TotalPayment = r.TotalPayment,
  420.         //                   ContainMoney = r.RentFee + r.OrderFee - r.TotalPayment,
  421.         //                   RentStatus = r.RentStatus == (int)RentStatusEnum.Approved ? "Đã duyệt" :
  422.         //                                r.RentStatus == (int)RentStatusEnum.InStay ? "Đang ở" :
  423.         //                                r.RentStatus == (int)RentStatusEnum.Paid ? "Chờ duyệt" : "Hủy"
  424.         //                   ,
  425.         //                   RentType = r.RentType == (int)RentTypeEnum.Day ? "Thuê ngày" :
  426.         //                       r.RentType == (int)RentTypeEnum.Hour ? "Thuê giờ" : "Qua đêm"
  427.         //               };
  428.         //    string fileName = string.Format("BaoCao_{0}_{1}.xlsx", StartTime.ToShortDateString(), EndTime.ToShortDateString());
  429.         //    ExcelFacade excelFacade = new ExcelFacade();
  430.         //    MemoryStream stream = excelFacade.CreateExcelStream<RentItem>(fileName, item.ToList(), "Sheet1", headerStrings);
  431.         //    //byte[] content = new byte[stream.Length];
  432.         //    //stream.Read(content, 0, (int)stream.Length);
  433.  
  434.         //    byte[] content = stream.ToArray();
  435.  
  436.         //    return new BinaryFileResult()
  437.         //    {
  438.         //        FileName = fileName,
  439.         //        Content = content
  440.         //    };
  441.         //}
  442.  
  443.     }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement