public int?CreateUpdateGuardPayment(GuardPaymentViewModel guardPaymentViewModel)
        {
            GuardPayment guardPayment = null;

            if (guardPaymentViewModel.GuardPaymentId > 0)
            {
                guardPayment = _repository.Find <GuardPayment>(x => x.GuardPaymentId == guardPaymentViewModel.GuardPaymentId);
                if (guardPayment == null)
                {
                    return(null);
                }

                guardPayment.GuardId      = guardPaymentViewModel.GuardId;
                guardPayment.PaymentDate  = guardPaymentViewModel.PaymentDate;
                guardPayment.StartDate    = guardPaymentViewModel.StartDate;
                guardPayment.EndDate      = guardPaymentViewModel.EndDate;
                guardPayment.Amount       = guardPaymentViewModel.Amount;
                guardPayment.TotalHours   = guardPaymentViewModel.TotalHours;
                guardPayment.HourlyRate   = guardPaymentViewModel.HourlyRate;
                guardPayment.Comments     = guardPaymentViewModel.Comments;
                guardPayment.ModifiedBy   = guardPaymentViewModel.ModifiedBy;
                guardPayment.ModifiedDate = DateTime.Now;

                _repository.Modify <GuardPayment>(guardPayment);
                return(guardPayment.GuardPaymentId);
            }

            Mapper.CreateMap <GuardPaymentViewModel, GuardPayment>();
            guardPayment = Mapper.Map <GuardPaymentViewModel, GuardPayment>(guardPaymentViewModel);

            guardPayment.CreatedDate = DateTime.Now;
            guardPayment.CreatedBy   = guardPaymentViewModel.CreatedBy;
            guardPayment.IsDeleted   = false;
            return(_repository.Insert <GuardPayment>(guardPayment));
        }
        public GuardPaymentViewModel GetGuardPayment(int id)
        {
            GuardPayment customer = _repository.Find <GuardPayment>(x => x.GuardPaymentId == id);

            if (customer == null)
            {
                return(null);
            }

            Mapper.CreateMap <GuardPayment, GuardPaymentViewModel>();
            return(Mapper.Map <GuardPayment, GuardPaymentViewModel>(customer));
        }
        public int DeleteGuardPayment(int id, int UserId)
        {
            GuardPayment guardPayment = _repository.Find <GuardPayment>(x => x.GuardPaymentId == id);

            if (guardPayment == null)
            {
                return(0);
            }
            guardPayment.IsDeleted   = true;
            guardPayment.DeletedBy   = UserId;
            guardPayment.DeletedDate = DateTime.Now;
            return(_repository.Modify <GuardPayment>(guardPayment));
        }
        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);
            }
        }