Completed
Push — development ( 7faa8d...b5300f )
by Ashutosh
15:42
created

BaseCartController::getValueForOtherState()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 4
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Model\Payment\Tax;
6
use App\Model\Payment\TaxProductRelation;
7
use App\Model\Product\Product;
8
use Bugsnag;
9
use Cart;
10
use Exception;
11
use Illuminate\Http\Request;
12
use Session;
13
14
class BaseCartController extends ExtendedBaseCartController
15
{
16
    public function getCartCollection($items)
17
    {
18
        if ($items == null) {
19
            $cartCollection = Cart::getContent();
20
        } else {
21
            $cartCollection = $items;
22
        }
23
24
        return $cartCollection;
25
    }
26
27
    /**
28
     * @param type $tax_class_id
29
     *
30
     * @throws \Exception
31
     *
32
     * @return type
33
     */
34
    public function getTaxByPriority($taxClassId)
35
    {
36
        try {
37
            $taxe_relation = Tax::where('tax_classes_id', $taxClassId)->get();
38
39
            return $taxe_relation;
40
        } catch (\Exception $ex) {
41
            Bugsnag::notifyException($ex);
42
43
            throw new \Exception('error in get tax priority');
44
        }
45
    }
46
47
    /**
48
     *   Get tax value for Same State.
49
     *
50
     * @param type $productid
51
     * @param type $c_gst
52
     * @param type $s_gst
53
     *                        return type
54
     */
55
    public function getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes)
56
    {
57
        try {
58
            $value = '';
59
            $value = $taxes->toArray()[0]['active'] ?
60
61
                  (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
62
                        $c_gst + $s_gst.'%' : 0) : 0;
63
64
            return $value;
65
        } catch (Exception $ex) {
66
            Bugsnag::notifyException($ex);
67
68
            return redirect()->back()->with('fails', $ex->getMessage());
69
        }
70
    }
71
72
    /**
73
     *   Get tax value for Other States.
74
     *
75
     * @param type $productid
76
     * @param type $i_gst
77
     *                        return type
78
     */
79
    public function getValueForOtherState($productid, $i_gst, $taxClassId, $taxes)
80
    {
81
        $value = '';
82
        $value = $taxes->toArray()[0]['active'] ? //If the Current Class is active
83
              (TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ?
84
                        $i_gst.'%' : 0) : 0; //IGST
85
86
        return $value;
87
    }
88
89
    /**
90
     *  Get tax value for Union Territory States.
91
     *
92
     * @param type $productid
93
     * @param type $c_gst
94
     * @param type $ut_gst
95
     *                        return type
96
     */
97
    public function getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes)
98
    {
99
        $value = '';
100
        $value = $taxes->toArray()[0]['active'] ?
101
             (TaxProductRelation::where('product_id', $productid)
102
                ->where('tax_class_id', $taxClassId)
103
                ->count() ? $ut_gst + $c_gst.'%' : 0) : 0;
104
105
        return $value;
106
    }
107
108
    public function getValueForOthers($productid, $taxClassId, $taxes)
109
    {
110
        $otherRate = 0;
111
        $status = $taxes->toArray()[0]['active'];
112
        if ($status && (TaxProductRelation::where('product_id', $productid)
113
            ->where('tax_class_id', $taxClassId)->count() > 0)) {
114
            $otherRate = Tax::where('tax_classes_id', $taxClassId)->first()->rate;
115
        }
116
117
        $value = $otherRate.'%';
118
119
        return $value;
120
    }
121
122
    /**
123
     * @param type $id
124
     *
125
     * @return array
126
     */
127
    public function addCurrencyAttributes($id)
128
    {
129
        try {
130
            $currency = $this->currency();
131
            $product = $this->product->where('id', $id)->first();
132
            if ($product) {
133
                $productCurrency = $this->currency();
134
                $currency = $this->currency->where('code', $productCurrency)->get()->toArray();
135
            } else {
136
                $currency = [];
137
            }
138
139
            return $currency;
140
        } catch (\Exception $ex) {
141
            throw new \Exception($ex->getMessage());
142
        }
143
    }
144
145
    /**
146
     * @param Request $request
147
     *
148
     * @return type
149
     */
150
    public function postContactUs(Request $request)
151
    {
152
        $this->validate($request, [
153
            'name'    => 'required',
154
            'email'   => 'required|email',
155
            'message' => 'required',
156
        ]);
157
158
        $set = new \App\Model\Common\Setting();
159
        $set = $set->findOrFail(1);
160
161
        try {
162
            $from = $set->email;
163
            $fromname = $set->company;
164
            $toname = '';
165
            $to = '[email protected]';
166
            $data = '';
167
            $data .= 'Name: '.$request->input('name').'<br/s>';
168
            $data .= 'Email: '.$request->input('email').'<br/>';
169
            $data .= 'Message: '.$request->input('message').'<br/>';
170
            $data .= 'Mobile: '.$request->input('Mobile').'<br/>';
171
172
            $subject = 'Faveo billing enquiry';
173
            $this->templateController->mailing($from, $to, $data, $subject, [], $fromname, $toname);
174
175
            return redirect()->back()->with('success', 'Your message was sent successfully. Thanks.');
176
        } catch (\Exception $ex) {
177
            return redirect()->back()->with('fails', $ex->getMessage());
178
        }
179
    }
180
181
    public function cartRemove(Request $request)
182
    {
183
        $id = $request->input('id');
184
        Cart::remove($id);
185
186
        return 'success';
187
    }
188
189
    public function reduseQty(Request $request)
190
    {
191
        $id = $request->input('id');
192
        Cart::update($id, [
193
            'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3
194
        ]);
195
196
        return 'success';
197
    }
198
199
    public function updateQty(Request $request)
200
    {
201
        $id = $request->input('productid');
202
        $qty = $request->input('qty');
203
        Cart::update($id, [
204
            'quantity' => [
205
                'relative' => false,
206
                'value'    => $qty,
207
            ],
208
        ]);
209
210
        return 'success';
211
    }
212
213
    public function addProduct($id)
214
    {
215
        try {
216
            $qty = 1;
217
218
            $currency = $this->currency();
219
            $product = Product::where('id', $id)->first();
220
            if ($product) {
221
                $actualPrice = $this->cost($product->id);
222
                $currency = $this->currency();
223
                $productName = $product->name;
224
                $planid = 0;
225
                if ($this->checkPlanSession() === true) {
226
                    $planid = Session::get('plan');
227
                }
228
                $isTaxApply = $product->tax_apply;
229
                $taxConditions = $this->checkTax($id);
230
231
                /*
232
                 * Check if this product allow multiple qty
233
                 */
234
                if ($product->multiple_qty == 1) {
235
                    // Allow
236
                } else {
237
                    $qty = 1;
238
                }
239
240
                $items = ['id' => $id, 'name' => $productName, 'price' => $actualPrice,
241
                 'quantity' => $qty, 'attributes' => ['currency' => [[$currency]]]];
242
                $items = array_merge($items, $taxConditions);
243
244
                return $items;
245
            }
246
        } catch (\Exception $e) {
247
            Bugsnag::notifyException($e);
248
        }
249
    }
250
251
    /**
252
     * @param type $userid
253
     *
254
     * @throws \Exception
255
     *
256
     * @return string
257
     */
258
    public function currency($userid = '')
259
    {
260
        try {
261
            $currency = 'INR';
262
            if ($this->checkCurrencySession() === true) {
263
                $currency = Session::get('currency');
264
            }
265
266
            if (\Auth::user()) {
267
                $currency = \Auth::user()->currency;
268
                if ($currency == 'USD' || $currency == '1') {
269
                    $currency = 'USD';
270
                }
271
            }
272
            if ($userid != '') {
273
                $user = new \App\User();
274
                $currency = $user->find($userid)->currency;
275
                if ($currency == 'USD' || $currency == '1') {
276
                    $currency = 'USD';
277
                } else {
278
                    $currency = 'INR';
279
                }
280
            }
281
282
            return $currency;
283
        } catch (\Exception $ex) {
284
            throw new \Exception($ex->getMessage());
285
        }
286
    }
287
288
    /**
289
     * @param type $productid
290
     *
291
     * @throws \Exception
292
     *
293
     * @return bool
294
     */
295
    public function allowSubscription($productid)
296
    {
297
        try {
298
            $reponse = false;
299
            $product = $this->product->find($productid);
300
            if ($product) {
301
                if ($product->subscription == 1) {
302
                    $reponse = true;
303
                }
304
            }
305
306
            return $reponse;
307
        } catch (\Exception $ex) {
308
            throw new \Exception($ex->getMessage());
309
        }
310
    }
311
312
    /**
313
     * @param type $id
314
     * @param type $key
315
     * @param type $value
316
     */
317
    public function cartUpdate($id, $key, $value)
318
    {
319
        try {
320
            Cart::update(
321
                    $id, [
322
                $key => $value, // new item name
323
                    ]
324
            );
325
        } catch (\Exception $ex) {
326
            throw new \Exception($ex->getMessage());
327
        }
328
    }
329
330
    /**
331
     * @param type $iso
332
     *
333
     * @throws \Exception
334
     *
335
     * @return string
336
     */
337
    public static function findCountryByGeoip($iso)
338
    {
339
        try {
340
            $country = \App\Model\Common\Country::where('country_code_char2', $iso)->first();
341
            if ($country) {
342
                return $country->country_code_char2;
343
            } else {
344
                return '';
345
            }
346
        } catch (\Exception $ex) {
347
            throw new \Exception($ex->getMessage());
348
        }
349
    }
350
351
    /**
352
     * @param type $iso
353
     *
354
     * @throws \Exception
355
     *
356
     * @return array
357
     */
358
    public static function findStateByRegionId($iso)
359
    {
360
        try {
361
            $states = \App\Model\Common\State::where('country_code_char2', $iso)
362
            ->pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
363
364
            return $states;
365
        } catch (\Exception $ex) {
366
            throw new \Exception($ex->getMessage());
367
        }
368
    }
369
370
      /**
371
     * @throws \Exception
372
     */
373
    public function removePlanSession()
374
    {
375
        try {
376
            if (Session::has('plan')) {
377
                Session::forget('plan');
378
            }
379
        } catch (\Exception $ex) {
380
            throw new \Exception($ex->getMessage());
381
        }
382
    }
383
384
    /**
385
     * @throws \Exception
386
     *
387
     * @return bool
388
     */
389
    public function checkPlanSession()
390
    {
391
        try {
392
            if (Session::has('plan')) {
393
                return true;
394
            }
395
396
            return false;
397
        } catch (\Exception $ex) {
398
            throw new \Exception($ex->getMessage());
399
        }
400
    }
401
402
}
403