Completed
Branch feature-dynamic-fields (3b03cc)
by Ashutosh
09:05
created

CartController::contactUs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Common\TemplateController;
6
use App\Model\Common\Country;
7
use App\Model\Common\Setting;
8
use App\Model\Payment\Currency;
9
use App\Model\Payment\PlanPrice;
10
use App\Model\Payment\Tax;
11
use App\Model\Payment\TaxByState;
12
use App\Model\Payment\TaxOption;
13
use App\Model\Payment\TaxProductRelation;
14
use App\Model\Product\Product;
15
use App\Traits\TaxCalculation;
16
use Bugsnag;
17
use Cart;
18
use Illuminate\Http\Request;
19
use Session;
20
21
class CartController extends BaseCartController
22
{
23
    use TaxCalculation;
0 ignored issues
show
introduced by
The trait App\Traits\TaxCalculation requires some properties which are not provided by App\Http\Controllers\Front\CartController: $rate, $i_gst
Loading history...
24
25
    public $templateController;
26
    public $product;
27
    public $currency;
28
    public $addons;
29
    public $addonRelation;
30
    public $licence;
31
    public $tax_option;
32
    public $tax_by_state;
33
    public $setting;
34
35
    public function __construct()
36
    {
37
        $templateController = new TemplateController();
38
        $this->templateController = $templateController;
39
40
        $product = new Product();
41
        $this->product = $product;
42
43
        $plan_price = new PlanPrice();
44
        $this->$plan_price = $plan_price;
45
46
        $currency = new Currency();
47
        $this->currency = $currency;
48
49
        $tax = new Tax();
50
        $this->tax = $tax;
51
52
        $setting = new Setting();
53
        $this->setting = $setting;
54
55
        $tax_option = new TaxOption();
56
        $this->tax_option = $tax_option;
57
58
        $tax_by_state = new TaxByState();
59
        $this->tax_by_state = new $tax_by_state();
60
    }
61
62
    public function productList(Request $request)
63
    {
64
        $cont = new \App\Http\Controllers\Front\PageController();
65
        $location = $cont->getLocation();
66
        $country = $this->findCountryByGeoip($location['iso_code']);
67
        $states = $this->findStateByRegionId($location['iso_code']);
68
        $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
69
        $state_code = $location['iso_code'].'-'.$location['state'];
70
        $state = $this->getStateByCode($state_code);
71
        $mobile_code = $this->getMobileCodeByIso($location['iso_code']);
72
        $currency = $this->currency();
73
74
        \Session::put('currency', $currency);
75
        if (!\Session::has('currency')) {
76
            \Session::put('currency', 'INR');
77
        }
78
79
        try {
80
            $page_controller = new PageController();
81
82
            return $page_controller->cart();
83
        } catch (\Exception $ex) {
84
            return redirect()->back()->with('fails', $ex->getMessage());
85
        }
86
    }
87
88
    /*
89
     * The first request to the cart Page comes here
90
     * Get Plan id and Product id as Request
91
     *
92
     * @param  int  $plan   Planid;
93
     * @param  int  $id     Productid;
94
     */
95
    public function cart(Request $request)
96
    {
97
        try {
98
            $plan = '';
99
            if ($request->has('subscription')) {//put he Plan id sent into session variable
100
                $plan = $request->get('subscription');
101
                Session::put('plan', $plan);
102
            }
103
            $id = $request->input('id');
104
            if (!array_key_exists($id, Cart::getContent())) {
105
                $items = $this->addProduct($id);
106
                \Cart::add($items); //Add Items To the Cart Collection
107
            }
108
109
            return redirect('show/cart');
110
        } catch (\Exception $ex) {
111
            app('log')->error($ex->getMessage());
112
            Bugsnag::notifyException($ex->getMessage());
113
114
            return redirect()->back()->with('fails', $ex->getMessage());
115
        }
116
    }
117
118
    /*
119
     * Show the cart with all the Cart Attributes and Cart Collections
120
     * Link: https://github.com/darryldecode/laravelshoppingcart
121
     */
122
    public function showCart()
123
    {
124
        try {
125
            $cartCollection = Cart::getContent();
126
            $attributes = [];
127
            foreach ($cartCollection as $item) {
128
                $attributes[] = $item->attributes;
129
                $cart_currency = $attributes[0]['currency']['currency'];
130
                if (\Auth::user()) {//If User is Loggen in and his currency changes after logginng in then remove his previous order from cart
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
131
                    $currency = \Auth::user()->currency;
132
                    if ($cart_currency != $currency) {
133
                        $id = $item->id;
134
                        Cart::session(\Auth::user()->id)->remove($id);
135
                        $items = $this->addProduct($id);
136
                        Cart::add($items);
137
                    }
138
                }
139
            }
140
141
            return view('themes.default1.front.cart', compact('cartCollection', 'attributes'));
142
        } catch (\Exception $ex) {
143
            app('log')->error($ex->getMessage());
144
            Bugsnag::notifyException($ex->getMessage());
145
146
            return redirect()->back()->with('fails', $ex->getMessage());
147
        }
148
    }
149
150
    public function checkTax($productid, $user_state = '', $user_country = '')
151
    {
152
        try {
153
            $taxCondition = [];
154
            $tax_attribute = [];
155
            $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
156
            $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
157
                'name'   => 'null',
158
                'type'   => 'tax',
159
                'target' => 'item',
160
                'value'  => '0%',
161
            ]);
162
            $cont = new \App\Http\Controllers\Front\PageController();
163
            $location = $cont->getLocation();
164
            $country = $this->findCountryByGeoip($location['iso_code']); //Get country by geopip
165
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
166
            $state_code = $location['iso_code'].'-'.$location['state'];
167
            $state = $this->getStateByCode($state_code); //match the geoip state with billing table state.
168
            $geoip_state = $this->getGeoipState($state_code, $user_state);
169
            $geoip_country = $this->getGeoipCountry($location['iso_code'], $user_country);
170
171
            if ($this->tax_option->findOrFail(1)->inclusive == 0) {
172
                $tax_enable = $this->tax_option->findOrFail(1)->tax_enable;
173
                //Check the state of user for calculating GST(cgst,igst,utgst,sgst)
174
                $user_state = TaxByState::where('state_code', $geoip_state)->first();
175
                $origin_state = $this->setting->first()->state; //Get the State of origin
176
                $tax_class_id = TaxProductRelation::where('product_id', $productid)->pluck('tax_class_id')->toArray();
177
178
                if (count($tax_class_id) > 0) {//If the product is allowed for tax (Check in tax_product relation table)
179
                    if ($tax_enable == 1) {//If GST is Enabled
180
181
                         $details = $this->getDetailsWhenUserStateIsIndian(
182
                             $user_state,
183
                             $origin_state,
184
                          $productid,
185
                             $geoip_state,
186
                             $geoip_country
187
                         );
188
189
                        $c_gst = $details['cgst'];
190
                        $s_gst = $details['sgst'];
191
                        $i_gst = $details['igst'];
192
                        $ut_gst = $details['utgst'];
193
194
                        $state_code = $details['statecode'];
195
196
                        $status = $details['status'];
197
                        $taxes = $details['taxes'];
198
                        $status = $details['status'];
199
                        $value = $details['value'];
200
                        $rate = $details['rate'];
201
                        foreach ($taxes as $key => $tax) {
202
                            //All the da a attribute that is sent to the checkout Page if tax_compound=0
203
                            if ($taxes[0]) {
204
                                $tax_attribute[$key] = ['name' => $tax->name, 'c_gst'=>$c_gst,
205
206
                               's_gst'         => $s_gst, 'i_gst'=>$i_gst, 'ut_gst'=>$ut_gst,
207
                                'state'        => $state_code, 'origin_state'=>$origin_state,
208
                                 'tax_enable'  => $tax_enable, 'rate'=>$value, 'status'=>$status, ];
209
210
                                $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
211
                                            'name'   => 'no compound', 'type'   => 'tax',
212
                                            'target' => 'item', 'value'  => $value,
213
                                          ]);
214
                            } else {
215
                                $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
216
                                $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
217
                                           'name'   => 'null', 'type'   => 'tax',
218
                                           'target' => 'item', 'value'  => '0%',
219
                                           'rate'   => 0, 'tax_enable' =>0,
220
                                         ]);
221
                            }
222
                        }
223
                    } elseif ($tax_enable == 0) {//If Tax enable is 0 and other tax is available
224
225
                        $details = $this->whenOtherTaxAvailableAndTaxNotEnable($tax_class_id, $productid, $geoip_state, $geoip_country);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
226
227
                        $taxes = $details['taxes'];
228
                        $value = $details['value'];
229
                        $status = $details['status'];
230
                        foreach ($taxes as $key => $tax) {
231
                            $tax_attribute[$key] = ['name' => $tax->name,
232
                            'rate'                         => $value, 'tax_enable'=>0, 'status' => $status, ];
233
                            $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([
234
                                'name'   => $tax->name,
235
                                'type'   => 'tax',
236
                                'target' => 'item',
237
                                'value'  => $value,
238
                            ]);
239
                        }
240
                    }
241
                }
242
            }
243
244
            return ['conditions' => $taxCondition, 'tax_attributes'=>  $tax_attribute];
245
        } catch (\Exception $ex) {
246
            Bugsnag::notifyException($ex);
247
248
            throw new \Exception('Can not check the tax');
249
        }
250
    }
251
252
    public function cartRemove(Request $request)
253
    {
254
        $id = $request->input('id');
255
        Cart::remove($id);
256
257
        return 'success';
258
    }
259
260
    /**
261
     * @return type
262
     */
263
    public function addCouponUpdate()
264
    {
265
        try {
266
            $code = \Input::get('coupon');
267
            $cart = Cart::getContent();
268
            foreach ($cart as $item) {
269
                $id = $item->id;
270
            }
271
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
272
            $result = $promo_controller->checkCode($code, $id);
273
            if ($result == 'success') {
274
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...updated-successfully')) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
275
            }
276
277
            return redirect()->back();
278
        } catch (\Exception $ex) {
279
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
280
        }
281
    }
282
283
    /**
284
     * @param type $price
285
     *
286
     * @throws \Exception
287
     *
288
     * @return type
289
     */
290
    public static function rounding($price)
291
    {
292
        try {
293
            $tax_rule = new \App\Model\Payment\TaxOption();
294
            $rule = $tax_rule->findOrFail(1);
295
            $rounding = $rule->rounding;
296
297
            return $price;
298
        } catch (\Exception $ex) {
299
            Bugsnag::notifyException($ex);
300
            // throw new \Exception('error in get tax priority');
301
        }
302
    }
303
304
    /**
305
     * @return type
306
     */
307
    public function contactUs()
308
    {
309
        try {
310
            return view('themes.default1.front.contact');
311
        } catch (\Exception $ex) {
312
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
313
        }
314
    }
315
316
    /**
317
     * @param Request $request
318
     *
319
     * @return type
320
     */
321
    public function postContactUs(Request $request)
322
    {
323
        $this->validate($request, [
324
            'name'    => 'required',
325
            'email'   => 'required|email',
326
            'message' => 'required',
327
        ]);
328
329
        $set = new \App\Model\Common\Setting();
330
        $set = $set->findOrFail(1);
331
332
        try {
333
            $from = $set->email;
334
            $fromname = $set->company;
335
            $toname = '';
336
            $to = $set->company_email;
337
            $data = '';
338
            $data .= 'Name: '.strip_tags($request->input('name')).'<br/>';
339
            $data .= 'Email: '.strip_tags($request->input('email')).'<br/>';
340
            $data .= 'Message: '.strip_tags($request->input('message')).'<br/>';
341
            $data .= 'Mobile: '.strip_tags($request->input('country_code').$request->input('Mobile')).'<br/>';
342
            $subject = 'Faveo billing enquiry';
343
            $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname);
344
            //$this->templateController->Mailing($from, $to, $data, $subject);
345
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...successfully. Thanks.') also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
346
        } catch (\Exception $ex) {
347
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
348
        }
349
    }
350
351
    /**
352
     * @param type $code
353
     *
354
     * @throws \Exception
355
     *
356
     * @return type
357
     */
358
    public static function getCountryByCode($code)
359
    {
360
        try {
361
            $country = \App\Model\Common\Country::where('country_code_char2', $code)->first();
362
            if ($country) {
363
                return $country->nicename;
364
            }
365
        } catch (\Exception $ex) {
366
            throw new \Exception($ex->getMessage());
367
        }
368
    }
369
370
    /**
371
     * @param type $name
372
     *
373
     * @throws \Exception
374
     *
375
     * @return string
376
     */
377
    public static function getTimezoneByName($name)
378
    {
379
        try {
380
            $timezone = \App\Model\Common\Timezone::where('name', $name)->first();
381
            if ($timezone) {
382
                $timezone = $timezone->id;
383
            } else {
384
                $timezone = '114';
385
            }
386
387
            return $timezone;
388
        } catch (\Exception $ex) {
389
            throw new \Exception($ex->getMessage());
390
        }
391
    }
392
393
    /**
394
     * @param type $code
395
     *
396
     * @throws \Exception
397
     *
398
     * @return type
399
     */
400
    public static function getStateByCode($code)
401
    {
402
        try {
403
            $result = ['id' => '', 'name' => ''];
404
405
            $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first();
406
            if ($subregion) {
407
                $result = ['id' => $subregion->state_subdivision_code,
408
                 'name'         => $subregion->state_subdivision_name, ];
409
            }
410
411
            return $result;
412
        } catch (\Exception $ex) {
413
            throw new \Exception($ex->getMessage());
414
        }
415
    }
416
417
    /**
418
     * @param type $id
419
     *
420
     * @throws \Exception
421
     *
422
     * @return type
423
     */
424
    public static function getStateNameById($id)
425
    {
426
        try {
427
            $name = '';
428
            $subregion = \App\Model\Common\State::where('state_subdivision_id', $id)->first();
429
            if ($subregion) {
430
                $name = $subregion->state_subdivision_name;
431
            }
432
433
            return $name;
434
        } catch (\Exception $ex) {
435
            throw new \Exception($ex->getMessage());
436
        }
437
    }
438
439
    /**
440
     * @param type $rate
441
     * @param type $price
442
     *
443
     * @return type
444
     */
445
    public static function taxValue($rate, $price)
446
    {
447
        try {
448
            $result = '';
449
            if ($rate) {
0 ignored issues
show
introduced by
$rate is of type App\Http\Controllers\Front\type, thus it always evaluated to true.
Loading history...
450
                $rate = str_replace('%', '', $rate);
451
                $tax = intval($price) * ($rate / 100);
452
                $result = $tax;
453
454
                $result = self::rounding($result);
455
            }
456
457
            return $result;
458
        } catch (\Exception $ex) {
459
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Front\type.
Loading history...
460
        }
461
    }
462
463
    /**
464
     * @param type $userid
465
     *
466
     * @throws \Exception
467
     *
468
     * @return string
469
     */
470
    public static function currency($userid = '')
471
    {
472
        try {
473
            $currency = Setting::find(1)->default_currency;
474
            $currency_symbol = Setting::find(1)->default_symbol;
475
            if (!\Auth::user()) {//When user is not logged in
476
                $cont = new \App\Http\Controllers\Front\PageController();
477
                $location = $cont->getLocation();
478
                $country = self::findCountryByGeoip($location['iso_code']);
479
                $userCountry = Country::where('country_code_char2', $country)->first();
480
                $currencyStatus = $userCountry->currency->status;
481
                if ($currencyStatus == 1) {
482
                    $currency = $userCountry->currency->code;
483
                    $currency_symbol = $userCountry->currency->symbol;
484
                }
485
            }
486
            if (\Auth::user()) {
487
                $currency = \Auth::user()->currency;
488
                $currency_symbol = \Auth::user()->currency_symbol;
489
            }
490
            if ($userid != '') {//For Admin Panel Clients
491
                $currencyAndSymbol = self::getCurrency($userid);
492
                $currency = $currencyAndSymbol['currency'];
493
                $currency_symbol = $currencyAndSymbol['symbol'];
494
            }
495
496
            return ['currency'=>$currency, 'symbol'=>$currency_symbol];
497
        } catch (\Exception $ex) {
498
            throw new \Exception($ex->getMessage());
499
        }
500
    }
501
502
    /*
503
    * Get Currency And Symbol For Admin Panel Clients
504
    */
505
    public static function getCurrency($userid)
506
    {
507
        $user = new \App\User();
508
        $currency = $user->find($userid)->currency;
509
        $symbol = $user->find($userid)->currency_symbol;
510
511
        return ['currency'=>$currency, 'symbol'=>$symbol];
512
    }
513
514
    /**
515
     * @param int $productid
516
     * @param int $userid
517
     * @param int $planid
518
     *
519
     * @return string
520
     */
521
    public function cost($productid, $userid = '', $planid = '')
522
    {
523
        try {
524
            $cost = $this->planCost($productid, $userid, $planid);
525
526
            return $cost;
527
        } catch (\Exception $ex) {
528
            Bugsnag::notifyException($ex->getMessage());
529
            app('log')->error($ex->getMessage());
530
        }
531
    }
532
533
    /**
534
     * @throws \Exception
535
     *
536
     * @return bool
537
     */
538
    public function checkCurrencySession()
539
    {
540
        try {
541
            if (Session::has('currency')) {
542
                return true;
543
            }
544
545
            return false;
546
        } catch (\Exception $ex) {
547
            throw new \Exception($ex->getMessage());
548
        }
549
    }
550
}
551