Changeset View
Changeset View
Standalone View
Standalone View
src/app/Entitlement.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Carbon\Carbon; | |||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | |||||
/** | /** | ||||
* The eloquent definition of an Entitlement. | * The eloquent definition of an Entitlement. | ||||
* | * | ||||
* Owned by a {@link \App\User}, billed to a {@link \App\Wallet}. | * Owned by a {@link \App\User}, billed to a {@link \App\Wallet}. | ||||
* | * | ||||
* @property \App\User $owner The owner of this entitlement (subject). | * @property \App\User $owner The owner of this entitlement (subject). | ||||
* @property \App\Sku $sku The SKU to which this entitlement applies. | * @property \App\Sku $sku The SKU to which this entitlement applies. | ||||
* @property \App\Wallet $wallet The wallet to which this entitlement is charged. | * @property \App\Wallet $wallet The wallet to which this entitlement is charged. | ||||
* @property \App\Domain|\App\User $entitleable The entitled object (receiver of the entitlement). | * @property \App\Domain|\App\User $entitleable The entitled object (receiver of the entitlement). | ||||
*/ | */ | ||||
class Entitlement extends Model | class Entitlement extends Model | ||||
{ | { | ||||
use SoftDeletes; | |||||
/** | /** | ||||
* This table does not use auto-increment. | * This table does not use auto-increment. | ||||
* | * | ||||
* @var boolean | * @var boolean | ||||
*/ | */ | ||||
public $incrementing = false; | public $incrementing = false; | ||||
/** | /** | ||||
Show All 9 Lines | class Entitlement extends Model | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'sku_id', | 'sku_id', | ||||
'owner_id', | 'owner_id', | ||||
'wallet_id', | 'wallet_id', | ||||
'entitleable_id', | 'entitleable_id', | ||||
'entitleable_type', | 'entitleable_type', | ||||
'cost', | |||||
'description' | 'description' | ||||
]; | ]; | ||||
public function changelogEntry() | |||||
{ | |||||
return \App\Changelog::where('entitlement_id', $this->id)->latest()->first(); | |||||
} | |||||
public function chargeWallet() | |||||
{ | |||||
$charges = 0; | |||||
$entry = $this->changelogEntry(); | |||||
\Log::debug("entry: " . var_export($entry->toArray(), true)); | |||||
if ($entry->updated_at >= Carbon::now()->subDays(14)) { | |||||
\Log::debug(">= 14 days ago, skipping"); | |||||
return $charges; | |||||
} | |||||
if ($entry->updated_at > Carbon::now()->subMonths(1)) { | |||||
\Log::debug("> 1 month ago, skipping"); | |||||
return $charges; | |||||
} | |||||
$diff = $entry->updated_at->diffInMonths(Carbon::now()); | |||||
if ($this->cost < 1) { | |||||
\Log::debug("{$this->sku->title} is a free entitlement, skipping"); | |||||
$entry->updated_at = $entry->updated_at->addMonths($diff); | |||||
$entry->save(); | |||||
return $charges; | |||||
} | |||||
$charges += $this->cost * $diff; | |||||
$this->wallet->debit($this->cost * $diff); | |||||
$entry->updated_at = $entry->updated_at->addMonths($diff); | |||||
$entry->save(); | |||||
return $charges; | |||||
} | |||||
/** | /** | ||||
* Principally entitleable objects such as 'Domain' or 'User'. | * Principally entitleable objects such as 'Domain' or 'User'. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function entitleable() | public function entitleable() | ||||
{ | { | ||||
return $this->morphTo(); | return $this->morphTo(); | ||||
Show All 32 Lines |