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