1010use App \Contracts \AddsTimeTrackings ;
1111use Illuminate \Support \Facades \Validator ;
1212use Illuminate \Validation \ValidationException ;
13+ use DB ;
1314
1415class AddTimeTracking implements AddsTimeTrackings
1516{
@@ -25,38 +26,56 @@ public function add($employee, array $data, array $pauseTimes)
2526 Validator::make ($ data ,[
2627 'starts_at ' => ['required ' , $ this ->dateFormatter ->dateTimeFormatRule () ],
2728 'ends_at ' => ['required ' , $ this ->dateFormatter ->dateTimeFormatRule () , 'after_or_equal:starts_at ' ],
28- 'manual_pause ' => ['required ' , 'boolean ' ],
2929 'description ' => ['nullable ' , 'string ' ]
3030 ])->validateWithBag ('addTimeTracking ' );
3131
3232 $ startsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['starts_at ' ]);
3333 $ endsAt = $ this ->dateFormatter ->timeStrToCarbon ($ data ['ends_at ' ]);
3434
35- //check date
3635 $ this ->ensureDateIsNotBeforeEmploymentDate ($ employee , $ startsAt );
3736 $ this ->ensureDateIsNotTooFarInTheFuture ($ endsAt );
38-
39- //check time
4037 $ this ->ensureGivenTimeIsNotOverlappingWithExisting ($ employee , $ startsAt , $ endsAt );
4138
42- //check pause times
43- $ periods = PeriodCalculator::fromTimesArray ($ pauseTimes )->periods ;
39+ $ this ->validatePauseTimes (
40+ PeriodCalculator::fromTimesArray ($ pauseTimes ),
41+ $ startsAt ,
42+ $ endsAt
43+ );
4444
45- foreach ( $ periods as $ index => $ period ) {
46- $ this -> ensurePeriodIsNotTooSmall ( $ period );
47- $ this -> ensurePeriodsAreNotOverlapping ( $ periods , $ index , $ period );
48- $ this -> ensurePeriodWithinWorkingHours ( $ period , $ startsAt , $ endsAt );
49- }
45+ DB :: transaction ( function () use ( $ employee , $ startsAt , $ endsAt , $ data , $ pauseTimes ) {
46+ $ trackedTime = $ employee -> timeTrackings ()-> create ( array_merge ([
47+ ' location_id ' => $ employee -> currentLocation -> id ,
48+ ' starts_at ' => $ startsAt ,
49+ ' ends_at ' => $ endsAt ,
5050
51- $ trackedTime = $ employee ->timeTrackings ()->create (array_merge ([
52- 'location_id ' => $ employee ->currentLocation ->id ,
53- 'starts_at ' => $ startsAt ,
54- 'ends_at ' => $ endsAt
55- ], Arr::except ($ data , ['starts_at ' ,'ends_at ' ])));
51+ ], Arr::except ($ data , ['starts_at ' ,'ends_at ' ])));
52+
53+ $ trackedTime ->pauseTimes ()->createMany ($ pauseTimes );
54+
55+ $ trackedTime ->updatePauseTime ();
56+ });
5657
57- $ trackedTime ->pauseTimes ()->createMany ($ pauseTimes );
5858 }
5959
60+ protected function validatePauseTimes ($ pauseTimePeriodCalculator , $ startsAt , $ endsAt )
61+ {
62+ if (!$ pauseTimePeriodCalculator ->hasPeriods ()) {
63+ return ;
64+ }
65+
66+ $ pauseTimePeriodCalculator ->periods ->each (function ($ period , $ index ) use ($ pauseTimePeriodCalculator , $ startsAt , $ endsAt ) {
67+ $ this ->ensurePeriodIsNotTooSmall ($ period );
68+ $ this ->ensurePeriodsAreNotOverlapping ($ pauseTimePeriodCalculator ->periods , $ index , $ period );
69+ $ this ->ensurePeriodWithinWorkingHours ($ period , $ startsAt , $ endsAt );
70+ });
71+ }
72+
73+ protected function calculatePauseTimeFromDefaultRestingTimes ($ employee , $ workingTimeInSeconds )
74+ {
75+ return optional (
76+ $ employee ->defaultRestingTimes ()->firstWhere ('min_hours ' ,'<= ' ,$ workingTimeInSeconds )
77+ )->duration ->inSeconds ();
78+ }
6079
6180 protected function ensureDateIsNotTooFarInTheFuture ($ endsAt )
6281 {
@@ -115,7 +134,7 @@ protected function ensurePeriodWithinWorkingHours($period, $startsAt, $endsAt)
115134
116135 protected function ensurePeriodsAreNotOverlapping ($ periods , $ index , $ period )
117136 {
118- $ haystack = Arr::except ($ periods , [$ index ]);
137+ $ haystack = Arr::except ($ periods-> toArray () , [$ index ]);
119138 foreach ($ haystack as $ needle ) {
120139 /**
121140 * @var CarbonPeriod $period
0 commit comments