belongsTo( 'User' ); } public static function saveTarget( $targetID, $target ) { $target = (array) $target; if ( $targetID == 0 ) { $target = self::create( $target ); $targetID = $target['id']; } else { self::whereId( $targetID )->update( $target ); } return $targetID; } public static function getTargetHoursPerEmployee( $employeeID, $year, $monthID ) { $months = $monthID == 0 ? [1,2,3,4,5,6,7,8,9,10,11,12] : [$monthID]; $targetHours = 0; foreach($months as $month) { $targetHours += self::getCurrentMonthTarget( $employeeID, $year, $month); } return $targetHours; } public static function getCurrentMonthTarget( $employeeID, $year, $month ) { $monthTarget = Self::whereUserId($employeeID) ->where('startyear', '<=', $year) ->where('startmonth', '<=', $month) ->orderBy('startyear', 'DESC') ->orderBy('startmonth', 'DESC'); if( $monthTarget->count() == 0 ) return 0; $monthtarget = $monthTarget->first(); return $monthtarget->target; } }