/
home
/
corsairdevelopme
/
public_html
/
amplivo-console
/
app
/
Models
/
Upload File
HOME
<?php namespace App\Models; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Log; class CurrencyRedemptionRate extends Model { const EUR = 1; const USD = 2; const BTC = 1000; const ETH = 1001; const BCH = 1002; const XRP = 1003; const ORT = 5000; const ORTP = 5001; const FLP = 5001; const ISF = 5002; const CSR = 5003; const LTCT = 9999; const WALLET_TYPE = [ 1 => CurrencyRedemptionRate::EUR, 2 => CurrencyRedemptionRate::EUR, 18 => CurrencyRedemptionRate::EUR, 8 => CurrencyRedemptionRate::FLP, 12 => CurrencyRedemptionRate::ISF, 21 => CurrencyRedemptionRate::FLP, 22 => CurrencyRedemptionRate::FLP, 13 => CurrencyRedemptionRate::EUR, 19 => CurrencyRedemptionRate::CSR, ]; const TYPE_PROMPT = 'prompt'; const TYPE_SCHEDULED = 'scheduled'; const STATUS_ACTIVE = 'active'; const STATUS_DELETED = 'deleted'; const STATUS_SCHEDULED = 'scheduled'; const STATUS_OVERWRITTEN = 'overwritten'; use HasFactory; public $currencyId; public $currentModel; protected $table = "currency_redemption_rate"; protected $fillable = ['currency_id', 'rate', 'type', 'status', 'admin_id', 'activated_at']; public function getCurrentRate() { $this->refresh(); return $this->currentModel->rate ?? null; // return $this->currentModel->rateFormatted ?? null; } public function refresh() { $this->currentModel = $this->getCurrentModel(); if ($this->currentModel) { /** * @var CurrencyRedemptionRate[] */ $oldModels = $this->getPreviousActiveOrScheduledModels($this->currentModel); foreach ($oldModels as $i => $oldModel) { $nextModel = $oldModels[$i] ?? $this->currentModel; $oldModel->status = CurrencyRedemptionRate::STATUS_OVERWRITTEN; $oldModel->updated_at = $nextModel->activated_at; if ($oldModel->save()) { Log::error($oldModel->errors); } } $this->currentModel->status = CurrencyRedemptionRate::STATUS_ACTIVE; $this->currentModel->updated_at = $this->currentModel->activated_at; $this->currentModel->save(); } } /** * Undocumented function. * * @return CurrencyRedemptionRate */ private function getCurrentModel() { return CurrencyRedemptionRate:: where( 'activated_at', '<=', Carbon::now(), ) ->where( 'currency_id', $this->currency_id, )->whereIn( 'status', [CurrencyRedemptionRate::STATUS_ACTIVE, CurrencyRedemptionRate::STATUS_SCHEDULED], ) ->orderBy( 'activated_at', 'desc', ) ->first(); } /** * Undocumented function. * * @param CurrencyRedemptionRate $model * * @return CurrencyRedemptionRate[] */ private function getPreviousActiveOrScheduledModels($model) { return CurrencyRedemptionRate:: where( 'currency_id', $this->currency_id, ) ->whereIn( 'status', [CurrencyRedemptionRate::STATUS_ACTIVE, CurrencyRedemptionRate::STATUS_SCHEDULED], ) ->where( 'activated_at', '<', $model->activated_at, ) ->orderBy( 'activated_at', 'asc', ) ->get(); } }