Esempio n. 1
0
        private async Task AddBreakExecuteAsync()
        {
            var newBreak = new Break
            {
                StartBreakTime = DateTime.Now.TimeOfDay.TotalMinutes,
                EndBreakTime   = DateTime.Now.TimeOfDay.TotalMinutes,
                TimeAccount    = _currentAccounting
            };

            if (!_currentAccounting.IsClosed)
            {
                _timeAccountingContext.Breaks.Add(newBreak);
                await _timeAccountingContext.SaveChangesAsync()
                .ConfigureAwait(false);
            }
            else
            {
                Breaks.Add(newBreak);
            }
        }
Esempio n. 2
0
        private async Task CurrentExecuteAsync()
        {
            IsEnable = false;
            await Task.Delay(100);

            var currentDate       = DateTime.Today;
            var currentAccounting = await _timeAccountingContext.TimeAccounts
                                    .Include(x => x.Breaks)
                                    .SingleOrDefaultAsync(x => x.WorkDate == currentDate && !x.IsClosed)
                                    .ConfigureAwait(false);

            var todayWorks = await _timeAccountingContext.TimeAccounts.Where(x => x.WorkDate == currentDate && x.IsClosed).ToArrayAsync();

            var workedTime = todayWorks?.Sum(v => v.Breaks == null
                                    ? (v.EndWorkTime - v.StartWorkTime).TotalMinutes
                                    : (v.EndWorkTime - v.StartWorkTime).TotalMinutes
                                             - v.Breaks.Sum(d => d.EndBreakTime - d.StartBreakTime))
                             ?? 0;

            if (currentAccounting != null)
            {
                currentAccounting.Breaks = new ObservableCollection <Break>
                                           (
                    currentAccounting.Breaks.OrderBy(y => y.StartBreakTime)
                                           );
                await _navigationService.NavigateToAsync
                (
                    _accountFactory(currentAccounting, workedTime, false)
                );

                await Task.Delay(150);

                IsEnable = true;
                return;
            }
            var isWorkDay = DateService.IsWorking(currentDate.DayOfWeek);

            if (!isWorkDay)
            {
                var result = await Application.Current.MainPage.DisplayAlert(
                    TranslationCodeExtension.GetTranslation("WeekendAlertTitle"),
                    TranslationCodeExtension.GetTranslation("WeekendAlertText"),
                    TranslationCodeExtension.GetTranslation("YesWeekendAlertText"),
                    TranslationCodeExtension.GetTranslation("NoText"));

                if (!result)
                {
                    IsEnable = true;
                    return;
                }
                await Task.Delay(50);
            }
            var previousAccount = await _timeAccountingContext.TimeAccounts
                                  .Include(x => x.Breaks)
                                  .OrderByDescending(x => x.WorkDate)
                                  .FirstOrDefaultAsync(x => x.WorkDate < currentDate && !x.IsClosed)
                                  .ConfigureAwait(false);

            if (previousAccount != null && workedTime <= default(double))
            {
                if (previousAccount.IsStarted)
                {
                    previousAccount.IsClosed    = true;
                    previousAccount.EndWorkTime = new TimeSpan(23, 59, 59);

                    if (previousAccount.StartWorkTime > previousAccount.EndWorkTime)
                    {
                        previousAccount.StartWorkTime = previousAccount.EndWorkTime;
                    }

                    var workTime = previousAccount.Breaks == null
                        ? (previousAccount.EndWorkTime - previousAccount.StartWorkTime).TotalMinutes
                        : (previousAccount.EndWorkTime - previousAccount.StartWorkTime).TotalMinutes
                                   - previousAccount.Breaks.Sum(d => d.EndBreakTime - d.StartBreakTime);
                    previousAccount.OverWork = previousAccount.IsWorking
                        ? workTime - DateService.WorkingTime(previousAccount.WorkDate.DayOfWeek)
                        : workTime;
                    _timeAccountingContext.Entry(previousAccount).State = EntityState.Modified;
                }
                else
                {
                    _timeAccountingContext.TimeAccounts.Remove(previousAccount);
                }

                await _timeAccountingContext.SaveChangesAsync()
                .ConfigureAwait(false);
            }

            var notClosedAccounts = await _timeAccountingContext.TimeAccounts
                                    .Include(x => x.Breaks)
                                    .Where(x => !x.IsClosed)
                                    .ToArrayAsync()
                                    .ConfigureAwait(false); // Can be if user change date on the device

            if (notClosedAccounts.Length != 0)
            {
                foreach (var notClosedAccount in notClosedAccounts)
                {
                    if (notClosedAccount.Breaks.Count != 0)
                    {
                        _timeAccountingContext.Breaks.RemoveRange(notClosedAccount.Breaks);
                    }
                    _timeAccountingContext.TimeAccounts.Remove(notClosedAccount);
                }
            }

            currentAccounting = new TimeAccount {
                WorkDate = currentDate, IsWorking = isWorkDay
            };
            _timeAccountingContext.TimeAccounts.Add(currentAccounting);
            await _timeAccountingContext.SaveChangesAsync()
            .ConfigureAwait(false);

            await _navigationService.NavigateToAsync
            (
                _accountFactory(currentAccounting, workedTime, false)
            );

            await Task.Delay(150);

            IsEnable = true;
        }
Esempio n. 3
0
        private void PopulateFromViewModel(Break vm)
        {
            StartDinnerTmPck.Time = TimeSpan.FromMinutes(vm.StartBreakTime);
            EndDinnerTmPck.Time   = TimeSpan.FromMinutes(vm.EndBreakTime);

            Observable.FromEventPattern <EventHandler, EventArgs>(
                h => EndDinnerBtn.Clicked += h,
                h => EndDinnerBtn.Clicked -= h)
            .Throttle(TimeSpan.FromMilliseconds(100), RxApp.MainThreadScheduler)
            .Subscribe(_ =>
            {
                EndDinnerTmPck.Time = DateTime.Now.TimeOfDay;
                if (StartDinnerTmPck.Time > EndDinnerTmPck.Time)
                {
                    StartDinnerTmPck.Time = EndDinnerTmPck.Time;
                }
            });

            Observable.FromEventPattern <EventHandler, EventArgs>(
                h => StartDinnerBtn.Clicked += h,
                h => StartDinnerBtn.Clicked -= h)
            .Throttle(TimeSpan.FromMilliseconds(100), RxApp.MainThreadScheduler)
            .Subscribe(_ =>
            {
                StartDinnerTmPck.Time = DateTime.Now.TimeOfDay;
                if (StartDinnerTmPck.Time > EndDinnerTmPck.Time)
                {
                    EndDinnerTmPck.Time = StartDinnerTmPck.Time;
                }
            });

            this.WhenAnyValue(x => x.StartDinnerTmPck.Time)
            .Skip(1)
            .Where(x => Math.Abs(x.TotalMinutes - ViewModel.StartBreakTime) > 0.017)
            .Subscribe(async _ =>
            {
                if (StartDinnerTmPck.Time > EndDinnerTmPck.Time)
                {
                    EndDinnerTmPck.Time = StartDinnerTmPck.Time;
                }

                ViewModel.StartBreakTime = StartDinnerTmPck.Time.TotalMinutes;

                if (ViewModel.TimeAccount.IsClosed)
                {
                    return;
                }
                _timeAccountingContext.Entry(ViewModel).State = EntityState.Modified;
                await _timeAccountingContext.SaveChangesAsync()
                .ConfigureAwait(false);
            });

            this.WhenAnyValue(x => x.EndDinnerTmPck.Time)
            .Skip(1)
            .Where(x => Math.Abs(x.TotalMinutes - ViewModel.EndBreakTime) > 0.017)
            .Subscribe(async _ =>
            {
                if (StartDinnerTmPck.Time > EndDinnerTmPck.Time)
                {
                    StartDinnerTmPck.Time = EndDinnerTmPck.Time;
                }

                ViewModel.EndBreakTime = EndDinnerTmPck.Time.TotalMinutes;

                if (ViewModel.TimeAccount.IsClosed)
                {
                    return;
                }
                _timeAccountingContext.Entry(ViewModel).State = EntityState.Modified;
                await _timeAccountingContext.SaveChangesAsync()
                .ConfigureAwait(false);
            });
        }