Completed
Push — master ( fde4fb...b78da7 )
by vijay
111:36 queued 59:13
created

CartController::getCountryByCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 10
loc 10
rs 9.4285
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Common\TemplateController;
6
use App\Http\Controllers\Controller;
7
use App\Model\Payment\Currency;
8
use App\Model\Payment\Tax;
9
use App\Model\Payment\TaxOption;
10
use App\Model\Product\Product;
11
use Cart;
12
use Illuminate\Http\Request;
13
14
class CartController extends Controller
15
{
16
    public $templateController;
17
    public $product;
18
    public $currency;
19
    public $addons;
20
    public $addonRelation;
21
    public $licence;
22
    public $tax_option;
23
24
    public function __construct()
25
    {
26
        $templateController = new TemplateController();
27
        $this->templateController = $templateController;
28
29
        $product = new Product();
30
        $this->product = $product;
31
32
        $currency = new Currency();
33
        $this->currency = $currency;
34
35
        $tax = new Tax();
36
        $this->tax = $tax;
0 ignored issues
show
Bug introduced by
The property tax does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
        $tax_option = new TaxOption();
39
        $this->tax_option = $tax_option;
40
    }
41
42
    public function ProductList(Request $request)
43
    {
44
        $location = \GeoIP::getLocation();
45
        //dd($location);
46
47
        if ($location['country'] == 'India') {
48
            $currency = 'INR';
49
        } else {
50
            $currency = 'USD';
51
        }
52
        \Session::put('currency', $currency);
53
        if (!\Session::has('currency')) {
54
            \Session::put('currency', 'INR');
55
//dd(\Session::get('currency'));
56
        }
57
58
        try {
59
            return $this->templateController->show(1);
60
        } catch (\Exception $ex) {
61
            return redirect()->back()->with('fails', $ex->getMessage());
62
        }
63
    }
64
65
    public function Cart(Request $request)
66
    {
67
        try {
68
            $id = $request->input('id');
69
            //dd($id);
70
            if (!array_key_exists($id, Cart::getContent()->toArray())) {
71
                $items = $this->addProduct($id);
72
                Cart::add($items);
73
            }
74
75
            return redirect('show/cart');
76
        } catch (\Exception $ex) {
77
            dd($ex);
78
79
            return redirect()->back()->with('fails', $ex->getMessage());
80
        }
81
    }
82
83
    public function showCart()
84
    {
85
        try {
86
            $attributes = [];
87
            $cartCollection = Cart::getContent();
88
            foreach ($cartCollection as $item) {
89
                $attributes[] = $item->attributes;
90
            }
91
92
            return view('themes.default1.front.cart', compact('cartCollection', 'attributes'));
93
        } catch (\Exception $ex) {
94
            dd($ex);
95
96
            return redirect()->back()->with('fails', $ex->getMessage());
97
        }
98
    }
99
100
    public function checkTax($productid)
101
    {
102
        try {
103
            $tax_attribute[0] = ['name' => 'null', 'rate' => 0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tax_attribute was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tax_attribute = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
104
            $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
0 ignored issues
show
Coding Style Comprehensibility introduced by
$taxCondition was never initialized. Although not strictly required by PHP, it is generally a good practice to add $taxCondition = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
105
                'name'   => 'null',
106
                'type'   => 'tax',
107
                'target' => 'item',
108
                'value'  => '0%',
109
            ]);
110
//dd($tax_attribute);
111
            $product = $this->product->findOrFail($productid);
112
113
            $location = \GeoIP::getLocation();
114
            $counrty_iso = $location['isoCode'];
115
            $state_code = $location['isoCode'].'-'.$location['state'];
116
            $geoip_country = '';
117
            $geoip_state = '';
118
            if (\Auth::user()) {
119
                $geoip_country = \Auth::user()->country;
120
                $geoip_state = \Auth::user()->state;
121
            }
122
            if ($geoip_country == '') {
123
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($counrty_iso);
124
            }
125
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
126
            if ($geoip_state == '') {
127
                if (key_exists('id', $geoip_state_array)) {
128
                    $geoip_state = $geoip_state_array['id'];
129
                }
130
            }
131
132
//dd($product);
133
            if ($this->tax_option->findOrFail(1)->inclusive == 0) {
134
                $tax_rule = $this->tax_option->findOrFail(1);
135
                $product1 = $tax_rule->inclusive;
136
                $shop = $tax_rule->shop_inclusive;
0 ignored issues
show
Unused Code introduced by
$shop is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
                $cart = $tax_rule->cart_inclusive;
0 ignored issues
show
Unused Code introduced by
$cart is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
                if ($product->tax()->first()) {
139
                    $tax_class_id = $product->tax()->first()->tax_class_id;
140
                    if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
141
                        if ($product1 == 0) {
142
                            $taxes = $this->getTaxByPriority($tax_class_id);
143
                            $rate = 0;
144
                            foreach ($taxes as $key => $tax) {
145
                                if ($tax->country == $geoip_country || $tax->state == $geoip_state || ($tax->country == '' && $tax->state == '')) {
146
                                    if ($tax->compound == 1) {
147
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
148
                                        $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([
149
                                            'name'   => $tax->name,
150
                                            'type'   => 'tax',
151
                                            'target' => 'item',
152
                                            'value'  => $tax->rate.'%',
153
                                        ]);
154
                                    } else {
155
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
156
                                        $rate += $tax->rate;
157
                                        $taxCondition[0] = new \Darryldecode\Cart\CartCondition([
158
                                            'name'   => 'no compound',
159
                                            'type'   => 'tax',
160
                                            'target' => 'item',
161
                                            'value'  => $rate.'%',
162
                                        ]);
163
                                    }
164
                                }
165
                            }
166 View Code Duplication
                        } else {
167
                            if ($product->tax()->first()) {
168
                                $tax_class_id = $product->tax()->first()->tax_class_id;
169
                                if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
170
                                    $taxes = $this->getTaxByPriority($tax_class_id);
171
                                    foreach ($taxes as $key => $tax) {
172
                                        $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
173
                                    }
174
                                }
175
                            }
176
                        }
177 View Code Duplication
                    } else {
178
                        if ($product->tax()->first()) {
179
                            $tax_class_id = $product->tax()->first()->tax_class_id;
180
                            if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
181
                                $taxes = $this->getTaxByPriority($tax_class_id);
182
                                foreach ($taxes as $key => $tax) {
183
                                    $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
184
                                }
185
                            }
186
                        }
187
                    }
188
                }
189 View Code Duplication
            } else {
190
                if ($product->tax()->first()) {
191
                    $tax_class_id = $product->tax()->first()->tax_class_id;
192
                    if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
193
                        $taxes = $this->getTaxByPriority($tax_class_id);
194
                        foreach ($taxes as $key => $tax) {
195
                            $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $tax->rate];
196
                        }
197
                    }
198
                }
199
            }
200
            $currency_attribute = $this->addCurrencyAttributes($productid);
201
202
            return ['conditions' => $taxCondition, 'attributes' => ['tax' => $tax_attribute, 'currency' => $currency_attribute]];
203
        } catch (\Exception $ex) {
204
            dd($ex);
205
            throw new \Exception('Can not check the tax');
206
        }
207
    }
208
209
    public function checkTaxOld($isTaxApply, $id)
210
    {
211
        try {
212
            $rate1 = 0;
213
            $rate2 = 0;
214
            $name1 = 'null';
215
            $name2 = 'null';
216
217
            if ($ruleEnabled) {
218
                $enabled = $ruleEnabled->status;
0 ignored issues
show
Bug introduced by
The variable $ruleEnabled does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
219
                $type = $ruleEnabled->type;
220
                $compound = $ruleEnabled->compound;
221
                if ($enabled == 1 && $type == 'exclusive') {
222
                    if ($isTaxApply == 1) {
223
                        $tax1 = $this->tax->where('level', 1)->first();
224
                        $tax2 = $this->tax->where('level', 2)->first();
225 View Code Duplication
                        if ($tax1) {
226
                            $name1 = $tax1->name;
227
                            $rate1 = $tax1->rate;
228
                            $taxCondition1 = new \Darryldecode\Cart\CartCondition([
229
                                'name'   => $name1,
230
                                'type'   => 'tax',
231
                                'target' => 'item',
232
                                'value'  => $rate1.'%',
233
                            ]);
234
                        } else {
235
                            $taxCondition1 = new \Darryldecode\Cart\CartCondition([
236
                                'name'   => $name1,
237
                                'type'   => 'tax',
238
                                'target' => 'item',
239
                                'value'  => $rate1,
240
                            ]);
241
                        }
242 View Code Duplication
                        if ($tax2) {
243
                            $name2 = $tax2->name;
244
                            $rate2 = $tax2->rate;
245
                            $taxCondition2 = new \Darryldecode\Cart\CartCondition([
246
                                'name'   => $name2,
247
                                'type'   => 'tax',
248
                                'target' => 'item',
249
                                'value'  => $rate2.'%',
250
                            ]);
251
                        } else {
252
                            $taxCondition2 = new \Darryldecode\Cart\CartCondition([
253
                                'name'   => $name2,
254
                                'type'   => 'tax',
255
                                'target' => 'item',
256
                                'value'  => $rate2,
257
                            ]);
258
                        }
259
                    } else {
260
                        $taxCondition1 = new \Darryldecode\Cart\CartCondition([
261
                            'name'   => $name1,
262
                            'type'   => 'tax',
263
                            'target' => 'item',
264
                            'value'  => $rate1,
265
                        ]);
266
                        $taxCondition2 = new \Darryldecode\Cart\CartCondition([
267
                            'name'   => $name2,
268
                            'type'   => 'tax',
269
                            'target' => 'item',
270
                            'value'  => $rate2,
271
                        ]);
272
                    }
273
                    $currency_attribute = $this->addCurrencyAttributes($id);
274
//dd($currency_attribute);
275
                    if ($compound == 1) {
276
                        return ['conditions' => [$taxCondition1, $taxCondition2], 'attributes' => ['tax' => [['name' => $name1, 'rate' => $rate1], ['name' => $name2, 'rate' => $rate2]], 'currency' => $currency_attribute]];
277
                    } else {
278
                        return ['conditions' => $taxCondition2, 'attributes' => ['tax' => [['name' => $name2, 'rate' => $rate2]], 'currency' => $currency_attribute]];
279
                    }
280
                }
281
            }
282
        } catch (\Exception $ex) {
283
            dd($ex);
284
            throw new \Exception('Can not check the tax');
285
        }
286
    }
287
288
    public function CartRemove(Request $request)
289
    {
290
        $id = $request->input('id');
291
//dd($id);
292
        Cart::remove($id);
293
294
        return 'success';
295
    }
296
297 View Code Duplication
    public function ReduseQty(Request $request)
298
    {
299
        $id = $request->input('id');
300
        Cart::update($id, [
301
            'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3
302
        ]);
303
//dd(Cart::getContent());
304
        return 'success';
305
    }
306
307 View Code Duplication
    public function IncreaseQty(Request $request)
308
    {
309
        $id = $request->input('id');
310
        Cart::update($id, [
311
            'quantity' => +1, // so if the current product has a quantity of 4, it will add 1 and will result to 5
312
        ]);
313
//dd(Cart::getContent());
314
        return 'success';
315
    }
316
317
    public function AddAddons($id)
318
    {
319
        $addon = $this->addons->where('id', $id)->first();
320
321
        $isTaxApply = $addon->tax_addon;
322
323
        $taxConditions = $this->CheckTax($isTaxApply);
324
325
        $items = ['id' => 'addon'.$addon->id, 'name' => $addon->name, 'price' => $addon->selling_price, 'quantity' => 1];
326
        $items = array_merge($items, $taxConditions);
327
328
//dd($items);
329
330
        return $items;
331
    }
332
333
    public function GetProductAddons($productId)
334
    {
335
        $addons = [];
336
        if ($this->addonRelation->where('product_id', $productId)->count() > 0) {
337
            $addid = $this->addonRelation->where('product_id', $productId)->pluck('addon_id')->toArray();
338
            $addons = $this->addons->whereIn('id', $addid)->get();
339
        }
340
341
        return $addons;
342
    }
343
344
    public function addProduct($id)
345
    {
346
        $location = \GeoIP::getLocation();
347
        //dd($location);
348
349
        if ($location['country'] == 'India') {
350
            $currency = 'INR';
351
        } else {
352
            $currency = 'USD';
353
        }
354
        \Session::put('currency', $currency);
355
        if (!\Session::has('currency')) {
356
            \Session::put('currency', 'INR');
357
//dd(\Session::get('currency'));
358
        }
359
        $currency = \Session::get('currency');
360
        //dd($currency);
361
//        if (!$currency) {
362
//            $currency = 'USD';
363
//        }
364
        $product = $this->product->where('id', $id)->first();
365
366
        if ($product) {
367
            $productCurrency = $product->price()->where('currency', $currency)->first()->currency;
368
            $actualPrice = $product->price()->where('currency', $currency)->first()->sales_price;
369
            if (!$actualPrice) {
370
                $actualPrice = $product->price()->where('currency', $currency)->first()->price;
371
            }
372
            $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
373
374
            $productName = $product->name;
375
376
            /*
377
             * Check the Tax is On
378
             */
379
            $isTaxApply = $product->tax_apply;
0 ignored issues
show
Unused Code introduced by
$isTaxApply is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
380
381
            $taxConditions = $this->checkTax($id);
382
//dd($taxConditions);
383
384
            /*
385
             * Check if this product allow multiple qty
386
             */
387
            if ($product->multiple_qty == 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
388
                // Allow
389
            } else {
390
                $qty = 1;
391
            }
392
            $items = ['id' => $id, 'name' => $productName, 'price' => $actualPrice, 'quantity' => $qty, 'attributes' => ['currency' => [[$currency]]]];
0 ignored issues
show
Bug introduced by
The variable $qty does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
393
            $items = array_merge($items, $taxConditions);
394
            //dd($items);
395
            return $items;
396
        }
397
    }
398
399
    public function ClearCart()
400
    {
401
        foreach (Cart::getContent() as $item) {
402
            if (\Session::has('domain'.$item->id)) {
403
                \Session::forget('domain'.$item->id);
404
            }
405
        }
406
        Cart::clear();
407
408
        return redirect('show/cart')->with('warning', 'Your cart is empty! ');
409
    }
410
411
    public function LicenceCart($id)
412
    {
413
        try {
414
            $licence = $this->licence->where('id', $id)->first();
415
416
            $isTaxApply = 0;
417
418
            $taxConditions = $this->CheckTax($isTaxApply);
419
420
            $items = ['id' => $licence->id, 'name' => $licence->name, 'price' => $licence->price, 'quantity' => 1, 'attributes' => ['number_of_sla' => $licence->number_of_sla]];
421
            $items = array_merge($items, $taxConditions);
422
            Cart::clear();
423
            Cart::add($items);
424
425
            return view('themes.default1.front.cart', compact('cartCollection'));
426
        } catch (\Exception $ex) {
427
            dd($ex);
428
            throw new \Exception('Problem while adding licence to cart');
429
        }
430
    }
431
432
    public function cartUpdate($id, $key, $value)
433
    {
434
        try {
435
            Cart::update($id, [
436
                $key => $value, // new item name
437
                    ]
438
            );
439
        } catch (\Exception $ex) {
440
        }
441
    }
442
443
    public function addCurrencyAttributes($id)
444
    {
445
        try {
446
            $currency = \Session::get('currency');
447
            $product = $this->product->where('id', $id)->first();
448
//dd($product);
449 View Code Duplication
            if ($product) {
450
                $productCurrency = $product->price()->where('currency', $currency)->first()->currency;
451
                $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
452
            } else {
453
                $currency = [];
454
            }
455
456
            return $currency;
457
        } catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
458
        }
459
    }
460
461
    public function addCouponUpdate()
462
    {
463
        try {
464
            $code = \Input::get('coupon');
465
//dd($code);
466
            $cart = Cart::getContent();
467
            foreach ($cart as $item) {
468
                $id = $item->id;
469
            }
470
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
471
            $result = $promo_controller->checkCode($code, $id);
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
472
//dd($result);
473
            if ($result == 'success') {
474
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
475
            }
476
        } catch (\Exception $ex) {
477
            dd($ex);
478
479
            return redirect()->back()->with('fails', $ex->getMessage());
480
        }
481
    }
482
483
    public function getTaxByPriority($tax_class_id)
484
    {
485
        try {
486
            $taxe_relation = $this->tax->where('tax_classes_id', $tax_class_id)->groupBy('level')->get();
487
488
            return $taxe_relation;
489
        } catch (\Exception $ex) {
490
            dd($ex);
491
            throw new \Exception('error in get tax priority');
492
        }
493
    }
494
495
    public static function rounding($price)
496
    {
497
        try {
498
            $tax_rule = new \App\Model\Payment\TaxOption();
499
            $rule = $tax_rule->findOrFail(1);
500
            $rounding = $rule->rounding;
501
            if ($rounding == 1) {
502
                return round($price);
503
            } else {
504
                return $price;
505
            }
506
        } catch (\Exception $ex) {
507
            dd($ex);
508
            throw new \Exception('error in get tax priority');
509
        }
510
    }
511
512
    public function contactUs()
513
    {
514
        try {
515
            return view('themes.default1.front.contact');
516
        } catch (\Exception $ex) {
517
            return redirect()->back()->with('fails', $ex->getMessage());
518
        }
519
    }
520
521
    public function postContactUs(Request $request)
522
    {
523
        $this->validate($request, [
524
            'name'    => 'required',
525
            'email'   => 'required|email',
526
            'message' => 'required',
527
        ]);
528
529
        $set = new \App\Model\Common\Setting();
530
        $set = $set->findOrFail(1);
531
532
        try {
533
            $from = $set->email;
534
            $fromname = $set->company;
535
            $toname = '';
536
            $to = '[email protected]';
537
            $data = '';
538
            $data .= 'Name: '.$request->input('name').'<br/s>';
539
            $data .= 'Email: '.$request->input('email').'<br/>';
540
            $data .= 'Message: '.$request->input('message').'<br/>';
541
            $data .= 'Mobile: '.$request->input('Mobile').'<br/>';
542
543
            $subject = 'Faveo billing enquiry';
544
            $this->templateController->Mailing($from, $to, $data, $subject, [], $fromname, $toname);
545
            //$this->templateController->Mailing($from, $to, $data, $subject);
546
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
547
        } catch (\Exception $ex) {
548
            return redirect()->back()->with('fails', $ex->getMessage());
549
        }
550
    }
551
552
    public function addCartBySlug($slug)
553
    {
554
        try {
555
            if ($slug == 'helpdesk-with-kb-pro-edition') {
556
                $id = 8;
557
            }
558
            if ($slug == 'helpdesk-and-kb-community') {
559
                $id = 7;
560
            }
561
562
            return redirect("pricing?id=$id");
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
563
        } catch (\Exception $ex) {
564
            return redirect()->back()->with('fails', $ex->getMessage());
565
        }
566
    }
567
568 View Code Duplication
    public static function findCountryByGeoip($iso)
569
    {
570
        try {
571
            $country = \App\Model\Common\Country::where('country_code_char2', $iso)->first();
572
            if ($country) {
573
                return $country->country_code_char2;
574
            } else {
575
                return "US";
576
            }
577
        } catch (\Exception $ex) {
578
            throw new \Exception($ex->getMessage());
579
        }
580
    }
581
582 View Code Duplication
    static function getCountryByCode($code) {
583
        try {
584
            $country = \App\Model\Common\Country::where('country_code_char2', $code)->first();
585
            if ($country) {
586
                return $country->country_name;
587
            }
588
        } catch (\Exception $ex) {
589
            throw new \Exception($ex->getMessage());
590
        }
591
    }
592
593
    public static function findStateByRegionId($iso)
594
    {
595
        try {
596
            if ($iso) {
597
                $states = \App\Model\Common\State::where('country_code_char2', $iso)->lists('state_subdivision_name', 'state_subdivision_code')->toArray();
598
            } else {
599
                $states = [];
600
            }
601
602
            return $states;
603
        } catch (\Exception $ex) {
604
            throw new \Exception($ex->getMessage());
605
        }
606
    }
607
608
    public static function getTimezoneByName($name)
609
    {
610
        try {
611
            if ($name) {
612
                $timezone = \App\Model\Common\Timezone::where('name', $name)->first();
613
                if ($timezone) {
614
                    $timezone = $timezone->id;
615
                } else {
616
                    $timezone = '114';
617
                }
618
            } else {
619
                $timezone = '114';
620
            }
621
622
            return $timezone;
623
        } catch (\Exception $ex) {
624
            throw new \Exception($ex->getMessage());
625
        }
626
    }
627
628
    public static function getStateByCode($code)
629
    {
630
        try {
631
            if (is_int($code)) {
632
                return [];
633
            }
634
            if ($code) {
635
                $subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first();
636
                if (!$subregion) {
637
                    return [];
638
                }
639
640
                return ['id' => $subregion->state_subdivision_code, 'name' => $subregion->state_subdivision_name];
641
            } else {
642
                return [];
643
            }
644
        } catch (\Exception $ex) {
645
            throw new \Exception($ex->getMessage());
646
        }
647
    }
648
649
    public static function calculateTax($productid, $currency, $cart = 1, $cart1 = 0, $shop = 0)
650
    {
651
        try {
652
            $template_controller = new TemplateController();
653
            $result = $template_controller->checkTax($productid, $currency, $cart, $cart1, $shop);
654
            $result = self::rounding($result);
655
656
            return $result;
657
        } catch (\Exception $ex) {
658
            return redirect()->back()->with('fails', $ex->getMessage());
659
        }
660
    }
661
662
    public static function taxValue($rate, $price)
663
    {
664
        try {
665
            $tax = $price / (($rate / 100) + 1);
666
            $result = $price - $tax;
667
            $result = self::rounding($result);
668
669
            return $result;
670
        } catch (\Exception $ex) {
671
            return redirect()->back()->with('fails', $ex->getMessage());
672
        }
673
    }
674
}
675