tippspiel/pfosten/app/User.php

250 lines
7.2 KiB
PHP

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; // already implements CanResetPassword
use App\Season;
use App\Payment;
use App\Round;
use App\Game;
use Carbon;
use Log;
use DB;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'firstname', 'lastname', 'email', 'password', 'gets_reminders', 'iban', 'bankname' , 'blz', 'kontonr'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
// Only the current season.
public static function is_season_paid($user_id,$seasonId=-1) {
$season=Season::getCurrent();
if ($seasonId!=-1) { $season=Season::where('id','=',$seasonId)->first();}
$retval="Ja";
if (User::payment_date($user_id, $season->id)==null) { $retval="Nein"; }
return $retval;
}
public static function payment_date($user_id,$season_id) {
$season = Season::getCurrent();
if ($season) {
$payment = Payment::where([
['user_id', '=', $user_id],
['season_id', '=', $season->id]
])->first();
if ($payment) {
if ($payment->paid_date) {
return $payment->paid_date;
}
} else {
$payment= Payment::create([
'user_id' => $user_id,
'season_id'=> $season->id,
'paid_date'=> null
]);
$payment->save();
}
}
return null;
}
// Aufgerechnete Summe. wird $maxround_id mit übergeben, dann wird nur
// bis zur entsprechenden Runde aufsummiert. Das setzt voraus, dass die
// Runden in Reihenfolge eingegeben wurden...
public static function getTotalScore($user_id,$maxround_id=1000000) {
$season = Season::getCurrent();
$rounds = Round::where('season_id','=',$season->id)->get();
$totalScore=0;
foreach($rounds as $round) {
if ($round->id <= $maxround_id) {
$totalScore += User::getRoundScore($user_id, $round->id);
}
}
return $totalScore;
}
public static function getRoundScore($user_id, $round_id) {
$games = Game::where('round_id','=',$round_id)->get();
$totalScore=0;
foreach ($games as $game) {
$tipp = Tipp::getTipp($user_id,$game->id);
$score = Tipp::getScore($tipp->id);
$totalScore+=$score;
}
return $totalScore;
}
// This totally does not belong here. But i am too afraid to put it into
// a separate php file that gets smashed by autoregenerate.
public static function toGermanDate($engDate)
{
$values = explode("-", $engDate);
$year = $values[0];
$month = $values[1];
$day = explode(" ", $values[2])[0];
$timeval = explode(" ", $engDate)[1];
$hour = explode(":", $timeval)[0];
$minute = explode(":", $timeval)[1];
return $day.".".$month.".".$year." ".$hour.":".$minute;
}
public static function toDBDate($deDate)
{
if (!$deDate) return "";
$values = explode(" ", $deDate);
$year = explode(".",$values[0])[2];
$month = explode(".",$values[0])[1];
$day = explode(".", $values[0])[0];
Log::info("values[0]=".$values[0]." day=".$day."count=". count($values));
$hour="00";
$minute="00";
if (count($values)>1) {
$hour = explode(":", $values[1])[0];
$minute = explode(":", $values[1])[1];
}
$dbDate=$year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute . ":00";
Log::info("deDate=".$deDate." converted to:".$dbDate);
return $dbDate;
}
public static function hasMissingTipp($user_id) {
$round = Round::getFirstTippableRound();
if (!$round) return false;
$round_id = $round->id;
$games = Game::where('round_id','=',$round_id)->get();
foreach ($games as $game) {
$tipp = Tipp::where([['game_id','=',$game->id],['user_id','=',$user_id]])->first();
if(!$tipp) {
Log::info("Tipp doesnt exist: game_id=".$game->id.", user_id=".$user_id);
return 1;
}
if ($tipp->score1 == -1 || $tipp->score2 == -1) {
//Log::info("!!! User ".$user_id." has a missing tipp for game_id ".$game->id.", round = ".$round_id);
return 1;
}
}
return 0;
}
public static function hasMissingJoker($user_id) {
$round=Round::getFirstTippableRound();
if (!$round) return false;
$round_id = $round->id;
$games = Game::where('round_id','=',$round_id." user_id=".$user_id)->get();
$joker=0;
foreach ($games as $game) {
$tipp = Tipp::where([['game_id','=',$game->id],['user_id','=',$user_id]])->first();
if(!$tipp) {
Log::info("tipp is null! OMG! // Infos: user_id=".$user_id.", game_id=".$game->id.", round_id=".$round_id);
return true;
}
if ($tipp->joker == true) { $joker=1; }
//Log::info("!!! Joker for user_id ".$user_id.", round_id=".$round_id.", game_id=".$game->id." was found.");
}
if ($joker==1) {
return false;
}
//Log::info("!!! Joker is missing for user_id=".$user_id." round_id=".$round_id);
return true;
}
// aktiv = hat die Saison bezahlt.
public static function getActives($seasonId=-1) {
$season=Season::getCurrent();
if (!$season) return array();
if ($seasonId != -1) { $season = Season::where('id','=',$seasonId)->first(); }
$seasonId = $season->id;
$users = User::all();
$payments = Payment::where('season_id','=',$seasonId)->get();
$payref = array();
foreach($payments as $payment) {
if ($payment->paid_date) {
$payref[$payment->user_id] = 'ok';
}
}
$userlist=array();
foreach($users as $user) {
if (array_key_exists($user->id,$payref)) {
if($payref[$user->id]) {
array_push($userlist,$user);
}
}
}
return $userlist;
}
public static function isDeleted($user_id) {
$user=User::where('id','=',$user_id)->first();
if (!$user) return true;
if ($user->deleted) return true;
return false;
}
public static function softDelete($user_id) {
$user = User::where('id','=',$user_id)->first();
if ($user) { $user->deleted = Carbon\Carbon::now();}
$user->save();
$season = Season::getCurrent();
Payment::set_payment_date($user_id,$season->id,null);
}
public static function getNames() {
$users=User::all();
$usernames = array();
foreach($users as $user) {
$usernames[$user->id] = $user->name;
}
return $usernames;
}
}