Skip to content

A free open source IT asset/license management system

Full kits manipulation without users intagration

Minaev Dmitriy79d979f47f2b
diff --git a/app/Http/Controllers/Api/PredefinedKitsController.php b/app/Http/Controllers/Api/PredefinedKitsController.php
index 03740bfc33..e72966ec82 100644
--- a/app/Http/Controllers/Api/PredefinedKitsController.php
+++ b/app/Http/Controllers/Api/PredefinedKitsController.php
@@ -202,7 +202,7 @@ class PredefinedKitsController extends Controller
}
$kit->licenses()->sync([$license_id => ['quantity' => $quantity]]);
- return response()->json(Helper::formatStandardApiResponse('success', null, 'License updated')); // TODO: trans
+ return response()->json(Helper::formatStandardApiResponse('success', $kit, 'License updated')); // TODO: trans
}
/**
@@ -213,13 +213,13 @@ class PredefinedKitsController extends Controller
* @param int $kit_id
* @return \Illuminate\Http\Response
*/
- public function destroyLicense($kit_id, $license_id)
+ public function detachLicense($kit_id, $license_id)
{
$this->authorize('update', PredefinedKit::class);
$kit = PredefinedKit::findOrFail($id);
$kit->licenses()->detach($license_id);
- return response()->json(Helper::formatStandardApiResponse('success', null, 'Delete was successfull')); // TODO: trans
+ return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Delete was successfull')); // TODO: trans
}
/**
@@ -249,18 +249,23 @@ class PredefinedKitsController extends Controller
{
//return response()->json(Helper::formatStandardApiResponse('error', 'string11', dd($request))); // TODO: trans
- $this->authorize('update', PredefinedKit::class);
-
- $kit = PredefinedKit::findOrFail($kit_id);
-
- $quantity = $request->input('quantity', 1);
- if( $quantity < 1) {
- $quantity = 1;
- }
- //echo $request->get('model');
- $kit->models()->attach( $request->get('model'), ['quantity' => $quantity]);
-
- return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Model added successfull')); // TODO: trans
+ $this->authorize('update', PredefinedKit::class);
+
+ $kit = PredefinedKit::findOrFail($kit_id);
+
+ $model_id = $request->get('model');
+ $quantity = $request->input('quantity', 1);
+ if( $quantity < 1) {
+ $quantity = 1;
+ }
+ //echo $request->get('model');
+ $relation = $kit->models();
+ if( $relation->find($model_id) ) {
+ return response()->json(Helper::formatStandardApiResponse('error', null, 'Model already exists'));
+ }
+ $relation->attach($model_id, ['quantity' => $quantity]);
+
+ return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Model added successfull')); // TODO: trans
}
/**
@@ -280,9 +285,9 @@ class PredefinedKitsController extends Controller
if( $quantity < 1) {
$quantity = 1;
}
- $kit->models()->sync([$model_id => ['quantity' => $quantity]]);
+ $kit->models()->syncWithoutDetaching([$model_id => ['quantity' => $quantity]]);
- return response()->json(Helper::formatStandardApiResponse('success', null, 'License updated')); // TODO: trans
+ return response()->json(Helper::formatStandardApiResponse('success', $kit, 'License updated')); // TODO: trans
}
/**
@@ -293,12 +298,12 @@ class PredefinedKitsController extends Controller
* @param int $kit_id
* @return \Illuminate\Http\Response
*/
- public function destroyModel($kit_id, $model_id)
+ public function detachModel($kit_id, $model_id)
{
$this->authorize('update', PredefinedKit::class);
$kit = PredefinedKit::findOrFail($id);
$kit->models()->detach($model_id);
- return response()->json(Helper::formatStandardApiResponse('success', null, 'Delete was successfull')); // TODO: trans
+ return response()->json(Helper::formatStandardApiResponse('success', $kit, 'Delete was successfull')); // TODO: trans
}
}
diff --git a/app/Http/Controllers/Kits/PredefinedKitsController.php b/app/Http/Controllers/Kits/PredefinedKitsController.php
index 93c64553f7..4fd717b2fa 100644
--- a/app/Http/Controllers/Kits/PredefinedKitsController.php
+++ b/app/Http/Controllers/Kits/PredefinedKitsController.php
@@ -156,8 +156,8 @@ class PredefinedKitsController extends Controller
}
// Delete childs
- $kit->models()->delete();
- $kit->licenses()->delete();
+ $kit->models()->detach();
+ $kit->licenses()->detach();
// Delete the kit
$kit->delete();
@@ -225,7 +225,11 @@ class PredefinedKitsController extends Controller
* @param int $modelId
* @return View
*/
- public function updateModel(Request $request, $kit_id) {
+ 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
@@ -239,7 +243,18 @@ class PredefinedKitsController extends Controller
// $quantity = 1;
// }
- $validator = \Validator::make($request->all(), $kit->modelRules);
+ // $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);
}
@@ -279,7 +294,7 @@ class PredefinedKitsController extends Controller
$kit->models()->detach($model_id);
// Redirect to the kit management page
- return redirect()->route('kits.index')->with('success', 'Kit was successfully deleted'); // TODO: trans
+ return redirect()->route('kits.edit', $kit_id)->with('success', 'Model was successfully detached'); // TODO: trans
}
diff --git a/app/Models/PredefinedKit.php b/app/Models/PredefinedKit.php
index 5f2b46f4c2..7564bafab5 100644
--- a/app/Models/PredefinedKit.php
+++ b/app/Models/PredefinedKit.php
@@ -8,6 +8,7 @@ use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
+use Illuminate\Validation\Rule;
/**
* Model for Categories. Categories are a higher-level group
@@ -34,10 +35,28 @@ class PredefinedKit extends SnipeModel
public $modelRules = [
'model_id' => 'required|exists:models,id',
+ // 'model_id' => [
+ // 'required',
+ // 'exists:models,id',
+ // Rule::unique('kits_models')->where('model_id', $model_id)->whereNot('kit_id', $this->id)
+ // ],
'quantity' => 'required|integer|min:1',
'pivot_id' => 'integer|exists:kits_models,id'
];
+ public function makeModelRules($model_id) {
+ return [
+ // 'model_id' => 'required|exists:models,id',
+ 'model_id' => [
+ 'required',
+ 'exists:models,id',
+ Rule::unique('kits_models')->whereNot('model_id', $model_id)->where('kit_id', $this->id)
+ ],
+ 'quantity' => 'required|integer|min:1',
+ 'pivot_id' => 'integer|exists:kits_models,id'
+ ];
+ }
+
public $licenseRules = [
'license_id' => 'required|exists:licenses,id',
'quantity' => 'required|integer|min:1',
@@ -83,6 +102,11 @@ class PredefinedKit extends SnipeModel
return $this->belongsToMany('\App\Models\AssetModel', 'kits_models', 'kit_id', 'model_id')->withPivot('id', 'quantity');
}
+ public function assets()
+ {
+ return $this->hasManyThrough('\App\Models\Asset', '\App\Models\AssetModel', 'country_id', 'user_id');
+ }
+
/**
* Establishes the kits -> licenses relationship
*
@@ -96,6 +120,9 @@ class PredefinedKit extends SnipeModel
}
+ public function applyToUser(User $user) {
+ $models = $this->models();
+ }
/**
* -----------------------------------------------
diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php
index 02bf3dc072..0ccda1ea3b 100644
--- a/app/Providers/RouteServiceProvider.php
+++ b/app/Providers/RouteServiceProvider.php
@@ -64,6 +64,7 @@ class RouteServiceProvider extends ServiceProvider
require base_path('routes/web/components.php');
require base_path('routes/web/users.php');
require base_path('routes/web/kits.php');
+ require base_path('routes/web/dbtest.php');
require base_path('routes/web.php');
});
}
diff --git a/routes/api.php b/routes/api.php
index f5300b1e38..cf7f749612 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -794,7 +794,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
Route::delete('licenses/{license_id}',
[
'as' => 'api.kits.licenses.destroy',
- 'uses' => 'PredefinedKitsController@destroyLicense',
+ 'uses' => 'PredefinedKitsController@detachLicense',
]
);
@@ -823,7 +823,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
Route::delete('models/{model_id}',
[
'as' => 'api.kits.models.destroy',
- 'uses' => 'PredefinedKitsController@destroyModel',
+ 'uses' => 'PredefinedKitsController@detachModel',
]
);
diff --git a/routes/web/kits.php b/routes/web/kits.php
index 204af83f84..2392104682 100644
--- a/routes/web/kits.php
+++ b/routes/web/kits.php
@@ -57,6 +57,7 @@ Route::group([ 'prefix' => 'kits/{kit_id}', 'middleware' => ['auth'] ], function
[
'as' => 'kits.models.update',
'uses' => 'Kits\PredefinedKitsController@updateModel',
+ 'parameters' => [2 => 'kit_id', 1 => 'model_id']
]
);
@@ -64,6 +65,7 @@ Route::group([ 'prefix' => 'kits/{kit_id}', 'middleware' => ['auth'] ], function
[
'as' => 'kits.models.edit',
'uses' => 'Kits\PredefinedKitsController@editModel',
+
]
);