Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompanyLicenses
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 8
650
0.00% covered (danger)
0.00%
0 / 1
 scopeActive
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 company
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTotalNumberOfLicensesAvailableAttribute
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getTotalNumberOfLicensesRemainingAttribute
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 totalLicenses
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 status
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
72
 restoreLicenseCountForDeletedUsers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 reduceCompanyLicenseCountForNewUsers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Http\Models\Admin;
4
5use App\Http\Models\Moloquent;
6use Carbon\Carbon;
7use Illuminate\Database\Eloquent\Casts\Attribute;
8use Illuminate\Database\Eloquent\Factories\HasFactory;
9use Illuminate\Database\Eloquent\Relations\BelongsTo;
10use MongoDB\Laravel\Eloquent\SoftDeletes;
11
12class CompanyLicenses extends Moloquent
13{
14    use HasFactory, SoftDeletes;
15
16    const PAID = 'Paid';
17
18    const PAST_DUE_30 = 'Past Due 30';
19
20    const PAST_DUE_60 = 'Past Due 60';
21
22    const PAST_DUE_90 = 'Past Due 90';
23
24    const CANCELLED = 'Cancelled';
25
26    protected $fillable = [
27        'company_id',
28        'term_of_contract',
29        'contract_end_date',
30        'auto_renew_license',
31        'contract_start_date',
32        'business_pro_enterprise_plus',
33        'total_starter_license_count',
34        'total_starter_license_remaining',
35        'total_growth_license_count',
36        'total_growth_license_remaining',
37        'total_sales_pro_license_count',
38        'total_sales_pro_license_remaining',
39        'total_sales_pro_teams_license_count',
40        'total_sales_pro_teams_license_remaining',
41    ];
42
43    /**
44     * The accessors to append to the model's array form.
45     *
46     * @var array
47     */
48    protected $appends = [
49        'total_number_of_licenses_available',
50        'total_number_of_licenses_remaining',
51        'total_licenses',
52        'status',
53    ];
54
55    public function scopeActive($query)
56    {
57        $now = Carbon::now()->format('Y-m-d H:i:s');
58
59        return $query->where('contract_end_date', '>=', $now)
60            ->where('contract_start_date', '<=', $now)
61            ->whereHas('company', function ($query) {
62                $query->whereNull('deactivated_at');
63            });
64    }
65
66    public function company(): BelongsTo
67    {
68        return $this->belongsTo(Company::class);
69    }
70
71    public function getTotalNumberOfLicensesAvailableAttribute()
72    {
73        return $this->total_starter_license_count +
74        $this->total_growth_license_count +
75        $this->total_sales_pro_license_count +
76        $this->total_sales_pro_teams_license_count;
77    }
78
79    public function getTotalNumberOfLicensesRemainingAttribute()
80    {
81        return $this->total_starter_license_remaining +
82        $this->total_growth_license_remaining +
83        $this->total_sales_pro_license_remaining +
84        $this->total_sales_pro_teams_license_remaining;
85    }
86
87    /**
88     * Get the total license count
89     */
90    protected function totalLicenses(): Attribute
91    {
92        return Attribute::make(
93            get: fn (mixed $value, array $attributes) => ($attributes['total_starter_license_count'] ?? 0) +
94                ($attributes['total_growth_license_count'] ?? 0) +
95                ($attributes['total_sales_pro_license_count'] ?? 0) +
96                ($attributes['total_sales_pro_teams_license_count'] ?? 0)
97        );
98    }
99
100    /**
101     * Get the status attribute
102     */
103    protected function status(): Attribute
104    {
105        return Attribute::make(
106            get: function (mixed $value, array $attributes) {
107
108                if (empty($this->company)) {
109                    return self::CANCELLED;
110                }
111
112                $now = now();
113                $endDate = Carbon::parse($attributes['contract_end_date']);
114                $pastDueDays = $now->diffInDays($endDate);
115
116                return match (true) {
117                    $endDate >= $now => self::PAID,
118                    $endDate < $now && $pastDueDays <= 30 => self::PAST_DUE_30,
119                    $endDate < $now && $pastDueDays > 30 && $pastDueDays <= 60 => self::PAST_DUE_60,
120                    $endDate < $now && $pastDueDays > 60 && $pastDueDays <= 90 => self::PAST_DUE_90,
121                    $endDate < $now && $pastDueDays > 90 => self::CANCELLED
122                };
123            }
124        );
125    }
126
127    // ToDo: total license, available, remaining fields
128    // auto computation factoring admin editing total license
129    public function restoreLicenseCountForDeletedUsers($plan)
130    {
131        if ($plan->identifier == 'sales-pro-yearly') {
132            $this->total_sales_pro_license_remaining = $this->total_sales_pro_license_remaining + 1;
133        } elseif ($plan->identifier == 'growth-yearly') {
134            $this->total_growth_license_remaining = $this->total_growth_license_remaining + 1;
135        } elseif ($plan->identifier == 'starter-yearly') {
136            $this->total_starter_license_remaining = $this->total_starter_license_remaining + 1;
137        } elseif ($plan->identifier == 'pro-plan-teams-smb' || $plan->identifier == 'pro-plan-teams-ent') {
138            $this->total_sales_pro_teams_license_remaining = $this->total_sales_pro_teams_license_remaining + 1;
139        }
140        $this->save();
141    }
142
143    public function reduceCompanyLicenseCountForNewUsers($plan)
144    {
145        if ($plan->identifier == 'sales-pro-yearly') {
146            $this->total_sales_pro_license_remaining = $this->total_sales_pro_license_remaining - 1;
147        } elseif ($plan->identifier == 'growth-yearly') {
148            $this->total_growth_license_remaining = $this->total_growth_license_remaining - 1;
149        } elseif ($plan->identifier == 'starter-yearly') {
150            $this->total_starter_license_remaining = $this->total_starter_license_remaining - 1;
151        } elseif ($plan->identifier == 'pro-plan-teams-smb' || $plan->identifier == 'pro-plan-teams-ent') {
152            $this->total_sales_pro_teams_license_remaining = $this->total_sales_pro_teams_license_remaining - 1;
153        }
154        $this->save();
155    }
156}