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

ExtendedBaseCartController::planCost()   B

Complexity

Conditions 6
Paths 34

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 28
rs 8.9617
c 0
b 0
f 0
cc 6
nc 34
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Plan;
7
use App\Model\Payment\Tax;
8
use App\Model\Payment\TaxClass;
9
use App\Model\Product\Product;
10
use Bugsnag;
11
use Cart;
12
use Session;
13
14
class ExtendedBaseCartController extends Controller
15
{
16
    /**
17
     * @return type
18
     */
19
    public function addCouponUpdate()
20
    {
21
        try {
22
            $code = \Input::get('coupon');
23
            $cart = Cart::getContent();
24
            $id = '';
25
            foreach ($cart as $item) {
26
                $id = $item->id;
27
            }
28
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
29
            $result = $promo_controller->checkCode($code, $id);
30
            if ($result == 'success') {
31
                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...
32
            }
33
34
            return redirect()->back();
35
        } catch (\Exception $ex) {
36
            Bugsnag::notifyException($ex);
37
38
            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...
39
        }
40
    }
41
42
    /**
43
     * @return type
44
     */
45
    public function clearCart()
46
    {
47
        foreach (Cart::getContent() as $item) {
48
            if (\Session::has('domain'.$item->id)) {
49
                \Session::forget('domain'.$item->id);
50
            }
51
        }
52
53
        $this->removePlanSession();
54
        $renew_control = new \App\Http\Controllers\Order\RenewController();
55
        $renew_control->removeSession();
56
        Cart::clear();
57
58
        return redirect('show/cart');
59
    }
60
61
    /**
62
     * @throws \Exception
63
     */
64
    public function removePlanSession()
65
    {
66
        try {
67
            if (Session::has('plan')) {
68
                Session::forget('plan');
69
            }
70
        } catch (\Exception $ex) {
71
            throw new \Exception($ex->getMessage());
72
        }
73
    }
74
75
    /**
76
     * @throws \Exception
77
     *
78
     * @return bool
79
     */
80
    public function checkPlanSession()
81
    {
82
        try {
83
            if (Session::has('plan')) {
84
                return true;
85
            }
86
87
            return false;
88
        } catch (\Exception $ex) {
89
            throw new \Exception($ex->getMessage());
90
        }
91
    }
92
93
    /**
94
     * @param type $iso
95
     *
96
     * @throws \Exception
97
     *
98
     * @return type
99
     */
100
    public static function getMobileCodeByIso($iso)
101
    {
102
        try {
103
            $code = '';
104
            if ($iso != '') {
105
                $mobile = \DB::table('mobile')->where('iso', $iso)->first();
106
                if ($mobile) {
107
                    $code = $mobile->phonecode;
108
                }
109
            }
110
111
            return $code;
112
        } catch (\Exception $ex) {
113
            throw new \Exception($ex->getMessage());
114
        }
115
    }
116
117
    /**
118
     * Get Cost For a particular Plan.
119
     *
120
     * @param int $productid
121
     * @param int $userid
122
     * @param int $planid
123
     *
124
     * @throws \Exception
125
     *
126
     * @return int
127
     */
128
    public function planCost($productid, $userid, $planid = '')
129
    {
130
        try {
131
            $cost = 0;
132
            $months = 0;
133
            if ($this->checkPlanSession() === true) {
134
                $planid = Session::get('plan');
135
            }
136
            $plan = Plan::where('id', $planid)->where('product', $productid)->first();
137
            if ($plan) { //Get the Total Plan Cost if the Plan Exists For a Product
138
                $months = 1;
139
                $cont = new CartController();
140
                $currency = $cont->currency($userid);
141
                $product = Product::find($productid);
142
                $days = $plan->periods->pluck('days')->first();
143
                $price = ($product->planRelation->find($planid)->planPrice->where('currency', $currency['currency'])->first()->add_price);
0 ignored issues
show
Bug introduced by
The property planRelation does not seem to exist on App\Model\Product\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
144
                if ($days) { //If Period Is defined for a Particular Plan ie no. of Days Generated
145
                    $months = $days >= '365' ? $days / 30 / 12 : $days / 30;
146
                }
147
                $finalPrice = str_replace(',', '', $price);
148
                $cost = round($months) * $finalPrice;
149
            }
150
151
            return $cost;
152
        } catch (\Exception $ex) {
153
            dd($ex);
154
155
            throw new \Exception($ex->getMessage());
156
        }
157
    }
158
159
    public function getGeoipCountry($country_iso, $user_country = '')
160
    {
161
        $geoip_country = '';
162
        if (\Auth::user()) {
163
            if (\Auth::user()->role == 'admin') {
164
                $geoip_country = $user_country;
165
            } elseif (\Auth::user()->role == 'user') {
166
                $geoip_country = \Auth::user()->country;
167
            }
168
169
            if ($geoip_country == '') {
170
                $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso);
171
            }
172
        }
173
174
        return $geoip_country;
175
    }
176
177
    public function getGeoipState($state_code, $user_state = '')
178
    {
179
        $geoip_state = '';
180
        if (\Auth::user()) {
181
            if (\Auth::user()->role == 'admin') {
182
                $geoip_state = $user_state;
183
            } elseif (\Auth::user()->role == 'user') {
184
                $geoip_state = \Auth::user()->state;
185
            }
186
            if ($geoip_state == '') {
187
                $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
188
                if (array_key_exists('id', $geoip_state_array)) {
189
                    $geoip_state = $geoip_state_array['id'];
190
                }
191
            }
192
        }
193
194
        return $geoip_state;
195
    }
196
197
    /**
198
     * When from same Indian State.
199
     */
200
    public function getTaxWhenIndianSameState($user_state, $origin_state, $productid, $c_gst, $s_gst, $state_code, $status)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 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...
201
    {
202
        $taxes = [0];
203
        $value = '';
204
        $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id  of state
205
        if ($taxClassId) {
206
            $taxes = $this->getTaxByPriority($taxClassId);
207
            $value = $this->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
208
            if ($value == 0) {
209
                $status = 0;
210
            }
211
        }
212
213
        return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value];
214
    }
215
216
    /**
217
     * When from other Indian State.
218
     */
219
    public function getTaxWhenIndianOtherState($user_state, $origin_state, $productid, $i_gst, $state_code, $status)
220
    {
221
        $taxes = [0];
222
        $value = '';
223
        $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id  of state
224
        if ($taxClassId) {
225
            $taxes = $this->getTaxByPriority($taxClassId);
226
            $value = $this->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
227
            if ($value == '') {
228
                $status = 0;
229
            }
230
        }
231
232
        return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value];
233
    }
234
235
    /**
236
     * When from Union Territory.
237
     */
238
    public function getTaxWhenUnionTerritory(
239
        $user_state,
240
        $origin_state,
241
        $productid,
242
        $c_gst,
243
        $ut_gst,
244
        $state_code,
245
        $status
246
    ) {
247
        $taxClassId = TaxClass::where('name', 'Union Territory GST')
248
        ->pluck('id')->toArray(); //Get the class Id  of state
249
        if ($taxClassId) {
250
            $taxes = $this->getTaxByPriority($taxClassId);
251
            $value = $this->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
252
            if ($value == '') {
253
                $status = 0;
254
            }
255
        } else {
256
            $taxes = [0];
257
            $value = '';
258
        }
259
260
        return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value];
261
    }
262
263
    /**
264
     * When from Other Country and tax is applied for that country or state.
265
     */
266
    public function getTaxForSpecificCountry($taxClassId, $productid, $status)
267
    {
268
        $taxes = $this->getTaxByPriority($taxClassId);
269
        $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
270
        if ($value == '') {
271
            $status = 0;
272
        }
273
        $rate = $value;
274
275
        return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value, 'rate'=>$value];
276
    }
277
278
    /**
279
     * When from Other Country and tax is applied for Any country and state.
280
     */
281
    public function getTaxForAnyCountry($taxClassId, $productid, $status)
282
    {
283
        $taxes = $this->getTaxByPriority($taxClassId);
284
        $value = $this->getValueForOthers($productid, $taxClassId, $taxes);
285
        if ($value == '') {
286
            $status = 0;
287
        }
288
        $rate = $value;
289
290
        return ['taxes'=>$taxes, 'status'=>$status, 'value'=>$value, 'rate'=>$value];
291
    }
292
}
293