From 3ee76be7e37311fc1d835ca8350fbdb8481b6d23 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 23 Oct 2024 17:50:22 +0100 Subject: [PATCH] Added manufacturer and model_number to components Signed-off-by: snipe --- .../Controllers/Api/ComponentsController.php | 14 ++++++- .../Api/ManufacturersController.php | 3 +- .../Components/ComponentsController.php | 4 ++ .../Transformers/ComponentsTransformer.php | 2 + .../Transformers/ManufacturersTransformer.php | 1 + app/Models/Component.php | 40 ++++++++++++++++++- app/Models/Manufacturer.php | 5 +++ app/Presenters/ComponentPresenter.php | 16 +++++++- app/Presenters/ManufacturerPresenter.php | 11 ++++- ...acturer_id_model_number_to_consumables.php | 30 ++++++++++++++ resources/views/components/edit.blade.php | 2 + resources/views/manufacturers/view.blade.php | 38 +++++++++++++++++- 12 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2024_10_23_162301_add_manufacturer_id_model_number_to_consumables.php diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 561e13c9cd..8ee5b80e83 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -38,6 +38,7 @@ class ComponentsController extends Controller 'name', 'min_amt', 'order_number', + 'model_number', 'serial', 'purchase_date', 'purchase_cost', @@ -47,7 +48,7 @@ class ComponentsController extends Controller ]; $components = Component::select('components.*') - ->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser'); + ->with('company', 'location', 'category', 'assets', 'supplier', 'adminuser', 'manufacturer'); if ($request->filled('search')) { $components = $components->TextSearch($request->input('search')); @@ -69,6 +70,14 @@ class ComponentsController extends Controller $components->where('supplier_id', '=', $request->input('supplier_id')); } + if ($request->filled('manufacturer_id')) { + $components->where('manufacturer_id', '=', $request->input('manufacturer_id')); + } + + if ($request->filled('model_number')) { + $components->where('model_number', '=', $request->input('model_number')); + } + if ($request->filled('location_id')) { $components->where('location_id', '=', $request->input('location_id')); } @@ -98,6 +107,9 @@ class ComponentsController extends Controller case 'supplier': $components = $components->OrderSupplier($order); break; + case 'manufacturer': + $components = $components->OrderManufacturer($order); + break; case 'created_by': $components = $components->OrderByCreatedBy($order); break; diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index f111ef6c83..f716fbbf7f 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -60,7 +60,8 @@ class ManufacturersController extends Controller ->withCount('assets as assets_count') ->withCount('licenses as licenses_count') ->withCount('consumables as consumables_count') - ->withCount('accessories as accessories_count'); + ->withCount('accessories as accessories_count') + ->withCount('components as components_count'); if ($request->input('deleted') == 'true') { $manufacturers->onlyTrashed(); diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php index 430984767e..c4d9bcec36 100644 --- a/app/Http/Controllers/Components/ComponentsController.php +++ b/app/Http/Controllers/Components/ComponentsController.php @@ -73,6 +73,8 @@ class ComponentsController extends Controller $component->name = $request->input('name'); $component->category_id = $request->input('category_id'); $component->supplier_id = $request->input('supplier_id'); + $component->manufacturer_id = $request->input('manufacturer_id'); + $component->model_number = $request->input('model_number'); $component->location_id = $request->input('location_id'); $component->company_id = Company::getIdForCurrentUser($request->input('company_id')); $component->order_number = $request->input('order_number', null); @@ -150,6 +152,8 @@ class ComponentsController extends Controller $component->name = $request->input('name'); $component->category_id = $request->input('category_id'); $component->supplier_id = $request->input('supplier_id'); + $component->manufacturer_id = $request->input('manufacturer_id'); + $component->model_number = $request->input('model_number'); $component->location_id = $request->input('location_id'); $component->company_id = Company::getIdForCurrentUser($request->input('company_id')); $component->order_number = $request->input('order_number'); diff --git a/app/Http/Transformers/ComponentsTransformer.php b/app/Http/Transformers/ComponentsTransformer.php index 70572c9494..f98edd6e3f 100644 --- a/app/Http/Transformers/ComponentsTransformer.php +++ b/app/Http/Transformers/ComponentsTransformer.php @@ -38,6 +38,8 @@ class ComponentsTransformer 'name' => e($component->category->name), ] : null, 'supplier' => ($component->supplier) ? ['id' => $component->supplier->id, 'name'=> e($component->supplier->name)] : null, + 'manufacturer' => ($component->manufacturer) ? ['id' => $component->manufacturer->id, 'name'=> e($component->manufacturer->name)] : null, + 'model_number' => ($component->model_number) ? e($component->model_number) : null, 'order_number' => e($component->order_number), 'purchase_date' => Helper::getFormattedDateObject($component->purchase_date, 'date'), 'purchase_cost' => Helper::formatCurrencyOutput($component->purchase_cost), diff --git a/app/Http/Transformers/ManufacturersTransformer.php b/app/Http/Transformers/ManufacturersTransformer.php index e08aaa7436..d6954c1d24 100644 --- a/app/Http/Transformers/ManufacturersTransformer.php +++ b/app/Http/Transformers/ManufacturersTransformer.php @@ -36,6 +36,7 @@ class ManufacturersTransformer 'licenses_count' => (int) $manufacturer->licenses_count, 'consumables_count' => (int) $manufacturer->consumables_count, 'accessories_count' => (int) $manufacturer->accessories_count, + 'components_count' => (int) $manufacturer->components_count, 'created_by' => ($manufacturer->adminuser) ? [ 'id' => (int) $manufacturer->adminuser->id, 'name'=> e($manufacturer->adminuser->present()->fullName()), diff --git a/app/Models/Component.php b/app/Models/Component.php index 761c76f097..fb77bf0824 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -38,6 +38,7 @@ class Component extends SnipeModel 'min_amt' => 'integer|min:0|nullable', 'purchase_date' => 'date_format:Y-m-d|nullable', 'purchase_cost' => 'numeric|nullable|gte:0|max:9999999999999', + 'manufacturer_id' => 'integer|exists:manufacturers,id|nullable', ]; /** @@ -60,6 +61,8 @@ class Component extends SnipeModel 'company_id', 'supplier_id', 'location_id', + 'manufacturer_id', + 'model_number', 'name', 'purchase_cost', 'purchase_date', @@ -77,7 +80,15 @@ class Component extends SnipeModel * * @var array */ - protected $searchableAttributes = ['name', 'order_number', 'serial', 'purchase_cost', 'purchase_date', 'notes']; + protected $searchableAttributes = [ + 'name', + 'order_number', + 'serial', + 'purchase_cost', + 'purchase_date', + 'notes', + 'model_number', + ]; /** * The relations and their attributes that should be included when searching the model. @@ -89,6 +100,7 @@ class Component extends SnipeModel 'company' => ['name'], 'location' => ['name'], 'supplier' => ['name'], + 'manufacturer' => ['name'], ]; @@ -183,6 +195,19 @@ class Component extends SnipeModel return $this->belongsTo(\App\Models\Supplier::class, 'supplier_id'); } + + /** + * Establishes the item -> manufacturer relationship + * + * @author [A. Gianotto] [] + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ + public function manufacturer() + { + return $this->belongsTo(\App\Models\Manufacturer::class, 'manufacturer_id'); + } + /** * Establishes the component -> action logs relationship * @@ -311,6 +336,19 @@ class Component extends SnipeModel return $query->leftJoin('suppliers', 'components.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order); } + /** + * Query builder scope to order on manufacturer + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderManufacturer($query, $order) + { + return $query->leftJoin('manufacturers', 'components.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order); + } + public function scopeOrderByCreatedBy($query, $order) { return $query->leftJoin('users as admin_sort', 'components.created_by', '=', 'admin_sort.id')->select('components.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); diff --git a/app/Models/Manufacturer.php b/app/Models/Manufacturer.php index 6e72b3a2bb..1b31f496d3 100755 --- a/app/Models/Manufacturer.php +++ b/app/Models/Manufacturer.php @@ -78,6 +78,7 @@ class Manufacturer extends SnipeModel && (($this->licenses_count ?? $this->licenses()->count()) === 0) && (($this->consumables_count ?? $this->consumables()->count()) === 0) && (($this->accessories_count ?? $this->accessories()->count()) === 0) + && (($this->components_count ?? $this->components()->count()) === 0) && ($this->deleted_at == ''); } @@ -106,6 +107,10 @@ class Manufacturer extends SnipeModel return $this->hasMany(\App\Models\Consumable::class, 'manufacturer_id'); } + public function components() + { + return $this->hasMany(\App\Models\Component::class, 'manufacturer_id'); + } public function adminuser() { diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php index f32bb56d57..39a177592d 100644 --- a/app/Presenters/ComponentPresenter.php +++ b/app/Presenters/ComponentPresenter.php @@ -66,8 +66,20 @@ class ComponentPresenter extends Presenter 'title' => trans('general.supplier'), 'visible' => false, 'formatter' => 'suppliersLinkObjFormatter', - ], - [ + ], [ + 'field' => 'model_number', + 'searchable' => true, + 'sortable' => true, + 'title' => trans('admin/models/table.modelnumber'), + ], [ + 'field' => 'manufacturer', + 'searchable' => true, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.manufacturer'), + 'visible' => false, + 'formatter' => 'manufacturersLinkObjFormatter', + ], [ 'field' => 'qty', 'searchable' => false, 'sortable' => true, diff --git a/app/Presenters/ManufacturerPresenter.php b/app/Presenters/ManufacturerPresenter.php index ea29974f34..dfefec2998 100644 --- a/app/Presenters/ManufacturerPresenter.php +++ b/app/Presenters/ManufacturerPresenter.php @@ -124,8 +124,15 @@ class ManufacturerPresenter extends Presenter 'title' => trans('general.accessories'), 'visible' => true, 'class' => 'css-accessory', - ], - [ + ], [ + 'field' => 'components_count', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.components'), + 'visible' => true, + 'class' => 'css-component', + ], [ 'field' => 'created_by', 'searchable' => false, 'sortable' => true, diff --git a/database/migrations/2024_10_23_162301_add_manufacturer_id_model_number_to_consumables.php b/database/migrations/2024_10_23_162301_add_manufacturer_id_model_number_to_consumables.php new file mode 100644 index 0000000000..0180ac0edd --- /dev/null +++ b/database/migrations/2024_10_23_162301_add_manufacturer_id_model_number_to_consumables.php @@ -0,0 +1,30 @@ +integer('manufacturer_id')->after('purchase_cost')->nullable()->default(null); + $table->string('model_number')->after('purchase_cost')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('components', function (Blueprint $table) { + $table->dropColumn('manufacturer_id'); + $table->dropColumn('model_number'); + }); + } +}; diff --git a/resources/views/components/edit.blade.php b/resources/views/components/edit.blade.php index ce77b5130d..206418f894 100644 --- a/resources/views/components/edit.blade.php +++ b/resources/views/components/edit.blade.php @@ -20,6 +20,8 @@ @include ('partials.forms.edit.quantity') @include ('partials.forms.edit.minimum_quantity') @include ('partials.forms.edit.serial', ['fieldname' => 'serial']) +@include ('partials.forms.edit.manufacturer-select', ['translated_name' => trans('general.manufacturer'), 'fieldname' => 'manufacturer_id']) +@include ('partials.forms.edit.model_number') @include ('partials.forms.edit.company-select', ['translated_name' => trans('general.company'), 'fieldname' => 'company_id']) @include ('partials.forms.edit.location-select', ['translated_name' => trans('general.location'), 'fieldname' => 'location_id']) @include ('partials.forms.edit.supplier-select', ['translated_name' => trans('general.supplier'), 'fieldname' => 'supplier_id']) diff --git a/resources/views/manufacturers/view.blade.php b/resources/views/manufacturers/view.blade.php index 49e151e459..e900a1b010 100644 --- a/resources/views/manufacturers/view.blade.php +++ b/resources/views/manufacturers/view.blade.php @@ -79,6 +79,19 @@ {{ trans('general.consumables') }} {!! ($manufacturer->consumables->count() > 0 ) ? ''.number_format($manufacturer->consumables->count()).'' : '' !!} + + + +
  • + + + +
  • @@ -186,12 +199,35 @@ class="table table-striped snipe-table" data-url="{{ route('api.consumables.index', ['manufacturer_id' => $manufacturer->id]) }}" data-export-options='{ - "fileName": "export-manufacturers-{{ str_slug($manufacturer->name) }}-consumabled-{{ date('Y-m-d') }}", + "fileName": "export-manufacturers-{{ str_slug($manufacturer->name) }}-consumables-{{ date('Y-m-d') }}", "ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"] }'> + +
    + + +