// GET: /LeaveRequest/Details/5
		public ActionResult Details(int? id, int empId)
		{
			if (acm.cekSession() == false)
				return RedirectToAction("Logout", "Account");

			lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
			if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
				return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var tlq = db.transLeaveRequests.Where(x => x.leaveRequestID == id && x.employeeID == empId).ToList();
			if (tlq == null)
			{
				return HttpNotFound();
			}

			transLeaveRequest transleaverequest = new transLeaveRequest();
			foreach (var a in tlq as IEnumerable<transLeaveRequest>)
			{
				transleaverequest.acknowledgeBy = a.acknowledgeBy;
				transleaverequest.acknowledgeDate = a.acknowledgeDate;
				transleaverequest.acknowledgeStatus = a.acknowledgeStatus;
				transleaverequest.approvedBy = a.approvedBy;
				transleaverequest.approvedDate = a.approvedDate;
				transleaverequest.approvedStatus = a.approvedStatus;
				transleaverequest.createdDate = a.createdDate;
				transleaverequest.createdUser = a.createdUser;
				transleaverequest.employeeID = a.employeeID;
				transleaverequest.category = a.category;
				transleaverequest.cutiInHoliday = a.cutiInHoliday;
				transleaverequest.leaveDescription = a.leaveDescription;
				transleaverequest.leaveEndDate = a.leaveEndDate;
				transleaverequest.leaveRequestID = a.leaveRequestID;
				transleaverequest.leaveStartDate = a.leaveStartDate;
				transleaverequest.modifiedDate = a.modifiedDate;
				transleaverequest.modifiedUser = a.modifiedUser;

				var employeeName = db.employees.Where(x => x.employeeID == a.employeeID).ToList();
				ViewBag.employeeName = employeeName[0].employeeName;
			}
			return View(transleaverequest);
		}
		public async Task<ActionResult> ApprovalProcess(string act, int id, int empID)
		{
			if (acm.cekSession() == false)
				return RedirectToAction("Logout", "Account");

			lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;

			string authorizer = string.Empty;
			ViewBag.ApprovalAuth = false;
			ViewBag.AcknowledgeAuth = false;
			foreach (string b in lvm.listAuthorize as List<string>)
			{
				string[] auth = b.Split('_');
				if (auth[0].ToString() == "A1" || auth[0].ToString() == "A2")
					if (b.Contains((Url.Action("")).Replace("/", "")))
					{
						authorizer += auth[0].ToString();
						if (auth[0].ToString() == "A1")
							ViewBag.ApprovalAuth = true;
						else if (auth[0].ToString() == "A2")
						{
							ViewBag.ApprovalAuth = true;
							ViewBag.AcknowledgeAuth = true;
						}
					}
			}
			if (ViewBag.ApprovalAuth == false && lvm.isAdmin == true)
				ViewBag.ApprovalAuth = true;

			var tlq = db.transLeaveRequests.AsNoTracking().Where(x => x.leaveRequestID == id && x.employeeID == empID).ToList();
			if (tlq == null)
			{
				return HttpNotFound();
			}

			transLeaveRequest model = new transLeaveRequest();
			foreach (var a in tlq as IEnumerable<transLeaveRequest>)
			{
				model.acknowledgeBy = a.acknowledgeBy;
				model.acknowledgeDate = a.acknowledgeDate;
				model.acknowledgeStatus = a.acknowledgeStatus;
				model.approvedBy = a.approvedBy;
				model.approvedDate = a.approvedDate;
				model.approvedStatus = a.approvedStatus;
				model.createdDate = a.createdDate;
				model.createdUser = a.createdUser;
				model.employeeID = a.employeeID;
				model.leaveDescription = a.leaveDescription;
				model.leaveEndDate = a.leaveEndDate;
				model.leaveRequestID = a.leaveRequestID;
				model.leaveStartDate = a.leaveStartDate;
				model.modifiedDate = a.modifiedDate;
				model.modifiedUser = a.modifiedUser;
			}

			if (authorizer == "A1")
			{
				model.approvedStatus = act == "approved" ? true : false;
				model.approvedDate = DateTime.Now;
				model.approvedBy = lvm.userID;
			}
			else if (authorizer == "A2")
			{
				model.acknowledgeStatus = act == "approved" ? true : false;
				model.acknowledgeDate = DateTime.Now;
				model.acknowledgeBy = lvm.userID;
			}
			else if (authorizer == "A1A2")
			{
				model.approvedStatus = act == "approved" ? true : false;
				model.approvedDate = DateTime.Now;
				model.approvedBy = lvm.userID;
				model.acknowledgeStatus = act == "approved" ? true : false;
				model.acknowledgeDate = DateTime.Now;
				model.acknowledgeBy = lvm.userID;

			}

			try
			{
				var bucho = "tampan";
				db.Entry(model).State = EntityState.Modified;
				db.SaveChanges();

				SqlCommand cmd = new SqlCommand();
				cmd.CommandText = "[dbo].[sp_recountSisaSaldoCuti]";
				cmd.Connection = new SqlConnection(con.conString);
				cmd.Connection.Open();
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.AddWithValue("@leaveRequestID", id);
				cmd.Parameters.AddWithValue("@year", DateTime.Now.Year);
				cmd.ExecuteNonQuery();
				cmd.Connection.Close();
			}
			catch (Exception exc)
			{
				var bucho = "tampan";
			}

			return RedirectToAction("Index");
		}
		public ActionResult Approval(int? id, int empId)
		{
			if (acm.cekSession() == false)
				return RedirectToAction("Logout", "Account");

			lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;

			ViewBag.ApprovalAuth = false;
			ViewBag.AcknowledgeAuth = false;
			foreach (string b in lvm.listAuthorize as List<string>)
			{
				string[] auth = b.Split('_');
				if (auth[0].ToString() == "A1" || auth[0].ToString() == "A2")
					if (b.Contains((Url.Action("")).Replace("/", "")))
					{
						if (auth[0].ToString() == "A1")
							ViewBag.ApprovalAuth = true;
						else if (auth[0].ToString() == "A2")
						{
							ViewBag.ApprovalAuth = true;
							ViewBag.AcknowledgeAuth = true;
						}
					}
			}
			if (ViewBag.ApprovalAuth == false && lvm.isAdmin == true)
				ViewBag.ApprovalAuth = true;

			if (ViewBag.ApprovalAuth == false && lvm.isAdmin == false)
				return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}

			var tlq = db.transLeaveRequests.Where(x => x.leaveRequestID == id && x.employeeID == empId).ToList();
			if (tlq == null)
			{
				return HttpNotFound();
			}

			transLeaveRequest transleaverequest = new transLeaveRequest();
			foreach (var a in tlq as IEnumerable<transLeaveRequest>)
			{
				transleaverequest.acknowledgeBy = a.acknowledgeBy;
				transleaverequest.acknowledgeDate = a.acknowledgeDate;
				transleaverequest.acknowledgeStatus = a.acknowledgeStatus;
				transleaverequest.approvedBy = a.approvedBy;
				transleaverequest.approvedDate = a.approvedDate;
				transleaverequest.approvedStatus = a.approvedStatus;
				transleaverequest.createdDate = a.createdDate;
				transleaverequest.createdUser = a.createdUser;
				transleaverequest.employeeID = a.employeeID;
				transleaverequest.leaveDescription = a.leaveDescription;
				transleaverequest.category = a.category;
				transleaverequest.cutiInHoliday = a.cutiInHoliday;
				transleaverequest.leaveEndDate = a.leaveEndDate;
				transleaverequest.leaveRequestID = a.leaveRequestID;
				transleaverequest.leaveStartDate = a.leaveStartDate;
				transleaverequest.modifiedDate = a.modifiedDate;
				transleaverequest.modifiedUser = a.modifiedUser;

				var employeeName = db.employees.Where(x => x.employeeID == a.employeeID).ToList();
				ViewBag.employeeName = employeeName[0].employeeName;
			}
			return View(transleaverequest);
		}
		public ActionResult Edit(transLeaveRequest transleaverequest)
		{
			if (acm.cekSession() == false)
				return RedirectToAction("Logout", "Account");

			lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
			if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
				return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

			if (ModelState.IsValid)
			{
				var originTLQ = db.transLeaveRequests.Where(x => x.leaveRequestID == transleaverequest.leaveRequestID).Single();

				originTLQ.modifiedDate = DateTime.Now;
				originTLQ.modifiedUser = lvm.userID;

				originTLQ.category = transleaverequest.category;
				originTLQ.leaveDescription = transleaverequest.leaveDescription;

				if (transleaverequest.approvedStatus != null &&
					transleaverequest.approvedStatus != originTLQ.approvedStatus)
				{
					originTLQ.approvedDate = DateTime.Now;
					originTLQ.approvedBy = lvm.userID;
					originTLQ.approvedStatus = transleaverequest.approvedStatus;
					if (originTLQ.approvedStatus == true)
					{
						var SisaCuti = db.sisaSaldoCutis.Where(x => x.employeeID == lvm.employeeId && x.year == DateTime.Now.Year).Single();

						var timespan = originTLQ.leaveEndDate - originTLQ.leaveStartDate;
						var JumlahPermintaan = timespan.Days + 1; //belum dihitung hari libur

						if (originTLQ.leaveStartDate == transleaverequest.leaveStartDate &&
							originTLQ.leaveEndDate == transleaverequest.leaveEndDate)
						{
							int cutiYgdisetujui = JumlahPermintaan - originTLQ.cutiInHoliday;
							editSisaCuti(SisaCuti, cutiYgdisetujui, originTLQ.category);
						}
						else
						{
							int cutiDiharilibur = FindCutiInHoliday(transleaverequest.leaveStartDate, transleaverequest.leaveEndDate);
							int cutiYgdisetujui = JumlahPermintaan - cutiDiharilibur;
							originTLQ.cutiInHoliday = cutiDiharilibur;
							editSisaCuti(SisaCuti, cutiYgdisetujui, originTLQ.category);
						}

						db.Entry(SisaCuti).State = EntityState.Modified;
						db.SaveChanges();
					}
				}

				if (transleaverequest.acknowledgeStatus != null &&
					transleaverequest.acknowledgeStatus != originTLQ.acknowledgeStatus)
				{
					originTLQ.acknowledgeDate = DateTime.Now;
					originTLQ.acknowledgeBy = lvm.userID;
					originTLQ.acknowledgeStatus = transleaverequest.acknowledgeStatus;
				}

				originTLQ.leaveStartDate = transleaverequest.leaveStartDate;
				originTLQ.leaveEndDate = transleaverequest.leaveEndDate;

				db.Entry(originTLQ).State = EntityState.Modified;
				db.SaveChanges();
				return RedirectToAction("Index");
			}
			else
			{
				var employeeName = db.employees.Where(x => x.employeeID == transleaverequest.employeeID).ToList();
				ViewBag.employeeName = employeeName[0].employeeName;
			}
			return View(transleaverequest);
		}
		public ActionResult Create(ViewModelCuti dataCuti)
		{
			var buch = "tampan";
			if (acm.cekSession() == false)
				return RedirectToAction("Logout", "Account");

			lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
			if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
				return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

			var timespan = dataCuti.leaveEndDate - dataCuti.leaveStartDate;
			var JumlahPermintaan = timespan.Days + 1; //belum dihitung hari libur

			int cutidiharilibur = FindCutiInHoliday(dataCuti.leaveStartDate, dataCuti.leaveEndDate); //jumlah cuti di hari libur pada tanggal yg diminta

			dataCuti.cutiInHoliday = cutidiharilibur;
			JumlahPermintaan = JumlahPermintaan - cutidiharilibur;
			
			var jumsisacuti = 0;
			if (dataCuti.category.Equals("tahunan"))
			{
				jumsisacuti = dataCuti.amount;
			}
			else if (dataCuti.category.Equals("persalinan"))
			{
				jumsisacuti = dataCuti.amount2;
			}
			else if (dataCuti.category.Equals("kemalangan"))
			{
				jumsisacuti = dataCuti.amount3;
			}
			else if (dataCuti.category.Equals("lain-lain"))
			{
				jumsisacuti = dataCuti.amount4;
			}


			if (JumlahPermintaan > jumsisacuti)
			{
				ModelState.AddModelError("leaveEndDate", "Jatah cuti tidak mencukupi permintaan anda");
			}

			if (DateTime.Compare(dataCuti.leaveEndDate, dataCuti.leaveStartDate) < 0)
			{
				ModelState.AddModelError("leaveEndDate", "Tanggal akhir tidak boleh kurang dari Tanggal awal");
			}

			if (ModelState.IsValid)
			{
				var CutiRequest = new transLeaveRequest
				{
					createdDate = DateTime.Now,
					createdUser = lvm.userID,
					employeeID = dataCuti.employeeID,
					leaveStartDate = dataCuti.leaveStartDate,
					leaveEndDate = dataCuti.leaveEndDate,
					category = dataCuti.category,
					leaveDescription = dataCuti.leaveDescription,
					cutiInHoliday = dataCuti.cutiInHoliday
				};
				db.transLeaveRequests.Add(CutiRequest);
				db.SaveChanges();
				return RedirectToAction("Index");
			}

			return View(dataCuti);
		}
        public ActionResult Edit(transLeaveRequest transleaverequest)
        {
            if (acm.cekSession() == false)
                return RedirectToAction("Logout", "Account");

            lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
            if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
                return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

            if (ModelState.IsValid)
            {
                transleaverequest.modifiedDate = DateTime.Now;
                transleaverequest.modifiedUser = lvm.userID;
                db.Entry(transleaverequest).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(transleaverequest);
        }