1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\InvoiceRequest; |
6
|
|
|
use App\Models\Invoice; |
7
|
|
|
use App\Models\Paymentmethod; |
8
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
9
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
10
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
11
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation; |
12
|
|
|
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; |
13
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
14
|
|
|
|
15
|
|
|
class InvoiceCrudController extends CrudController |
16
|
|
|
{ |
17
|
|
|
use ListOperation; |
|
|
|
|
18
|
|
|
use UpdateOperation; |
|
|
|
|
19
|
|
|
use DeleteOperation; |
20
|
|
|
use ShowOperation { show as traitShow; } |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Configure the CrudPanel object. Apply settings to all operations. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function setup() |
28
|
|
|
{ |
29
|
|
|
CRUD::setModel(Invoice::class); |
30
|
|
|
CRUD::setRoute(config('backpack.base.route_prefix').'/invoice'); |
31
|
|
|
CRUD::setEntityNameStrings(__('invoice'), __('invoices')); |
32
|
|
|
if (! config('invoicing.price_categories_enabled')) { |
33
|
|
|
$this->crud->addButtonFromView('top', 'createInvoice', 'createInvoice', 'start'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Define what happens when the List operation is loaded. |
39
|
|
|
* |
40
|
|
|
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
protected function setupListOperation() |
44
|
|
|
{ |
45
|
|
|
if (config('app.currency_position') === 'before') { |
46
|
|
|
$currency = ['prefix' => config('app.currency_symbol')]; |
47
|
|
|
} else { |
48
|
|
|
$currency = ['suffix' => config('app.currency_symbol')]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (config('invoicing.invoice_numbering') === 'manual') { |
52
|
|
|
CRUD::column('receipt_number'); |
53
|
|
|
} else { |
54
|
|
|
CRUD::column('invoice_number'); |
55
|
|
|
|
56
|
|
|
CRUD::addColumn([ |
57
|
|
|
'name' => 'invoiceType', |
58
|
|
|
'type' => 'relationship', |
59
|
|
|
'label' => __('Type'), |
60
|
|
|
'searchLogic' => false, |
61
|
|
|
'attribute' => 'name', |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->crud->addColumn([ |
66
|
|
|
'name' => 'created_at', |
67
|
|
|
'key' => 'year', |
68
|
|
|
'label' => __('Year'), |
69
|
|
|
'type' => 'date', |
70
|
|
|
'format' => 'Y', |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$this->crud->addColumn([ |
74
|
|
|
'name' => 'created_at', |
75
|
|
|
'key' => 'date', |
76
|
|
|
'label' => __('Date'), |
77
|
|
|
'type' => 'date', |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
CRUD::column('client_name')->label(__('Client name')); |
81
|
|
|
CRUD::column('client_idnumber')->label(__('Client ID Number')); |
82
|
|
|
CRUD::column('client_address')->label(__('Client address')); |
83
|
|
|
CRUD::column('client_email')->label(__('Client email')); |
84
|
|
|
$this->crud->addColumn( |
85
|
|
|
array_merge([ |
86
|
|
|
'label' => __('Total price'), |
87
|
|
|
'type' => 'model_function', |
88
|
|
|
'function_name' => 'totalPrice', |
89
|
|
|
], $currency) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->crud->addColumn( |
93
|
|
|
array_merge([ |
94
|
|
|
'name' => 'balance', |
95
|
|
|
'label' => __('Remaining balance'), |
96
|
|
|
'type' => 'number', |
97
|
|
|
], $currency) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
CRUD::addFilter( |
101
|
|
|
[ |
102
|
|
|
'type' => 'date_range', |
103
|
|
|
'name' => 'from_to', |
104
|
|
|
'label'=> __('Date range'), |
105
|
|
|
], |
106
|
|
|
false, |
107
|
|
|
function ($value) { // if the filter is active, apply these constraints |
108
|
|
|
$dates = json_decode($value, null, 512, JSON_THROW_ON_ERROR); |
109
|
|
|
|
110
|
|
|
if ($dates->from) { |
111
|
|
|
CRUD::addClause('where', 'date', '>=', $dates->from); |
112
|
|
|
} |
113
|
|
|
if ($dates->to) { |
114
|
|
|
CRUD::addClause('where', 'date', '<=', $dates->to.' 23:59:59'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function setupUpdateOperation() |
121
|
|
|
{ |
122
|
|
|
CRUD::setValidation(InvoiceRequest::class); |
123
|
|
|
|
124
|
|
|
CRUD::field('date'); |
125
|
|
|
|
126
|
|
|
if (config('invoicing.invoice_numbering') === 'manual') { |
127
|
|
|
CRUD::field('receipt_number'); |
128
|
|
|
} else { |
129
|
|
|
CRUD::field('invoice_number'); |
130
|
|
|
|
131
|
|
|
CRUD::addField([ |
132
|
|
|
'name' => 'invoiceType', |
133
|
|
|
'type' => 'relationship', |
134
|
|
|
'label' => 'Type', |
135
|
|
|
'searchLogic' => false, |
136
|
|
|
'attribute' => 'name', |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
CRUD::field('client_name'); |
141
|
|
|
CRUD::field('client_idnumber'); |
142
|
|
|
CRUD::field('client_address'); |
143
|
|
|
CRUD::field('client_email'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function show($id) |
147
|
|
|
{ |
148
|
|
|
$invoice = Invoice::findOrFail($id)->load('payments'); |
149
|
|
|
|
150
|
|
|
if (! backpack_user()->can('enrollments.edit')) { |
151
|
|
|
abort(403); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return view('invoices.show', [ |
155
|
|
|
'invoice' => $invoice, |
156
|
|
|
'availablePaymentMethods' => Paymentmethod::all(), |
157
|
|
|
'editable' => true, |
158
|
|
|
'afterSuccessUrl' => $invoice->enrollments->count() > 0 ? "/enrollment/{$invoice->enrollments->first()->id}/show" : '/invoice', // TODO fix this, an invoice can theoretically contain several enrollments |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|