Completed
Push — development ( c0a597...4b3f17 )
by Ashutosh
11:26
created

PromotionController::checkCode()   A

Complexity

Conditions 5
Paths 15

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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

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