PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / api / Resource / SubscriptionResource.php

SubscriptionResource.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.26, at api/Resource/SubscriptionResource.php

121 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api\Resource;
4
5 use FluentCart\App\Models\Order;
6 use FluentCart\App\Models\Query\QueryParser;
7 use FluentCart\App\Models\Query\Sort;
8 use FluentCart\App\Models\Subscription;
9 use FluentCart\App\Helpers\Status;
10 use FluentCart\Framework\Database\Orm\Builder;
11 use FluentCart\Framework\Support\Arr;
12
13 class SubscriptionResource extends BaseResourceApi
14 {
15 public static function getQuery(): Builder
16 {
17 return Order::query()
18 ->where(function (Builder $query) {
19 $query->where('parent_id', 0)
20 ->orWhereNull('parent_id');
21 })
22 ->whereHas('order_items', function (Builder $query) {
23 $query->where('payment_type', 'subscription');
24 });
25 }
26
27 public static function get(array $params = [])
28 {
29
30 $query = Subscription::query();
31
32 $dynamicConditions = Arr::get($params, 'dynamic_filters') ?? [];
33 QueryParser::make()->parse($query, $dynamicConditions);
34
35 $activeView = Arr::get($params, 'active_view');
36 $acceptedViewsMaps = [
37 Status::SUBSCRIPTION_ACTIVE => 'status',
38 Status::SUBSCRIPTION_PENDING => 'status',
39 Status::SUBSCRIPTION_INTENDED => 'status',
40 Status::SUBSCRIPTION_PAUSED => 'status',
41 Status::SUBSCRIPTION_TRIALING => 'status',
42 Status::SUBSCRIPTION_CANCELED => 'status',
43 Status::SUBSCRIPTION_FAILING => 'status',
44 Status::SUBSCRIPTION_EXPIRING => 'status',
45 Status::SUBSCRIPTION_EXPIRED => 'status',
46 ];
47
48 if(isset($acceptedViewsMaps[$activeView])) {
49 $query->where($acceptedViewsMaps[$activeView], $activeView);
50 }
51
52 $sortBy = Arr::get($params, 'sort_by', 'id');
53 $sortType = Arr::get($params, 'sort_type', 'DESC');
54
55 $allowedSortFields = ['id', 'next_billing_date', 'created_at', 'status'];
56 if (in_array($sortBy, $allowedSortFields)) {
57 $query->orderBy($sortBy, $sortType);
58 }
59
60 $search = trim(Arr::get($params, 'search', ''));
61 $searchTerms = explode(' ', $search);
62
63 $sortCriteria = Arr::get($params, 'sort_criteria', []);
64 Sort::make()->apply($query, $sortCriteria);
65
66 $with = ['customer'];
67
68 return $query->with($with)
69 ->where(function ($q) use ($searchTerms) {
70 $q->where('id', 'LIKE', "%{$searchTerms[0]}%")
71 ->orWhere('status', 'LIKE', "%{$searchTerms[0]}%")
72 ->orWhere('item_name', 'LIKE', "%{$searchTerms[0]}%")
73 ->orWhere('current_payment_method', 'LIKE', "%{$searchTerms[0]}%")
74 ->orWhere('parent_order_id', 'LIKE', "%{$searchTerms[0]}%")
75 ->orWhereHas('customer', function ($query) use ($searchTerms) {
76 $query->where('first_name', 'LIKE', "%{$searchTerms[0]}%")
77 ->orWhere('last_name', 'LIKE', "%{$searchTerms[0]}%");
78 });
79 })
80 ->paginate(Arr::get($params, 'per_page'), ['*'], 'page', Arr::get($params, 'page'));
81 }
82
83
84 public static function find($id, $params = [])
85 {
86
87 }
88
89 public static function create($data, $params = [])
90 {
91
92 }
93
94
95 public static function update($data, $id, $params = [])
96 {
97
98 }
99
100 public static function delete($id, $params = [])
101 {
102
103 }
104
105 public static function view($id)
106 {
107 return Subscription::where('id', $id)
108 ->with([
109 'customer.shipping_address' => function ($query) {
110 $query->where('is_primary', 1);
111 },
112 'customer.billing_address' => function ($query) {
113 $query->where('is_primary', 1);
114 },
115 'activities',
116 'order.transactions'
117 ])
118 ->first();
119 }
120 }
121