PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
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 1.2.0 All 47 releases
fluent-cart / api / Resource / SubscriptionResource.php

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

124 lines 4.2 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 // product is eager-loaded so the appended display_item_name accessor
67 // resolves the product-prefixed label without an N+1 per row.
68 $with = ['customer', 'product'];
69
70 return $query->with($with)
71 ->where(function ($q) use ($searchTerms) {
72 $q->where('id', 'LIKE', "%{$searchTerms[0]}%")
73 ->orWhere('status', 'LIKE', "%{$searchTerms[0]}%")
74 ->orWhere('item_name', 'LIKE', "%{$searchTerms[0]}%")
75 ->orWhere('current_payment_method', 'LIKE', "%{$searchTerms[0]}%")
76 ->orWhere('parent_order_id', 'LIKE', "%{$searchTerms[0]}%")
77 ->orWhereHas('customer', function ($query) use ($searchTerms) {
78 $query->where('first_name', 'LIKE', "%{$searchTerms[0]}%")
79 ->orWhere('last_name', 'LIKE', "%{$searchTerms[0]}%");
80 });
81 })
82 ->paginate(Arr::get($params, 'per_page'), ['*'], 'page', Arr::get($params, 'page'));
83 }
84
85
86 public static function find($id, $params = [])
87 {
88
89 }
90
91 public static function create($data, $params = [])
92 {
93
94 }
95
96
97 public static function update($data, $id, $params = [])
98 {
99
100 }
101
102 public static function delete($id, $params = [])
103 {
104
105 }
106
107 public static function view($id)
108 {
109 return Subscription::where('id', $id)
110 ->with([
111 'product',
112 'customer.shipping_address' => function ($query) {
113 $query->where('is_primary', 1);
114 },
115 'customer.billing_address' => function ($query) {
116 $query->where('is_primary', 1);
117 },
118 'activities',
119 'order.transactions'
120 ])
121 ->first();
122 }
123 }
124