Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
Show All 27 Lines | protected $attributes = [ | ||||
'currency' => 'CHF' | 'currency' => 'CHF' | ||||
]; | ]; | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'currency' | 'currency' | ||||
]; | ]; | ||||
protected $nullable = [ | protected $nullable = [ | ||||
'description' | 'description', | ||||
]; | ]; | ||||
protected $casts = [ | protected $casts = [ | ||||
'balance' => 'integer', | 'balance' => 'integer', | ||||
]; | ]; | ||||
protected $guarded = ['balance']; | protected $guarded = ['balance']; | ||||
Show All 9 Lines | public function addController(User $user) | ||||
if (!$this->controllers->contains($user)) { | if (!$this->controllers->contains($user)) { | ||||
$this->controllers()->save($user); | $this->controllers()->save($user); | ||||
} | } | ||||
} | } | ||||
public function chargeEntitlements($apply = true) | public function chargeEntitlements($apply = true) | ||||
{ | { | ||||
$charges = 0; | $charges = 0; | ||||
$discount = $this->discount ? $this->discount->discount : 0; | |||||
$discount = (100 - $discount) / 100; | |||||
foreach ($this->entitlements()->get()->fresh() as $entitlement) { | foreach ($this->entitlements()->get()->fresh() as $entitlement) { | ||||
// This entitlement has been created less than or equal to 14 days ago (this is at | // This entitlement has been created less than or equal to 14 days ago (this is at | ||||
// maximum the fourteenth 24-hour period). | // maximum the fourteenth 24-hour period). | ||||
if ($entitlement->created_at > Carbon::now()->subDays(14)) { | if ($entitlement->created_at > Carbon::now()->subDays(14)) { | ||||
continue; | continue; | ||||
} | } | ||||
// This entitlement was created, or billed last, less than a month ago. | // This entitlement was created, or billed last, less than a month ago. | ||||
if ($entitlement->updated_at > Carbon::now()->subMonths(1)) { | if ($entitlement->updated_at > Carbon::now()->subMonthsWithoutOverflow(1)) { | ||||
continue; | continue; | ||||
} | } | ||||
// created more than a month ago -- was it billed? | // created more than a month ago -- was it billed? | ||||
if ($entitlement->updated_at <= Carbon::now()->subMonths(1)) { | if ($entitlement->updated_at <= Carbon::now()->subMonthsWithoutOverflow(1)) { | ||||
$diff = $entitlement->updated_at->diffInMonths(Carbon::now()); | $diff = $entitlement->updated_at->diffInMonths(Carbon::now()); | ||||
$charges += $entitlement->cost * $diff; | $cost = (int) ($entitlement->cost * $discount * $diff); | ||||
$charges += $cost; | |||||
// if we're in dry-run, you know... | // if we're in dry-run, you know... | ||||
if (!$apply) { | if (!$apply) { | ||||
continue; | continue; | ||||
} | } | ||||
$entitlement->updated_at = $entitlement->updated_at->copy()->addMonths($diff); | $entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff); | ||||
$entitlement->save(); | $entitlement->save(); | ||||
$this->debit($entitlement->cost * $diff); | // TODO: This would be better done out of the loop (debit() will call save()), | ||||
// but then, maybe we should use a db transaction | |||||
$this->debit($cost); | |||||
} | } | ||||
} | } | ||||
return $charges; | return $charges; | ||||
} | } | ||||
/** | /** | ||||
* The discount assigned to the wallet. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |||||
*/ | |||||
public function discount() | |||||
{ | |||||
return $this->belongsTo('App\Discount', 'discount_id', 'id'); | |||||
} | |||||
/** | |||||
* Calculate the expected charges to this wallet. | * Calculate the expected charges to this wallet. | ||||
* | * | ||||
* @return int | * @return int | ||||
*/ | */ | ||||
public function expectedCharges() | public function expectedCharges() | ||||
{ | { | ||||
return $this->chargeEntitlements(false); | return $this->chargeEntitlements(false); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |