1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Sofa\Eloquence\Eloquence; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class Subscription extends Model |
10
|
|
|
{ |
11
|
|
|
//Eloquence Search mapping |
12
|
|
|
use Eloquence; |
13
|
|
|
use createdByUser, updatedByUser; |
14
|
|
|
|
15
|
|
|
protected $table = 'trn_subscriptions'; |
16
|
|
|
|
17
|
|
|
protected $fillable = [ |
18
|
|
|
'member_id', |
19
|
|
|
'invoice_id', |
20
|
|
|
'plan_id', |
21
|
|
|
'status', |
22
|
|
|
'is_renewal', |
23
|
|
|
'start_date', |
24
|
|
|
'end_date', |
25
|
|
|
'created_by', |
26
|
|
|
'updated_by', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
protected $dates = ['created_at', 'updated_at', 'start_date', 'end_date']; |
30
|
|
|
|
31
|
|
|
protected $searchableColumns = [ |
32
|
|
|
'Member.member_code' => 20, |
33
|
|
|
'start_date' => 20, |
34
|
|
|
'end_date' => 20, |
35
|
|
|
'Member.name' => 20, |
36
|
|
|
'Plan.plan_name' => 20, |
37
|
|
|
'Invoice.invoice_number' => 20, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
public function scopeDashboardExpiring($query) |
41
|
|
|
{ |
42
|
|
|
return $query |
43
|
|
|
->with(['members' => function ($query) { |
44
|
|
|
$query->where('status', '=', \constStatus::Active); |
45
|
|
|
}]) |
46
|
|
|
->where('end_date', '<', Carbon::today()->addDays(7)) |
47
|
|
|
->where('status', '=', \constSubscription::onGoing); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function scopeDashboardExpired($query) |
51
|
|
|
{ |
52
|
|
|
return $query |
53
|
|
|
->with(['members' => function ($query) { |
54
|
|
|
$query->where('status', '=', \constStatus::Active); |
55
|
|
|
}]) |
56
|
|
|
->where('status', '=', \constSubscription::Expired); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function scopeIndexQuery($query, $sorting_field, $sorting_direction, $drp_start, $drp_end) |
60
|
|
|
{ |
61
|
|
|
$sorting_field = ($sorting_field != null ? $sorting_field : 'created_at'); |
62
|
|
|
$sorting_direction = ($sorting_direction != null ? $sorting_direction : 'desc'); |
63
|
|
|
|
64
|
|
|
if ($drp_start == null or $drp_end == null) { |
65
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->orderBy($sorting_field, $sorting_direction); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->whereBetween('trn_subscriptions.created_at', [$drp_start, $drp_end])->orderBy($sorting_field, $sorting_direction); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function scopeExpiring($query, $sorting_field, $sorting_direction, $drp_start, $drp_end) |
72
|
|
|
{ |
73
|
|
|
$sorting_field = ($sorting_field != null ? $sorting_field : 'created_at'); |
74
|
|
|
$sorting_direction = ($sorting_direction != null ? $sorting_direction : 'desc'); |
75
|
|
|
|
76
|
|
|
if ($drp_start == null or $drp_end == null) { |
77
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->where('trn_subscriptions.end_date', '<', Carbon::today()->addDays(7))->where('trn_subscriptions.status', '=', \constSubscription::onGoing)->orderBy($sorting_field, $sorting_direction); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->where('trn_subscriptions.end_date', '<', Carbon::today()->addDays(7))->where('trn_subscriptions.status', '=', \constSubscription::onGoing)->whereBetween('trn_subscriptions.created_at', [$drp_start, $drp_end])->orderBy($sorting_field, $sorting_direction); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function scopeExpired($query, $sorting_field, $sorting_direction, $drp_start, $drp_end) |
84
|
|
|
{ |
85
|
|
|
$sorting_field = ($sorting_field != null ? $sorting_field : 'created_at'); |
86
|
|
|
$sorting_direction = ($sorting_direction != null ? $sorting_direction : 'desc'); |
87
|
|
|
|
88
|
|
|
if ($drp_start == null or $drp_end == null) { |
89
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->where('trn_subscriptions.status', '=', \constSubscription::Expired)->where('trn_subscriptions.status', '!=', \constSubscription::renewed)->orderBy($sorting_field, $sorting_direction); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $query->leftJoin('mst_plans', 'trn_subscriptions.plan_id', '=', 'mst_plans.id')->select('trn_subscriptions.*', 'mst_plans.plan_name')->where('trn_subscriptions.status', '=', \constSubscription::Expired)->where('trn_subscriptions.status', '!=', \constSubscription::renewed)->whereBetween('trn_subscriptions.created_at', [$drp_start, $drp_end])->orderBy($sorting_field, $sorting_direction); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function member() |
96
|
|
|
{ |
97
|
|
|
return $this->belongsTo('App\Member', 'member_id'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function plan() |
101
|
|
|
{ |
102
|
|
|
return $this->belongsTo('App\Plan', 'plan_id'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function invoice() |
106
|
|
|
{ |
107
|
|
|
return $this->belongsTo('App\Invoice', 'invoice_id'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|