Completed
Push — development ( c6edbc...615590 )
by Ashutosh
09:59
created

RenewController::removeSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Order\Invoice;
7
use App\Model\Order\InvoiceItem;
8
use App\Model\Order\Order;
9
use App\Model\Payment\Plan;
10
use App\Model\Product\Product;
11
use App\Model\Product\Subscription;
12
use App\User;
13
use Carbon\Carbon;
14
use Exception;
15
use Illuminate\Http\Request;
16
use Session;
17
18
class RenewController extends BaseRenewController
19
{
20
    protected $sub;
21
    protected $plan;
22
    protected $order;
23
    protected $invoice;
24
    protected $item;
25
    protected $product;
26
    protected $user;
27
28
    public function __construct()
29
    {
30
        $sub = new Subscription();
31
        $this->sub = $sub;
32
33
        $plan = new Plan();
34
        $this->plan = $plan;
35
36
        $order = new Order();
37
        $this->order = $order;
38
39
        $invoice = new Invoice();
40
        $this->invoice = $invoice;
41
42
        $item = new InvoiceItem();
43
        $this->item = $item;
44
45
        $product = new Product();
46
        $this->product = $product;
47
48
        $user = new User();
49
        $this->user = $user;
50
    }
51
52
    public function renewBySubId($id, $planid, $payment_method, $cost, $code)
53
    {
54
        try {
55
            $plan = $this->plan->find($planid);
56
            $days = $plan->days;
57
            $sub = $this->sub->find($id);
58
            $current = $sub->ends_at;
59
            $ends = $this->getExpiryDate($current, $days);
60
            $sub->ends_at = $ends;
61
            $sub->save();
62
            $this->invoiceBySubscriptionId($id, $planid, $cost);
63
64
            return $sub;
65
        } catch (Exception $ex) {
66
            dd($ex);
67
68
            throw new Exception($ex->getMessage());
69
        }
70
    }
71
72
    public function successRenew($invoice)
73
    {
74
        try {
75
            $invoice->status = 'success';
76
            $invoice->save();
77
78
            $id = Session::get('subscription_id');
79
80
            $planid = Session::get('plan_id');
81
            $plan = $this->plan->find($planid);
82
            $days = $plan->days;
83
            $sub = $this->sub->find($id);
84
            $current = $sub->ends_at;
85
            $ends = $this->getExpiryDate($current, $days);
86
            $sub->ends_at = $ends;
87
            $sub->save();
88
            $this->removeSession();
89
        } catch (Exception $ex) {
90
            throw new Exception($ex->getMessage());
91
        }
92
    }
93
94
    //Tuesday, June 13, 2017 08:06 AM
95
96
    
97
98
99
100
101
    public function getProductById($id)
102
    {
103
        try {
104
            $product = $this->product->where('id', $id)->first();
105
            if ($product) {
106
                return $product;
107
            }
108
        } catch (Exception $ex) {
109
            throw new Exception($ex->getMessage());
110
        }
111
    }
112
113
    public function getUserById($id)
114
    {
115
        try {
116
            $user = $this->user->where('id', $id)->first();
117
            if ($user) {
118
                return $user;
119
            }
120
        } catch (Exception $ex) {
121
            throw new Exception($ex->getMessage());
122
        }
123
    }
124
125
126
127
    public function createOrderInvoiceRelation($orderid, $invoiceid)
128
    {
129
        try {
130
            $relation = new \App\Model\Order\OrderInvoiceRelation();
131
            $relation->create([
132
                'order_id'   => $orderid,
133
                'invoice_id' => $invoiceid,
134
            ]);
135
        } catch (Exception $ex) {
136
            throw new Exception($ex->getMessage());
137
        }
138
    }
139
140
    public function getPriceByProductId($productid, $userid)
141
    {
142
        try {
143
            $product = $this->getProductById($productid);
144
            if (!$product) {
145
                throw new Exception('Product has removed from database');
146
            }
147
            $currency = $this->getUserCurrencyById($userid);
148
            $price = $product->price()->where('currency', $currency)->first();
149
            if (!$price) {
150
                throw new Exception('Price has removed from database');
151
            }
152
            $cost = $price->sales_price;
153
            if (!$cost) {
154
                $cost = $price->regular_price;
155
            }
156
157
            return $cost;
158
        } catch (Exception $ex) {
159
            dd($ex);
160
161
            throw new Exception($ex->getMessage());
162
        }
163
    }
164
165
    public function getUserCurrencyById($userid)
166
    {
167
        try {
168
            $user = $this->user->find($userid);
169
            if (!$user) {
170
                throw new Exception('User has removed from database');
171
            }
172
173
            return $user->currency;
174
        } catch (\Exception $ex) {
175
            throw new \Exception($ex->getMessage());
176
        }
177
    }
178
179
    public function tax($product, $cost, $userid)
180
    {
181
        try {
182
            $controller = new InvoiceController();
183
            $tax = $controller->checkTax($product->id, $userid);
184
            $tax_name = '';
185
            $tax_rate = '';
186
            if (!empty($tax)) {
187
188
                    //dd($value);
189
                $tax_name .= $tax[0].',';
190
                $tax_rate .= $tax[1].',';
191
            }
192
            $grand_total = $controller->calculateTotal($tax_rate, $cost);
193
194
            return \App\Http\Controllers\Front\CartController::rounding($grand_total);
195
        } catch (\Exception $ex) {
196
            throw new \Exception($ex->getMessage());
197
        }
198
    }
199
200
    public function renew($id, Request $request)
201
    {
202
        $this->validate($request, [
203
            'plan'           => 'required',
204
            'payment_method' => 'required',
205
            'cost'           => 'required',
206
            'code'           => 'exists:promotions,code',
207
        ]);
208
209
        try {
210
            $planid = $request->input('plan');
211
            $payment_method = $request->input('payment_method');
212
            $code = $request->input('code');
213
            $cost = $request->input('cost');
214
            $renew = $this->renewBySubId($id, $planid, $payment_method, $cost, $code = '');
215
216
            if ($renew) {
217
                return redirect()->back()->with('success', 'Renewed Successfully');
218
            }
219
220
            return redirect()->back()->with('fails', 'Can not Process');
221
        } catch (Exception $ex) {
222
            return redirect()->back()->back()->with('fails', $ex->getMessage());
223
        }
224
    }
225
226
    public function renewForm($id)
227
    {
228
        try {
229
            $sub = $this->sub->find($id);
230
            $userid = $sub->user_id;
231
            $plans = $this->plan->pluck('name', 'id')->toArray();
232
233
            return view('themes.default1.renew.renew', compact('id', 'plans', 'userid'));
234
        } catch (Exception $ex) {
235
            return redirect()->back()->with('fails', $ex->getMessage());
236
        }
237
    }
238
239
 
240
    
241
242
    public function renewByClient($id, Request $request)
243
    {
244
        $this->validate($request, [
245
            'plan'           => 'required',
246
            'payment_method' => 'required',
247
            'cost'           => 'required',
248
            'code'           => 'exists:promotions,code',
249
        ]);
250
251
        try {
252
            $planid = $request->input('plan');
253
            $payment_method = $request->input('payment_method');
254
            $code = $request->input('code');
255
            $cost = $request->input('cost');
256
            $items = $this->invoiceBySubscriptionId($id, $planid, $cost);
257
            $invoiceid = $items->invoice_id;
258
            $this->setSession($id, $planid);
259
260
            return redirect('paynow/'.$invoiceid);
261
        } catch (\Exception $ex) {
262
            throw new \Exception($ex->getMessage());
263
        }
264
    }
265
266
    public function setSession($sub_id, $planid)
267
    {
268
        Session::put('subscription_id', $sub_id);
269
        Session::put('plan_id', $planid);
270
    }
271
272
    public function removeSession()
273
    {
274
        Session::forget('subscription_id');
275
        Session::forget('plan_id');
276
        Session::forget('invoiceid');
277
    }
278
279
    public function checkRenew()
280
    {
281
        $res = false;
282
        if (Session::has('subscription_id') && Session::has('plan_id')) {
283
            $res = true;
284
        }
285
286
        return $res;
287
    }
288
289
    public function getExpiryDate($end, $days)
290
    {
291
        $date = Carbon::parse($end);
292
        $expiry_date = $date->addDay($days);
293
294
        return $expiry_date;
295
    }
296
}
297