1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Traits; |
6
|
|
|
|
7
|
|
|
use Gewaer\Models\Subscription; |
8
|
|
|
use Gewaer\Models\UserCompanyAppsActivities; |
9
|
|
|
use Gewaer\Exception\SubscriptionPlanLimitException; |
10
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use Phalcon\Di; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait ResponseTrait |
16
|
|
|
* |
17
|
|
|
* @package Gewaer\Traits |
18
|
|
|
*/ |
19
|
|
|
trait SubscriptionPlanLimitTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get the key for the subscriptoin plan limit |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
8 |
|
private function getSubcriptionPlanLimitModelKey() : string |
27
|
|
|
{ |
28
|
8 |
|
$key = $this->subscriptionPlanLimitKey ?? (new ReflectionClass($this))->getShortName(); |
29
|
8 |
|
return strtolower($key) . '_total'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Validate if the current module for this app is at the limit of the paid plan |
34
|
|
|
* |
35
|
|
|
* @return boolean |
36
|
|
|
*/ |
37
|
4 |
|
public function isAtLimit() : bool |
38
|
|
|
{ |
39
|
4 |
|
if (!Di::getDefault()->has('userData')) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
$subcription = Subscription::getActiveForThisApp(); |
44
|
4 |
|
$appPlan = $subcription->appPlan; |
|
|
|
|
45
|
|
|
|
46
|
4 |
|
if (is_object($appPlan)) { |
47
|
|
|
//get the current module limit for this plan |
48
|
4 |
|
$appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey()); |
|
|
|
|
49
|
|
|
|
50
|
4 |
|
if (!is_null($appPlanLimit)) { |
51
|
|
|
//get tht total activity of the company current plan |
52
|
3 |
|
$currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey()); |
53
|
|
|
|
54
|
3 |
|
if ($currentCompanyAppActivityTotal >= $appPlanLimit) { |
55
|
1 |
|
throw new SubscriptionPlanLimitException(_($subcription->company->name . ' has reach the limit of it current plan ' . $appPlan->name . ' please upgrade or contact support')); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Call at the afterCreate of all modules which are part of a plan activity |
65
|
|
|
* |
66
|
|
|
* @return boolean |
67
|
|
|
*/ |
68
|
6 |
|
public function updateAppActivityLimit() : bool |
69
|
|
|
{ |
70
|
6 |
|
if (!Di::getDefault()->has('userData')) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
$companyAppActivityLimit = UserCompanyAppsActivities::findFirst([ |
75
|
6 |
|
'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2', |
76
|
6 |
|
'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()] |
77
|
|
|
]); |
78
|
|
|
|
79
|
6 |
|
if (is_object($companyAppActivityLimit)) { |
80
|
|
|
//its a varchar so lets make sure we convert it to int |
81
|
4 |
|
$companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1; |
82
|
4 |
|
if (!$companyAppActivityLimit->save()) { |
83
|
4 |
|
throw new ServerErrorHttpException((string)current($companyAppActivityLimit->getMessages())); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
2 |
|
$userCopmanyAppsActivites = new UserCompanyAppsActivities(); |
87
|
2 |
|
$userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|