2018-10-31 06:06:38 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Models\Traits\Searchable;
|
|
|
|
use App\Presenters\Presentable;
|
2024-09-19 08:06:36 -07:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2018-11-06 08:27:28 -08:00
|
|
|
use Illuminate\Validation\Rule;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Watson\Validating\ValidatingTrait;
|
2018-10-31 06:06:38 -07:00
|
|
|
|
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* Model for predefined kits.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2019-02-23 11:44:03 -08:00
|
|
|
* @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
|
2018-10-31 06:06:38 -07:00
|
|
|
* @version v1.0
|
|
|
|
*/
|
|
|
|
class PredefinedKit extends SnipeModel
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
protected $presenter = \App\Presenters\PredefinedKitPresenter::class;
|
2024-09-19 08:06:36 -07:00
|
|
|
use HasFactory;
|
2018-10-31 06:06:38 -07:00
|
|
|
use Presentable;
|
|
|
|
protected $table = 'kits';
|
|
|
|
|
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* Category validation rules
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public $rules = [
|
|
|
|
'name' => 'required|min:1|max:255|unique',
|
|
|
|
];
|
2018-10-31 06:06:38 -07:00
|
|
|
|
|
|
|
use ValidatingTrait;
|
|
|
|
|
|
|
|
public $modelRules = [
|
|
|
|
'model_id' => 'required|exists:models,id',
|
|
|
|
'quantity' => 'required|integer|min:1',
|
2021-06-10 13:15:52 -07:00
|
|
|
'pivot_id' => 'integer|exists:kits_models,id',
|
2018-10-31 06:06:38 -07:00
|
|
|
];
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* this rules use in edit an attached asset model form
|
|
|
|
* see PredefinedKit::_makeRuleHelper function for details
|
|
|
|
* @param int $model_id
|
|
|
|
* @param bool $new = true if append a new element to kit
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function makeModelRules($model_id, $new = false)
|
2019-02-23 11:44:03 -08:00
|
|
|
{
|
|
|
|
return $this->_makeRuleHelper('models', 'kits_models', 'model_id', $model_id, $new);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* this rules use in edit an attached license form
|
|
|
|
* see PredefinedKit::_makeRuleHelper function for details
|
|
|
|
* @param int $license_id
|
|
|
|
* @param bool $new = true if append a new element to kit
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function makeLicenseRules($license_id, $new = false)
|
2019-02-23 11:44:03 -08:00
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
return $this->_makeRuleHelper('licenses', 'kits_licenses', 'license_id', $license_id, $new);
|
|
|
|
}
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* this rules use in edit an attached accessory form
|
|
|
|
* see PredefinedKit::_makeRuleHelper function for details
|
|
|
|
* @param int $accessoriy_id
|
|
|
|
* @param bool $new = true if append a new element to kit
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function makeAccessoryRules($accessory_id, $new = false)
|
2019-02-23 11:44:03 -08:00
|
|
|
{
|
|
|
|
return $this->_makeRuleHelper('accessories', 'kits_accessories', 'accessory_id', $accessory_id, $new);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
2019-02-23 11:44:03 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this rules use in edit an attached consumable form
|
|
|
|
* see PredefinedKit::_makeRuleHelper function for details
|
|
|
|
* @param int $consumable_id
|
|
|
|
* @param bool $new = true if append a new element to kit
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function makeConsumableRules($consumable_id, $new = false)
|
2019-02-23 11:44:03 -08:00
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
return $this->_makeRuleHelper('consumables', 'kits_consumables', 'consumable_id', $consumable_id, $new);
|
|
|
|
}
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* Make rules for validation kit attached elements via Illuminate\Validation\Rule
|
|
|
|
* checks:
|
|
|
|
* uniqueness of the record in table for this kit
|
|
|
|
* existence of record in table
|
|
|
|
* and simple types check
|
|
|
|
* @param string $table element table name
|
|
|
|
* @param string $pivot_table kit+element table name
|
|
|
|
* @param string $pivot_elem_key element key name inside pivot table
|
|
|
|
* @param int $element_id
|
|
|
|
* @param bool $new = true if append a new element to kit
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _makeRuleHelper($table, $pivot_table, $pivot_elem_key, $element_id, $new)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
$rule = [
|
|
|
|
$pivot_elem_key => [
|
2018-11-06 08:27:28 -08:00
|
|
|
'required',
|
2019-02-19 11:19:00 -08:00
|
|
|
"exists:$table,id",
|
2021-06-10 13:15:52 -07:00
|
|
|
Rule::unique($pivot_table)->whereNot($pivot_elem_key, $element_id)->where('kit_id', $this->id),
|
2018-11-06 08:27:28 -08:00
|
|
|
],
|
2021-06-10 13:15:52 -07:00
|
|
|
'quantity' => 'required|integer|min:1',
|
2018-11-06 08:27:28 -08:00
|
|
|
];
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $new) {
|
2019-02-19 11:19:00 -08:00
|
|
|
$rule['pivot_id'] = "integer|exists:$pivot_table,id";
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
return $rule;
|
2018-11-06 08:27:28 -08:00
|
|
|
}
|
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2021-06-10 13:15:52 -07:00
|
|
|
'name',
|
2018-10-31 06:06:38 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
use Searchable;
|
2019-02-23 11:44:03 -08:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* The attributes that should be included when searching the kit.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2018-10-31 06:06:38 -07:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $searchableAttributes = ['name'];
|
|
|
|
|
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* The relations and their attributes that should be included when searching the kit.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2018-10-31 06:06:38 -07:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $searchableRelations = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes the kits -> models relationship
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
|
|
*/
|
|
|
|
public function models()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsToMany(\App\Models\AssetModel::class, 'kits_models', 'kit_id', 'model_id')->withPivot('id', 'quantity');
|
2018-10-31 06:06:38 -07:00
|
|
|
}
|
|
|
|
|
2018-11-06 08:27:28 -08:00
|
|
|
public function assets()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->hasManyThrough(\App\Models\Asset::class, \App\Models\AssetModel::class, 'country_id', 'user_id');
|
2018-11-06 08:27:28 -08:00
|
|
|
}
|
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
/**
|
|
|
|
* Establishes the kits -> licenses relationship
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
|
|
*/
|
|
|
|
public function licenses()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsToMany(\App\Models\License::class, 'kits_licenses', 'kit_id', 'license_id')->withPivot('id', 'quantity');
|
2018-10-31 06:06:38 -07:00
|
|
|
}
|
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
/**
|
|
|
|
* Establishes the kits -> licenses relationship
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
|
|
*/
|
|
|
|
public function consumables()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsToMany(\App\Models\Consumable::class, 'kits_consumables', 'kit_id', 'consumable_id')->withPivot('id', 'quantity');
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* Establishes the kits -> licenses relationship
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
|
|
*/
|
2019-02-19 11:19:00 -08:00
|
|
|
public function accessories()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsToMany(\App\Models\Accessory::class, 'kits_accessories', 'kit_id', 'accessory_id')->withPivot('id', 'quantity');
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
2018-10-31 06:06:38 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* -----------------------------------------------
|
|
|
|
* BEGIN QUERY SCOPES
|
|
|
|
* -----------------------------------------------
|
|
|
|
**/
|
|
|
|
}
|