Completed
Push — development ( 7faa8d...b5300f )
by Ashutosh
15:42
created

CartController::removePlanSession()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Common\TemplateController;
6
use App\Model\Common\Setting;
7
use App\Model\Payment\Currency;
8
use App\Model\Payment\PlanPrice;
9
use App\Model\Payment\Tax;
10
use App\Model\Payment\TaxByState;
11
use App\Model\Payment\TaxClass;
12
use App\Model\Payment\TaxOption;
13
use App\Model\Payment\TaxProductRelation;
14
use App\Model\Product\Product;
15
use Bugsnag;
16
use Cart;
17
use Exception;
18
use Illuminate\Http\Request;
19
use Session;
20
21
class CartController extends BaseCartController
22
{
23
    public $templateController;
24
    public $product;
25
    public $currency;
26
    public $addons;
27
    public $addonRelation;
28
    public $licence;
29
    public $tax_option;
30
    public $tax_by_state;
31
    public $setting;
32
33
    public function __construct()
34
    {
35
        $templateController = new TemplateController();
36
        $this->templateController = $templateController;
37
38
        $product = new Product();
39
        $this->product = $product;
40
41
        $plan_price = new PlanPrice();
42
        $this->$plan_price = $plan_price;
43
44
        $currency = new Currency();
45
        $this->currency = $currency;
46
47
        $tax = new Tax();
48
        $this->tax = $tax;
49
50
        $setting = new Setting();
51
        $this->setting = $setting;
52
53
        $tax_option = new TaxOption();
54
        $this->tax_option = $tax_option;
55
56
        $tax_by_state = new TaxByState();
57
        $this->tax_by_state = new $tax_by_state();
58
59
        // $this->middleware('Inatall');
60
        // $this->middleware('admin');
61
    }
62
63
    public function productList(Request $request)
64
    {
65
        try {
66
            $cont = new \App\Http\Controllers\Front\GetPageTemplateController();
67
            $location = $cont->getLocation();
68
        } catch (\Exception $ex) {
69
            $location = false;
70
            $error = $ex->getMessage();
71
        }
72
73
        $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
1 ignored issue
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
74
        $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
75
        $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
76
        $state_code = $location['countryCode'].'-'.$location['region'];
77
        $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
78
        $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
79
        $currency = $cont->getCurrency($location);
80
81
        \Session::put('currency', $currency);
82
        if (!\Session::has('currency')) {
83
            \Session::put('currency', 'INR');
84
        }
85
86
        try {
87
            $page_controller = new PageController();
88
89
            return $page_controller->cart();
90
        } catch (\Exception $ex) {
91
            return redirect()->back()->with('fails', $ex->getMessage());
92
        }
93
    }
94
95
    public function cart(Request $request)
96
    {
97
        try {
98
            $plan = '';
99
100
            if ($request->has('subscription')) {
101
                $plan = $request->get('subscription');
102
103
                Session::put('plan', $plan);
104
            }
105
            $id = $request->input('id');
106
107
            if (!array_key_exists($id, Cart::getContent())) {
108
                $items = $this->addProduct($id);
109
110
                Cart::add($items);
111
            }
112
113
            return redirect('show/cart');
114
        } catch (\Exception $ex) {
115
            // dd($ex);
116
            return redirect()->back()->with('fails', $ex->getMessage());
117
        }
118
    }
119
120
    public function showCart()
121
    {
122
        try {
123
            $currency = 'INR';
124
            $cart_currency = 'INR';
125
            $attributes = [];
126
            $cartCollection = Cart::getContent();
127
            foreach ($cartCollection as $item) {
128
                $attributes[] = $item->attributes;
129
                $cart_currency = $attributes[0]['currency'];
130
                $currency = $attributes[0]['currency'];
131
                if (\Auth::user()) {
132
                    $cart_currency = $attributes[0]['currency'];
133
                    $user_currency = \Auth::user()->currency;
134
                    $user_country = \Auth::user()->country;
135
                    $user_state = \Auth::user()->state;
136
                    $currency = 'INR';
137
                    if ($user_currency == 1 || $user_currency == 'USD') {
138
                        $currency = 'USD';
139
                    }
140
                    if ($cart_currency != $currency) {
141
                        $id = $item->id;
142
                        Cart::remove($id);
143
                        $items = $this->addProduct($id);
144
145
                        Cart::add($items);
146
                        //
147
                    }
148
                }
149
            }
150
            // if ($cart_currency != $currency) {
151
            //     return redirect('show/cart');
152
            // }
153
            //dd(Cart::getContent());
154
155
            return view('themes.default1.front.cart', compact('cartCollection', 'attributes'));
156
        } catch (\Exception $ex) {
157
            //dd($ex);
158
159
            return redirect()->back()->with('fails', $ex->getMessage());
160
        }
161
    }
162
163
    public function checkTax($productid, $user_state = '', $user_country = '')
164
    {
165
        try {
166
            $taxCondition = [];
167
            $tax_attribute = [];
168
            $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
169
            $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
170
                'name'   => 'null',
171
                'type'   => 'tax',
172
                'target' => 'item',
173
                'value'  => '0%',
174
            ]);
175
            $cont = new \App\Http\Controllers\Front\GetPageTemplateController();
176
            $location = $cont->getLocation();
177
178
            $country = $this->findCountryByGeoip($location['countryCode']);
179
            $states = $this->findStateByRegionId($location['countryCode']);
180
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
181
            $state_code = $location['countryCode'].'-'.$location['region'];
182
            $state = $this->getStateByCode($state_code);
183
            $mobile_code = $this->getMobileCodeByIso($location['countryCode']);
184
            $country_iso = $location['countryCode'];
185
186
            $geoip_state = $this->getGeoipState($state_code, $user_state);
187
            $geoip_country = $this->getGeoipCountry($country_iso, $user_country);
188
189
            if ($this->tax_option->findOrFail(1)->inclusive == 0) {
190
                $tax_rule = $this->tax_option->findOrFail(1);
191
                $shop = $tax_rule->shop_inclusive;
192
                $cart = $tax_rule->cart_inclusive;
193
                $tax_enable = $this->tax_option->findOrFail(1)->tax_enable;
194
                //Check the state of user for calculating GST(cgst,igst,utgst,sgst)
195
                $user_state = TaxByState::where('state_code', $geoip_state)->first();
196
                $origin_state = $this->setting->first()->state; //Get the State of origin
197
                $tax_class_id = TaxProductRelation::where('product_id', $productid)->pluck('tax_class_id')->toArray();
198
199
                if ($tax_class_id) {//If the product is allowed for tax (Check in tax_product relation table)
200
                   if ($tax_enable == 1) {//If GST is Enabled
201
202
                       $state_code = '';
203
                       $c_gst = '';
204
                       $s_gst = '';
205
                       $i_gst = '';
206
                       $ut_gst = '';
207
                       $value = '';
208
                       $rate = '';
209
                       $status = 1;
210
211
                       if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user
212
                           $c_gst = $user_state->c_gst;
213
                           $s_gst = $user_state->s_gst;
214
                           $i_gst = $user_state->i_gst;
215
                           $ut_gst = $user_state->ut_gst;
216
                           $state_code = $user_state->state_code;
217
218
                           if ($state_code == $origin_state) {//If user and origin state are same
219
                               $rateForSameState = $this->getTaxWhenIndianSameState($user_state,
220
                                $origin_state, $productid, $c_gst, $s_gst, $state_code, $status);
221
222
                               $taxes = $rateForSameState['taxes'];
223
                               $status = $rateForSameState['status'];
224
                               $value = $rateForSameState['value'];
225
                           } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
226
                               $rateForOtherState = $this->getTaxWhenIndianOtherState($user_state,
227
                                $origin_state, $productid, $i_gst, $state_code, $status);
228
                               $taxes = $rateForOtherState['taxes'];
229
                               $status = $rateForOtherState['status'];
230
                               $value = $rateForOtherState['value'];
231
                           } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
232
                               $rateForUnionTerritory = $this->getTaxWhenUnionTerritory($user_state,
233
                                $origin_state, $productid, $c_gst, $ut_gst, $state_code, $status);
234
                               $taxes = $rateForUnionTerritory['taxes'];
235
                               $status = $rateForUnionTerritory['status'];
236
                               $value = $rateForUnionTerritory['value'];
237
                           }
238
                       } else {//If user from other Country
239
                           $taxClassId = Tax::where('state', $geoip_state)
240
                           ->orWhere('country', $geoip_country)
241
                           ->pluck('tax_classes_id')->first();
242
                           if ($taxClassId) { //if state equals the user State or country equals user country
243
                               $taxForSpecificCountry = $this->getTaxForSpecificCountry($taxClassId,
244
                                $productid, $status);
245
                               $taxes = $taxForSpecificCountry['taxes'];
246
                               $status = $taxForSpecificCountry['status'];
247
                               $value = $taxForSpecificCountry['value'];
248
                               $rate = $taxForSpecificCountry['value'];
249
                           } else {//if Tax is selected for Any Country Any State
250
                               $taxClassId = Tax::where('country', '')
251
                               ->where('state', 'Any State')
252
                               ->pluck('tax_classes_id')->first();
253
                               if ($taxClassId) {
254
                                   $taxForAnyCountry = $this->getTaxForAnyCountry($taxClassId, $productid, $status);
255
                                   $taxes = $taxForAnyCountry['taxes'];
256
                                   $status = $taxForAnyCountry['status'];
257
                                   $value = $taxForAnyCountry['value'];
258
                                   $rate = $taxForAnyCountry['value'];
259
                               } else {
260
                                   $taxes = [0];
261
                               }
262
                           }
263
                       }
264
                       foreach ($taxes as $key => $tax) {
265
266
                                    //All the da a attribute that is sent to the checkout Page if tax_compound=0
267
                           if ($taxes[0]) {
268
                               $tax_attribute[$key] = ['name' => $tax->name, 'c_gst'=>$c_gst,
269
                               's_gst'                        => $s_gst, 'i_gst'=>$i_gst, 'ut_gst'=>$ut_gst,
270
                                'state'                       => $state_code, 'origin_state'=>$origin_state,
271
                                 'tax_enable'                 => $tax_enable, 'rate'=>$value, 'status'=>$status, ];
272
273
                               $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
274
275
                                            'name'   => 'no compound',
276
                                            'type'   => 'tax',
277
                                            'target' => 'item',
278
                                            'value'  => $value,
279
                                          ]);
280
                           } else {
281
                               $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
282
                               $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
283
                                           'name'   => 'null',
284
                                           'type'   => 'tax',
285
                                           'target' => 'item',
286
                                           'value'  => '0%',
287
                                         ]);
288
                           }
289
                       }
290
                   } elseif ($tax_enable == 0) {//If Tax enable is 0
291
                       $status = 1;
292
                       if ($this->tax_option->findOrFail(1)->tax_enable == 0) {
293
                           $taxClassId = Tax::where('country', '')
294
                           ->where('state', 'Any State')
295
                           ->pluck('tax_classes_id')->first(); //In case of India when
296
                           // other tax is available and tax is not enabled
297
                           if ($taxClassId) {
298
                               $taxes = $this->getTaxByPriority($taxClassId);
299
                               $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
300
                               if ($value == 0) {
301
                                   $status = 0;
302
                               }
303
                               $rate = $value;
304
                               foreach ($taxes as $key => $tax) {
305
                                   $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $value, 'tax_enable'=>0, 'status' => $status];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
306
                                   $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([
307
308
                                            'name'   => $tax->name,
309
                                            'type'   => 'tax',
310
                                            'target' => 'item',
311
                                            'value'  => $value,
312
                                        ]);
313
                               }
314
                           } else {//In case of other country
315
                               //when tax is available and tax is not enabled
316
                               //(Applicable when Global Tax class for any country and state is not there)
317
                               $taxClassId = Tax::where('state', $geoip_state)
318
                               ->orWhere('country', $geoip_country)
319
                               ->pluck('tax_classes_id')->first();
320
                               if ($taxClassId) { //if state equals the user State
321
                                   $taxes = $this->getTaxByPriority($taxClassId);
322
                                   $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
323
                                   if ($value == '') {
324
                                       $status = 0;
325
                                   }
326
                                   $rate = $value;
327
                               }
328
                               foreach ($taxes as $key => $tax) {
329
                                   $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $value, 'tax_enable'=>0, 'status' => $status];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
330
                                   $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([
331
332
                                            'name'   => $tax->name,
333
                                            'type'   => 'tax',
334
                                            'target' => 'item',
335
                                            'value'  => $value,
336
                                        ]);
337
                               }
338
                           }
339
                       }
340
                   }
341
                } else {
342
                    $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
343
                    $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
344
                'name'   => 'null',
345
                'type'   => 'tax',
346
                'target' => 'item',
347
                'value'  => '0%',
348
            ]);
349
                }
350
            }
351
352
            $currency_attribute = $this->addCurrencyAttributes($productid);
353
354
            return ['conditions' => $taxCondition,
355
            'attributes'         => ['tax' => $tax_attribute,
356
            'currency'                     => $currency_attribute, ], ];
357
        } catch (\Exception $ex) {
358
            Bugsnag::notifyException($ex);
359
360
            throw new \Exception('Can not check the tax');
361
        }
362
    }
363
364
    /**
365
     *   Get tax value for Same State.
366
     *
367
     * @param type $productid
368
     * @param type $c_gst
369
     * @param type $s_gst
370
     *                        return type
371
     */
372
    public function getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes)
373
    {
374
        try {
375
            $value = '';
376
            $value = $taxes->toArray()[0]['active'] ?
377
378
                  (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
379
                        $c_gst + $s_gst.'%' : 0) : 0;
380
381
            return $value;
382
        } catch (Exception $ex) {
383
            Bugsnag::notifyException($ex);
384
385
            return redirect()->back()->with('fails', $ex->getMessage());
386
        }
387
    }
388
389
    /**
390
     *   Get tax value for Other States.
391
     *
392
     * @param type $productid
393
     * @param type $i_gst
394
     *                        return type
395
     */
396
    public function getValueForOtherState($productid, $i_gst, $taxClassId, $taxes)
397
    {
398
        $value = '';
399
        $value = $taxes->toArray()[0]['active'] ? //If the Current Class is active
400
              (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
401
                        $i_gst.'%' : 0) : 0; //IGST
402
403
        return $value;
404
    }
405
406
    /**
407
     *  Get tax value for Union Territory States.
408
     *
409
     * @param type $productid
410
     * @param type $c_gst
411
     * @param type $ut_gst
412
     *                        return type
413
     */
414
    public function getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes)
415
    {
416
        $value = '';
417
        $value = $taxes->toArray()[0]['active'] ?
418
             (TaxProductRelation::where('product_id', $productid)
419
              ->where('tax_class_id', $taxClassId)->count() ? $ut_gst + $c_gst.'%' : 0) : 0;
420
421
        return $value;
422
    }
423
424
    public function otherRate($productid)
425
    {
426
        // $taxClassOther = TaxClass::where('name', 'Others')->pluck('id')->toArray();
427
        // $otherRate = TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassOther)->count() ? Tax::where('tax_classes_id', $taxClassOther)->first()->rate : '';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 191 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
428
        $otherRate = '';
429
430
        return $otherRate;
431
    }
432
433
    public function getValueForOthers($productid, $taxClassId, $taxes)
434
    {
435
        $otherRate = 0;
436
        $status = $taxes->toArray()[0]['active'];
437
        if ($status && (TaxProductRelation::where('product_id', $productid)
438
          ->where('tax_class_id', $taxClassId)->count() > 0)) {
439
            $otherRate = Tax::where('tax_classes_id', $taxClassId)->first()->rate;
440
        }
441
442
        // $value= $taxes->toArray()[0]['active'] ?
443
        //  (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() != 0) ?
444
        //  $otherRate = Tax::where('tax_classes_id', $taxClassId)->first()->rate;
445
446
        $value = $otherRate.'%';
447
448
        return $value;
449
    }
450
451
452
    public function cartRemove(Request $request)
453
    {
454
        $id = $request->input('id');
455
        Cart::remove($id);
456
457
        return 'success';
458
    }
459
460
    public function reduseQty(Request $request)
461
    {
462
        $id = $request->input('id');
463
        Cart::update($id, [
464
            'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3
465
        ]);
466
467
        return 'success';
468
    }
469
470
    public function updateQty(Request $request)
471
    {
472
        $id = $request->input('productid');
473
        $qty = $request->input('qty');
474
        Cart::update($id, [
475
            'quantity' => [
476
                'relative' => false,
477
                'value'    => $qty,
478
            ],
479
        ]);
480
481
        return 'success';
482
    }
483
484
485
    /**
486
     * @return type
487
     */
488
    public function clearCart()
489
    {
490
        foreach (Cart::getContent() as $item) {
491
            if (\Session::has('domain'.$item->id)) {
492
                \Session::forget('domain'.$item->id);
493
            }
494
        }
495
        $this->removePlanSession();
496
        $renew_control = new \App\Http\Controllers\Order\RenewController();
497
        $renew_control->removeSession();
498
        Cart::clear();
499
500
        return redirect('show/cart');
501
    }
502
503
    
504
505
    /**
506
     * @param type $id
507
     * @param type $key
508
     * @param type $value
509
     */
510
    public function cartUpdate($id, $key, $value)
511
    {
512
        try {
513
            Cart::update(
514
                    $id, [
515
                $key => $value, // new item name
516
                    ]
517
            );
518
        } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
519
        }
520
    }
521
522
    /**
523
     * @param type $id
524
     *
525
     * @return array
526
     */
527
    public function addCurrencyAttributes($id)
528
    {
529
        try {
530
            $currency = $this->currency();
531
            $product = $this->product->where('id', $id)->first();
532
            if ($product) {
533
                $productCurrency = $this->currency();
534
                $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
535
            } else {
536
                $currency = [];
537
            }
538
539
            return $currency;
540
        } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
541
        }
542
    }
543
544
    /**
545
     * @return type
546
     */
547
    public function addCouponUpdate()
548
    {
549
        try {
550
            $code = \Input::get('coupon');
551
            $cart = Cart::getContent();
552
            foreach ($cart as $item) {
553
                $id = $item->id;
554
            }
555
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
556
            $result = $promo_controller->checkCode($code, $id);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $id seems to be defined by a foreach iteration on line 552. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
557
            if ($result == 'success') {
558
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
559
            }
560
561
            return redirect()->back();
562
        } catch (\Exception $ex) {
563
            dd($ex);
564
565
            return redirect()->back()->with('fails', $ex->getMessage());
566
        }
567
    }
568
569
    /**
570
     * @param type $tax_class_id
571
     *
572
     * @throws \Exception
573
     *
574
     * @return type
575
     */
576
    public function getTaxByPriority($taxClassId)
577
    {
578
        try {
579
            $taxe_relation = $this->tax->where('tax_classes_id', $taxClassId)->get();
580
581
            return $taxe_relation;
582
        } catch (\Exception $ex) {
583
            Bugsnag::notifyException($ex);
584
585
            throw new \Exception('error in get tax priority');
586
        }
587
    }
588
589
    /**
590
     * @param type $price
591
     *
592
     * @throws \Exception
593
     *
594
     * @return type
595
     */
596
    public static function rounding($price)
597
    {
598
        try {
599
            $tax_rule = new \App\Model\Payment\TaxOption();
600
            $rule = $tax_rule->findOrFail(1);
601
            $rounding = $rule->rounding;
602
            if ($rounding == 1) {
603
                // $price = str_replace(',', '', $price);
604
605
                return round($price);
606
            } else {
607
                return $price;
608
            }
609
        } catch (\Exception $ex) {
610
            dd($ex);
611
            Bugsnag::notifyException($ex);
612
            // throw new \Exception('error in get tax priority');
613
        }
614
    }
615
616
    /**
617
     * @return type
618
     */
619
    public function contactUs()
620
    {
621
        try {
622
            return view('themes.default1.front.contact');
623
        } catch (\Exception $ex) {
624
            return redirect()->back()->with('fails', $ex->getMessage());
625
        }
626
    }
627
628
    /**
629
     * @param Request $request
630
     *
631
     * @return type
632
     */
633
    public function postContactUs(Request $request)
634
    {
635
        $this->validate($request, [
636
            'name'    => 'required',
637
            'email'   => 'required|email',
638
            'message' => 'required',
639
        ]);
640
641
        $set = new \App\Model\Common\Setting();
642
        $set = $set->findOrFail(1);
643
644
        try {
645
            $from = $set->email;
646
            $fromname = $set->company;
647
            $toname = '';
648
            $to = '[email protected]';
649
            $data = '';
650
            $data .= 'Name: '.$request->input('name').'<br/s>';
651
            $data .= 'Email: '.$request->input('email').'<br/>';
652
            $data .= 'Message: '.$request->input('message').'<br/>';
653
            $data .= 'Mobile: '.$request->input('Mobile').'<br/>';
654
655
            $subject = 'Faveo billing enquiry';
656
            $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname);
657
            //$this->templateController->Mailing($from, $to, $data, $subject);
658
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
659
        } catch (\Exception $ex) {
660
            return redirect()->back()->with('fails', $ex->getMessage());
661
        }
662
    }
663
664
665
666
    /**
667
     * @param type $code
668
     *
669
     * @throws \Exception
670
     *
671
     * @return type
672
     */
673
    public static function getCountryByCode($code)
674
    {
675
        try {
676
            $country = \App\Model\Common\Country::where('country_code_char2', $code)->first();
677
            if ($country) {
678
                return $country->country_name;
679
            }
680
        } catch (\Exception $ex) {
681
            throw new \Exception($ex->getMessage());
682
        }
683
    }
684
685
    /**
686
     * @param type $name
687
     *
688
     * @throws \Exception
689
     *
690
     * @return string
691
     */
692
    public static function getTimezoneByName($name)
693
    {
694
        try {
695
            if ($name) {
696
                $timezone = \App\Model\Common\Timezone::where('name', $name)->first();
697
                if ($timezone) {
698
                    $timezone = $timezone->id;
699
                } else {
700
                    $timezone = '114';
701
                }
702
            } else {
703
                $timezone = '114';
704
            }
705
706
            return $timezone;
707
        } catch (\Exception $ex) {
708
            throw new \Exception($ex->getMessage());
709
        }
710
    }
711
712
    /**
713
     * @param type $code
714
     *
715
     * @throws \Exception
716
     *
717
     * @return type
718
     */
719
    public static function getStateByCode($code)
720
    {
721
        try {
722
            $result = ['id' => '', 'name' => ''];
723
            if ($code) {
0 ignored issues
show
introduced by
$code is of type App\Http\Controllers\Front\type, thus it always evaluated to true.
Loading history...
724
                $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first();
725
                if ($subregion) {
726
                    $result = ['id' => $subregion->state_subdivision_code,
727
                     'name' => $subregion->state_subdivision_name];
728
                    }
729
            }
730
731
            return $result;
732
        } catch (\Exception $ex) {
733
            throw new \Exception($ex->getMessage());
734
        }
735
    }
736
737
    /**
738
     * @param type $id
739
     *
740
     * @throws \Exception
741
     *
742
     * @return type
743
     */
744
    public static function getStateNameById($id)
745
    {
746
        try {
747
            $name = '';
748
            $subregion = \App\Model\Common\State::where('state_subdivision_id', $id)->first();
749
            if ($subregion) {
750
                $name = $subregion->state_subdivision_name;
751
            }
752
753
            return $name;
754
        } catch (\Exception $ex) {
755
            throw new \Exception($ex->getMessage());
756
        }
757
    }
758
759
    /**
760
     * @param type $productid
761
     * @param type $price
762
     * @param type $cart
763
     * @param type $cart1
764
     * @param type $shop
765
     *
766
     * @return type
767
     */
768
    public static function calculateTax($productid, $price, $cart = 1, $cart1 = 0, $shop = 0)
769
    {
770
        try {
771
            $template_controller = new TemplateController();
772
            $result = $template_controller->checkTax($productid, $price, $cart, $cart1, $shop);
773
774
            $result = self::rounding($result);
775
776
            return $result;
777
        } catch (\Exception $ex) {
778
            return redirect()->back()->with('fails', $ex->getMessage());
779
        }
780
    }
781
782
    /**
783
     * @param type $rate
784
     * @param type $price
785
     *
786
     * @return type
787
     */
788
    public static function taxValue($rate, $price)
789
    {
790
        try {
791
            $tax = $price * ($rate / 100);
792
            $result = $tax;
793
794
            $result = self::rounding($result);
795
796
            return $result;
797
        } catch (\Exception $ex) {
798
            return redirect()->back()->with('fails', $ex->getMessage());
799
        }
800
    }
801
802
    /**
803
     * @return type
804
     */
805
    public static function addons()
806
    {
807
        try {
808
            $items = Cart::getContent();
809
            $cart_productids = [];
810
            if (count($items) > 0) {
811
                foreach ($items as $key => $item) {
812
                    $cart_productids[] = $key;
813
                }
814
            }
815
            $_this = new self();
816
            $products = $_this->products($cart_productids);
817
818
            return $products;
819
        } catch (\Exception $ex) {
820
            return redirect()->back()->with('fails', $ex->getMessage());
821
        }
822
    }
823
824
    /**
825
     * @param type $ids
826
     *
827
     * @throws \Exception
828
     *
829
     * @return type
830
     */
831
    public function products($ids)
832
    {
833
        $parents_string = [];
834
        $parent = [];
835
        $productid = [];
836
837
        try {
838
            $parents = $this->product
839
                    ->whereNotNull('parent')
840
                    ->where('parent', '!=', 0)
841
                    ->where('category', 'addon')
842
                    ->pluck('parent', 'id')
843
                    ->toArray();
844
            foreach ($parents as $key => $parent) {
845
                if (is_array($parent)) {
846
                    $parent = implode(',', $parent);
847
                }
848
                $parents_string[$key] = $parent;
849
            }
850
            if (count($parents_string) > 0) {
851
                foreach ($parents_string as $key => $value) {
852
                    if (strpos($value, ',') !== false) {
853
                        $value = explode(',', $value);
854
                    }
855
                    $parent[$key] = $value;
856
                }
857
            }
858
859
            foreach ($parent as $key => $id) {
860
                if (in_array($id, $ids)) {
861
                    $productid[] = $key;
862
                }
863
                if (is_array($id)) {
864
                    foreach ($id as $i) {
865
                        if (in_array($i, $ids)) {
866
                            $productid[] = $key;
867
                        }
868
                    }
869
                }
870
            }
871
            $parent_products = $this->getProductById($productid);
872
873
            return $parent_products;
874
        } catch (\Exception $ex) {
875
            Bugsnag::notifyException($ex);
876
877
            throw new \Exception($ex->getMessage());
878
        }
879
    }
880
881
    /**
882
     * @param type $ids
883
     *
884
     * @throws \Exception
885
     *
886
     * @return type
887
     */
888
    public function getProductById($ids)
889
    {
890
        try {
891
            $products = [];
892
            if (count($ids) > 0) {
893
                $products = $this->product
894
                        ->whereIn('id', $ids)
895
                        ->get();
896
            }
897
898
            return $products;
899
        } catch (\Exception $ex) {
900
            dd($ex);
901
902
            throw new \Exception($ex->getMessage());
903
        }
904
    }
905
906
    /**
907
     * @param type $iso
908
     *
909
     * @throws \Exception
910
     *
911
     * @return type
912
     */
913
    public static function getMobileCodeByIso($iso)
914
    {
915
        try {
916
            $code = '';
917
            if ($iso != '') {
918
                $mobile = \DB::table('mobile')->where('iso', $iso)->first();
919
                if ($mobile) {
920
                    $code = $mobile->phonecode;
921
                }
922
            }
923
924
            return $code;
925
        } catch (\Exception $ex) {
926
            throw new \Exception($ex->getMessage());
927
        }
928
    }
929
930
    /**
931
     * @param type $userid
932
     *
933
     * @throws \Exception
934
     *
935
     * @return string
936
     */
937
    public function currency($userid = '')
938
    {
939
        try {
940
            $currency = 'INR';
941
            if ($this->checkCurrencySession() == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
942
                $currency = Session::get('currency');
943
            }
944
945
            if (\Auth::user()) {
946
                $currency = \Auth::user()->currency;
947
                if ($currency == 'USD' || $currency == '1') {
948
                    $currency = 'USD';
949
                }
950
            }
951
            if ($userid != '') {
952
                $user = new \App\User();
953
                $currency = $user->find($userid)->currency;
954
                if ($currency == 'USD' || $currency == '1') {
955
                    $currency = 'USD';
956
                } else {
957
                    $currency = 'INR';
958
                }
959
            }
960
            // dd($currency);
961
            return $currency;
962
        } catch (\Exception $ex) {
963
            throw new \Exception($ex->getMessage());
964
        }
965
    }
966
967
    /**
968
     * @param type $productid
969
     * @param type $userid
970
     * @param type $planid
971
     *
972
     * @return type
973
     */
974
    public function cost($productid, $userid = '', $planid = '')
975
    {
976
        try {
977
            $cost = $this->planCost($productid, $userid, $planid);
978
            if ($cost == 0) {
979
                $cost = $this->productCost($productid, $userid);
980
            }
981
982
            return self::rounding($cost);
983
        } catch (\Exception $ex) {
984
            // throw new \Exception($ex->getMessage());
985
        }
986
    }
987
988
    /**
989
     * @param type $productid
990
     * @param type $userid
991
     *
992
     * @throws \Exception
993
     *
994
     * @return type
995
     */
996
    public function productCost($productid, $userid = '')
997
    {
998
        try {
999
            $sales = 0;
1000
            $currency = $this->currency($userid);
1001
            $product = $this->product->find($productid);
1002
            $price = $product->price()->where('currency', $currency)->first();
1003
            if ($price) {
1004
                $sales = $price->sales_price;
1005
                if ($sales == 0) {
1006
                    $sales = $price->price;
1007
                }
1008
            }
1009
            //}
1010
1011
            return $sales;
1012
        } catch (\Exception $ex) {
1013
            throw new \Exception($ex->getMessage());
1014
        }
1015
    }
1016
1017
    /**
1018
     * @param type $productid
1019
     * @param type $userid
1020
     * @param type $planid
1021
     *
1022
     * @throws \Exception
1023
     *
1024
     * @return type
1025
     */
1026
    public function planCost($productid, $userid, $planid = '')
1027
    {
1028
        try {
1029
            $cost = 0;
1030
            $subscription = $this->allowSubscription($productid);
1031
            if ($this->checkPlanSession() == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
1032
                $planid = Session::get('plan');
1033
            }
1034
1035
            if ($subscription == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
1036
                $plan = new \App\Model\Payment\Plan();
1037
                $plan = $plan->where('id', $planid)->where('product', $productid)->first();
1038
1039
                if ($plan) {
1040
                    $currency = $this->currency($userid);
1041
                    $price = $plan->planPrice()
1042
                                    ->where('currency', $currency)
1043
                                    ->first()
1044
                            ->add_price;
1045
                    $days = $plan->days;
1046
                    $months = $days / 30 / 12;
1047
                    $cost = round($months) * $price;
1048
                }
1049
            }
1050
1051
            return $cost;
1052
        } catch (\Exception $ex) {
1053
            throw new \Exception($ex->getMessage());
1054
        }
1055
    }
1056
1057
  
1058
    /**
1059
     * @throws \Exception
1060
     *
1061
     * @return bool
1062
     */
1063
    public function checkCurrencySession()
1064
    {
1065
        try {
1066
            if (Session::has('currency')) {
1067
                return true;
1068
            }
1069
1070
            return false;
1071
        } catch (\Exception $ex) {
1072
            throw new \Exception($ex->getMessage());
1073
        }
1074
    }
1075
1076
    /**
1077
     * @param type $productid
1078
     *
1079
     * @throws \Exception
1080
     *
1081
     * @return bool
1082
     */
1083
    public function allowSubscription($productid)
1084
    {
1085
        try {
1086
            $reponse = false;
1087
            $product = $this->product->find($productid);
1088
            if ($product) {
1089
                if ($product->subscription == 1) {
1090
                    $reponse = true;
1091
                }
1092
            }
1093
1094
            return $reponse;
1095
        } catch (\Exception $ex) {
1096
            throw new \Exception($ex->getMessage());
1097
        }
1098
    }
1099
}
1100