Skip to content

Commit

Permalink
Merge pull request #15720 from snipe/15695_adds_manufacturer_and_mode…
Browse files Browse the repository at this point in the history
…l_number_to_components

Fixed #15695 - Added manufacturer and model_number to components
  • Loading branch information
snipe authored Oct 23, 2024
2 parents d58f878 + 3ee76be commit af56493
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 8 deletions.
14 changes: 13 additions & 1 deletion app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function index(Request $request) : JsonResponse | array
'name',
'min_amt',
'order_number',
'model_number',
'serial',
'purchase_date',
'purchase_cost',
Expand All @@ -47,7 +48,7 @@ public function index(Request $request) : JsonResponse | array
];

$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'));
Expand All @@ -69,6 +70,14 @@ public function index(Request $request) : JsonResponse | array
$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'));
}
Expand Down Expand Up @@ -98,6 +107,9 @@ public function index(Request $request) : JsonResponse | array
case 'supplier':
$components = $components->OrderSupplier($order);
break;
case 'manufacturer':
$components = $components->OrderManufacturer($order);
break;
case 'created_by':
$components = $components->OrderByCreatedBy($order);
break;
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/ManufacturersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function index(Request $request) : JsonResponse | array
->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();
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Components/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function store(ImageUploadRequest $request)
$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);
Expand Down Expand Up @@ -150,6 +152,8 @@ public function update(ImageUploadRequest $request, $componentId = null)
$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');
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Transformers/ComponentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function transformComponent(Component $component)
'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),
Expand Down
1 change: 1 addition & 0 deletions app/Http/Transformers/ManufacturersTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function transformManufacturer(Manufacturer $manufacturer = null)
'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()),
Expand Down
40 changes: 39 additions & 1 deletion app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand All @@ -60,6 +61,8 @@ class Component extends SnipeModel
'company_id',
'supplier_id',
'location_id',
'manufacturer_id',
'model_number',
'name',
'purchase_cost',
'purchase_date',
Expand All @@ -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.
Expand All @@ -89,6 +100,7 @@ class Component extends SnipeModel
'company' => ['name'],
'location' => ['name'],
'supplier' => ['name'],
'manufacturer' => ['name'],
];


Expand Down Expand Up @@ -183,6 +195,19 @@ public function supplier()
return $this->belongsTo(\App\Models\Supplier::class, 'supplier_id');
}


/**
* Establishes the item -> manufacturer relationship
*
* @author [A. Gianotto] [<[email protected]>]
* @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
*
Expand Down Expand Up @@ -311,6 +336,19 @@ public function scopeOrderSupplier($query, $order)
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);
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Manufacturer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function isDeletable()
&& (($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 == '');
}

Expand Down Expand Up @@ -106,6 +107,10 @@ public function consumables()
return $this->hasMany(\App\Models\Consumable::class, 'manufacturer_id');
}

public function components()
{
return $this->hasMany(\App\Models\Component::class, 'manufacturer_id');
}

public function adminuser()
{
Expand Down
16 changes: 14 additions & 2 deletions app/Presenters/ComponentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,20 @@ public static function dataTableLayout()
'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,
Expand Down
11 changes: 9 additions & 2 deletions app/Presenters/ManufacturerPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ public static function dataTableLayout()
'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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('components', function (Blueprint $table) {
$table->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');
});
}
};
2 changes: 2 additions & 0 deletions resources/views/components/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
38 changes: 37 additions & 1 deletion resources/views/manufacturers/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@
{{ trans('general.consumables') }}
{!! ($manufacturer->consumables->count() > 0 ) ? '<badge class="badge badge-secondary">'.number_format($manufacturer->consumables->count()).'</badge>' : '' !!}
</span>
</a>
</li>

<li>
<a href="#components" data-toggle="tab">

<span class="hidden-lg hidden-md">
<x-icon type="components" class="fa-2x" />
</span>
<span class="hidden-xs hidden-sm">
{{ trans('general.components') }}
{!! ($manufacturer->components->count() > 0 ) ? '<badge class="badge badge-secondary">'.number_format($manufacturer->components->count()).'</badge>' : '' !!}
</span>

</a>
</li>
Expand Down Expand Up @@ -186,12 +199,35 @@ class="table table-striped snipe-table"
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"]
}'>
</table>

</div> <!-- /.tab-pan consumables-->

<div class="tab-pane fade" id="components">

<table
data-columns="{{ \App\Presenters\ComponentPresenter::dataTableLayout() }}"
data-cookie-id-table="componentsTable"
data-pagination="true"
data-id-table="componentsTable"
data-search="true"
data-show-footer="true"
data-side-pagination="server"
data-show-columns="true"
data-show-export="true"
data-show-refresh="true"
data-sort-order="asc"
id="componentsTable"
class="table table-striped snipe-table"
data-url="{{ route('api.components.index', ['manufacturer_id' => $manufacturer->id]) }}"
data-export-options='{
"fileName": "export-manufacturers-{{ str_slug($manufacturer->name) }}-components-{{ date('Y-m-d') }}",
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
}'>
</table>

</div> <!-- /.tab-pan consumables-->

Expand Down

0 comments on commit af56493

Please sign in to comment.