Test Setup Failed
Push — development ( f3657b...6e7a0e )
by Ashutosh
11:11
created

PromotionController::checkCode()   A

Complexity

Conditions 5
Paths 23

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 38
rs 9.2088
c 0
b 0
f 0
cc 5
nc 23
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
14
class PromotionController extends BasePromotionController
15
{
16
    public $promotion;
17
    public $product;
18
    public $promoRelation;
19
    public $type;
20
    public $invoice;
21
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
        $this->middleware('admin');
26
27
        $promotion = new Promotion();
28
        $this->promotion = $promotion;
29
30
        $product = new Product();
31
        $this->product = $product;
32
33
        $promoRelation = new PromoProductRelation();
34
        $this->promoRelation = $promoRelation;
35
36
        $type = new PromotionType();
37
        $this->type = $type;
38
39
        $invoice = new Invoice();
40
        $this->invoice = $invoice;
41
    }
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Response
47
     */
48
    public function index()
49
    {
50
        try {
51
            return view('themes.default1.payment.promotion.index');
52
        } catch (\Exception $ex) {
53
            return redirect()->back()->with('fails', $ex->getMessage());
54
        }
55
    }
56
57
    public function getPromotion()
58
    {
59
        $new_promotion = $this->promotion->select('code', 'type', 'id')->get();
60
61
        return\ DataTables::of($new_promotion)
62
                            ->addColumn('checkbox', function ($model) {
63
                                return "<input type='checkbox' class='promotion_checkbox'
64
                                 value=".$model->id.' name=select[] id=check>';
65
                            })
66
                        ->addColumn('code', function ($model) {
67
                            return ucfirst($model->code);
68
                        })
69
                        ->addColumn('type', function ($model) {
70
                            return $this->type->where('id', $model->type)->first()->name;
71
                        })
72
                        ->addColumn('products', function ($model) {
73
                            $selected = $this->promoRelation->select('product_id')
74
                            ->where('promotion_id', $model->id)->get();
75
                            $result = [];
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);
81
                            } else {
82
                                return 'None';
83
                            }
84
                        })
85
                        ->addColumn('action', function ($model) {
86
                            return '<a href='.url('promotions/'.$model->id.'/edit')
87
                            ." class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' 
88
                            style='color:white;'> </i>&nbsp;&nbsp;Edit</a>";
89
                        })
90
                         ->rawColumns(['checkbox', 'code', 'products', 'action'])
91
92
                        ->make(true);
93
    }
94
95
    /**
96
     * Show the form for creating a new resource.
97
     *
98
     * @return \Response
99
     */
100
    public function create()
101
    {
102
        try {
103
            $product = $this->product->pluck('name', 'id')->toArray();
104
            $type = $this->type->pluck('name', 'id')->toArray();
105
106
            return view('themes.default1.payment.promotion.create', compact('product', 'type'));
107
        } catch (\Exception $ex) {
108
            return redirect()->back()->with('fails', $ex->getMessage());
109
        }
110
    }
111
112
    /**
113
     * Store a newly created resource in storage.
114
     *
115
     * @return \Response
116
     */
117
    public function store(PromotionRequest $request)
118
    {
119
        try {
120
            $promo = $this->promotion->fill($request->input())->save();
121
            //dd($this->promotion);
122
            $products = $request->input('applied');
123
124
            foreach ($products as $product) {
125
                $this->promoRelation->create(['product_id' => $product, 'promotion_id' => $this->promotion->id]);
126
            }
127
128
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
129
        } catch (\Exception $ex) {
130
            return redirect()->back()->with('fails', $ex->getMessage());
131
        }
132
    }
133
134
    /**
135
     * Show the form for editing the specified resource.
136
     *
137
     * @param int $id
138
     *
139
     * @return \Response
140
     */
141
    public function edit($id)
142
    {
143
        try {
144
            $promotion = $this->promotion->where('id', $id)->first();
145
            $product = $this->product->pluck('name', 'id')->toArray();
146
            $type = $this->type->pluck('name', 'id')->toArray();
147
            $selectedProduct = $this->promoRelation
148
            ->where('promotion_id', $id)
149
            ->pluck('product_id', 'product_id')->toArray();
150
151
            return view('themes.default1.payment.promotion.edit',
152
             compact('product', 'promotion', 'selectedProduct', 'type'));
153
        } catch (\Exception $ex) {
154
            return redirect()->back()->with('fails', $ex->getMessage());
155
        }
156
    }
157
158
    /**
159
     * Update the specified resource in storage.
160
     *
161
     * @param int $id
162
     *
163
     * @return \Response
164
     */
165
    public function update($id, PromotionRequest $request)
166
    {
167
        try {
168
            $promotion = $this->promotion->where('id', $id)->first();
169
            $promotion->fill($request->input())->save();
170
            /* Delete the products has this id */
171
            $deletes = $this->promoRelation->where('promotion_id', $id)->get();
172
            foreach ($deletes as $delete) {
173
                $delete->delete();
174
            }
175
            /* Update the realtion details */
176
            $products = $request->input('applied');
177
            foreach ($products as $product) {
178
                $this->promoRelation->create(['product_id' => $product, 'promotion_id' => $promotion->id]);
179
            }
180
181
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
182
        } catch (\Exception $ex) {
183
            return redirect()->back()->with('fails', $ex->getMessage());
184
        }
185
    }
186
187
    /**
188
     * Remove the specified resource from storage.
189
     *
190
     * @param int $id
191
     *
192
     * @return \Response
193
     */
194
    public function destroy(Request $request)
195
    {
196
        try {
197
            $ids = $request->input('select');
198
            if (!empty($ids)) {
199
                foreach ($ids as $id) {
200
                    $promotion = $this->promotion->where('id', $id)->first();
201
                    if ($promotion) {
202
                        $promotion->delete();
203
                    } else {
204
                        echo "<div class='alert alert-danger alert-dismissable'>
205
                    <i class='fa fa-ban'></i>
206
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
207
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
208
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
209
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
210
                </div>';
211
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
212
                    }
213
                }
214
                echo "<div class='alert alert-success alert-dismissable'>
215
                    <i class='fa fa-ban'></i>
216
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
217
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
218
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
219
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
220
                </div>';
221
            } else {
222
                echo "<div class='alert alert-danger alert-dismissable'>
223
                    <i class='fa fa-ban'></i>
224
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
225
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
226
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
227
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
228
                </div>';
229
                //echo \Lang::get('message.select-a-row');
230
            }
231
        } catch (\Exception $e) {
232
            echo "<div class='alert alert-danger alert-dismissable'>
233
                    <i class='fa fa-ban'></i>
234
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
235
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
236
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
237
                        '.$e->getMessage().'
238
                </div>';
239
        }
240
    }
241
242
    public function checkCode($code, $productid)
243
    {
244
        try {
245
            $inv_cont = new \App\Http\Controllers\Order\InvoiceController();
246
            $promo = $inv_cont->getPromotionDetails($code);
247
            $value = $this->findCostAfterDiscount($promo->id, $productid);
248
249
            $coupon = new CartCondition([
250
                'name'   => $promo->code,
251
                'type'   => 'coupon',
252
                'target' => 'item',
253
                'value'  => $value,
254
            ]);
255
256
            $userId = \Auth::user()->id;
257
            \Cart::update($productid, [
258
           'id'        => $productid,
259
           'price'     => $value,
260
          'conditions' => $coupon,
261
262
           // new item price, price can also be a string format like so: '98.67'
263
          ]);
264
            $items = \Cart::getContent();
265
            \Session::put('items', $items);
266
267
            foreach ($items as $item) {
268
                if ($item->conditions) {
269
                // if (count($item->conditions) == 2 || count($item->conditions) == 1) {
270
                    \Cart::addItemCondition($productid, $coupon);
271
                }
272
            }
273
274
            return 'success';
275
        } catch (\Exception $ex) {
276
            if (!\Auth::user()) {
277
                throw new \Exception('Please Login');
278
            } else {
279
                throw new \Exception($ex->getMessage());
280
            }
281
        }
282
    }
283
284
    public function checkNumberOfUses($code)
285
    {
286
        try {
287
            $promotion = $this->promotion->where('code', $code)->first();
288
            $uses = $promotion->uses;
289
            if ($uses == 0) {
290
                return 'success';
291
            }
292
            $used_number = $this->invoice->where('coupon_code', $code)->count();
293
            if ($uses >= $used_number) {
294
                return 'success';
295
            } else {
296
                return 'fails';
297
            }
298
        } catch (\Exception $ex) {
299
            throw new \Exception(\Lang::get('message.find-cost-error'));
300
        }
301
    }
302
303
    public function checkExpiry($code)
304
    {
305
        try {
306
            $promotion = $this->promotion->where('code', $code)->first();
307
            $start = $promotion->start;
308
            $end = $promotion->expiry;
309
            //dd($end);
310
            $now = \Carbon\Carbon::now();
311
            $inv_cont = new \App\Http\Controllers\Order\InvoiceController();
312
            $getExpiryStatus = $inv_cont->getExpiryStatus($start, $end, $now);
313
314
            return $getExpiryStatus;
315
        } catch (\Exception $ex) {
316
            throw new \Exception(\Lang::get('message.check-expiry'));
317
        }
318
    }
319
}
320