diff --git a/app/Utilities/Versions.php b/app/Utilities/Versions.php index a400f80af..fabbcb8a2 100644 --- a/app/Utilities/Versions.php +++ b/app/Utilities/Versions.php @@ -2,7 +2,11 @@ namespace App\Utilities; +use App\Models\Module\Module; use App\Traits\SiteApi; +use App\Traits\Jobs; +use App\Jobs\Install\DisableModule; +use App\Jobs\Install\UninstallModule; use App\Utilities\Date; use GrahamCampbell\Markdown\Facades\Markdown; use Illuminate\Support\Arr; @@ -10,7 +14,7 @@ use Illuminate\Support\Facades\Cache; class Versions { - use SiteApi; + use SiteApi, Jobs; public static function changelog() { @@ -136,6 +140,8 @@ class Versions $url = 'apps/' . $alias . '/version/' . $version . '/' . $info['akaunting']; $versions[$alias] = static::getLatestVersion($url, $version); + + static::enforceSubscriptionStatus($alias, $versions[$alias]); } return $versions; @@ -161,6 +167,10 @@ class Versions $versions[$alias] = static::getLatestVersion($url, $version); + if ($alias != 'core') { + static::enforceSubscriptionStatus($alias, $versions[$alias]); + } + Cache::put('versions', $versions, Date::now()->addHours(6)); return $versions[$alias]; @@ -174,6 +184,7 @@ class Versions $version->latest = $latest; $version->errors = false; $version->message = ''; + $version->subscription = null; if (! $body = static::getResponseBody('GET', $url, ['timeout' => 10])) { return $version; @@ -187,10 +198,60 @@ class Versions $version->latest = $body->data->latest; $version->errors = $body->errors; $version->message = $body->message; + $version->subscription = $body->data?->subscription ?? null; return $version; } + public static function enforceSubscriptionStatus($alias, $version): void + { + if (! $version->subscription) { + return; + } + + if ($version->subscription->expired_at > Date::now()->startOfDay()) { + return; + } + + $status = 'active'; + + switch ($version->subscription->status) { + case 'expired': + case 'canceled': + $status = 'disabled'; + break; + case 'chargeback': + case 'not_paid': + case 'none': + case 'refund': + $status = 'uninstalled'; + break; + default: + // Do nothing + break; + } + + if ($status == 'active') { + return; + } + + $module_companies = Module::allCompanies()->alias($alias)->get(); + + foreach ($module_companies as $module) { + switch ($status) { + case 'disabled': + dispatch_sync(new DisableModule($alias, $module->company_id)); + break; + case 'uninstalled': + dispatch_sync(new UninstallModule($alias, $module->company_id)); + break; + default: + // Do nothing + break; + } + } + } + public static function getUpdates() { return Cache::remember('updates', Date::now()->addHours(6), function () {