Completed
Push — development ( 55bb16...2dbf65 )
by Ashutosh
09:53
created

ExtendedBaseCartController::getMobileCodeByIso()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Model\Product\Product;
8
use App\Model\Payment\Plan;
9
use App\Model\Payment\PlanPrice;
10
use Cart;
11
use Session;
12
13
class ExtendedBaseCartController extends Controller
14
{
15
     /**
16
     * @return type
17
     */
18
    public function addCouponUpdate()
19
    {
20
        try {
21
            $code = \Input::get('coupon');
22
            $cart = Cart::getContent();
23
            $id = '';
24
            foreach ($cart as $item) {
25
                $id = $item->id;
26
            }
27
            $promo_controller = new \App\Http\Controllers\Payment\PromotionController();
28
            $result = $promo_controller->checkCode($code, $id);
29
            if ($result == 'success') {
30
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
31
            }
32
33
            return redirect()->back();
34
        } catch (\Exception $ex) {
35
            Bugsnag::notifyException($ex);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Front\Bugsnag was not found. Did you mean Bugsnag? If so, make sure to prefix the type with \.
Loading history...
36
37
            return redirect()->back()->with('fails', $ex->getMessage());
38
        }
39
    }
40
41
        /**
42
     * @return type
43
     */
44
    public function clearCart()
45
    {
46
        foreach (Cart::getContent() as $item) {
47
            if (\Session::has('domain'.$item->id)) {
48
                \Session::forget('domain'.$item->id);
49
            }
50
        }
51
        $this->removePlanSession();
52
        $renew_control = new \App\Http\Controllers\Order\RenewController();
53
        $renew_control->removeSession();
54
        Cart::clear();
55
56
        return redirect('show/cart');
57
    }
58
59
     /**
60
     * @throws \Exception
61
     */
62
    public function removePlanSession()
63
    {
64
        try {
65
            if (Session::has('plan')) {
66
                Session::forget('plan');
67
            }
68
        } catch (\Exception $ex) {
69
            throw new \Exception($ex->getMessage());
70
        }
71
    }
72
73
     /**
74
     * @throws \Exception
75
     *
76
     * @return bool
77
     */
78
    public function checkPlanSession()
79
    {
80
        try {
81
            if (Session::has('plan')) {
82
                return true;
83
            }
84
85
            return false;
86
        } catch (\Exception $ex) {
87
            throw new \Exception($ex->getMessage());
88
        }
89
    }
90
91
     /**
92
     * @param type $iso
93
     *
94
     * @throws \Exception
95
     *
96
     * @return type
97
     */
98
    public static function getMobileCodeByIso($iso)
99
    {
100
        try {
101
            $code = '';
102
            if ($iso != '') {
103
                $mobile = \DB::table('mobile')->where('iso', $iso)->first();
104
                if ($mobile) {
105
                    $code = $mobile->phonecode;
106
                }
107
            }
108
109
            return $code;
110
        } catch (\Exception $ex) {
111
            throw new \Exception($ex->getMessage());
112
        }
113
    }
114
115
116
     
117
118
    /**
119
     * @param type $productid
120
     * @param type $userid
121
     *
122
     * @throws \Exception
123
     *
124
     * @return type
125
     */
126
    public function productCost($productid, $userid = '')
127
    {
128
        try {
129
            $sales = 0;
130
            $currency = $this->currency($userid);
131
            $product = Product::find($productid);
132
133
            // $price = $product->price()->where('currency', $currency)->first();
134
            $plan_id = Plan::where('product', $productid)->pluck('id')->first();
135
            $price = PlanPrice::where('plan_id', $plan_id)->where('currency', $currency)->pluck('add_price')->first();
136
            if ($price) {
137
                $sales = $price;
138
                // if ($sales == 0) {
139
                //     $sales = $price->price;
140
                // }
141
            }
142
            //}
143
144
            return $sales;
145
        } catch (\Exception $ex) {
146
            throw new \Exception($ex->getMessage());
147
        }
148
    }
149
150
    /**
151
     * @param type $productid
152
     * @param type $userid
153
     * @param type $planid
154
     *
155
     * @throws \Exception
156
     *
157
     * @return type
158
     */
159
    public function planCost($productid, $userid, $planid = '')
160
    {
161
        try {
162
            $cost = 0;
163
            $subscription = $this->allowSubscription($productid);
164
            if ($this->checkPlanSession() === true) {
165
                $planid = Session::get('plan');
166
            }
167
168
            if ($subscription === true) {
169
                $plan = new \App\Model\Payment\Plan();
170
                $plan = $plan->where('id', $planid)->where('product', $productid)->first();
171
                $items = (\Session::get('items'));
172
173
                if ($plan) {
174
                    $currency = $this->currency($userid);
175
                    $price = $plan->planPrice()
176
                                    ->where('currency', $currency)
177
                                    ->first()
178
                            ->add_price;
179
                    $days = $plan->days;
180
                    $months = $days / 30 / 12;
181
                    if ($items != null) {
182
                        $cost = $items[$productid]['price'];
183
                    } else {
184
                        $cost = round($months) * $price;
185
                    }
186
                }
187
            }
188
189
            return $cost;
190
        } catch (\Exception $ex) {
191
            throw new \Exception($ex->getMessage());
192
        }
193
    }
194
195
196
197
}
198