Skip to content

Commit 9b44dfd

Browse files
authored
Merge pull request #16297 from snipe/add_field_to_checkin_checkout
Fixed #6188 - Added custom fields to checkin/checkout screens
2 parents 917b9f0 + 9abf302 commit 9b44dfd

File tree

15 files changed

+191
-48
lines changed

15 files changed

+191
-48
lines changed

app/Http/Controllers/Assets/AssetCheckinController.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,12 @@ class AssetCheckinController extends Controller
2727
* @param string $backto
2828
* @since [v1.0]
2929
*/
30-
public function create($assetId, $backto = null) : View | RedirectResponse
30+
public function create(Asset $asset, $backto = null) : View | RedirectResponse
3131
{
32-
// Check if the asset exists
33-
if (is_null($asset = Asset::find($assetId))) {
34-
// Redirect to the asset management page with error
35-
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
36-
}
3732

3833
$this->authorize('checkin', $asset);
3934

4035
// This asset is already checked in, redirect
41-
4236
if (is_null($asset->assignedTo)) {
4337
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.already_checked_in'));
4438
}
@@ -47,7 +41,11 @@ public function create($assetId, $backto = null) : View | RedirectResponse
4741
return redirect()->route('hardware.show', $asset->id)->with('error', trans('admin/hardware/general.model_invalid_fix'));
4842
}
4943

50-
return view('hardware/checkin', compact('asset'))->with('statusLabel_list', Helper::statusLabelList())->with('backto', $backto)->with('table_name', 'Assets');
44+
return view('hardware/checkin', compact('asset'))
45+
->with('item', $asset)
46+
->with('statusLabel_list', Helper::statusLabelList())
47+
->with('backto', $backto)
48+
->with('table_name', 'Assets');
5149
}
5250

5351
/**
@@ -91,6 +89,9 @@ public function store(AssetCheckinRequest $request, $assetId = null, $backto = n
9189
$asset->status_id = e($request->get('status_id'));
9290
}
9391

92+
// Add any custom fields that should be included in the checkout
93+
$asset->customFieldsForCheckinCheckout('display_checkin');
94+
9495
$this->migrateLegacyLocations($asset);
9596

9697
$asset->location_id = $asset->rtd_location_id;
@@ -128,6 +129,9 @@ function (Builder $query) use ($asset) {
128129

129130
session()->put('redirect_option', $request->get('redirect_option'));
130131

132+
// Add any custom fields that should be included in the checkout
133+
$asset->customFieldsForCheckinCheckout('display_checkin');
134+
131135
if ($asset->save()) {
132136

133137
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));

app/Http/Controllers/Assets/AssetCheckoutController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function create(Asset $asset) : View | RedirectResponse
3939
if ($asset->availableForCheckout()) {
4040
return view('hardware/checkout', compact('asset'))
4141
->with('statusLabel_list', Helper::deployableStatusLabelList())
42-
->with('table_name', 'Assets');
42+
->with('table_name', 'Assets')
43+
->with('item', $asset);
4344
}
4445

4546
return redirect()->route('hardware.index')
@@ -88,6 +89,7 @@ public function store(AssetCheckoutRequest $request, $assetId) : RedirectRespons
8889
$asset->status_id = $request->get('status_id');
8990
}
9091

92+
9193
if(!empty($asset->licenseseats->all())){
9294
if(request('checkout_to_type') == 'user') {
9395
foreach ($asset->licenseseats as $seat){
@@ -97,23 +99,26 @@ public function store(AssetCheckoutRequest $request, $assetId) : RedirectRespons
9799
}
98100
}
99101

102+
// Add any custom fields that should be included in the checkout
103+
$asset->customFieldsForCheckinCheckout('display_checkout');
104+
100105
$settings = \App\Models\Setting::getSettings();
101106

102107
// We have to check whether $target->company_id is null here since locations don't have a company yet
103108
if (($settings->full_multiple_companies_support) && ((!is_null($target->company_id)) && (!is_null($asset->company_id)))) {
104109
if ($target->company_id != $asset->company_id){
105-
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('general.error_user_company'));
110+
return redirect()->route('hardware.checkout.create', $asset)->with('error', trans('general.error_user_company'));
106111
}
107112
}
108113

109-
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
114+
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
110115

111116
if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, $request->get('note'), $request->get('name'))) {
112117
return redirect()->to(Helper::getRedirectOption($request, $asset->id, 'Assets'))
113118
->with('success', trans('admin/hardware/message.checkout.success'));
114119
}
115120
// Redirect to the asset management page with error
116-
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors());
121+
return redirect()->route("hardware.checkout.create", $asset)->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors());
117122
} catch (ModelNotFoundException $e) {
118123
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
119124
} catch (CheckoutNotAllowed $e) {

app/Http/Controllers/CustomFieldsController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function store(CustomFieldRequest $request) : RedirectResponse
104104
"auto_add_to_fieldsets" => $request->get("auto_add_to_fieldsets", 0),
105105
"show_in_listview" => $request->get("show_in_listview", 0),
106106
"show_in_requestable_list" => $request->get("show_in_requestable_list", 0),
107+
"display_checkin" => $request->get("display_checkin", 0),
108+
"display_checkout" => $request->get("display_checkout", 0),
107109
"created_by" => auth()->id()
108110
]);
109111

@@ -246,6 +248,8 @@ public function update(CustomFieldRequest $request, CustomField $field) : Redire
246248
$field->auto_add_to_fieldsets = $request->get("auto_add_to_fieldsets", 0);
247249
$field->show_in_listview = $request->get("show_in_listview", 0);
248250
$field->show_in_requestable_list = $request->get("show_in_requestable_list", 0);
251+
$field->display_checkin = $request->get("display_checkin", 0);
252+
$field->display_checkout = $request->get("display_checkout", 0);
249253

250254
if ($request->get('format') == 'CUSTOM REGEX') {
251255
$field->format = e($request->get('custom_format'));

app/Models/Asset.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ public function validationRules()
413413
return $this->rules;
414414
}
415415

416+
public function customFieldsForCheckinCheckout($checkin_checkout) {
417+
// Check to see if any of the custom fields were included on the form and if they have any values
418+
if (($this->model) && ($this->model->fieldset) && ($this->model->fieldset->fields)) {
419+
foreach ($this->model->fieldset->fields as $field) {
420+
if (($field->{$checkin_checkout} == 1) && (request()->has($field->db_column))){
421+
$this->{$field->db_column} = request()->get($field->db_column);
422+
}
423+
}
424+
}
425+
}
426+
416427

417428
/**
418429
* Establishes the asset -> depreciation relationship

app/Models/Loggable.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function setImported(bool $bool): void
3434
*/
3535
public function logCheckout($note, $target, $action_date = null, $originalValues = [])
3636
{
37+
3738
$log = new Actionlog;
39+
40+
$fields_array = [];
41+
3842
$log = $this->determineLogItemType($log);
3943
if (auth()->user()) {
4044
$log->created_by = auth()->id();
@@ -55,6 +59,7 @@ public function logCheckout($note, $target, $action_date = null, $originalValues
5559
$log->target_type = get_class($target);
5660
$log->target_id = $target->id;
5761

62+
5863
// Figure out what the target is
5964
if ($log->target_type == Location::class) {
6065
$log->location_id = $target->id;
@@ -64,6 +69,21 @@ public function logCheckout($note, $target, $action_date = null, $originalValues
6469
$log->location_id = $target->location_id;
6570
}
6671

72+
if (static::class == Asset::class) {
73+
if ($asset = Asset::find($log->item_id)) {
74+
75+
// add the custom fields that were changed
76+
if ($asset->model->fieldset) {
77+
$fields_array = [];
78+
foreach ($asset->model->fieldset->fields as $field) {
79+
if ($field->display_checkout == 1) {
80+
$fields_array[$field->db_column] = $asset->{$field->db_column};
81+
}
82+
}
83+
}
84+
}
85+
}
86+
6787
$log->note = $note;
6888
$log->action_date = $action_date;
6989

@@ -72,7 +92,10 @@ public function logCheckout($note, $target, $action_date = null, $originalValues
7292
}
7393

7494
$changed = [];
75-
$originalValues = array_intersect_key($originalValues, array_flip(['action_date','name','status_id','location_id','expected_checkin']));
95+
$array_to_flip = array_keys($fields_array);
96+
$array_to_flip = array_merge($array_to_flip, ['action_date','name','status_id','location_id','expected_checkin']);
97+
$originalValues = array_intersect_key($originalValues, array_flip($array_to_flip));
98+
7699

77100
foreach ($originalValues as $key => $value) {
78101
if ($key == 'action_date' && $value != $action_date) {
@@ -119,6 +142,8 @@ public function logCheckin($target, $note, $action_date = null, $originalValues
119142
{
120143
$log = new Actionlog;
121144

145+
$fields_array = [];
146+
122147
if($target != null){
123148
$log->target_type = get_class($target);
124149
$log->target_id = $target->id;
@@ -135,6 +160,16 @@ public function logCheckin($target, $note, $action_date = null, $originalValues
135160
if (static::class == Asset::class) {
136161
if ($asset = Asset::find($log->item_id)) {
137162
$asset->increment('checkin_counter', 1);
163+
164+
// add the custom fields that were changed
165+
if ($asset->model->fieldset) {
166+
$fields_array = [];
167+
foreach ($asset->model->fieldset->fields as $field) {
168+
if ($field->display_checkin == 1) {
169+
$fields_array[$field->db_column] = $asset->{$field->db_column};
170+
}
171+
}
172+
}
138173
}
139174
}
140175
}
@@ -152,9 +187,14 @@ public function logCheckin($target, $note, $action_date = null, $originalValues
152187
}
153188

154189
$changed = [];
155-
$originalValues = array_intersect_key($originalValues, array_flip(['action_date','name','status_id','location_id','rtd_location_id','expected_checkin']));
190+
191+
$array_to_flip = array_keys($fields_array);
192+
$array_to_flip = array_merge($array_to_flip, ['action_date','name','status_id','location_id','expected_checkin']);
193+
194+
$originalValues = array_intersect_key($originalValues, array_flip($array_to_flip));
156195

157196
foreach ($originalValues as $key => $value) {
197+
158198
if ($key == 'action_date' && $value != $action_date) {
159199
$changed[$key]['old'] = $value;
160200
$changed[$key]['new'] = is_string($action_date) ? $action_date : $action_date->format('Y-m-d H:i:s');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('custom_fields', function (Blueprint $table) {
15+
$table->boolean('display_checkin')->default(0);
16+
$table->boolean('display_checkout')->default(0);
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('custom_fields', function (Blueprint $table) {
26+
$table->dropColumn('display_checkin');
27+
$table->dropColumn('display_checkout');
28+
});
29+
}
30+
};

resources/lang/en-US/admin/custom_fields/general.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@
5757
'show_in_requestable_list_short' => 'Show in requestable assets list',
5858
'show_in_requestable_list' => 'Show value in requestable assets list. Encrypted fields will not be shown',
5959
'encrypted_options' => 'This field is encrypted, so some display options will not be available.',
60+
'display_checkin' => 'Display in checkin forms',
61+
'display_checkout' => 'Display in checkout forms',
6062

6163
];

resources/views/custom_fields/fields/edit.blade.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,15 @@
157157

158158

159159
<!-- Auto-Add to Future Fieldsets -->
160-
<div class="col-md-9 col-md-offset-3">
160+
<div class="col-md-9 col-md-offset-3" style="padding-bottom: 10px;">
161161
<label class="form-control">
162162
<input type="checkbox" name="auto_add_to_fieldsets" aria-label="auto_add_to_fieldsets" value="1"{{ (old('auto_add_to_fieldsets') || $field->auto_add_to_fieldsets) ? ' checked="checked"' : '' }}>
163163
{{ trans('admin/custom_fields/general.auto_add_to_fieldsets') }}
164164
</label>
165165
</div>
166166

167167
<!-- Show in list view -->
168-
<div class="col-md-9 col-md-offset-3">
168+
<div class="col-md-9 col-md-offset-3" style="padding-bottom: 10px;">
169169
<label class="form-control">
170170
<input type="checkbox" name="show_in_listview" aria-label="show_in_listview" value="1"{{ (old('show_in_listview') || $field->show_in_listview) ? ' checked="checked"' : '' }}>
171171
{{ trans('admin/custom_fields/general.show_in_listview') }}
@@ -176,31 +176,49 @@
176176
@if ((!$field->id) || ($field->field_encrypted=='0'))
177177

178178
<!-- Show in requestable list view -->
179-
<div class="col-md-9 col-md-offset-3" id="show_in_requestable_list">
179+
<div class="col-md-9 col-md-offset-3" id="show_in_requestable_list" style="padding-bottom: 10px;">
180180
<label class="form-control">
181181
<input type="checkbox" name="show_in_requestable_list" aria-label="show_in_requestable_list" value="1"{{ (old('show_in_requestable_list') || $field->show_in_requestable_list) ? ' checked="checked"' : '' }}>
182182
{{ trans('admin/custom_fields/general.show_in_requestable_list') }}
183183
</label>
184184
</div>
185185

186186
<!-- Show in Email -->
187-
<div class="col-md-9 col-md-offset-3" id="show_in_email">
187+
<div class="col-md-9 col-md-offset-3" id="show_in_email" style="padding-bottom: 10px;">
188188
<label class="form-control">
189189
<input type="checkbox" name="show_in_email" aria-label="show_in_email" value="1"{{ (old('show_in_email') || $field->show_in_email) ? ' checked="checked"' : '' }}>
190190
{{ trans('admin/custom_fields/general.show_in_email') }}
191191
</label>
192192
</div>
193193

194194
<!-- Value Must be Unique -->
195-
<div class="col-md-9 col-md-offset-3" id="is_unique">
195+
<div class="col-md-9 col-md-offset-3" id="is_unique" style="padding-bottom: 10px;">
196196
<label class="form-control">
197197
<input type="checkbox" name="is_unique" aria-label="is_unique" value="1"{{ (old('is_unique') || $field->is_unique) ? ' checked="checked"' : '' }}>
198198
{{ trans('admin/custom_fields/general.is_unique') }}
199199
</label>
200200
</div>
201201
@endif
202202

203-
<!-- Show in View All Assets profile view -->
203+
204+
<!-- Show in Checkout Form -->
205+
<div class="col-md-9 col-md-offset-3" id="display_checkout" style="padding-bottom: 10px;">
206+
<label class="form-control">
207+
<input type="checkbox" name="display_checkout" aria-label="display_checkout" value="1" {{ (old('display_checkout') || $field->display_checkout) ? ' checked="checked"' : '' }}>
208+
{{ trans('admin/custom_fields/general.display_checkout') }}
209+
</label>
210+
</div>
211+
212+
<!-- Show in Checkin Form -->
213+
<div class="col-md-9 col-md-offset-3" id="display_checkin" style="padding-bottom: 10px;">
214+
<label class="form-control">
215+
<input type="checkbox" name="display_checkin" aria-label="display_checkin" value="1" {{ (old('display_checkin') || $field->display_checkin) ? ' checked="checked"' : '' }}>
216+
{{ trans('admin/custom_fields/general.display_checkin') }}
217+
</label>
218+
</div>
219+
220+
221+
<!-- Show in View All Assets profile view -->
204222
<div class="col-md-9 col-md-offset-3" id="display_in_user_view">
205223
<label class="form-control">
206224
<input type="checkbox" name="display_in_user_view" aria-label="display_in_user_view" value="1" {{ (old('display_in_user_view') || $field->display_in_user_view) ? ' checked="checked"' : '' }}>

resources/views/hardware/checkin.blade.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
{{ $asset->model->name }}
5454
@else
5555
<span class="text-danger text-bold">
56-
<x-icon type="warning" />
57-
{{ trans('admin/hardware/general.model_invalid')}}
58-
</span>
56+
<x-icon type="warning" />
57+
{{ trans('admin/hardware/general.model_invalid')}}
58+
</span>
5959
{{ trans('admin/hardware/general.model_invalid_fix')}}
6060
<a href="{{ route('hardware.edit', $asset->id) }}">
6161
<strong>{{ trans('admin/hardware/general.edit') }}</strong>
@@ -113,6 +113,18 @@
113113
</div>
114114
</div>
115115

116+
<!-- Custom fields -->
117+
@include("models/custom_fields_form", [
118+
'model' => $asset->model,
119+
'show_display_checkin_fields' => 'true'
120+
])
121+
122+
123+
124+
125+
126+
127+
116128
<!-- Note -->
117129
<div class="form-group {{ $errors->has('note') ? 'error' : '' }}">
118130
<label for="note" class="col-md-3 control-label">

resources/views/hardware/checkout.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@
136136
</div>
137137
</div>
138138

139+
<!-- Custom fields -->
140+
@include("models/custom_fields_form", [
141+
'model' => $asset->model,
142+
'show_display_checkout_fields' => 'true'
143+
])
144+
139145
<!-- Note -->
140146
<div class="form-group {{ $errors->has('note') ? 'error' : '' }}">
141147
<label for="note" class="col-md-3 control-label">

0 commit comments

Comments
 (0)