Completed
Push — development ( c78083...b93250 )
by Ashutosh
11:07 queued 10s
created

BaseTemplateController::getDuration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Model\Payment\Plan;
6
use Bugsnag;
7
8
class BaseTemplateController extends ExtendedBaseTemplateController
9
{
10
    public function ifStatement($rate, $price, $cart1, $shop1, $country = '', $state = '')
11
    {
12
        try {
13
            $tax_rule = $this->tax_rule->find(1);
14
            $product = $tax_rule->inclusive;
15
            $shop = $tax_rule->shop_inclusive;
16
            $cart = $tax_rule->cart_inclusive;
17
            $result = $price;
18
            $controller = new \App\Http\Controllers\Front\GetPageTemplateController();
19
            $location = $controller->getLocation();
20
21
            $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
22
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
23
            $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
24
            $state_code = $location['countryCode'].'-'.$location['region'];
25
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
26
            $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
27
            $country_iso = $location['countryCode'];
28
29
            $geoip_country = '';
30
            $geoip_state = '';
31
            if (\Auth::user()) {
32
                $geoip_country = \Auth::user()->country;
33
                $geoip_state = \Auth::user()->state;
34
            }
35
            if ($geoip_country == '') {
36
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
37
            }
38
            $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
39
            if ($geoip_state == '') {
40
                if (array_key_exists('id', $geoip_state_array)) {
41
                    $geoip_state = $geoip_state_array['id'];
42
                }
43
            }
44
            $result = $this->getResult($country, $geoip_country, $state, $geoip_state,
45
                $shop, $cart, $cart1, $shop1, $rate, $product, $price);
46
47
            return $result;
48
        } catch (\Exception $ex) {
49
            Bugsnag::notifyException($ex);
50
51
            throw new \Exception($ex->getMessage());
52
        }
53
    }
54
55
    public function getResult($country, $geoip_country, $state, $geoip_state,
56
     $shop, $cart, $cart1, $shop1, $rate, $product, $price)
57
    {
58
        if ($country == $geoip_country || $state == $geoip_state || ($country == '' && $state == '')) {
59
            $result = $this->getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1);
60
        }
61
62
        return $result;
63
    }
64
65
    public function getCartResult($product, $shop, $cart, $rate, $price, $cart1, $shop1)
66
    {
67
        if ($product == 1) {
68
            $result = $this->getTotalSub($shop, $cart, $rate, $price, $cart1, $shop1);
69
        }
70
71
        if ($product == 0) {
72
            $result = $this->getTotalCart($shop, $cart, $rate, $price, $cart1, $shop1);
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
93
        return $result;
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
111
        return $result;
112
    }
113
114
    public function getPrice($months,$price, $currency, $value, $cost)
115
    {
116
        if ($currency == 'INR') {
117
            $price[$value->id] = $months  .'  ' . '₹'.' '.$cost;
118
        } else {
119
            $price[$value->id] = $months  .'  ' . '$'.' '.$cost;
120
        }
121
122
        return $price;
123
    }
124
125
    public function prices($id)
126
    {
127
        $plan = new Plan();
128
        $plans = $plan->where('product', $id)->orderBy('id','desc')->get();
129
        $price = [];
130
        $cart_controller = new \App\Http\Controllers\Front\CartController();
131
        $currency = $cart_controller->currency();
132
133
        foreach ($plans as $value) {
134
            $cost = $value->planPrice()->where('currency', $currency)->first()->add_price;
135
            $cost = \App\Http\Controllers\Front\CartController::rounding($cost);
136
            if ($value->days >= 366){ 
137
            $months = intval($value->days / 30 / 12).' '.'year';
138
            }elseif ($value->days < 365){
139
             $months = intval($value->days / 30 ).' '.'months';
140
        }
141
        else {
142
            $months = '';
143
        }
144
         $price = $this->getPrice($months,$price, $currency, $value, $cost);
145
        }
146
       
147
       
148
        // $this->leastAmount($id);
149
150
        return $price ;
151
         }
152
    
153
154
    public function withoutTaxRelation($productid, $currency)
155
    {
156
        try {
157
            $product = $this->product->findOrFail($productid);
158
            $controller = new \App\Http\Controllers\Front\CartController();
159
            $price = $controller->cost($productid);
160
161
            return $price;
162
        } catch (\Exception $ex) {
163
            Bugsnag::notifyException($ex);
164
165
            throw new \Exception($ex->getMessage());
166
        }
167
    }
168
169
    public function taxProcess($taxes, $price, $cart, $shop)
170
    {
171
        try {
172
            $rate = '';
173
            $tax_amount = '';
174
            foreach ($taxes as $tax) {
175
                if ($tax->compound != 1) {
176
                    $rate += $tax->rate;
177
                } else {
178
                    $rate = $tax->rate;
179
                }
180
                $tax_amount = $this->ifStatement($rate, $price, $cart, $shop, $tax->country, $tax->state);
181
            }
182
            // dd($tax_amount);
183
            return $tax_amount;
184
        } catch (\Exception $ex) {
185
            Bugsnag::notifyException($ex);
186
187
            throw new \Exception($ex->getMessage());
188
        }
189
    }
190
191
    public function getTaxAmount($cart, $taxes, $price, $cart1, $shop)
192
    {
193
        if ($cart == 1) {
194
            $tax_amount = $this->taxProcess($taxes, $price, $cart1, $shop);
195
        } else {
196
            $rate = '';
197
            foreach ($taxes as $tax) {
198
                if ($tax->compound != 1) {
199
                    $rate += $tax->rate;
200
                } else {
201
                    $rate = $tax->rate;
202
                    $price = $this->calculateTotal($rate, $price);
203
                }
204
                $tax_amount = $this->calculateTotal($rate, $price);
205
            }
206
        }
207
208
        return $tax_amount;
209
    }
210
211
    public function calculateTotal($rate, $price)
212
    {
213
        $tax_amount = $price * ($rate / 100);
214
        $total = $price + $tax_amount;
215
216
        return $total;
217
    }
218
219
     public function getDuration($value)
220
    {
221
     if (strpos($value, 'Y') == true){
1 ignored issue
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($value, 'Y') of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
222
             $duration = '/Year';
223
         }
224
         elseif(strpos($value, 'M') == true){
1 ignored issue
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($value, 'M') of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
225
            $duration = '/Month';
226
         }
227
         else{
228
            $duration = '/One-Time';
229
         }
230
         return $duration;
231
    }
232
233
}
234