Test Setup Failed
Push — development ( 07e8d5...22dd1e )
by Ashutosh
10:11
created

PromotionController::checkCode()   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 37
rs 8.8977
c 0
b 0
f 0
cc 6
nc 25
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Payment;
4
5
use App\Http\Requests\Payment\PromotionRequest;
6
use App\Model\Order\Invoice;
7
use App\Model\Payment\PromoProductRelation;
8
use App\Model\Payment\Promotion;
9
use App\Model\Payment\PromotionType;
10
use App\Model\Product\Product;
11
use Darryldecode\Cart\CartCondition;
12
use Illuminate\Http\Request;
13
use App\Http\Controllers\Front\CartController;
14
15
16
class PromotionController extends BasePromotionController
17
{
18
    public $promotion;
19
    public $product;
20
    public $promoRelation;
21
    public $type;
22
    public $invoice;
23
24
    public function __construct()
25
    {
26
        $this->middleware('auth');
27
        $this->middleware('admin');
28
29
        $promotion = new Promotion();
30
        $this->promotion = $promotion;
31
32
        $product = new Product();
33
        $this->product = $product;
34
35
        $promoRelation = new PromoProductRelation();
36
        $this->promoRelation = $promoRelation;
37
38
        $type = new PromotionType();
39
        $this->type = $type;
40
41
        $invoice = new Invoice();
42
        $this->invoice = $invoice;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Payment\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
49
     */
50
    public function index()
51
    {
52
        try {
53
            return view('themes.default1.payment.promotion.index');
54
        } catch (\Exception $ex) {
55
            return redirect()->back()->with('fails', $ex->getMessage());
56
        }
57
    }
58
59
    public function getPromotion()
60
    {
61
        $new_promotion = $this->promotion->select('code', 'type', 'id')->get();
62
63
        return\ DataTables::of($new_promotion)
64
                            ->addColumn('checkbox', function ($model) {
65
                                return "<input type='checkbox' class='promotion_checkbox' value=".$model->id.' name=select[] id=check>';
66
                            })
67
                        ->addColumn('code', function ($model) {
68
                            return ucfirst($model->code);
69
                        })
70
                        ->addColumn('type', function ($model) {
71
                            return $this->type->where('id', $model->type)->first()->name;
72
                        })
73
                        ->addColumn('products', function ($model) {
74
                            $selected = $this->promoRelation->select('product_id')->where('promotion_id', $model->id)->get();
75
76
                            foreach ($selected as $key => $select) {
77
                                $result[$key] = $this->product->where('id', $select->product_id)->first()->name;
78
                            }
79
                            if (!empty($result)) {
80
                                return implode(',', $result);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 76. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
81
                            } else {
82
                                return 'None';
83
                            }
84
                        })
85
                        ->addColumn('action', function ($model) {
86
                            return '<a href='.url('promotions/'.$model->id.'/edit')." class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' style='color:white;'> </i>&nbsp;&nbsp;Edit</a>";
87
                        })
88
                         ->rawColumns(['checkbox', 'code', 'products', 'action'])
89
90
                        ->make(true);
91
    }
92
93
    /**
94
     * Show the form for creating a new resource.
95
     *
96
     * @return Response
97
     */
98
    public function create()
99
    {
100
        try {
101
            $product = $this->product->pluck('name', 'id')->toArray();
102
            $type = $this->type->pluck('name', 'id')->toArray();
103
104
            return view('themes.default1.payment.promotion.create', compact('product', 'type'));
105
        } catch (\Exception $ex) {
106
            return redirect()->back()->with('fails', $ex->getMessage());
107
        }
108
    }
109
110
    /**
111
     * Store a newly created resource in storage.
112
     *
113
     * @return Response
114
     */
115
    public function store(PromotionRequest $request)
116
    {
117
        try {
118
            $promo = $this->promotion->fill($request->input())->save();
119
            //dd($this->promotion);
120
            $products = $request->input('applied');
121
122
            foreach ($products as $product) {
123
                $this->promoRelation->create(['product_id' => $product, 'promotion_id' => $this->promotion->id]);
124
            }
125
126
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
127
        } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Payment\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
128
            return redirect()->back()->with('fails', $ex->getMessage());
129
        }
130
    }
131
132
    /**
133
     * Show the form for editing the specified resource.
134
     *
135
     * @param int $id
136
     *
137
     * @return Response
138
     */
139
    public function edit($id)
140
    {
141
        try {
142
            $promotion = $this->promotion->where('id', $id)->first();
143
            $product = $this->product->pluck('name', 'id')->toArray();
144
            $type = $this->type->pluck('name', 'id')->toArray();
145
            $selectedProduct = $this->promoRelation->where('promotion_id', $id)->pluck('product_id', 'product_id')->toArray();
146
            //dd($selectedProduct);
147
            return view('themes.default1.payment.promotion.edit', compact('product', 'promotion', 'selectedProduct', 'type'));
148
        } catch (\Exception $ex) {
149
            return redirect()->back()->with('fails', $ex->getMessage());
150
        }
151
    }
152
153
    /**
154
     * Update the specified resource in storage.
155
     *
156
     * @param int $id
157
     *
158
     * @return Response
159
     */
160
    public function update($id, PromotionRequest $request)
161
    {
162
        try {
163
            $promotion = $this->promotion->where('id', $id)->first();
164
            $promotion->fill($request->input())->save();
165
            /* Delete the products has this id */
166
            $deletes = $this->promoRelation->where('promotion_id', $id)->get();
167
            foreach ($deletes as $delete) {
168
                $delete->delete();
169
            }
170
            /* Update the realtion details */
171
            $products = $request->input('applied');
172
            foreach ($products as $product) {
173
                $this->promoRelation->create(['product_id' => $product, 'promotion_id' => $promotion->id]);
174
            }
175
176
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
177
        } catch (Exception $ex) {
178
            return redirect()->back()->with('fails', $ex->getMessage());
179
        }
180
    }
181
182
    /**
183
     * Remove the specified resource from storage.
184
     *
185
     * @param int $id
186
     *
187
     * @return Response
188
     */
189
    public function destroy(Request $request)
190
    {
191
        try {
192
            $ids = $request->input('select');
193
            if (!empty($ids)) {
194
                foreach ($ids as $id) {
195
                    $promotion = $this->promotion->where('id', $id)->first();
196
                    if ($promotion) {
197
                        $promotion->delete();
198
                    } else {
199
                        echo "<div class='alert alert-danger alert-dismissable'>
200
                    <i class='fa fa-ban'></i>
201
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */
202
                    \Lang::get('message.failed').'
203
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
204
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
205
                </div>';
206
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
207
                    }
208
                }
209
                echo "<div class='alert alert-success alert-dismissable'>
210
                    <i class='fa fa-ban'></i>
211
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */
212
                    \Lang::get('message.success').'
213
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
214
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
215
                </div>';
216
            } else {
217
                echo "<div class='alert alert-danger alert-dismissable'>
218
                    <i class='fa fa-ban'></i>
219
                    <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.alert') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

219
                    <b>"./** @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
Loading history...
220
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
221
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
222
                </div>';
223
                //echo \Lang::get('message.select-a-row');
224
            }
225
        } catch (\Exception $e) {
226
            echo "<div class='alert alert-danger alert-dismissable'>
227
                    <i class='fa fa-ban'></i>
228
                    <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */
229
                    \Lang::get('message.failed').'
230
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
231
                        '.$e->getMessage().'
232
                </div>';
233
        }
234
    }
235
236
    public function checkCode($code, $productid)
237
    {
238
        try {
239
            $inv_cont = new \App\Http\Controllers\Order\InvoiceController();
240
            $promo = $inv_cont->getPromotionDetails($code);
241
            $value = $this->findCostAfterDiscount($promo->id, $productid);
242
243
            $coupon = new CartCondition([
244
                'name'   => $promo->code,
245
                'type'   => 'coupon',
246
                'target' => 'item',
247
                'value'  => $value,
248
            ]);
249
250
            $userId = \Auth::user()->id;
251
            \Cart::update($productid, [
252
           'id'        => $productid,
253
           'price'     => $value,
254
          'conditions' => $coupon,
255
256
           // new item price, price can also be a string format like so: '98.67'
257
          ]);
258
            $items = \Cart::getContent();
259
            \Session::put('items', $items);
260
261
            foreach ($items as $item) {
262
                if (count($item->conditions) == 2 || count($item->conditions) == 1) {
263
                    \Cart::addItemCondition($productid, $coupon);
264
                }
265
            }
266
267
            return 'success';
268
        } catch (\Exception $ex) {
269
            if(!\Auth::user()){
270
                throw new \Exception('Please Login');
271
            }else{
272
            throw new \Exception(\Lang::get('message.check-code-error'));
273
        }
274
        }
275
    }
276
277
    public function checkNumberOfUses($code)
278
    {
279
        try {
280
            $promotion = $this->promotion->where('code', $code)->first();
281
            $uses = $promotion->uses;
282
            if ($uses == 0) {
283
                return 'success';
284
            }
285
            $used_number = $this->invoice->where('coupon_code', $code)->count();
286
            if ($uses >= $used_number) {
287
                return 'success';
288
            } else {
289
                return 'fails';
290
            }
291
        } catch (\Exception $ex) {
292
            throw new \Exception(\Lang::get('message.find-cost-error'));
293
        }
294
    }
295
296
    public function checkExpiry($code)
297
    {
298
        try {
299
            $promotion = $this->promotion->where('code', $code)->first();
300
            $start = $promotion->start;
301
            $end = $promotion->expiry;
302
            //dd($end);
303
            $now = \Carbon\Carbon::now();
304
            $inv_cont = new \App\Http\Controllers\Order\InvoiceController();
305
            $getExpiryStatus = $inv_cont->getExpiryStatus($start, $end, $now);
306
307
            return $getExpiryStatus;
308
        } catch (\Exception $ex) {
309
            throw new \Exception(\Lang::get('message.check-expiry'));
310
        }
311
    }
312
}
313