Completed
Push — development ( ae2e62...5eba9c )
by Ashutosh
12:27 queued 02:41
created

CoupCodeAndInvoiceSearch::destroy()   A

Complexity

Conditions 5
Paths 14

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 46
rs 9.1928
c 0
b 0
f 0
cc 5
nc 14
nop 1
1
<?php
2
3
namespace App\Traits;
4
5
use App\Model\Order\Invoice;
6
use App\Model\Payment\Plan;
7
use App\Model\Payment\PlanPrice;
8
use App\Model\Payment\Promotion;
9
use App\Model\Product\Product;
10
use Bugsnag;
11
use Illuminate\Http\Request;
12
13
//////////////////////////////////////////////////////////////////////////////
14
// ADVANCE SEARCH FOR INVOICE AND COUPON CODE CALCULATION
15
//////////////////////////////////////////////////////////////////////////////
16
17
trait CoupCodeAndInvoiceSearch
18
{
19
    public function checkCode($code, $productid, $currency)
20
    {
21
        try {
22
            if ($code != '') {
23
                $promo = $this->promotion->where('code', $code)->first();
24
                //check promotion code is valid
25
                if (!$promo) {
26
                    throw new \Exception(\Lang::get('message.no-such-code'));
27
                }
28
                $relation = $promo->relation()->get();
29
                //check the relation between code and product
30
                if (count($relation) == 0) {
31
                    throw new \Exception(\Lang::get('message.no-product-related-to-this-code'));
32
                }
33
                //check the usess
34
                $cont = new \App\Http\Controllers\Payment\PromotionController();
35
                $uses = $cont->checkNumberOfUses($code);
36
                if ($uses != 'success') {
37
                    throw new \Exception(\Lang::get('message.usage-of-code-completed'));
38
                }
39
                //check for the expiry date
40
                $expiry = $this->checkExpiry($code);
0 ignored issues
show
Bug introduced by
It seems like checkExpiry() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

40
                /** @scrutinizer ignore-call */ 
41
                $expiry = $this->checkExpiry($code);
Loading history...
41
                if ($expiry != 'success') {
42
                    throw new \Exception(\Lang::get('message.usage-of-code-expired'));
43
                }
44
                $value = $this->findCostAfterDiscount($promo->id, $productid, $currency);
45
46
                return $value;
47
            } else {
48
                $product = $this->product->find($productid);
49
                $plans = Plan::where('product', $product)->pluck('id')->first();
50
                $price = PlanPrice::where('currency', $currency)->where('plan_id', $plans)->pluck('add_price')->first();
51
52
                return $price;
53
            }
54
        } catch (\Exception $ex) {
55
            throw new \Exception($ex->getMessage());
56
        }
57
    }
58
59
    public function findCostAfterDiscount($promoid, $productid, $currency)
60
    {
61
        try {
62
            $promotion = Promotion::findOrFail($promoid);
63
            $product = Product::findOrFail($productid);
64
            $promotion_type = $promotion->type;
65
            $promotion_value = $promotion->value;
66
            $planId = Plan::where('product', $productid)->pluck('id')->first();
67
            // dd($planId);
68
            $product_price = PlanPrice::where('plan_id', $planId)
69
            ->where('currency', $currency)->pluck('add_price')->first();
70
            $updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid);
71
72
            return $updated_price;
73
        } catch (\Exception $ex) {
74
            Bugsnag::notifyException($ex);
75
76
            throw new \Exception(\Lang::get('message.find-discount-error'));
77
        }
78
    }
79
80
    public function findCost($type, $value, $price, $productid)
81
    {
82
        $price = intval($price);
83
        switch ($type) {
84
            case 1:
85
                $percentage = $price * ($value / 100);
86
87
                return $price - $percentage;
88
            case 2:
89
                return $price - $value;
90
            case 3:
91
                return $value;
92
            case 4:
93
                return 0;
94
        }
95
    }
96
97
    public function advanceSearch($name = '', $invoice_no = '', $currency = '', $status = '', $from = '', $till = '')
98
    {
99
        $join = Invoice::leftJoin('users', 'invoices.user_id', '=', 'users.id');
100
        $this->name($name, $join);
101
        $this->invoice_no($invoice_no, $join);
102
        $this->status($status, $join);
103
        $this->searchcurrency($currency, $join);
104
105
        $this->invoice_from($from, $till, $join);
106
        $this->till_date($till, $from, $join);
107
108
        $join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at');
109
110
        $join = $join->orderBy('created_at', 'desc')
111
        ->select(
112
            'invoices.id',
113
            'first_name',
114
            'invoices.created_at',
115
            'invoices.currency',
116
            'user_id',
117
            'number',
118
            'status'
119
        );
120
121
        return $join;
122
    }
123
124
    public function name($name, $join)
125
    {
126
        if ($name) {
127
            $join = $join->where('first_name', $name);
128
129
            return $join;
130
        }
131
    }
132
133
    public function invoice_no($invoice_no, $join)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
134
    {
135
        if ($invoice_no) {
136
            $join = $join->where('number', $invoice_no);
137
138
            return $join;
139
        }
140
    }
141
142
    public function status($status, $join)
143
    {
144
        if ($status) {
145
            $join = $join->where('status', $status);
146
147
            return $join;
148
        }
149
    }
150
151
    public function searchcurrency($currency, $join)
152
    {
153
        if ($currency) {
154
            $join = $join->where('invoices.currency', $currency);
155
156
            return $join;
157
        }
158
        $return;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $return seems to be never defined.
Loading history...
159
    }
160
161
    public function invoice_from($from, $till, $join)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
162
    {
163
        if ($from) {
164
            $fromdate = date_create($from);
165
            $from = date_format($fromdate, 'Y-m-d H:m:i');
166
            $tills = date('Y-m-d H:m:i');
167
            $tillDate = $this->getTillDate($from, $till, $tills);
168
            $join = $join->whereBetween('invoices.created_at', [$from, $tillDate]);
169
170
            return $join;
171
        }
172
173
        return $join;
174
    }
175
176
    public function till_date($till, $from, $join)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
177
    {
178
        if ($till) {
179
            $tilldate = date_create($till);
180
            $till = date_format($tilldate, 'Y-m-d H:m:i');
181
            $froms = Invoice::first()->created_at;
182
            $fromDate = $this->getFromDate($from, $froms);
183
            $join = $join->whereBetween('invoices.created_at', [$fromDate, $till]);
184
185
            return $join;
186
        }
187
    }
188
189
    public function getExpiryStatus($start, $end, $now)
190
    {
191
        $whenDateNotSet = $this->whenDateNotSet($start, $end);
0 ignored issues
show
Bug introduced by
It seems like whenDateNotSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

191
        /** @scrutinizer ignore-call */ 
192
        $whenDateNotSet = $this->whenDateNotSet($start, $end);
Loading history...
192
        if ($whenDateNotSet) {
193
            return $whenDateNotSet;
194
        }
195
        $whenStartDateSet = $this->whenStartDateSet($start, $end, $now);
0 ignored issues
show
Bug introduced by
It seems like whenStartDateSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

195
        /** @scrutinizer ignore-call */ 
196
        $whenStartDateSet = $this->whenStartDateSet($start, $end, $now);
Loading history...
196
        if ($whenStartDateSet) {
197
            return $whenStartDateSet;
198
        }
199
        $whenEndDateSet = $this->whenEndDateSet($start, $end, $now);
0 ignored issues
show
Bug introduced by
It seems like whenEndDateSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

199
        /** @scrutinizer ignore-call */ 
200
        $whenEndDateSet = $this->whenEndDateSet($start, $end, $now);
Loading history...
200
        if ($whenEndDateSet) {
201
            return $whenEndDateSet;
202
        }
203
        $whenBothAreSet = $this->whenBothSet($start, $end, $now);
0 ignored issues
show
Bug introduced by
It seems like whenBothSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

203
        /** @scrutinizer ignore-call */ 
204
        $whenBothAreSet = $this->whenBothSet($start, $end, $now);
Loading history...
204
        if ($whenBothAreSet) {
205
            return $whenBothAreSet;
206
        }
207
    }
208
209
    public function getTillDate($from, $till, $tills)
210
    {
211
        if ($till) {
212
            $todate = date_create($till);
213
            $tills = date_format($todate, 'Y-m-d H:m:i');
214
        }
215
216
        return $tills;
217
    }
218
219
    public function getFromDate($from, $froms)
220
    {
221
        if ($from) {
222
            $fromdate = date_create($from);
223
            $froms = date_format($fromdate, 'Y-m-d H:m:i');
224
        }
225
226
        return $froms;
227
    }
228
229
    public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount)
230
    {
231
        try {
232
            $invoice = Invoice::find($invoiceid);
233
            $invoice_status = 'pending';
234
235
            $payment = $this->payment->create([
236
                'invoice_id'     => $invoiceid,
237
                'user_id'        => $invoice->user_id,
238
                'amount'         => $amount,
239
                'payment_method' => $payment_method,
240
                'payment_status' => $payment_status,
241
                'created_at'     => $payment_date,
242
            ]);
243
            $all_payments = $this->payment
244
            ->where('invoice_id', $invoiceid)
245
            ->where('payment_status', 'success')
246
            ->pluck('amount')->toArray();
247
            $total_paid = array_sum($all_payments);
248
            if ($total_paid >= $invoice->grand_total) {
249
                $invoice_status = 'success';
250
            }
251
            if ($invoice) {
252
                $invoice->status = $invoice_status;
253
                $invoice->save();
254
            }
255
256
            return $payment;
257
        } catch (\Exception $ex) {
258
            Bugsnag::notifyException($ex);
259
260
            throw new \Exception($ex->getMessage());
261
        }
262
    }
263
264
    /**
265
     * Remove the specified resource from storage.
266
     *
267
     * @param int $id
268
     *
269
     * @return \Response
270
     */
271
    public function destroy(Request $request)
272
    {
273
        try {
274
            $ids = $request->input('select');
275
            if (!empty($ids)) {
276
                foreach ($ids as $id) {
277
                    $invoice = $this->invoice->where('id', $id)->first();
278
                    if ($invoice) {
279
                        $invoice->delete();
280
                    } else {
281
                        echo "<div class='alert alert-danger alert-dismissable'>
282
                    <i class='fa fa-ban'></i>
283
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
284
                    /* @scrutinizer ignore-type */
285
                    \Lang::get('message.failed').'
286
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
287
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
288
                </div>';
289
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
290
                    }
291
                }
292
                echo "<div class='alert alert-success alert-dismissable'>
293
                    <i class='fa fa-ban'></i>
294
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
295
                    /* @scrutinizer ignore-type */
296
                    \Lang::get('message.success').'
297
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
298
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
299
                </div>';
300
            } else {
301
                echo "<div class='alert alert-danger alert-dismissable'>
302
                    <i class='fa fa-ban'></i>
303
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
304
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
305
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
306
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
307
                </div>';
308
                //echo \Lang::get('message.select-a-row');
309
            }
310
        } catch (\Exception $e) {
311
            echo "<div class='alert alert-danger alert-dismissable'>
312
                    <i class='fa fa-ban'></i>
313
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
314
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
315
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
316
                        '.$e->getMessage().'
317
                </div>';
318
        }
319
    }
320
321
        public function deletePayment(Request $request)
322
        {
323
            try {
324
                $ids = $request->input('select');
325
                if (!empty($ids)) {
326
                    foreach ($ids as $id) {
327
                        $payment = $this->payment->where('id', $id)->first();
328
                        if ($payment) {
329
                            $invoice = $this->invoice->find($payment->invoice_id);
330
                            if ($invoice) {
331
                                $invoice->status = 'pending';
332
                                $invoice->save();
333
                            }
334
                            $payment->delete();
335
                        } else {
336
                            echo "<div class='alert alert-danger alert-dismissable'>
337
                    <i class='fa fa-ban'></i>
338
                    <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> 
339
                    './* @scrutinizer ignore-type */\Lang::get('message.failed').'
340
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
341
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
342
                </div>';
343
                            //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
344
                        }
345
                    }
346
                    echo "<div class='alert alert-success alert-dismissable'>
347
                    <i class='fa fa-ban'></i>
348
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
349
                    /* @scrutinizer ignore-type */
350
                    \Lang::get('message.success').'
351
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
352
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
353
                </div>';
354
                } else {
355
                    echo "<div class='alert alert-danger alert-dismissable'>
356
                    <i class='fa fa-ban'></i>
357
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
358
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
359
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
360
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
361
                </div>';
362
                    //echo \Lang::get('message.select-a-row');
363
                }
364
            } catch (\Exception $e) {
365
                Bugsnag::notifyException($e);
366
                echo "<div class='alert alert-danger alert-dismissable'>
367
                    <i class='fa fa-ban'></i>
368
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
369
                    /* @scrutinizer ignore-type */ \Lang::get('message.failed').'
370
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
371
                        '.$e->getMessage().'
372
                </div>';
373
            }
374
        }
375
}
376