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
|
1 |
|
private function getSubcriptionPlanLimitModelKey() : string |
27
|
|
|
{ |
28
|
1 |
|
return strtolower((new ReflectionClass($this))->getShortName()) . '_total'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Validate if the current module for this app is at the limit of the paid plan |
33
|
|
|
* |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
2 |
|
public function isAtLimit() : bool |
37
|
|
|
{ |
38
|
2 |
|
if (!is_object($this->di->getUserData) && !$this->di->getUserData()->isLoggedIn()) { |
39
|
1 |
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$subcription = Subscription::getActiveForThisApp(); |
43
|
|
|
$appPlan = $subcription->appPlan; |
|
|
|
|
44
|
|
|
|
45
|
|
|
if (is_object($appPlan)) { |
46
|
|
|
//get the current module limit for this plan |
47
|
|
|
$appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
if (!is_null($appPlanLimit)) { |
50
|
|
|
//get tht total activity of the company current plan |
51
|
|
|
$currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey()); |
52
|
|
|
|
53
|
|
|
if ($currentCompanyAppActivityTotal >= $appPlanLimit) { |
54
|
|
|
throw new SubscriptionPlanLimitException(_($subcription->company->name . ' has reach the limit of it current plan ' . $appPlan->name . ' please upgrade or contact support')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Call at the afterCreate of all modules which are part of a plan activity |
64
|
|
|
* |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
3 |
|
public function updateAppActivityLimit() : bool |
68
|
|
|
{ |
69
|
3 |
|
if (!is_object($this->di->getUserData) && !$this->di->getUserData()->isLoggedIn()) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$companyAppActivityLimit = UserCompanyAppsActivities::findFirst([ |
74
|
|
|
'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2', |
75
|
|
|
'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()] |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
if (is_object($companyAppActivityLimit)) { |
79
|
|
|
//its a varchar so lets make sure we convert it to int |
80
|
|
|
$companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1; |
81
|
|
|
if (!$companyAppActivityLimit->save()) { |
82
|
|
|
throw new ServerErrorHttpException((string)current($companyAppActivityLimit->getMessages())); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$userCopmanyAppsActivites = new UserCompanyAppsActivities(); |
86
|
|
|
$userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|