Completed
Push — development ( a96186...0b761c )
by Ashutosh
10:54
created

PlanController::checkSubscription()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 2
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Currency;
7
use App\Model\Payment\Period;
8
use App\Model\Payment\Plan;
9
use App\Model\Payment\PlanPrice;
10
use App\Model\Product\Product;
11
use App\Model\Product\Subscription;
12
use Illuminate\Http\Request;
13
14
class PlanController extends Controller
15
{
16
    protected $currency;
17
    protected $price;
18
    protected $period;
19
    protected $product;
20
21
    public function __construct()
22
    {
23
        $this->middleware('auth');
24
        // $this->middleware('admin');
25
        $plan = new Plan();
26
        $this->plan = $plan;
27
        $subscription = new Subscription();
28
        $this->subscription = $subscription;
29
        $currency = new Currency();
30
        $this->currency = $currency;
31
        $price = new PlanPrice();
32
        $this->price = $price;
33
        $period = new Period();
34
        $this->period = $period;
35
        $product = new Product();
36
        $this->product = $product;
37
    }
38
39
    /**
40
     * Display a listing of the resource.
41
     *
42
     * @return \Response
43
     */
44
    public function index()
45
    {
46
        $currency = $this->currency->pluck('name', 'code')->toArray();
47
        $periods = $this->period->pluck('name', 'days')->toArray();
48
        $products = $this->product->pluck('name', 'id')->toArray();
49
50
        return view('themes.default1.product.plan.index', compact('currency', 'periods', 'products'));
51
    }
52
53
    /**
54
     * Get plans for chumper datatable.
55
     */
56
    public function getPlans()
57
    {
58
        $new_plan = Plan::select('id', 'name', 'days', 'product')->get();
59
60
        return\ DataTables::of($new_plan)
61
                        ->addColumn('checkbox', function ($model) {
62
                            return "<input type='checkbox' class='plan_checkbox' 
63
                            value=".$model->id.' name=select[] id=check>';
64
                        })
65
                        ->addColumn('name', function ($model) {
66
                            return ucfirst($model->name);
67
                        })
68
                        ->addColumn('days', function ($model) {
69
                            $months = $model->days / 30;
70
71
                            return round($months);
72
                        })
73
                        ->addColumn('product', function ($model) {
74
                            $productid = $model->product;
75
                            $product = $this->product->where('id', $productid)->first();
76
                            $response = '';
77
                            if ($product) {
78
                                $response = $product->name;
79
                            }
80
81
                            return ucfirst($response);
82
                        })
83
                        ->addColumn('action', function ($model) {
84
                            return '<a href='.url('plans/'.$model->id.'/edit')." 
85
                            class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' 
86
                            style='color:white;'> </i>&nbsp;&nbsp;Edit</a>";
87
                        })
88
                        ->rawColumns(['checkbox', 'name', 'days', 'product', 'action'])
89
                        ->make(true);
90
    }
91
92
    /**
93
     * Show the form for creating a new resource.
94
     *
95
     * @return \Response
96
     */
97
    public function create()
98
    {
99
        $currency = $this->currency->pluck('name', 'code')->toArray();
100
        $periods = $this->period->pluck('name', 'days')->toArray();
101
        $products = $this->product->pluck('name', 'id')->toArray();
102
103
        return view('themes.default1.product.plan.create', compact('currency', 'periods', 'products'));
104
    }
105
106
    /**
107
     * Store a newly created resource in storage.
108
     *
109
     * @return \Response
110
     */
111
    public function store(Request $request)
112
    {
113
        $subs = Product::where('id',$request->input('product'))->value('subscription');
114
        $days_rule = $subs == 1 ? 'required|' : 'sometimes|';
115
116
        $this->validate($request, [
117
            'name'        => 'required',
118
            'days'        => $days_rule . 'numeric',
119
            'add_price.*' => 'required',
120
            'product'     => 'required',
121
        ]);
122
123
        $this->plan->fill($request->input())->save();
124
        $add_prices = $request->input('add_price');
125
        $renew_prices = $request->input('renew_price');
126
        $product = $request->input('product');
127
        if (count($add_prices) > 0) {
128
            foreach ($add_prices as $key => $price) {
129
                $renew_price = '';
130
                if (array_key_exists($key, $renew_prices)) {
131
                    $renew_price = $renew_prices[$key];
132
                }
133
                $this->price->create([
134
                    'plan_id'     => $this->plan->id,
135
                    'currency'    => $key,
136
                    'add_price'   => $price,
137
                    'renew_price' => $renew_price,
138
                    'product'     => $product,
139
                ]);
140
            }
141
        }
142
143
        return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
144
    }
145
146
    /**
147
     * Display the specified resource.
148
     *
149
     * @param int $id
150
     *
151
     * @return \Response
152
     */
153
    public function show($id)
154
    {
155
    }
156
157
    /**
158
     * Show the form for editing the specified resource.
159
     *
160
     * @param int $id
161
     *
162
     * @return \Response
163
     */
164
    public function edit($id)
165
    {
166
        $plan = $this->plan->where('id', $id)->first();
167
        $currency = $this->currency->pluck('name', 'code')->toArray();
168
        $add_price = $this->price->where('plan_id', $id)->pluck('add_price', 'currency')->toArray();
169
        $renew_price = $this->price->where('plan_id', $id)->pluck('renew_price', 'currency')->toArray();
170
        $periods = $this->period->pluck('name', 'days')->toArray();
171
        $products = $this->product->pluck('name', 'id')->toArray();
172
        foreach ($products as $key => $product) {
173
          $selectedProduct = $this->product->where('id',$plan->product)
174
          ->pluck('name','id','subscription')->toArray();
175
       }
176
      $selectedPeriods = $this->period->where('days',$plan->days)
177
       ->pluck('name', 'days')->toArray();
178
179
        return view('themes.default1.product.plan.edit',
180
            compact('plan', 'currency', 'add_price', 'renew_price', 'periods', 'products',
181
                'selectedPeriods','selectedProduct'));
182
    }
183
184
    /**
185
     * Update the specified resource in storage.
186
     *
187
     * @param int $id
188
     *
189
     * @return \Response
190
     */
191
    public function update($id, Request $request)
192
    {
193
         $subs = Product::where('id',$request->input('product'))->value('subscription');
194
        $days_rule = $subs == 1 ? 'required|' : 'sometimes|';
195
196
        $this->validate($request, [
197
            'name'        => 'required',
198
            'add_price.*' => 'required',
199
            'product'     => 'required',
200
              'days'        => $days_rule . 'numeric',
201
        ]);
202
        $plan = $this->plan->where('id', $id)->first();
203
        $plan->fill($request->input())->save();
204
        $add_prices = $request->input('add_price');
205
        $renew_prices = $request->input('renew_price');
206
        $product = $request->input('product');
207
        $period = $request->input('days');
208
209
        if (count($add_prices) > 0) {
210
            $price = $this->price->where('plan_id', $id)->get();
211
212
            if (count($price) > 0) {
213
                foreach ($price as $delete) {
214
                    $delete->delete();
215
                }
216
            }
217
            foreach ($add_prices as $key => $price) {
218
                $renew_price = '';
219
                if (array_key_exists($key, $renew_prices)) {
220
                    $renew_price = $renew_prices[$key];
221
                }
222
                $this->price->create([
223
                    'plan_id'     => $plan->id,
224
                    'currency'    => $key,
225
                    'add_price'   => $price,
226
                    'renew_price' => $renew_price,
227
                    'product'     => $product,
228
                ]);
229
            }
230
        }
231
232
        return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
233
    }
234
235
    /**
236
     * Remove the specified resource from storage.
237
     *
238
     * @param int $id
239
     *
240
     * @return \Response
241
     */
242
    public function destroy(Request $request)
243
    {
244
        $ids = $request->input('select');
245
        if (!empty($ids)) {
246
            foreach ($ids as $id) {
247
                $plan = $this->plan->where('id', $id)->first();
248
                if ($plan) {
249
                    $plan->delete();
250
                } else {
251
                    echo "<div class='alert alert-success alert-dismissable'>
252
                    <i class='fa fa-ban'></i>
253
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
254
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
255
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
256
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
257
                </div>';
258
                    //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
259
                }
260
            }
261
            echo "<div class='alert alert-success alert-dismissable'>
262
                    <i class='fa fa-ban'></i>
263
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
264
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
265
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
266
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
267
                </div>';
268
        } else {
269
            echo "<div class='alert alert-success alert-dismissable'>
270
                    <i class='fa fa-ban'></i>
271
                    <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> '.
272
                    /* @scrutinizer ignore-type */ \Lang::get('message.success').'
273
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
274
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
275
                </div>';
276
            //echo \Lang::get('message.select-a-row');
277
        }
278
    }
279
280
    public function checkSubscription(Request $request)
281
    {
282
      try{
283
      $product_id = $request->input('product_id');
284
      $checkSubscription = Product::where('id',$product_id)->value('subscription');
285
      $result = ['subscription'=> $checkSubscription];
286
      return response()->json($result);
287
      }catch (\Exception $ex) {
288
            Bugsnag::notifyException($ex);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Product\Bugsnag was not found. Did you mean Bugsnag? If so, make sure to prefix the type with \.
Loading history...
289
            $result = ['subscription' => $ex->getMessage()];
290
291
            return response()->json($result);
292
        }
293
    }
294
}
295