birzha-legalizasia/app/Models/Account.php

179 lines
7.6 KiB
PHP
Executable File

<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
class Account extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'accounts';
// protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = [];
protected $fillable = [
'contacts',
'bank',
'vat',
'country_id',
'legalization_number',
'type',
'expires_at',
'broker_expires_at',
'broker_number'
];
protected $casts = [
'contacts' => 'array',
'bank' => 'array'
];
// protected $hidden = [];
protected $dates = ['expires_at', 'broker_expires_at'];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
public function preview(){
return '<a class="btn btn-sm btn-link" href="'. backpack_url('preview/'.$this->id) .'" data-toggle="tooltip">
<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">
<path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"/>
<path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"/>
</svg>
Preview</a>';
}
// public function export_account(){
// return '<a class="btn btn-sm btn-link" href="'.backpack_url('export-account-admin/'.$this->id) .'" data-toggle="tooltip">
// <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-filetype-docx" viewBox="0 0 16 16">
// <path fill-rule="evenodd" d="M14 4.5V11h-1V4.5h-2A1.5 1.5 0 0 1 9.5 3V1H4a1 1 0 0 0-1 1v9H2V2a2 2 0 0 1 2-2h5.5L14 4.5Zm-6.839 9.688v-.522a1.54 1.54 0 0 0-.117-.641.861.861 0 0 0-.322-.387.862.862 0 0 0-.469-.129.868.868 0 0 0-.471.13.868.868 0 0 0-.32.386 1.54 1.54 0 0 0-.117.641v.522c0 .256.04.47.117.641a.868.868 0 0 0 .32.387.883.883 0 0 0 .471.126.877.877 0 0 0 .469-.126.861.861 0 0 0 .322-.386 1.55 1.55 0 0 0 .117-.642Zm.803-.516v.513c0 .375-.068.7-.205.973a1.47 1.47 0 0 1-.589.627c-.254.144-.56.216-.917.216a1.86 1.86 0 0 1-.92-.216 1.463 1.463 0 0 1-.589-.627 2.151 2.151 0 0 1-.205-.973v-.513c0-.379.069-.704.205-.975.137-.274.333-.483.59-.627.257-.147.564-.22.92-.22.357 0 .662.073.916.22.256.146.452.356.59.63.136.271.204.595.204.972ZM1 15.925v-3.999h1.459c.406 0 .741.078 1.005.235.264.156.46.382.589.68.13.296.196.655.196 1.074 0 .422-.065.784-.196 1.084-.131.301-.33.53-.595.689-.264.158-.597.237-.999.237H1Zm1.354-3.354H1.79v2.707h.563c.185 0 .346-.028.483-.082a.8.8 0 0 0 .334-.252c.088-.114.153-.254.196-.422a2.3 2.3 0 0 0 .068-.592c0-.3-.04-.552-.118-.753a.89.89 0 0 0-.354-.454c-.158-.102-.361-.152-.61-.152Zm6.756 1.116c0-.248.034-.46.103-.633a.868.868 0 0 1 .301-.398.814.814 0 0 1 .475-.138c.15 0 .283.032.398.097a.7.7 0 0 1 .273.26.85.85 0 0 1 .12.381h.765v-.073a1.33 1.33 0 0 0-.466-.964 1.44 1.44 0 0 0-.49-.272 1.836 1.836 0 0 0-.606-.097c-.355 0-.66.074-.911.223-.25.148-.44.359-.571.633-.131.273-.197.6-.197.978v.498c0 .379.065.704.194.976.13.271.321.48.571.627.25.144.555.216.914.216.293 0 .555-.054.785-.164.23-.11.414-.26.551-.454a1.27 1.27 0 0 0 .226-.674v-.076h-.765a.8.8 0 0 1-.117.364.699.699 0 0 1-.273.248.874.874 0 0 1-.401.088.845.845 0 0 1-.478-.131.834.834 0 0 1-.298-.393 1.7 1.7 0 0 1-.103-.627v-.495Zm5.092-1.76h.894l-1.275 2.006 1.254 1.992h-.908l-.85-1.415h-.035l-.852 1.415h-.862l1.24-2.015-1.228-1.984h.932l.832 1.439h.035l.823-1.439Z"/>
// </svg>
// Export</a>';
// }
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function profile():MorphTo
{
return $this->morphTo();
}
public function country():BelongsTo
{
return $this->belongsTo(Country::class, 'country_id');
}
public function clients():HasMany
{
return $this->hasMany(Client::class);
}
public function applications():HasMany
{
return $this->hasMany(Application::class);
}
public function last_application(){
return $this->applications()->latest()->first();
}
public function broker_applications():HasMany
{
return $this->hasMany(BrokerApplication::class);
}
public function last_broker_application(){
return $this->broker_applications()->latest()->first();
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| ACCESSORS
|--------------------------------------------------------------------------
*/
public function getTypeAndNameAttribute(){
return trans('app.account.filter.'.$this->type).' '.$this->profile->full_name;
}
public function getAccountTypeAttribute(){
return trans('app.account.filter.'.$this->type);
}
public function getCanApplyAttribute(){
return is_null($this->last_application());
}
public function getCanExtendAttribute()
{
$month = Config::get('settings.legalization_extend') ?? 1;
if($application = $this->last_application()){
return $application->state == 'approved' && $this->expires_at->lte(Carbon::now()->subMonths($month));
}
return !empty($this->legalization_number) && !empty( $this->expires_at) && $this->expires_at->gte(Carbon::now()->subMonths($month));
}
public function getApplicationStatusAttribute(){
$application = $this->last_application();
return $application->state ?? null;
}
public function getCountryNameAttribute(){
return $this->country->name;
}
public function getCanApplyBrokerAttribute(){
return is_null($this->last_broker_application());
}
public function getCanExtendBrokerAttribute()
{
$month = Config::get('settings.legalization_extend') ?? 1;
if($broker_application = $this->last_broker_application()){
return $broker_application->state == 'approved' && $this->broker_expires_at->lte(Carbon::now()->subMonths($month));
}
return !empty($this->broker_number) && !empty($this->broker_expires_at) && $this->broker_expires_at->gte(Carbon::now()->subMonths($month));
}
public function getApplicationBrokerStatusAttribute(){
$broker_application = $this->last_broker_application();
return $broker_application->state ?? null;
}
/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}