Created
August 10, 2018 11:15
-
-
Save zloadmin/510fbfe99fadfb6b17ee9b454427bded to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Carbon\Carbon; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use App\Instrument; | |
use App\Source; | |
use App\Person; | |
use App\Company; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Support\Facades\Artisan; | |
use Illuminate\Support\Facades\Cache; | |
/** | |
* App\Forecast | |
* | |
* @mixin \Eloquent | |
*/ | |
class Forecast extends Model | |
{ | |
use SoftDeletes; | |
protected $fillable = [ | |
'id','text','date_of_forecast','instrument_id','source_id','person_id','company_id', 'result', 'duration', 'condition', 'kpt', | |
'term_of_forecast', 'int_term_of_forecast', 'forecast', 'int_forecast', | |
'odcs', 'min_for_period', 'average_for_period', 'max_for_period', 'odce', | |
'fm', 'pci' | |
]; | |
protected $dates = ['deleted_at']; | |
static $filter_fields = ['source', 'person', 'company', 'instrument']; | |
protected static function boot() | |
{ | |
parent::boot(); | |
if(\Auth::check()) { | |
static::addGlobalScope('withFavorite', function ($bulder) { | |
$bulder->with('favorite'); | |
}); | |
} | |
static::addGlobalScope('pci', function ($bulder) { | |
$bulder->where('pci', 1); | |
}); | |
} | |
/** | |
* @param Request $request | |
* @param $forecasts | |
*/ | |
protected static function getForecastById(Request $request, $forecasts) | |
{ | |
if ($request->has('forecast_id')) { | |
$forecasts->where('id', $request->get('forecast_id')); | |
} | |
} | |
/** | |
* @param Request $request | |
* @param $forecasts | |
*/ | |
protected static function getForecastsByFields(Request $request, $forecasts) | |
{ | |
foreach (self::$filter_fields as $field) { | |
$filed_multi = str_plural($field); | |
if ($request->has($filed_multi) && is_array($request->get($filed_multi))) { | |
$forecasts->where(function ($q) use ($request, $field, $filed_multi) { | |
foreach ($request->get($filed_multi) as $field_id) { | |
$q->orWhere($field . '_id', '=', $field_id); | |
} | |
}); | |
} | |
} | |
} | |
/** | |
* @param $forecasts | |
*/ | |
protected static function getForecastByRange($forecasts) | |
{ | |
if (preg_match('/\d{2}.\d{2}.\d{4}/', request('start_forecast')) && | |
preg_match('/\d{2}.\d{2}.\d{4}/', request('end_forecast'))) { | |
$start = Carbon::parse(request('start_forecast'))->startOfDay(); | |
$end = Carbon::parse(request('end_forecast'))->endOfDay(); | |
$forecasts->where('date_of_forecast', '>=', $start) | |
->where('date_of_forecast', '<=', $end); | |
} | |
} | |
public function instrument() | |
{ | |
return $this->belongsTo(Instrument::class); | |
} | |
public function source() | |
{ | |
return $this->belongsTo(Source::class); | |
} | |
public function person() | |
{ | |
return $this->belongsTo(Person::class); | |
} | |
public function company() | |
{ | |
return $this->belongsTo(Company::class); | |
} | |
public function company_trashed() | |
{ | |
return $this->belongsTo(Company::class, 'company_id', 'id')->withTrashed(); | |
} | |
public function person_trashed() | |
{ | |
return $this->belongsTo(Person::class, 'person_id', 'id')->withTrashed(); | |
} | |
public function getInstrumentLinkAttribute() | |
{ | |
return isset($this->instrument) && isset($this->instrument->our_name) ? link_to_route('instrument', $this->instrument->our_name, $this->instrument->id) : ''; | |
} | |
public function getSourceLinkAttribute() | |
{ | |
return isset($this->source_url) ? link_to($this->source_url, $this->source->own_name) : ''; | |
} | |
public function getSourceUrlAttribute() | |
{ | |
return isset($this->source) ? $this->source->own_link : ''; | |
} | |
public function getPersonLinkAttribute() | |
{ | |
return isset($this->person_url) ? link_to($this->person_url, $this->person->full_name) : ''; | |
} | |
public function getPersonUrlAttribute() | |
{ | |
return isset($this->person) ? route('person', $this->person->id) : ''; | |
} | |
public function getCompanyUrlAttribute() | |
{ | |
return isset($this->company) ? route('company', $this->company->id) : ''; | |
} | |
public function getDateRusAttribute() | |
{ | |
return Carbon::parse($this->date_of_forecast)->format('d.m.Y'); | |
} | |
public function getCompanyName($limit = null) | |
{ | |
return isset($this->company) ? $this->company->getName($limit) : ''; | |
} | |
public function getSourceName($limit = null) | |
{ | |
return isset($this->source) ? $this->source->getName($limit) : ''; | |
} | |
public function getPersonName($limit = null) | |
{ | |
if($limit) return str_limit($this->person->short_name, $limit) ?? ''; | |
return $this->person->short_name ?? ''; | |
} | |
public function getPersonNameByFamily($limit = null) | |
{ | |
return isset($this->person) ? $this->person->getNameByFamily($limit) : null; | |
} | |
public function getPersonLinkByFamily($limit = null) | |
{ | |
if(isset($this->person->short_name_by_family)) { | |
$name = $limit ? str_limit($this->person->short_name_by_family, $limit) : $this->person->short_name_by_family; | |
return link_to($this->person_url, $name); | |
} elseif(isset($this->person->name)) { | |
return $limit ? str_limit($this->person->name, $limit) : $this->person->name; | |
} else { | |
return ''; | |
} | |
} | |
public function getCompanyLinkByFamily($limit = null) | |
{ | |
if(isset($this->company->own_short_name)) { | |
$name = $limit ? str_limit($this->company->own_short_name, $limit) : $this->company->own_short_name; | |
return link_to($this->company_url, $name); | |
} elseif(isset($this->company->name)) { | |
return $limit ? str_limit($this->company->name, $limit) : $this->company->name; | |
} else { | |
return ''; | |
} | |
} | |
public function getTimestampAttribute() | |
{ | |
return Carbon::parse($this->date_of_forecast)->timestamp; | |
} | |
public function getYearAttribute() | |
{ | |
return Carbon::parse($this->date_of_forecast)->year; | |
} | |
/** | |
* @param Request $request | |
* @return $this|\Illuminate\Contracts\Pagination\LengthAwarePaginator | |
*/ | |
static public function getForecastsFromRequest(Request $request) | |
{ | |
$forecasts = Forecast::withoutGlobalScope('pci') | |
->with('instrument', 'source', 'person_trashed', 'company_trashed') | |
->orderBy('date_of_forecast', 'desc'); | |
$forecasts = $forecasts->users(); | |
self::getForecastById($request, $forecasts); | |
self::getForecastsByFields($request, $forecasts); | |
self::getForecastByRange($forecasts); | |
$forecasts = $forecasts->paginate(10); | |
return $forecasts; | |
} | |
public static function getStartForecast() | |
{ | |
$forecast = self::orderBy('date_of_forecast')->first(); | |
return isset($forecast->date_of_forecast) ? Carbon::parse($forecast->date_of_forecast)->timestamp : 0; | |
} | |
public static function getEndForecast() | |
{ | |
$forecast = self::orderByDesc('date_of_forecast')->first(); | |
return isset($forecast->date_of_forecast) ? Carbon::parse($forecast->date_of_forecast)->timestamp : Carbon::now()->timestamp; | |
} | |
static function getShowRangeByRequest() | |
{ | |
return request('start_forecast') && request('end_forecast') ? '(' . request('start_forecast') . ' - ' . request('end_forecast') . ')' : ''; | |
} | |
public function scopeFirstCompany($query) | |
{ | |
$query->whereNotNull('company_id')->orderBy('date_of_forecast'); | |
} | |
public function scopeLastCompany($query) | |
{ | |
$query->whereNotNull('company_id')->orderByDesc('date_of_forecast'); | |
} | |
public function scopeFirstPerson($query) | |
{ | |
$query->whereNotNull('person_id')->orderBy('date_of_forecast'); | |
} | |
public function scopeLastPerson($query) | |
{ | |
$query->whereNotNull('person_id')->orderByDesc('date_of_forecast'); | |
} | |
public function scopeByInstrument($query, \App\Instrument $instrumnent) | |
{ | |
$query->where('instrument_id', $instrumnent->id); | |
} | |
public function scopeByCompany($query, \App\Company $company) | |
{ | |
$query->where('company_id', $company->id); | |
} | |
public function scopeByPerson($query, \App\Person $person) | |
{ | |
$query->where('person_id', $person->id); | |
} | |
public function scopeStart($query, Carbon $start) | |
{ | |
$query->where('date_of_forecast', '>=', $start); | |
} | |
public function scopeEnd($query, Carbon $end) | |
{ | |
$query->where('date_of_forecast', '<=', $end); | |
} | |
public function scopeTrue($query) | |
{ | |
$query->where('result', 1); | |
} | |
public function scopeFalse($query) | |
{ | |
$query->where('result', 0); | |
} | |
public function scopeNull($query) | |
{ | |
$query->whereNull('result'); | |
} | |
public function scopeYear($query, $year) | |
{ | |
$start = Carbon::create($year)->startOfYear(); | |
$end = Carbon::create($year)->endOfYear(); | |
$query->where('date_of_forecast', '>=', $start)->where('date_of_forecast', '<=', $end); | |
} | |
static function truncateForecasts() | |
{ | |
Schema::disableForeignKeyConstraints(); | |
DB::table('forecasts')->truncate(); | |
Schema::enableForeignKeyConstraints(); | |
} | |
static function parseForecastsQueue() | |
{ | |
Artisan::queue('parse:everything'); | |
} | |
static function getCountTrue($forecasts) | |
{ | |
$i = 0; | |
foreach ($forecasts as $forecast) | |
if($forecast->result == 1) $i++; | |
return $i; | |
} | |
static function getCountFalse($forecasts) | |
{ | |
$i = 0; | |
foreach ($forecasts as $forecast) | |
if($forecast->result === 0) $i++; | |
return $i; | |
} | |
static function getCountNull($forecasts) | |
{ | |
$i = 0; | |
foreach ($forecasts as $forecast) | |
if($forecast->result === null) $i++; | |
return $i; | |
} | |
static function getTrueArrayByYear($forecasts) | |
{ | |
return self::getArrayByYear($forecasts, 1); | |
} | |
static function getFalseArrayByYear($forecasts) | |
{ | |
return self::getArrayByYear($forecasts, 0); | |
} | |
static function getNullArrayByYear($forecasts) | |
{ | |
return self::getArrayByYear($forecasts, null); | |
} | |
static function getArrayByYear($forecasts, $type) | |
{ | |
$array = self::getYears($forecasts, 0); | |
foreach ($forecasts as $forecast) | |
{ | |
if($forecast->result === $type) | |
{ | |
if(isset($array[$forecast->year])) { | |
$array[$forecast->year]++; | |
} else { | |
$array[$forecast->year] = 1; | |
} | |
} | |
} | |
return $array; | |
} | |
static function getYears($forecasts, $value = true) | |
{ | |
$years = []; | |
foreach ($forecasts as $forecast) { | |
$years[$forecast->year + 1] = $value; | |
$years[$forecast->year] = $value; | |
$years[$forecast->year - 1] = $value; | |
} | |
ksort($years); | |
return $years; | |
} | |
static function getByCache() | |
{ | |
return Cache::rememberForever('all_forecasts', function() { | |
return Forecast::all(); | |
}); | |
} | |
static function getYearByCache() | |
{ | |
return Cache::rememberForever('forecasts_years', function() { | |
return Forecast::getYears(Forecast::all()); | |
}); | |
} | |
static function getCountForecastsByInstruments($instruments, $years) | |
{ | |
$array = []; | |
foreach ($instruments as $instrument) { | |
foreach ($years as $year => $item) | |
{ | |
$array[$instrument->id]['all_forecasts'][$year] = Forecast::byInstrument($instrument)->year($year)->count(); | |
$array[$instrument->id]['true_forecasts'][$year] = Forecast::byInstrument($instrument)->year($year)->true()->count(); | |
$array[$instrument->id]['false_forecasts'][$year] = Forecast::byInstrument($instrument)->year($year)->false()->count(); | |
$array[$instrument->id]['null_forecasts'][$year] = Forecast::byInstrument($instrument)->year($year)->null()->count(); | |
} | |
} | |
return $array; | |
} | |
static function getCountForecastsByInstrumentsByCache($instruments, $years) | |
{ | |
return Cache::rememberForever('get_count_forecasts_by_instruments_by_cache', function() use ($instruments, $years) { | |
return self::getCountForecastsByInstruments($instruments, $years); | |
}); | |
} | |
static function companyYearAndQuarter(\App\Company $company, int $year, int $quarter) | |
{ | |
$start = Carbon::create($year,1,1)->addQuarter($quarter - 1)->startOfQuarter(); | |
$end = Carbon::create($year,1,1)->addQuarter($quarter - 1)->endOfQuarter(); | |
return self::byCompany($company)->start($start)->end($end)->get(); | |
} | |
static function personYearAndQuarter(\App\Person $person, int $year, int $quarter) | |
{ | |
$start = Carbon::create($year,1,1)->addQuarter($quarter - 1)->startOfQuarter(); | |
$end = Carbon::create($year,1,1)->addQuarter($quarter - 1)->endOfQuarter(); | |
return self::byPerson($person)->start($start)->end($end)->get(); | |
} | |
static function companyYear(\App\Company $company, int $year) | |
{ | |
$start = Carbon::create($year,1,1)->startOfYear(); | |
$end = Carbon::create($year,1,1)->endOfYear(); | |
return self::byCompany($company)->start($start)->end($end)->get(); | |
} | |
static function personYear(\App\Person $person, int $year) | |
{ | |
$start = Carbon::create($year,1,1)->startOfYear(); | |
$end = Carbon::create($year,1,1)->endOfYear(); | |
return self::byPerson($person)->start($start)->end($end)->get(); | |
} | |
static function getIndexPageForecasts() | |
{ | |
$forecasts = Forecast::withoutGlobalScope('pci')->with('source', 'person_trashed', 'company_trashed', 'instrument'); | |
$forecasts = $forecasts->users(); | |
$forecasts = $forecasts->take(10); | |
$forecasts = $forecasts->orderBy('date_of_forecast', 'desc'); | |
$forecasts = $forecasts->get(); | |
return $forecasts; | |
} | |
public function scopeUsers($query) | |
{ | |
if(!\Auth::check()) return $query->where('instrument_id', '!=', 13); | |
return $query; | |
} | |
public function favorite() | |
{ | |
return $this->hasOne(ForecastsFavorite::class, 'forecast_unique_id', 'unique_id')->where('user_id', auth()->id()); | |
} | |
public function isFavoried() : bool | |
{ | |
return ($this->favorite && $this->favorite->count()); | |
} | |
public function addFavorite() | |
{ | |
ForecastsFavorite::createByForecast($this); | |
} | |
public function removeFavorite() | |
{ | |
$this->favorite->delete();; | |
} | |
public function getColorClass() | |
{ | |
if(!$this) return ''; | |
if($this->result === null) return 'wait_color'; | |
if($this->result === 0) return 'false_color'; | |
return 'true_color'; | |
} | |
public function scopeFindByUniqueId($query, $unique_id) | |
{ | |
return $query->where('unique_id', $unique_id); | |
} | |
static function getFirstCompany() | |
{ | |
return self::firstCompany()->first() ? self::firstCompany()->first() : false; | |
} | |
static function getLastCompany() | |
{ | |
return self::lastCompany()->first() ? self::lastCompany()->first() : false; | |
} | |
static function getFirstPerson() | |
{ | |
return self::firstPerson()->first() ? self::firstPerson()->first() : false; | |
} | |
static function getLastPerson() | |
{ | |
return self::lastPerson()->first() ? self::lastPerson()->first() : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment