diff --git a/app/Http/Controllers/Kits/CheckoutKitController.php b/app/Http/Controllers/Kits/CheckoutKitController.php new file mode 100644 index 0000000000..260826d6c0 --- /dev/null +++ b/app/Http/Controllers/Kits/CheckoutKitController.php @@ -0,0 +1,78 @@ +kitService = $kitService; + } + + + /** + * Show Bulk Checkout Page + * @return View View to checkout multiple assets + */ + public function showCheckout($kit_id) + { + // METODO: добавить больше проверок, тут ещё и модель и прочее что мне надо бу + $this->authorize('checkout', Asset::class); + + $kit = PredefinedKit::findOrFail($kit_id); + return view('kits/checkout')->with('kit', $kit); + } + + /** + * Validate and process the new Predefined Kit data. + * + * @author [A. Gianotto] [] + * @since [v1.0] + * @return Redirect + */ + public function store(Request $request, $kit_id) + { + $user_id = e($request->get('user_id')); + if ( is_null($user = User::find( $user_id )) ) { + return redirect()->back()->with('error', trans('admin/users/message.user_not_found')); + } + + $kit = new PredefinedKit(); + $kit->id = $kit_id; + + $errors = $this->kitService->checkout($request, $kit, $user); + if( count($errors) > 0 ) { + return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->with('error_messages', $errors); + } + return redirect()->back()->with('success', trans('admin/hardware/message.checkout.success')); + + } +} diff --git a/app/Http/Controllers/Kits/PredefinedKitsController.php b/app/Http/Controllers/Kits/PredefinedKitsController.php index 4fd717b2fa..b13ef7d27a 100644 --- a/app/Http/Controllers/Kits/PredefinedKitsController.php +++ b/app/Http/Controllers/Kits/PredefinedKitsController.php @@ -39,8 +39,7 @@ class PredefinedKitsController extends Controller */ public function create() { - //$this->authorize('create', PredefinedKit::class); - + $this->authorize('create', PredefinedKit::class); return view('kits/create')->with('item', new PredefinedKit); } @@ -53,17 +52,15 @@ class PredefinedKitsController extends Controller */ public function store(ImageUploadRequest $request) { - //$this->authorize('create', AssetModel::class); + $this->authorize('create', PredefinedKit::class); // Create a new Predefined Kit $kit = new PredefinedKit; - - // Save the model data $kit->name = $request->input('name'); if(!$kit->save()) { return redirect()->back()->withInput()->withErrors($kit->getErrors()); } - + // METODO: удалить $model_ids = $request->input('models'); if (!is_array($model_ids)) { $model_ids = []; @@ -226,52 +223,25 @@ class PredefinedKitsController extends Controller * @return View */ public function updateModel(Request $request, $kit_id, $model_id) { - // $r = $request->all(); - // $r['__model_id'] = $model_id; - // $r['__kit_id'] = $kit_id; - // dd($r); + $this->authorize('update', PredefinedKit::class); if (is_null($kit = PredefinedKit::find($kit_id))) { // Redirect to the kits management page return redirect()->route('kits.index')->with('error','Kit does not exist'); // TODO: trans } - //return view('kits/create-model')->with('item', $kit); - - - // $quantity = $request->input('quantity', 1); - // if( $quantity < 1) { - // $quantity = 1; - // } - // $validator = \Validator::make($request->all(), $kit->modelRules); $validator = \Validator::make($request->all(), $kit->makeModelRules($model_id)); - // $pivot_id = $request->input('pivot_id'); - // $kit->models()->wherePivot('id', '!=', $pivot_id) - // ->wherePivot('id', '!=', $pivot_id) - // ->first()->pivot; - // $validator->after(function ($validator) use($kit) { - - // // if ($this->somethingElseIsInvalid()) { - // // $validator->errors()->add('field', 'Something is wrong with this field!'); - // // } - // }); + if ($validator->fails()) { return redirect()->back()->withInput()->withErrors($validator); } - // $kit->models()->sync([$request->input('model_id') => ['quantity' => $request->input('quantity')]]); - // $kit->models()->updateExistingPivot($request->input('pivot_id'), ['model_id' => $request->input('model_id'), 'quantity' => $request->input('quantity')]); - // $s = [$request->input('pivot_id') => ['model_id' => $request->input('model_id'), 'quantity' => $request->input('quantity')]]; - //dd($s); - // $changes = $kit->models()->syncWithoutDetaching([$request->input('pivot_id') => ['model_id' => $request->input('model_id'), 'quantity' => $request->input('quantity')]]); - // $changes = $kit->models()->syncWithoutDetaching(['1' => ['model_id' => '2', 'quantity' => '35']]); + $pivot = $kit->models()->wherePivot('id', $request->input('pivot_id'))->first()->pivot; - // $pivot = $kit->models()->newPivotStatement()->find('1'); - // $ret = $kit->models()->newPivotStatement()->find('1'); + $pivot->model_id = $request->input('model_id'); $pivot->quantity = $request->input('quantity'); $pivot->save(); - // return $this->edit($kit_id)->with('success', 'Model updated successfully.'); return redirect()->route('kits.edit', $kit_id)->with('success', 'Model updated successfully.'); // TODO: trans } diff --git a/app/Http/Transformers/PredefinedKitsTransformer.php b/app/Http/Transformers/PredefinedKitsTransformer.php index 9efa11fc4a..4596f3e747 100644 --- a/app/Http/Transformers/PredefinedKitsTransformer.php +++ b/app/Http/Transformers/PredefinedKitsTransformer.php @@ -30,10 +30,11 @@ class PredefinedKitsTransformer $permissions_array['available_actions'] = [ 'update' => Gate::allows('update', PredefinedKit::class), 'delete' => Gate::allows('delete', PredefinedKit::class), + 'checkout' => Gate::allows('checkout', PredefinedKit::class) ? true : false, // 'clone' => Gate::allows('create', PredefinedKit::class), // 'restore' => Gate::allows('create', PredefinedKit::class), ]; - + $array['user_can_checkout'] = true; $array += $permissions_array; return $array; } diff --git a/app/Presenters/PredefinedKitPresenter.php b/app/Presenters/PredefinedKitPresenter.php index 0a679174fc..05f86441cb 100644 --- a/app/Presenters/PredefinedKitPresenter.php +++ b/app/Presenters/PredefinedKitPresenter.php @@ -34,6 +34,16 @@ class PredefinedKitPresenter extends Presenter ] ]; + $layout[] = [ + "field" => "checkincheckout", + "searchable" => false, + "sortable" => false, + "switchable" => true, + "title" => trans('general.checkin').'/'.trans('general.checkout'), + "visible" => true, + "formatter" => "kitsInOutFormatter", + ]; + $layout[] = [ "field" => "actions", "searchable" => false, diff --git a/app/Services/PredefinedKitService.php b/app/Services/PredefinedKitService.php new file mode 100644 index 0000000000..d07f826107 --- /dev/null +++ b/app/Services/PredefinedKitService.php @@ -0,0 +1,99 @@ +models() + ->with( ['assets' => function($hasMany) { $hasMany->RTD(); }] ) + ->get(); + //$licenses = $kit->licenses()->with(['assets' => function($hasMany) { $hasMany->RTD(); }])->get(); + + // Check if the user exists + if (is_null($user) ) { + return [trans('admin/users/message.user_not_found')]; + } + + $assets_to_add = []; + $errors = []; + foreach($models as $model) { + $assets = $model->assets; + $quantity = $model->pivot->quantity; + foreach($assets as $asset) { + + if ($asset->availableForCheckout() + && !$asset->is($user)) { + + $this->authorize('checkout', $asset); + $quantity -= 1; + $assets_to_add []= $asset; + if($quantity <= 0) { + break; + } + } + } + if($quantity > 0) { + $errors []= "Don't have available assets for model " . $model->name . '. Need ' . $model->pivot->quantity . ' assets.'; // TODO: trans + } + } + + if( count($errors) > 0 ) { + return $errors; + } + + $checkout_at = date("Y-m-d H:i:s"); + if (($request->filled('checkout_at')) && ($request->get('checkout_at')!= date("Y-m-d"))) { + $checkout_at = $request->get('checkout_at'); + } + + $expected_checkin = ''; + if ($request->filled('expected_checkin')) { + $expected_checkin = $request->get('expected_checkin'); + } + + $admin = Auth::user(); + + $note = e($request->get('note')); + + $errors = DB::transaction(function () use ($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $note) { + + foreach ($assets_to_add as $asset) { + + $asset->location_id = $user->location_id; + + $error = $asset->checkOut($user, $admin, $checkout_at, $expected_checkin, $note, null); + + if ($error) { + array_merge_recursive($errors, $asset->getErrors()->toArray()); + } + } + return $errors; + }); + + return $errors; + + } catch (ModelNotFoundException $e) { + return [$e->getMessage()]; + } catch (CheckoutNotAllowed $e) { + return [$e->getMessage()]; + } + } + +} diff --git a/resources/views/kits/checkout.blade.php b/resources/views/kits/checkout.blade.php new file mode 100644 index 0000000000..d9ca25f55f --- /dev/null +++ b/resources/views/kits/checkout.blade.php @@ -0,0 +1,97 @@ +@extends('layouts/default') + +{{-- Page title --}} +@section('title') + Apply predefined kit{{-- TODO: trans --}} +@parent +@stop + +{{-- Page content --}} +@section('content') + + + + +
+ +
+
+
+

Apply predefined kit {{ $kit->name }} to user {{-- TODO: trans --}}

+
+
+
+ {{ csrf_field() }} + @include ('partials.forms.edit.user-select', ['translated_name' => trans('general.select_user'), 'fieldname' => 'user_id', 'required'=> 'true']) + + +
+ {{ Form::label('name', trans('admin/hardware/form.checkout_date'), array('class' => 'col-md-3 control-label')) }} +
+
+ + +
+ {!! $errors->first('checkout_at', ' :message') !!} +
+
+ + +
+ {{ Form::label('name', trans('admin/hardware/form.expected_checkin'), array('class' => 'col-md-3 control-label')) }} +
+
+ + +
+ {!! $errors->first('expected_checkin', ' :message') !!} +
+
+ + + +
+ {{ Form::label('note', trans('admin/hardware/form.notes'), array('class' => 'col-md-3 control-label')) }} +
+ + {!! $errors->first('note', ' :message') !!} +
+
+ +
+ +
+ +
+ + + +
+@stop + +@section('moar_scripts') +@include('partials/assets-assigned') + +@stop + +@section('notifications') +@parent + +@stop diff --git a/resources/views/notifications.blade.php b/resources/views/notifications.blade.php index dff9ab9531..4ba0bcae96 100755 --- a/resources/views/notifications.blade.php +++ b/resources/views/notifications.blade.php @@ -45,6 +45,19 @@ @endif +@if ($messages = Session::get('error_messages')) +@foreach ($messages as $message) +
+
+ + + Error: + {{ $message }} +
+
+@endforeach +@endif + @if ($message = Session::get('warning'))
diff --git a/resources/views/partials/forms/edit/kit-select.blade.php b/resources/views/partials/forms/edit/kit-select.blade.php new file mode 100644 index 0000000000..3bf251263d --- /dev/null +++ b/resources/views/partials/forms/edit/kit-select.blade.php @@ -0,0 +1,27 @@ +
+ + {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }} + +
+ +
+ +
+ @can('create', \App\Models\PredefinedKit::class) + @if ((!isset($hide_new)) || ($hide_new!='true')) + {{-- New --}} + @endif + @endcan +
+ + {!! $errors->first($fieldname, '
:message
') !!} + +
diff --git a/routes/web/kits.php b/routes/web/kits.php index 2392104682..e53c702699 100644 --- a/routes/web/kits.php +++ b/routes/web/kits.php @@ -76,4 +76,17 @@ Route::group([ 'prefix' => 'kits/{kit_id}', 'middleware' => ['auth'] ], function ] ); + Route::get('checkout', + [ + 'as' => 'kits.checkout.show', + 'uses' => 'Kits\CheckoutKitController@showCheckout', + ] + ); + + Route::post('checkout', + [ + 'as' => 'kits.checkout.store', + 'uses' => 'Kits\CheckoutKitController@store', + ] + ); }); // kits \ No newline at end of file