Test Setup Failed
Push — development ( c5582b...48e5db )
by Ashutosh
10:10
created

BaseTemplateController::getTotalCart()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
rs 8.0555
c 0
b 0
f 0
cc 9
nc 16
nop 6
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Model\Payment\Plan;
8
9
class BaseTemplateController extends Controller
10
{
11
12
    public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '')
13
    {
14
        try {
15
            $tax_rule = $this->tax_rule->find(1);
16
            $product = $tax_rule->inclusive;
17
            $shop = $tax_rule->shop_inclusive;
18
            $cart = $tax_rule->cart_inclusive;
19
            $result = $price;
20
            $controller = new \App\Http\Controllers\Front\GetPageTemplateController();
21
            $location = $controller->getLocation();
22
          
23
            $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
24
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
25
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
26
            $state_code = $location['countryCode'].'-'.$location['region'];
27
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
28
            $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
29
            $country_iso = $location['countryCode'];
30
31
            $geoip_country = '';
32
            $geoip_state = '';
33
            if (\Auth::user()) {
34
                $geoip_country = \Auth::user()->country;
35
                $geoip_state = \Auth::user()->state;
36
            }
37
            if ($geoip_country == '') {
38
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
39
            }
40
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
41
            if ($geoip_state == '') {
42
                if (array_key_exists('id', $geoip_state_array)) {
43
                    $geoip_state = $geoip_state_array['id'];
44
                }
45
            }
46
        $result = $this->getResult($country,$geoip_country,$state,$geoip_state,$shop,$cart,$cart1, $shop1,$rate,$product,$price);
47
          
48
        } catch (\Exception $ex) {
49
            Bugsnag::notifyException($ex);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Common\Bugsnag was not found. Did you mean Bugsnag? If so, make sure to prefix the type with \.
Loading history...
50
51
            throw new \Exception($ex->getMessage());
52
        }
53
    }
54
55
    public function getResult($country,$geoip_country,$state,$geoip_state,$shop,$cart,$cart1, $shop1,$rate,$product,$price)
56
    {
57
    	  if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) {
58
            $result = $this->getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1);
59
           
60
          }
61
         return $result;
62
    }
63
64
    public function getCartResult($product,$shop,$cart,$rate,$price,$cart1,$shop1)
65
    {
66
        if ($product==1)
67
        {
68
            $result = $this->getTotalSub($shop,$cart,$rate,$price,$cart1,$shop1);
69
        }
70
         
71
        if ($product == 0){
72
            $result= $this->getTotalCart($shop,$cart,$price,$cart1,$shop1,$shop);
73
        }
74
            
75
            return $result;
76
    }
77
78
    public function getTotalCart($shop,$cart,$rate,$price,$cart1,$shop1)
79
    {
80
        if ($shop == 0 && $cart == 0) {
81
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
82
            }
83
            if ($shop == 1 && $cart == 1) {
84
                $result = $this->calculateTotalcart($rate, $price, $cart1, $shop1);
85
            }
86
            if ($shop == 1 && $cart == 0) {
87
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1);
88
            }
89
            if ($shop == 0 && $cart == 1) {
90
                $result = $this->calculateTotalcart($rate, $price, $cart = 1, $shop = 1);
91
            }
92
            return $result;
93
    }
94
95
96
    public function getTotalSub($shop,$cart,$rate,$price,$cart1,$shop1)
97
    {
98
        if ($shop == 1 && $cart == 1) {
99
                $result = $this->calculateTotalcart($rate, $price, $cart1 = 0, $shop1 = 0);
100
            }
101
            if ($shop == 0 && $cart == 0) {
102
                $result = $this->calculateSub($rate, $price, $cart1 = 1, $shop1 = 1);
103
            }
104
            if ($shop == 1 && $cart == 0) {
105
                $result = $this->calculateSub($rate, $price, $cart1, $shop1 = 0);
106
            }
107
            if ($shop == 0 && $cart == 1) {
108
                $result = $this->calculateSub($rate, $price, $cart1 = 0, $shop1);
109
            }
110
            return $result;
111
112
    }
113
114
     public function calculateTotalcart($rate, $price, $cart, $shop)
115
    {
116
        if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) {
117
            $tax_amount = $price * ($rate / 100);
118
            $total = $price + $tax_amount;
119
            return $total;
120
        }
121
		 return $price;
122
    }
123
124
125
     public function calculateSub($rate, $price, $cart, $shop)
126
    {
127
        if (($cart == 1 && $shop == 1) || ($cart == 1 && $shop == 0) || ($cart == 0 && $shop == 1)) {
128
            $total = $price / (($rate / 100) + 1);
129
            return $total;
130
        }
131
        return $price;
132
    }
133
134
    public function getPrice($price,$currency,$value,$cost)
135
    {
136
	 if ($currency == 'INR') {
137
         $price[$value->id] = '₹'.' '.$cost;
138
        } else {
139
          $price[$value->id] = '$'.' '.$cost;
140
        }
141
        return $price;
142
    }
143
144
     public function prices($id)
145
    {
146
        $plan = new Plan();
147
        $plans = $plan->where('product', $id)->get();
148
        $price = array();
149
        $cart_controller = new \App\Http\Controllers\Front\CartController();
150
        $currency = $cart_controller->currency();
151
152
        foreach ($plans as $value) {
153
            $cost = $value->planPrice()->where('currency', $currency)->first()->add_price;
154
155
            $cost = \App\Http\Controllers\Front\CartController::rounding($cost);
156
            $months = round($value->days / 30 / 12);
157
            $price = $this->getPrice($price,$currency,$value,$cost);
158
           
159
        }
160
         $this->leastAmount($id);
161
162
        return $price;
163
    }
164
165
    public function withoutTaxRelation($productid, $currency)
166
    {
167
        try {
168
            $product = $this->product->findOrFail($productid);
169
            $controller = new \App\Http\Controllers\Front\CartController();
170
            $price = $controller->cost($productid);
171
             return $price;
172
        } catch (\Exception $ex) {
173
            Bugsnag::notifyException($ex);
174
            throw new \Exception($ex->getMessage());
175
        }
176
    }
177
178
179
    public function taxProcess($taxes, $price, $cart, $shop)
180
    {
181
        try {
182
            $rate = '';
183
            $tax_amount = '';
184
            foreach ($taxes as $tax) {
185
                if ($tax->compound != 1) {
186
                    $rate += $tax->rate;
187
                } else {
188
                    $rate = $tax->rate;
189
                }
190
                $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tax_amount is correct as $this->ifStatement($rate...->country, $tax->state) targeting App\Http\Controllers\Com...ntroller::ifStatement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
191
            }
192
            return $tax_amount;
193
        } catch (\Exception $ex) {
194
            Bugsnag::notifyException($ex);
195
196
            throw new \Exception($ex->getMessage());
197
        }
198
    }
199
200
    public function getTaxAmount($cart,$taxes, $price, $cart1, $shop)
201
    {
202
	  if ($cart == 1) {
203
            $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop);
204
        } else {
205
            $rate = '';
206
             foreach ($taxes as $tax) {
207
                if ($tax->compound != 1) {
208
                    $rate += $tax->rate;
209
                } else {
210
                    $rate = $tax->rate;
211
                   $price = $this->calculateTotal($rate, $price);
212
                }
213
                $tax_amount = $this->calculateTotal($rate, $price);
214
          }
215
        }
216
        return $tax_amount;
217
    }
218
219
220
221
    public function calculateTotal($rate, $price)
222
    {
223
	    $tax_amount = $price * ($rate / 100);
224
	    $total = $price + $tax_amount;
225
	    return $total;
226
    }
227
}
228