public List<CustomerPaymentViewModel> GetAllCustomerPayment()
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                List<CustomerPaymentViewModel> customerPaymentViewModel = (from customerPayment in objContext.CustomerPayments
                                                                           join customer in objContext.Customers on customerPayment.CustomerId equals customer.CustomerId
                                                                           join customerInvoice in objContext.CustomerInvoices on customerPayment.InvoiceId equals customerInvoice.InvoiceId
                                                                           select new CustomerPaymentViewModel
                                                                           {
                                                                               CustomerPaymentId=customerPayment.CustomerPaymentId,
                                                                               CustomerName = customer.Name,
                                                                               CustomerId = customer.CustomerId,
                                                                               PaymentDate=customerPayment.PaymentDate,
                                                                               InvoiceNo = customerInvoice.InvoiceNo,
                                                                               InvoiceId = customerPayment.InvoiceId,
                                                                               BeginDate = customerInvoice.BeginDate,
                                                                               EndDate = customerInvoice.EndDate,
                                                                               InvoiceDate = customerInvoice.InvoiceDate,
                                                                               Amount = customerPayment.Amount,
                                                                               TotalHours = customerInvoice.TotalHours,
                                                                               Comments = customerPayment.Comments,
                                                                               CreatedBy = customerPayment.CreatedBy,
                                                                               CreatedDate = customerPayment.CreatedDate,
                                                                               ModifiedBy = customerPayment.ModifiedBy,
                                                                               ModifiedDate = customerPayment.ModifiedDate,
                                                                               IsDeleted = customerPayment.IsDeleted,
                                                                               DeletedBy = customerPayment.DeletedBy,
                                                                               DeletedDate = customerPayment.DeletedDate
                                                                           }).Where(i=>i.IsDeleted==false).ToList();
                if (customerPaymentViewModel == null)
                    return null;

                return customerPaymentViewModel;
            }
        }
           public void LogException(Exception e, string extraInfo = null)
           {
           SecurityAgencyEntities context = new SecurityAgencyEntities();

               Error_Log obj = new Error_Log();
               obj.Message = e.Message;

               if (!string.IsNullOrEmpty(extraInfo))
                   obj.Message = obj.Message + "<br/> Extar Info :" + extraInfo;

               obj.Source = e.Source;
               obj.StackTrace = e.StackTrace;
               obj.TargetSite = e.TargetSite.ToString();
               obj.ErrorDate = DateTime.Now;
               obj.ExceptionDetail = e.ToString();

               DateTime dt = (DateTime)obj.ErrorDate;


               context.Error_Log.Add(obj);
               try
               {
                   context.SaveChanges();

               }
               catch
               {
             }
       }
        public List<CustomerInvoiceViewModel> GetAllCustomerInvoice()
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                List<CustomerInvoiceViewModel> customerInvoice = (from objcustomer in objContext.Customers
                                                                  join objcustomerinvoice in objContext.CustomerInvoices on objcustomer.CustomerId equals objcustomerinvoice.CustomerId
                                                                  select new CustomerInvoiceViewModel
                                                                  {
                                                                      InvoiceId = objcustomerinvoice.InvoiceId,
                                                                      CustomerId = objcustomerinvoice.CustomerId,
                                                                      CustomerName = objcustomer.Name,
                                                                      HourlyRate=objcustomerinvoice.HourlyRate,
                                                                      InvoiceNo = objcustomerinvoice.InvoiceNo,
                                                                      InvoiceDate = objcustomerinvoice.InvoiceDate,
                                                                      BeginDate = objcustomerinvoice.BeginDate,
                                                                      EndDate = objcustomerinvoice.EndDate,
                                                                      TotalHours = objcustomerinvoice.TotalHours,
                                                                      Amount = objcustomerinvoice.Amount,
                                                                      Description = objcustomerinvoice.Description,
                                                                      InternalComments = objcustomerinvoice.InternalComments,
                                                                      CreatedBy = objcustomerinvoice.CreatedBy,
                                                                      CreatedDate = objcustomerinvoice.CreatedDate,
                                                                      ModifiedBy = objcustomerinvoice.ModifiedBy,
                                                                      IsDeleted = objcustomerinvoice.IsDeleted,
                                                                      DeletedBy = objcustomerinvoice.DeletedBy,
                                                                      DeletedDate = objcustomerinvoice.DeletedDate

                                                                  }).Where(i=>i.IsDeleted==false).ToList();

                if (customerInvoice == null)
                    return null;

                return customerInvoice;
            }
        }
        public void GetAllPermissions(int RoleId)
        {

            SecurityAgencyEntities dbcontext = new SecurityAgencyEntities();
            List<PermissionViewModel> permission = (from u in dbcontext.Users
                                                    join rp in dbcontext.RolePermissions on u.RoleId equals rp.RoleId
                                                    join p in dbcontext.Permissions on new { a = rp.PermissionId } equals new { a = p.PermissionId }
                                                    where u.RoleId == RoleId
                                                    select new PermissionViewModel
                            {
                                ModuleId = p.ModuleId,
                                UserId = u.UserID,
                                RoleId = u.RoleId,
                                PermissionId = rp.PermissionId,
                                Name = p.Name,

                            }).ToList();

            Session["result"] = permission;

        }
        public List<GuardPaymentViewModel> GetAllGuardPayment()
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                List<GuardPaymentViewModel> guardPaymentViewModel = (from guardPayment in objContext.GuardPayments
                                                                     join guard in objContext.Guards on guardPayment.GuardId equals guard.GuardId
                                                                     where guardPayment.IsDeleted==false
                                                                     select new GuardPaymentViewModel
                                                                        {
                                                                            GuardPaymentId = guardPayment.GuardPaymentId,
                                                                            GuardId = guardPayment.GuardId,
                                                                            PaymentDate = guardPayment.PaymentDate,
                                                                            HourlyRate=guardPayment.HourlyRate,
                                                                            StartDate = guardPayment.StartDate,
                                                                            EndDate = guardPayment.EndDate,
                                                                            Amount = guardPayment.Amount,
                                                                            GuardName = guard.Name
                                                                        }).ToList();
                if (guardPaymentViewModel == null)
                    return null;

                return guardPaymentViewModel;
            }
        }
        public List<GuardPaymentViewModel> GetGuardPaymentReport(int GuardId)
        {
            using (SecurityAgencyEntities objSecurityAgencyEntities = new SecurityAgencyEntities())
            {
                List<getGuardPayment_Result> objectGuard = objSecurityAgencyEntities.getGuardPayment().ToList();

                if (objectGuard == null)
                    return null;

                if (GuardId!=0)
                {
                    objectGuard = objectGuard.Where(i => i.Guardid == GuardId).ToList();
                }
                

                Mapper.CreateMap<getGuardPayment_Result, GuardPaymentViewModel>();
                return Mapper.Map<List<getGuardPayment_Result>, List<GuardPaymentViewModel>>(objectGuard);
            }
        }
        public GuardPaymentViewModel GetPaymentAmount(string startDate, string endDate, int guardId)
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                DateTime _startDate = Convert.ToDateTime(startDate);
                DateTime _endDate = Convert.ToDateTime(endDate);
                //Get Guard Hourly Rate
                //GuardHourlyRate objectGuardHourlyRate = _repository.GetAll<GuardHourlyRate>().Where(x => x.GuardId == guardId).OrderByDescending(i => i.HourlyRateId).FirstOrDefault();
                //Get Last Start and End Date
                GuardPayment objectGuardPayment = objContext.GuardPayments.Where(i => i.GuardId == guardId && i.IsDeleted == false).OrderByDescending(i => i.EndDate).FirstOrDefault();
                Guard objectGuard = objContext.Guards.Where(i => i.GuardId == guardId).FirstOrDefault();
                if (objectGuardPayment != null)
                {
                    _startDate = objectGuardPayment.EndDate;
                    _endDate = objectGuardPayment.EndDate.AddDays(7);
                }
                List<GuardPaymentViewModel> guardPaymentViewModel = (from dailyLog in objContext.DailyLogs                                                                
                                                                 where (dailyLog.Dated > _startDate && dailyLog.Dated < _endDate)
                                                                 && (dailyLog.IsDeleted == false)
                                                                 && dailyLog.GuardId == guardId
                                                                 select new GuardPaymentViewModel
                                                                {
                                                                    TotalHours = dailyLog.Hours,
                                                                }).ToList();

                if (guardPaymentViewModel == null)
                    return null;

                GuardPaymentViewModel objectGuardPaymentViewModel = new GuardPaymentViewModel();
                objectGuardPaymentViewModel.HourlyRate = objectGuard.HourlyRate;
                objectGuardPaymentViewModel.TotalHours = guardPaymentViewModel.Sum(i => i.TotalHours);
                objectGuardPaymentViewModel.Amount = objectGuardPaymentViewModel.TotalHours * objectGuard.HourlyRate;
                objectGuardPaymentViewModel.StartDate = _startDate;
                objectGuardPaymentViewModel.EndDate = _endDate;
                return objectGuardPaymentViewModel;
            }


        }
        public ActionResult PermissionUserPopup(int id)
        {
            PermissionViewModel permissionviewmodel = new PermissionViewModel();
            permissionviewmodel.Rolelist = new SelectList(_userComponent.GetAllRole(), "RoleId", "Role1");
            permissionviewmodel.Modulelist = new SelectList(_userComponent.GetAllModules(), "ModuleId", "ModuleName");
            //permissionviewmodel .PermissionList=new SelectList(_userComponent.GetAllPermissions(),"PermissionId","Name");
            SecurityAgencyEntities dbcontext = new SecurityAgencyEntities();
            var query = (from m in dbcontext.Modules
                         join p in dbcontext.Permissions on m.ModuleId equals p.ModuleId
                         select new PermissionViewModel
                           {
                               ModuleId = m.ModuleId,
                               ModuleName = m.ModuleName,
                               PermissionId = p.PermissionId,
                               Name = p.Name

                           });

            permissionviewmodel.PermissionList = query.ToList();


            return PartialView("/Views/Permission/Permission.cshtml", permissionviewmodel);

        }
        public void AddRolePermissions(int RoleId, string Role, List<string> Permissions, int UserId)
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                RoleViewModel objectRoleViewModel = new RoleViewModel();

                objectRoleViewModel.RoleId = RoleId;
                objectRoleViewModel.Role1 = Role;
                objectRoleViewModel.Last_ModifiedBy = objectRoleViewModel.CreatedBy = UserId;
                objectRoleViewModel.Last_ModifiedDate = objectRoleViewModel.CreatedDate = DateTime.Now;
                objectRoleViewModel.IsDeleted = false;
                objectRoleViewModel.Active = true;
                RoleId = CreateRole(objectRoleViewModel);

                //Removing all previous permissions
                List<RolePermission> objectRolePermission = objContext.RolePermissions.Where(i => i.RoleId == RoleId).ToList();
                objContext.RolePermissions.RemoveRange(objectRolePermission);

                foreach (string strPermission in Permissions)
                {
                    RolePermission objectRolePermissionNew = new RolePermission();
                    objectRolePermissionNew.RoleId = RoleId;
                    objectRolePermissionNew.PermissionId = Convert.ToInt32(strPermission.Replace("chk_", ""));
                    objectRolePermissionNew.CreatedBy = UserId;
                    objectRolePermissionNew.CreatedDate = DateTime.Now;
                    objectRolePermissionNew.IsDeleted = false;
                    objContext.RolePermissions.Add(objectRolePermissionNew);
                }
                objContext.SaveChanges();
            }
        }
        public List<PermissionViewModel> GetPermissionForRoles(int RoleId)
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                List<RolePermission> permissionlist = _repository.GetAll<RolePermission>().Where(i=>i.RoleId==RoleId && i.IsDeleted==false).ToList();

                if (permissionlist == null)
                    return null;

                Mapper.CreateMap<RolePermission, PermissionViewModel>();

                return Mapper.Map<List<RolePermission>, List<PermissionViewModel>>(permissionlist);
            }
        }
        public List<PermissionViewModel> GetAllPermissions()
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                List<getRolePermissions_Result> permissionlist = objContext.getRolePermissions().ToList();

                if (permissionlist == null)
                    return null;

                Mapper.CreateMap<getRolePermissions_Result, PermissionViewModel>();

                return Mapper.Map<List<getRolePermissions_Result>, List<PermissionViewModel>>(permissionlist);
            }
        }
 public int CheckInvoiceOverlap(int customerId, string beginDate, string endDate, int inoviceId)
 {
     using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
     {
         DateTime _beginDate = Convert.ToDateTime(beginDate);
         DateTime _endDate = Convert.ToDateTime(endDate);
         var overlapDailyLog = objContext.CustomerInvoices.Where(i => i.IsDeleted == false && i.InvoiceId != inoviceId && i.CustomerId == customerId && (
                                                                     (_beginDate >= i.BeginDate && _beginDate <= i.EndDate)
                                                                 ||  (_endDate >= i.BeginDate && _endDate <= i.EndDate)
                                                                 ||  (i.BeginDate >= _beginDate && i.BeginDate <= _endDate)
                                                                 ||  (i.EndDate >= _beginDate && i.EndDate <= _endDate)
                                                                 )).FirstOrDefault();
         if (overlapDailyLog == null)
         {
             return 0;
         }
         else
         {
             return overlapDailyLog.InvoiceNo;
         }
     }
 }
 public bool CheckDailyLogOverlap(int customerId, string beginDate)
 {
     using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
     {
         DateTime _beginDate = Convert.ToDateTime(beginDate);
         var overlapDailyLog = objContext.CustomerInvoices.Where(i => i.IsDeleted == false && i.CustomerId == customerId && (i.BeginDate < _beginDate && i.EndDate >= _beginDate)).FirstOrDefault();
         if(overlapDailyLog==null)
         {
             return false;
         }
         else
         {
             return true;
         }
     }
    
 }
        public List<CustomerInvoiceViewModel> GetCustomersInvoiceForReport(string startDate, string endDate, int customerId, string invoiceType)
        {
            DateTime? _startDate = null;
            DateTime? _endDate = null;
            if (startDate != "") { _startDate = Convert.ToDateTime(startDate); };
            if (endDate != "") { _endDate = Convert.ToDateTime(endDate); };
            SecurityAgencyEntities ob = new SecurityAgencyEntities();
            List<getAllCustomerInvoice_Result> customer = ob.getAllCustomerInvoice(_startDate, _endDate).ToList();

            if(customerId!=0)
            {
                customer = customer.Where(i => i.customerid == customerId).ToList();
            }
            ////Check For invoiceType
            //if (invoiceType == SecurityAgency.Common.Utility.EnumUtility.InvoiceType.Paid.ToString())
            //{
            //    customer = customer.Where(i => i.CustomerPaymentId != 0).ToList();
            //}
            //else if (invoiceType == SecurityAgency.Common.Utility.EnumUtility.InvoiceType.Unpaid.ToString())
            //{
            //    customer = customer.Where(i => i.CustomerPaymentId == 0).ToList();
            //}
            if (customer == null)
                return null;

            Mapper.CreateMap<getAllCustomerInvoice_Result, CustomerInvoiceViewModel>();
            return Mapper.Map<List<getAllCustomerInvoice_Result>, List<CustomerInvoiceViewModel>>(customer);
        }
        public CustomerInvoiceViewModel GenerateInvoice(int invoiceId)
        {
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                CustomerInvoiceViewModel objectCustomerInvoiceViewModel = new CustomerInvoiceViewModel();
                objectCustomerInvoiceViewModel = (from customer in objContext.Customers
                                                  join customerInvoice in objContext.CustomerInvoices on customer.CustomerId equals customerInvoice.CustomerId
                                                  where customerInvoice.InvoiceId == invoiceId
                                                  select new CustomerInvoiceViewModel
                                                  {
                                                      InvoiceNo = customerInvoice.InvoiceNo,
                                                      Name = customer.Name,
                                                      HourlyRate=customerInvoice.HourlyRate,
                                                      Address = customer.Address,
                                                      Zip = customer.Zip,
                                                      ContactNo = customer.ContactNo,
                                                      InvoiceDate = customerInvoice.InvoiceDate,
                                                      TotalHours = customerInvoice.TotalHours,
                                                      Description = customerInvoice.Description,
                                                      Amount = customerInvoice.Amount,
                                                      BeginDate = customerInvoice.BeginDate,
                                                      EndDate = customerInvoice.EndDate,
                                                      CustomerId = customerInvoice.CustomerId
                                                  }).FirstOrDefault();

                //////Get values from hourlog table
                List<getCustomerInvoiceAmount_Result> objectgetCustomerInvoiceAmount_Result = objContext.getCustomerInvoiceAmount(objectCustomerInvoiceViewModel.BeginDate, objectCustomerInvoiceViewModel.EndDate, objectCustomerInvoiceViewModel.CustomerId).ToList();

                if (objectCustomerInvoiceViewModel == null)
                    return null;

                return objectCustomerInvoiceViewModel;

            }
        }
        public CustomerInvoiceViewModel GetCustomerHoursAndAmount(int customerId, string type, string beginDate, string endDate, int inoviceId)
        {
            CustomerInvoiceViewModel objectCustomerInvoiceViewModel = new CustomerInvoiceViewModel();
            using (SecurityAgencyEntities objContext = new SecurityAgencyEntities())
            {
                if (string.IsNullOrEmpty(type))
                {
                    //Get beginDate and endDate first
                    CustomerInvoice objectCustomerInvoice = _repository.GetAll<CustomerInvoice>().Where(x => x.CustomerId == customerId && x.IsDeleted == false && x.InvoiceId!= inoviceId).OrderByDescending(i => i.EndDate).FirstOrDefault();
                    if (objectCustomerInvoice != null)
                    {
                        beginDate = objectCustomerInvoice.EndDate.AddDays(1).ToString();
                        endDate = objectCustomerInvoice.EndDate.AddDays(7).ToString();
                    }
                }
                //////Get values from hourlog table
                List<getCustomerInvoiceAmount_Result> objectgetCustomerInvoiceAmount_Result = objContext.getCustomerInvoiceAmount(Convert.ToDateTime(beginDate), Convert.ToDateTime(endDate), customerId).ToList();

                objectCustomerInvoiceViewModel.TotalHours = Convert.ToInt32(objectgetCustomerInvoiceAmount_Result.Sum(i=>i.TotHours));
                objectCustomerInvoiceViewModel.Amount = Convert.ToInt32(objectgetCustomerInvoiceAmount_Result.Sum(i=>i.Amount));
                objectCustomerInvoiceViewModel.BeginDate = Convert.ToDateTime(beginDate);
                objectCustomerInvoiceViewModel.EndDate = Convert.ToDateTime(endDate);
                objectCustomerInvoiceViewModel.HourlyRate = Convert.ToDecimal(objectgetCustomerInvoiceAmount_Result.FirstOrDefault().HourlyRate);
                return objectCustomerInvoiceViewModel;
            } 
        }
        public List<GuardViewModel> GetGuardForReport(string startDate, string endDate)
        {
            DateTime? _startDate = null;
            DateTime? _endDate = null;
            if (startDate != "") { _startDate = Convert.ToDateTime(startDate); };
            if (endDate != "") { _endDate = Convert.ToDateTime(endDate); };
            SecurityAgencyEntities objEntities = new SecurityAgencyEntities();
            List<getAllGuards_Result> guards = objEntities.getAllGuards(_startDate, _endDate).ToList();

            if (guards == null)
                return null;

            Mapper.CreateMap<getAllGuards_Result, GuardViewModel>();
            return Mapper.Map<List<getAllGuards_Result>, List<GuardViewModel>>(guards);
        }
        //public void LogException(Exception e, string extraInfo = null)
        //{

        //    SecurityAgencyEntities context = new SecurityAgencyEntities();

        //    Error_Log obj = new Error_Log();
        //    obj.Message = e.Message;

        //    if (!string.IsNullOrEmpty(extraInfo))
        //        obj.Message = obj.Message + "<br/> Extar Info :" + extraInfo;

        //    obj.Source = e.Source;
        //    obj.StackTrace = e.StackTrace;
        //    obj.TargetSite = e.TargetSite.ToString();
        //    obj.ErrorDate = DateTime.Now;
        //    obj.ExceptionDetail = e.ToString();

        //    DateTime dt = (DateTime)obj.ErrorDate;


        //    context.Error_Log.InsertOnSubmit(obj);
        //    try
        //    {
        //        context.Error_Log.SubmitChanges();

        //    }
        //    catch
        //    {
        //    }

        //}
        public List<CustomerViewModel> GetCustomersForReport(string startDate, string endDate)
        {
            DateTime? _startDate = null;
            DateTime? _endDate = null;
            if (startDate != "") { _startDate = Convert.ToDateTime(startDate); };
            if (endDate != "") { _endDate = Convert.ToDateTime(endDate); };
            SecurityAgencyEntities ob = new SecurityAgencyEntities();
            List<getAllCustomers_Result> customer = ob.getAllCustomers(_startDate, _endDate).ToList();

            if (customer == null)
                return null;

            Mapper.CreateMap<getAllCustomers_Result, CustomerViewModel>();
            return Mapper.Map<List<getAllCustomers_Result>, List<CustomerViewModel>>(customer);
        }