Skip to content

Commit

Permalink
add ability for unlimited usage on consumables
Browse files Browse the repository at this point in the history
this can be achieved with -1
  • Loading branch information
harrisonratcliffe committed Nov 18, 2024
1 parent db139b0 commit 2ab4396
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
Schema::create('features', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('consumable');
$table->boolean('consumable')->default(false);
$table->boolean('quota')->default(false);
$table->boolean('postpaid')->default(false);
$table->integer('periodicity')->unsigned()->nullable();
Expand Down
33 changes: 19 additions & 14 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,12 @@ protected function consumeNotQuotaFeature(Feature $feature, ?float $consumption
: null;

$featureConsumption = $this->featureConsumptions()
->make([
'consumption' => $consumption,
'expired_at' => $consumptionExpiration,
])
->feature()
->associate($feature);
->whereFeatureId($feature->id)
->firstOrNew();

$featureConsumption->feature()->associate($feature);
$featureConsumption->consumption += $consumption;
$featureConsumption->expired_at = $consumptionExpiration;
$featureConsumption->save();

return $featureConsumption;
Expand Down Expand Up @@ -327,9 +326,12 @@ protected function getSubscriptionChargesForAFeature(Model $feature): float
return 0;
}

return $subscriptionFeature
->pivot
->charges;
$charges = $subscriptionFeature->pivot->charges;
if ($charges == -1) {
return INF;
}

return $charges;
}

protected function getTicketChargesForAFeature(Model $feature): float
Expand All @@ -341,9 +343,12 @@ protected function getTicketChargesForAFeature(Model $feature): float
return 0;
}

return $ticketFeature
->tickets
->sum('charges');
$charges = $ticketFeature->tickets->sum('charges');
if ($charges === -1) {
return INF;
}

return $charges;
}

public function getFeature(string $featureName): ?Feature
Expand Down Expand Up @@ -387,8 +392,8 @@ protected function loadTicketFeatures(): Collection
}

return $this->loadedTicketFeatures = Feature::with([
'tickets' => fn (HasMany $query) => $query->withoutExpired()->whereMorphedTo('subscriber', $this),
])
'tickets' => fn (HasMany $query) => $query->withoutExpired()->whereMorphedTo('subscriber', $this),
])
->whereHas(
'tickets',
fn (Builder $query) => $query->withoutExpired()->whereMorphedTo('subscriber', $this),
Expand Down

0 comments on commit 2ab4396

Please sign in to comment.