Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.33% covered (danger)
8.33%
1 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckCompaniesExpirationCommand
8.33% covered (danger)
8.33%
1 / 12
50.00% covered (danger)
50.00%
1 / 2
16.32
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Models\Admin\Company;
6use App\Http\Models\Admin\CompanyLicenses;
7use App\Http\Services\Admin\Companies\CompanyService;
8use Illuminate\Console\Command;
9use Illuminate\Support\Facades\Log;
10use Carbon\Carbon;
11
12class CheckCompaniesExpirationCommand extends Command
13{
14    /**
15     * The name and signature of the console command.
16     *
17     * @var string
18     */
19    protected $signature = 'app:check-company-expiration';
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Check when a company is going to expire';
27
28    public function __construct(
29        private CompanyService $companyService,
30    ) {
31        parent::__construct();
32    }
33
34    /**
35     * Execute the console command.
36     */
37    public function handle()
38    {
39        $this->info($this->description);
40
41        $companies = Company::where('deleted_at', null)->where('deactivated_at', null)->get();
42
43        foreach ($companies as $company) {
44            $companyLicense = CompanyLicenses::where('company_id', $company->id)->latest()->first();
45
46            $expirationDate = Carbon::parse($companyLicense->contract_end_date);
47            $today = Carbon::now();
48            $daysUntilExpiration = $today->diffInDays($expirationDate, false);
49
50            if ($daysUntilExpiration <= 0) {
51                $this->info($company->name . ' is expired on ' . $expirationDate->toString());
52                Log::info($company->name . ' is expired on ' . $expirationDate->toString());
53                $this->companyService->deactivateCompany($company, '', $expirationDate);
54            }
55        }
56    }
57}