PaymentMaster::resetFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Payment;
4
5
use Adminetic\Website\Exports\Payment\DateWisePaymentExport;
0 ignored issues
show
Bug introduced by
The type Adminetic\Website\Export...t\DateWisePaymentExport was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Adminetic\Website\Exports\Payment\PaymentGeneralExport;
0 ignored issues
show
Bug introduced by
The type Adminetic\Website\Export...nt\PaymentGeneralExport was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Adminetic\Website\Exports\Payment\TypeWisePaymentExport;
0 ignored issues
show
Bug introduced by
The type Adminetic\Website\Export...t\TypeWisePaymentExport was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Adminetic\Website\Models\Admin\Event;
0 ignored issues
show
Bug introduced by
The type Adminetic\Website\Models\Admin\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Adminetic\Website\Models\Admin\Payment;
10
use Carbon\Carbon;
11
use Illuminate\Support\Facades\Cache;
12
use Livewire\Component;
13
use Livewire\WithPagination;
14
use Maatwebsite\Excel\Facades\Excel;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Facades\Excel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class PaymentMaster extends Component
17
{
18
    use WithPagination;
0 ignored issues
show
Bug introduced by
The trait Livewire\WithPagination requires the property $paginationTheme which is not provided by Adminetic\Website\Http\L...n\Payment\PaymentMaster.
Loading history...
19
20
    public $payments;
21
22
    /*
23
    |--------------------------------------------------------------------------
24
    | Payment Date Mode
25
    |--------------------------------------------------------------------------
26
    |
27
    | 1 = Date Mode
28
    | 2 = Date Range Mode
29
    |
30
    */
31
    public $date_mode = 1;
32
33
    public $date;
34
    public $start_date;
35
    public $end_date;
36
37
    /*
38
    |--------------------------------------------------------------------------
39
    | Payment Type
40
    |--------------------------------------------------------------------------
41
    |
42
    | 1 => Income
43
    | 2 => Expense
44
    |
45
    */
46
    public $payment_type;
47
48
    /*
49
    |--------------------------------------------------------------------------
50
    | Event ID Filter
51
    |--------------------------------------------------------------------------
52
    */
53
    public $events;
54
    public $event_id;
55
56
    /*
57
    |--------------------------------------------------------------------------
58
    | Order By
59
    |--------------------------------------------------------------------------
60
    |
61
    | 1 = Latest
62
    | 2 = Oldest
63
    |
64
    */
65
    public $order_by;
66
67
    protected $listeners = ['initialize_payment_master' => 'initializePaymentMaster', 'filter_date' => 'filterDate', 'date_range_filter' => 'dateRangeFilter'];
68
69
    public function initializePaymentMaster()
70
    {
71
        $this->emit('initializePaymentMaster');
72
    }
73
74
    public function updatedDateMode()
75
    {
76
        $this->emit('initializePaymentMaster');
77
    }
78
79
    public function dateRangeFilter($start_date, $end_date)
80
    {
81
        $this->date_mode = 2;
82
        $this->start_date = Carbon::create($start_date);
83
        $this->end_date = Carbon::create($end_date);
84
        $this->getPayments();
85
        $this->emit('initializePaymentMaster');
86
    }
87
88
    public function filterDate($date)
89
    {
90
        $this->date_mode = 1;
91
        $this->date = Carbon::create($date);
92
        $this->getPayments();
93
        $this->emit('initializePaymentMaster');
94
    }
95
96
    public function updatedEventId()
97
    {
98
        $this->getPayments();
99
        $this->emit('initializePaymentMaster');
100
    }
101
102
    public function mount()
103
    {
104
        $this->getPayments();
105
        $this->events = Cache::get('events', Event::orderBy('position')->get());
106
    }
107
108
    public function render()
109
    {
110
        return view('website::livewire.admin.payment.payment-master');
111
    }
112
113
    public function resetFilter()
114
    {
115
        $this->order_by = null;
116
        $this->payment_type = null;
117
        $this->date_mode = null;
118
        $this->date = null;
119
        $this->start_date = null;
120
        $this->end_date = null;
121
    }
122
123
    public function getPayments()
124
    {
125
        $data = Payment::query();
126
127
        $data = $this->filterByEventId($data);
128
129
        $data = $this->filterByPaymentType($data);
130
131
        $data = $this->filterByDateMode($data);
132
133
        $data = $this->orderPayments($data);
134
135
        $this->payments = $data->get();
136
    }
137
138
    private function filterByEventId($data)
139
    {
140
        if (! is_null($this->event_id)) {
141
            $event = Event::find($this->event_id);
142
143
            return $data->whereIn('id', $event->payments->pluck('id'));
144
        }
145
146
        return $data;
147
    }
148
149
    private function filterByPaymentType($data)
150
    {
151
        $payment_type = $this->payment_type;
152
        if (! is_null($payment_type)) {
153
            return $data->where('type', $payment_type);
154
        }
155
156
        return $data;
157
    }
158
159
    private function filterByDateMode($data)
160
    {
161
        $date_mode = $this->date_mode;
162
        if (! is_null($date_mode)) {
0 ignored issues
show
introduced by
The condition is_null($date_mode) is always false.
Loading history...
163
            if ($date_mode == 1) {
164
                $date = $this->date ?? Carbon::now();
165
166
                return $data->whereDate('created_at', $date);
167
            }
168
            if ($date_mode == 2) {
169
                $start_date = $this->start_date;
170
                $end_date = $this->end_date;
171
                if (! is_null($start_date) && ! is_null($end_date)) {
172
                    return $data->whereBetween('created_at', [$start_date, $end_date]);
173
                }
174
            }
175
176
            return $data;
177
        }
178
179
        return $data;
180
    }
181
182
    private function orderPayments($data)
183
    {
184
        $order_by = $this->order_by;
185
186
        if (! is_null($order_by)) {
187
            if ($order_by == 1) {
188
                return $data->latest();
189
            } elseif ($order_by == 2) {
190
                return $data->oldest();
191
            }
192
        }
193
194
        return $data;
195
    }
196
197
    /* Export */
198
    public function export($type)
199
    {
200
        $payments = Payment::get();
201
        // General Export
202
        if ($type == 1) {
203
            return Excel::download(new PaymentGeneralExport($payments), 'payment_general_export.xlsx');
204
        }
205
        // Date Wise Payment Export
206
        elseif ($type == 2) {
207
            return Excel::download(new DateWisePaymentExport($payments), 'payment_datewise_export.xlsx');
208
        }
209
        // Payment Type Export
210
        elseif ($type == 3) {
211
            return Excel::download(new TypeWisePaymentExport($payments), 'type_wise_payment_export.xlsx');
212
        }
213
    }
214
}
215