/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     $eventId = Event::assign()->id;
     foreach (range(1, 8) as $index) {
         Team::create(['name' => $faker->words($faker->numberBetween(2, 4), true), 'number' => $faker->unique()->numberBetween(90, 320), 'event_id' => $eventId]);
     }
 }
 /**
  * Returns an array of Events
  *
  * @return array
  */
 public static function getEvents()
 {
     $events = [];
     foreach (Event::orderBy('start_date', 'desc')->get() as $event) {
         $events[$event->id] = $event->name;
     }
     return $events;
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     $teamIds = Team::lists('id')->toArray();
     $userIds = User::lists('id')->toArray();
     $eventId = Event::current()->id;
     foreach ($teamIds as $teamId) {
         for ($i = 1; $i <= $faker->numberBetween(2, floor(count($userIds) / count($teamIds))); $i++) {
             DB::table('users_pivot_team_user')->insert(['team_id' => $teamId, 'user_id' => $faker->unique()->randomElement($userIds), 'event_id' => $eventId]);
         }
     }
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Event::create(['name' => "2015 Fishing Festival", 'start_date' => (new Carbon("2015-09-04"))->format("d/m/Y"), 'end_date' => (new Carbon("2015-09-13"))->format("d/m/Y"), 'notes' => '2015 Festival notes.']);
     Event::create(['name' => "2016 Fishing Festival", 'start_date' => (new Carbon())->subMonth()->format("d/m/Y"), 'end_date' => (new Carbon())->addMonth()->format("d/m/Y"), 'notes' => 'Best fishing festival ever!!!']);
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param WeighInsRequest|Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(WeighInsRequest $request)
 {
     // TODO: extract this as it is also used in weigh ins request
     // Clear up the entries
     $request['entries'] = array_filter(array_map(function ($entry) {
         return array_filter($entry);
     }, $request['entries']));
     // If any exceptions are thrown any DB operations
     // will be automatically rolled back.
     DB::transaction(function () use($request) {
         $weighIn = new WeighIn();
         // Deal with boat section
         if (!empty($request['boat']) && !empty($request['boat']['name'])) {
             if (isset($request['boat']['id'])) {
                 // assign a boat to the weigh-in
                 $boat = Boat::findOrFail($request['boat']['id']);
             } else {
                 // create a new boat
                 $boat = new Boat();
                 $boat->name = $request['boat']['name'];
                 $boat->charter = isset($request['boat']['charter']) ? true : false;
                 if (!empty($request['boat']['skipper']['id'])) {
                     $boat->skipper()->associate(User::find($request['boat']['skipper']['id']));
                 }
                 $boat->save();
             }
             $weighIn->boat()->associate($boat);
         }
         // TODO: discuss with Rob if it makes sense
         //            if(!empty($request['team_id'])){
         //                $weighIn->team()->associate(Team::find($request['team_id']));
         //            }
         $weighIn->date = $request['date'];
         $weighIn->entry_ticket_number = $request['entry_ticket_number'];
         $weighIn->sheet_number = $request['sheet_number'];
         $weighIn->team_number = !empty($request['team_number']) ? $request['team_number'] : null;
         $weighIn->event()->associate(Event::find($request['event_id']));
         $weighIn->site()->associate(Site::find($request['weigh_in_site_id']));
         $weighIn->user()->associate(User::find($request['user_id']));
         $weighIn->save();
         foreach ($request['entries'] as $e) {
             $entry = new Entry();
             $entry->weight = Helpers::toDrams($e['species_weight']);
             $entry->percentage = Weight::calculatePercentage($e['species_id'], $e['sector_id'], $e['species_weight']);
             $entry->sector()->associate(Sector::find($e['sector_id']));
             $entry->location()->associate(Location::find($e['location_id']));
             $entry->event()->associate(Event::find($request['event_id']));
             $entry->species()->associate(Species::find($e['species_id']));
             $entry->user()->associate(User::find($request['user_id']));
             $entry->weighIn()->associate($weighIn);
             $entry->save();
             if (isset($e['categories'])) {
                 $categories = [];
                 foreach ($e['categories'] as $catId) {
                     $categories[$catId] = ['event_id' => (int) $request['event_id']];
                 }
                 $entry->categories()->sync($categories);
             }
         }
         //dd($request);
     });
     flash()->success('Success!', "New weigh-in has been created!");
     return redirect(route('admin.weigh-ins.create'));
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $event = Event::findOrFail($id);
     $eventName = "{$event->name}";
     $event->delete();
     flash()->overlay('Success!', "Event {$eventName} has been deleted!", 'success');
     return redirect(route('admin.settings.events.index'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     // If any exceptions are thrown any DB operations
     // will be automatically rolled back.
     DB::transaction(function () use($request, $id) {
         $team = Team::find($id);
         $team->name = $request['name'];
         $team->number = $request['number'];
         $team->event()->associate(Event::find($request['event_id']));
         if (!empty($request['members'])) {
             $this->syncTeamMembers($request, $team);
         } else {
             if (!$team->members->isEmpty()) {
                 $team->members()->detach();
             }
         }
         $team->save();
     });
     flash()->success('Success!', "Team data has been updated!");
     return redirect(route('admin.weigh-ins.teams.index'));
 }
 public function index()
 {
     $events = Event::orderBy('id')->get();
     return $this->respond(['currentEvent' => $this->eventsTransformer->transform(Event::assign()), 'data' => $this->eventsTransformer->transformCollection($events->all())]);
 }
 public function anglers()
 {
     $event = Event::current();
     $users = User::with(['categories', 'club'])->whereHas('weighIns', function ($query) use($event) {
         $query->where('event_id', '=', $event->id);
     })->get()->transform(function ($v) use($event) {
         $categories = null;
         if (!is_null($v->categories)) {
             foreach ($v->categories as $cat) {
                 $categories[] = ['id' => $cat['id'], 'name' => $cat['name']];
             }
         }
         $bestFish = Entry::with('species')->where('user_id', '=', $v['id'])->where('event_id', '=', $event->id)->orderBy('percentage', 'desc')->select('percentage', 'weight', 'species_id')->first();
         return ['id' => $v['id'], 'fullName' => $v['last_name'] . ' ' . $v['first_name'], 'categories' => $categories, 'club' => ['id' => $v['club']['id'], 'name' => $v['club']['name']], 'fishCount' => Entry::where('user_id', '=', $v['id'])->where('event_id', '=', $event->id)->get()->count(), 'bestFish' => ['name' => $bestFish['species']['name'], 'weight' => Helpers::fromDrams($bestFish['weight']), 'percentage' => round($bestFish['percentage']) . "%"]];
     })->toArray();
     return $this->respond(['data' => $users ?: null]);
 }
 /**
  * Returns an event based on current date
  *
  * @return \FishingFestival\Models\Events\Event
  */
 public static function assign()
 {
     $today = (new Carbon())->toDateString();
     return Event::where('start_date', '<=', $today)->where('end_date', '>=', $today)->first();
 }