diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 24a661eae68f..1d3be1016fce 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -11,6 +11,7 @@ namespace App\Console; +use App\Jobs\Cron\BillingSubscriptionCron; use App\Jobs\Cron\RecurringInvoicesCron; use App\Jobs\Ninja\AdjustEmailQuota; use App\Jobs\Ninja\CompanySizeCheck; @@ -53,6 +54,8 @@ class Kernel extends ConsoleKernel $schedule->job(new UpdateExchangeRates)->daily()->withoutOverlapping(); + $schedule->job(new BillingSubscriptionCron)->daily()->withoutOverlapping(); + $schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping(); /* Run hosted specific jobs */ diff --git a/app/Jobs/Cron/BillingSubscriptionCron.php b/app/Jobs/Cron/BillingSubscriptionCron.php new file mode 100644 index 000000000000..091cb10c5f83 --- /dev/null +++ b/app/Jobs/Cron/BillingSubscriptionCron.php @@ -0,0 +1,71 @@ +loopSubscriptions(); + } else { + //multiDB environment, need to + foreach (MultiDB::$dbs as $db) { + + MultiDB::setDB($db); + $this->loopSubscriptions(); + + } + } + } + + private function loopSubscriptions() + { + $client_subs = ClientSubscription::whereNull('deleted_at') + ->cursor() + ->each(function ($cs){ + $this->processSubscription($cs); + }); + } + + /* Our daily cron should check + + 1. Is the subscription still in trial phase? + 2. Check the recurring invoice and its remaining_cycles to see whether we need to cancel or perform any other function. + 3. Any notifications that need to fire? + */ + private function processSubscription($client_subscription) + { + + } +} diff --git a/app/Models/BillingSubscription.php b/app/Models/BillingSubscription.php index 57f8706f2075..160219812fce 100644 --- a/app/Models/BillingSubscription.php +++ b/app/Models/BillingSubscription.php @@ -1,5 +1,4 @@ belongsTo(Product::class); } + } diff --git a/app/Models/ClientSubscription.php b/app/Models/ClientSubscription.php index 8ca8caa87d37..ac7000cea2c0 100644 --- a/app/Models/ClientSubscription.php +++ b/app/Models/ClientSubscription.php @@ -1,4 +1,14 @@ isAdmin() || $user->hasPermission('create_billing_subscription') || $user->hasPermission('create_all'); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3d96377fa950..c017572692c3 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -12,7 +12,9 @@ namespace App\Providers; use App\Models\Account; +use App\Models\BillingSubscription; use App\Models\Client; +use App\Models\ClientSubscription; use App\Models\Company; use App\Models\CompanyGateway; use App\Models\CompanyToken; @@ -26,7 +28,9 @@ use App\Models\Quote; use App\Models\Task; use App\Models\User; use App\Observers\AccountObserver; +use App\Observers\BillingSubscriptionObserver; use App\Observers\ClientObserver; +use App\Observers\ClientSubscriptionObserver; use App\Observers\CompanyGatewayObserver; use App\Observers\CompanyObserver; use App\Observers\CompanyTokenObserver; @@ -75,9 +79,10 @@ class AppServiceProvider extends ServiceProvider Schema::defaultStringLength(191); - User::observe(UserObserver::class); Account::observe(AccountObserver::class); + BillingSubscription::observe(BillingSubscriptionObserver::class); Client::observe(ClientObserver::class); + ClientSubscription::observe(ClientSubscriptionObserver::class); Company::observe(CompanyObserver::class); CompanyGateway::observe(CompanyGatewayObserver::class); CompanyToken::observe(CompanyTokenObserver::class); @@ -89,6 +94,7 @@ class AppServiceProvider extends ServiceProvider Proposal::observe(ProposalObserver::class); Quote::observe(QuoteObserver::class); Task::observe(TaskObserver::class); + User::observe(UserObserver::class); // Queue::before(function (JobProcessing $event) { // // \Log::info('Event Job '.$event->connectionName); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 23d6f3a59220..60281db3b3fe 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -12,6 +12,7 @@ namespace App\Providers; use App\Models\Activity; +use App\Models\BillingSubscription; use App\Models\Client; use App\Models\Company; use App\Models\CompanyGateway; @@ -37,6 +38,7 @@ use App\Models\User; use App\Models\Vendor; use App\Models\Webhook; use App\Policies\ActivityPolicy; +use App\Policies\BillingSubscriptionPolicy; use App\Policies\ClientPolicy; use App\Policies\CompanyGatewayPolicy; use App\Policies\CompanyPolicy; @@ -73,6 +75,7 @@ class AuthServiceProvider extends ServiceProvider */ protected $policies = [ Activity::class => ActivityPolicy::class, + BillingSubscription::class => BillingSubscriptionPolicy::class, Client::class => ClientPolicy::class, Company::class => CompanyPolicy::class, CompanyToken::class => CompanyTokenPolicy::class,