Skip to content

Ability for unlimited consumables & prices to plans #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
],
"require": {
"php": "^8.0|^8.1|^8.2",
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0"
"php": "^8.0|^8.1|^8.2|^8.3|^8.4",
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0|^12.0"
},
"require-dev": {
"orchestra/testbench": "^6.22|^7.0|^8.0|^9.0",
Expand Down
10 changes: 5 additions & 5 deletions database/factories/FeatureConsumptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LucasDotVin\Soulbscription\Database\Factories;

use LucasDotVin\Soulbscription\Models\Feature;
use Illuminate\Database\Eloquent\Factories\Factory;
use LucasDotVin\Soulbscription\Models\Feature;
use LucasDotVin\Soulbscription\Models\FeatureConsumption;

class FeatureConsumptionFactory extends Factory
Expand All @@ -18,10 +18,10 @@ class FeatureConsumptionFactory extends Factory
public function definition()
{
return [
'feature_id' => Feature::factory(),
'consumption' => $this->faker->randomFloat(),
'expired_at' => $this->faker->dateTime(),
'subscriber_id' => $this->faker->randomNumber(),
'feature_id' => Feature::factory(),
'consumption' => $this->faker->randomFloat(),
'expired_at' => $this->faker->dateTime(),
'subscriber_id' => $this->faker->randomNumber(),
'subscriber_type' => $this->faker->word(),
];
}
Expand Down
12 changes: 6 additions & 6 deletions database/factories/FeatureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LucasDotVin\Soulbscription\Database\Factories;

use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use Illuminate\Database\Eloquent\Factories\Factory;
use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use LucasDotVin\Soulbscription\Models\Feature;

class FeatureFactory extends Factory
Expand All @@ -18,17 +18,17 @@ class FeatureFactory extends Factory
public function definition()
{
return [
'consumable' => $this->faker->boolean(),
'name' => $this->faker->words(asText: true),
'periodicity' => $this->faker->randomDigitNotNull(),
'consumable' => $this->faker->boolean(),
'name' => $this->faker->words(asText: true),
'periodicity' => $this->faker->randomDigitNotNull(),
'periodicity_type' => $this->faker->randomElement([
PeriodicityType::Year,
PeriodicityType::Month,
PeriodicityType::Week,
PeriodicityType::Day,
]),
'quota' => false,
'postpaid' => false,
'quota' => false,
'postpaid' => false,
];
}

Expand Down
8 changes: 4 additions & 4 deletions database/factories/PlanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LucasDotVin\Soulbscription\Database\Factories;

use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use Illuminate\Database\Eloquent\Factories\Factory;
use LucasDotVin\Soulbscription\Enums\PeriodicityType;
use LucasDotVin\Soulbscription\Models\Plan;

class PlanFactory extends Factory
Expand All @@ -18,9 +18,9 @@ class PlanFactory extends Factory
public function definition()
{
return [
'grace_days' => 0,
'name' => $this->faker->words(asText: true),
'periodicity' => $this->faker->randomDigitNotNull(),
'grace_days' => 0,
'name' => $this->faker->words(asText: true),
'periodicity' => $this->faker->randomDigitNotNull(),
'periodicity_type' => $this->faker->randomElement([
PeriodicityType::Year,
PeriodicityType::Month,
Expand Down
16 changes: 8 additions & 8 deletions database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LucasDotVin\Soulbscription\Database\Factories;

use LucasDotVin\Soulbscription\Models\Plan;
use Illuminate\Database\Eloquent\Factories\Factory;
use LucasDotVin\Soulbscription\Models\Plan;
use LucasDotVin\Soulbscription\Models\Subscription;

class SubscriptionFactory extends Factory
Expand All @@ -18,13 +18,13 @@ class SubscriptionFactory extends Factory
public function definition()
{
return [
'plan_id' => Plan::factory(),
'canceled_at' => null,
'started_at' => $this->faker->dateTime(),
'suppressed_at' => null,
'expired_at' => $this->faker->dateTime(),
'was_switched' => false,
'subscriber_id' => $this->faker->randomNumber(),
'plan_id' => Plan::factory(),
'canceled_at' => null,
'started_at' => $this->faker->dateTime(),
'suppressed_at' => null,
'expired_at' => $this->faker->dateTime(),
'was_switched' => false,
'subscriber_id' => $this->faker->randomNumber(),
'subscriber_type' => $this->faker->word(),
];
}
Expand Down
7 changes: 4 additions & 3 deletions database/factories/SubscriptionRenewalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace LucasDotVin\Soulbscription\Database\Factories;

use LucasDotVin\Soulbscription\Models\{Subscription, SubscriptionRenewal};
use Illuminate\Database\Eloquent\Factories\Factory;
use LucasDotVin\Soulbscription\Models\Subscription;
use LucasDotVin\Soulbscription\Models\SubscriptionRenewal;

class SubscriptionRenewalFactory extends Factory
{
Expand All @@ -18,8 +19,8 @@ public function definition()
{
return [
'subscription_id' => Subscription::factory(),
'overdue' => $this->faker->boolean(),
'renewal' => $this->faker->boolean(),
'overdue' => $this->faker->boolean(),
'renewal' => $this->faker->boolean(),
];
}
}
7 changes: 6 additions & 1 deletion database/migrations/2022_02_01_233701_create_plans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand All @@ -16,8 +17,12 @@ public function up()
$table->id();
$table->integer('grace_days')->default(0);
$table->string('name');
$table->string('display_name')->nullable();
$table->integer('periodicity')->unsigned()->nullable();
$table->string('periodicity_type')->nullable();
$table->decimal('price')->default('0.0');
$table->string('price_id')->nullable();
$table->string('currency')->nullable();
$table->softDeletes();
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand All @@ -15,7 +16,8 @@ public function up()
Schema::create('features', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('consumable');
$table->string('display_name')->nullable();
$table->boolean('consumable')->default(false);
$table->boolean('quota')->default(false);
$table->boolean('postpaid')->default(false);
$table->integer('periodicity')->unsigned()->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Enums/PeriodicityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function getDateDifference(Carbon $from, Carbon $to, string $unit)
{
$unitInPlural = Str::plural($unit);

$differenceMethodName = 'diffIn' . $unitInPlural;
$differenceMethodName = 'diffIn'.$unitInPlural;

return $from->{$differenceMethodName}($to);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Concerns/Expires.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Expires
{
public static function bootExpires()
{
static::addGlobalScope(new ExpiringScope());
static::addGlobalScope(new ExpiringScope);
}

public function initializeExpires()
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Concerns/ExpiresAndHasGraceDays.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait ExpiresAndHasGraceDays
{
public static function bootExpiresAndHasGraceDays()
{
static::addGlobalScope(new ExpiringWithGraceDaysScope());
static::addGlobalScope(new ExpiringWithGraceDaysScope);
}

public function initializeExpiresAndHasGraceDays()
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Concerns/HandlesRecurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait HandlesRecurrence
{
public function calculateNextRecurrenceEnd(Carbon|string $start = null): Carbon
public function calculateNextRecurrenceEnd(Carbon|string|null $start = null): Carbon
{
if (empty($start)) {
$start = now();
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
Loading