<?php
namespace App;
use App\Bank\Payment;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use DateTimeInterface;
class Store extends Authenticatable
{
use HasApiTokens, Notifiable;
use LogsActivity;
protected $guard = 'store';
protected $fillable = [
'session_status',
'name',
'username',
'logo',
'photo',
'nuit',
'balance',
'store_id',
'nib',
'address',
'manager',
'manager_phone_number',
'public_id',
'mobile_phone',
'account_number',
'qrcode',
'status',
'email',
'password',
'user_client_id',
'merchant_account_id',
'business_account_id',
'merchant_contract_id',
'industry_activity',
'user_id',
'firebase_token',
'longitude',
'latitude',
'store',
'qrcode_has_amount'
];
protected static $logAttributes = [
'session_status',
'name',
'logo',
'photo',
'nuit',
'balance',
'store_id',
'nib',
'address',
'manager',
'manager_phone_number',
'public_id',
'mobile_phone',
'account_number',
'qrcode',
'status',
'email',
'password',
'user_client_id',
'merchant_account_id',
'business_account_id',
'merchant_contract_id',
'industry_activity',
'user_id',
'firebase_token',
'longitude',
'latitude',
'store',
'qrcode_has_amount'
];
protected static $logName = 'store';
public function getDescriptionForEvent(string $eventName): string
{
return "This model has been {$eventName}";
}
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function setNameAttribute($value)
{
$this->attributes['name'] = ucwords(strtolower($value));
}
public function setEmailAttribute($value)
{
$this->attributes['email'] = strtolower($value);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('d-m-Y H:i:s');
}
function payments()
{
return $this->hasMany(Payment::class);
}
function paymentsGenerated()
{
return $this->hasMany(PaymentGeneration::class);
}
public function findForPassport($identifier)
{
return $this->orWhere('email', $identifier)->orWhere('username', $identifier)->first();
}
function amountGenerations()
{
return $this->hasMany(StoreAmountGeneration::class);
}
// function payment() {
// return $this->belongsTo(Payment::class);
// }
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(["*"]);
}
function user_client()
{
return $this->belongsTo(UserClient::class);
}
/**
* GET DADOS DA store pelo numero de conta
* @return User::getAccount(220000247); // GET store by account_number
*/
// todo OK
public static function getAccount($account_number)
{
return Store::query()->where('account_number', $account_number)->first();
}
// so dados da Loja | Account Number
public static function getStoreAccount($account_number)
{
return Store::query()
->where('account_number', $account_number)
->select('stores.*')
->first();
}
public static function getStoreByAccountId($id)
{
$store = Self::where('id', $id)->first();
return $store;
}
public static function getStoreByAccountNumber($account_number)
{
$store = Self::where('account_number', $account_number)->first();
return $store;
}
// so dados da Loja com os contracts | Account Number
public static function getStoreContractsAndConfigs($account_number_or_id)
{
return Store::query()
->join('merchant_contracts', 'merchant_contracts.store_id', 'stores.id')
->join('store_configs', 'store_configs.store_id', 'stores.id')
->join('ramo_activities', 'ramo_activities.id', 'stores.industry_activity')
//->leftJoin('merchant_contracts', 'merchant_contracts.store_id', 'stores.id')
//->leftJoin('store_configs', 'store_configs.store_id', 'stores.id')
->where('account_number', $account_number_or_id)
->orWhere('stores.id', $account_number_or_id)
->select(
'stores.*',
'merchant_contracts.nr_transaction',
'merchant_contracts.max_balance',
'merchant_contracts.min_amount',
'merchant_contracts.max_amount',
'merchant_contracts.taxa',
'store_configs.notify',
'store_configs.notify_push',
'store_configs.notify_sms',
'store_configs.notify_email',
'store_configs.use_period',
'store_configs.on_map',
'store_configs.accept_payment',
'store_configs.close_period',
'store_configs.coin',
'store_configs.bank_default',
'store_configs.use_refund',
'ramo_activities.logo as logo_category',
'ramo_activities.nome as category',
)
->first();
}
// Verificar o servico de notificacao activo
// public function getActiveNotificationService()
// {
// if (!$this->checkActiveStoreNotificationService()) return null;
// if ($this->store->notify_push) return array('service' => 'push', 'active' => 1);
// if ($this->store->notify_email) return array('service' => 'email', 'active' => 1);
// if ($this->store->notify_sms) return array('service' => 'sms', 'active' => 1);
// }
// private function checkActiveStoreNotificationService()
// {
// if ($this->store->notify) return true;
// return false;
// }
public function getLogoAttribute($logo)
{
return $_ENV['IMG_STORES_LOGO'] . $logo;
}
public function getPhotoAttribute($photo)
{
return $_ENV['IMG_STORES_LOGO'] . $photo;
}
public function getQrcodeAttribute($qrcode)
{
return $_ENV['IMG_STORES_QRCODE'] . $qrcode;
}
}