浏览代码

edit catalog: create/update/delete

Alexander Musikhin 7 月之前
父节点
当前提交
89138cfb14

+ 27 - 2
app/Http/Controllers/ProductController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\Http\Requests\StoreProductRequest;
 use App\Jobs\Export\ExportCatalog;
 use App\Jobs\Import\ImportCatalog;
 use App\Models\Product;
@@ -19,7 +20,6 @@ class ProductController extends Controller
         'id'        => 'products',
         'header'    => [
             'id'                        => 'ID',
-//            'year'                      => 'Год',
             'article'                   => 'Артикул',
             'nomenclature_number'       => 'Номер номенклатуры',
             'manufacturer'              => 'Производитель',
@@ -64,9 +64,34 @@ class ProductController extends Controller
         return view('catalog.index', $this->data);
     }
 
-    public function show()
+    public function show(Request $request, Product $product)
     {
+        $this->data['product'] = $product;
+        return view('catalog.edit', $this->data);
+    }
+
+    public function create()
+    {
+        $this->data['product'] = null;
+        return view('catalog.edit', $this->data);
+    }
 
+    public function store(StoreProductRequest $request)
+    {
+        Product::create($request->validated());
+        return redirect()->route('catalog.index');
+    }
+
+    public function update(StoreProductRequest $request, Product $product)
+    {
+        $product->update($request->validated());
+        return redirect()->route('catalog.index');
+    }
+
+    public function delete(Product $product)
+    {
+        $product->delete();
+        return redirect()->route('catalog.index');
     }
 
     /**

+ 38 - 0
app/Http/Requests/StoreProductRequest.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class StoreProductRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return auth()->check();
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules(): array
+    {
+        return [
+            'article'               => 'required|string',
+            'name_tz'               => 'required|string',
+            'type_tz'               => 'required|string',
+            'nomenclature_number'   => 'required|string',
+            'sizes'                 => 'required|string',
+            'type'                  => 'required|string',
+            'product_price'         => 'required|numeric',
+            'installation_price'    => 'required|numeric',
+            'total_price'           => 'required|numeric',
+            'manufacturer_name'     => 'required|string',
+            'note'                  => 'required|string',
+        ];
+    }
+}

+ 0 - 2
app/Models/Product.php

@@ -25,8 +25,6 @@ class Product extends Model
         'type_tz',
         'nomenclature_number',
         'sizes',
-        'manufacturer',
-        'unit',
         'type',
         'product_price',
         'installation_price',

+ 29 - 0
database/migrations/2025_04_23_201012_remove_fields_from_products_table.php

@@ -0,0 +1,29 @@
+<?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('products', function (Blueprint $table) {
+            $table->dropColumn(['manufacturer', 'unit']);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('products', function (Blueprint $table) {
+            $table->string('manufacturer')->after('sizes');
+            $table->string('unit')->after('manufacturer');
+        });
+    }
+};

+ 38 - 0
resources/views/catalog/edit.blade.php

@@ -0,0 +1,38 @@
+@extends('layouts.app')
+
+@section('content')
+    <div class="px-3">
+        <div class="col-xxl-6 offset-xxl-2">
+            <form action="{{ ($product) ? route('catalog.update', $product) : route('catalog.store') }}" method="post">
+
+                @csrf
+
+                @include('partials.input', ['name' => 'article', 'title' => 'Артикул', 'required' => true, 'value' => $product->article ?? ''])
+                @include('partials.input', ['name' => 'nomenclature_number', 'title' => 'Номер номенклатуры', 'required' => true, 'value' => $product->nomenclature_number ?? ''])
+                @include('partials.input', ['name' => 'name_tz', 'title' => 'Наименование по ТЗ', 'required' => true, 'value' => $product->name_tz ?? ''])
+                @include('partials.input', ['name' => 'type_tz', 'title' => 'Тип по ТЗ', 'required' => true, 'value' => $product->type_tz ?? ''])
+                @include('partials.input', ['name' => 'type', 'title' => 'Тип', 'required' => true, 'value' => $product->type ?? ''])
+                @include('partials.input', ['name' => 'manufacturer_name', 'title' => 'Наименование производителя', 'required' => true, 'value' => $product->manufacturer_name ?? ''])
+                @include('partials.input', ['name' => 'sizes', 'title' => 'Размеры', 'required' => true, 'value' => $product->sizes ?? ''])
+                @include('partials.input', ['name' => 'product_price', 'type' => 'number', 'title' => 'Цена товара', 'required' => true, 'value' => $product->product_price ?? ''])
+                @include('partials.input', ['name' => 'installation_price', 'type' => 'number', 'title' => 'Цена установки', 'required' => true, 'value' => $product->installation_price ?? ''])
+                @include('partials.input', ['name' => 'total_price', 'type' => 'number', 'title' => 'Итоговая цена', 'required' => true, 'value' => $product->total_price ?? ''])
+                @include('partials.textarea', ['name' => 'note', 'size' => 10, 'title' => 'Примечания', 'required' => true, 'value' => $product->note ?? ''])
+
+
+                @include('partials.submit', ['deleteDisabled' => (!isset($product)), 'delete' => ['form_id' => 'deleteProduct']])
+            </form>
+            @if($product)
+                <div class="visually-hidden">
+                    <form action="{{ route('catalog.delete', $product) }}" method="POST" id="deleteProduct">
+                        @csrf
+                        @method('DELETE')
+                    </form>
+                </div>
+            @endif
+        </div>
+    </div>
+    @if($errors->any())
+        @dump($errors)
+    @endif
+@endsection

+ 3 - 1
resources/views/catalog/index.blade.php

@@ -13,6 +13,7 @@
             <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exportModal">
                 Экспорт
             </button>
+            <a href="{{ route('catalog.create') }}" class="btn btn-primary">Добавить</a>
 
         </div>
     </div>
@@ -21,7 +22,8 @@
     @include('partials.table', [
         'id'        => $id,
         'header'    => $header,
-        'strings'   => $products
+        'strings'   => $products,
+        'routeName' => 'catalog.show',
     ])
 
     <div class="row pt-3 px-3">

+ 1 - 1
resources/views/partials/textarea.blade.php

@@ -4,7 +4,7 @@
         @isset($required) <sup>*</sup> @endisset
     </label>
     <div class="col-md-8">
-        <textarea type="{{ $type ?? 'text' }}" name="{{ $name }}" id="{{ $name }}"
+        <textarea type="{{ $type ?? 'text' }}" name="{{ $name }}" id="{{ $name }}" @if($size) rows="{{ $size }}" @endif
                class="form-control @error($name) is-invalid @enderror" @disabled($disabled ?? null) @required($required ?? null)
         >{{ old($name, $value ?? '') }}</textarea>
         @error($name)

+ 6 - 2
routes/web.php

@@ -56,7 +56,12 @@ Route::middleware('auth:web')->group(function () {
 
     // catalog
     Route::get('catalog', [ProductController::class, 'index'])->name('catalog.index');
+    Route::get('catalog/create', [ProductController::class, 'create'])->name('catalog.create');
     Route::get('catalog/{product}', [ProductController::class, 'show'])->name('catalog.show');
+    Route::post('catalog', [ProductController::class, 'store'])->name('catalog.store');
+    Route::post('catalog/{product}', [ProductController::class, 'update'])->name('catalog.update');
+    Route::delete('catalog/{product}', [ProductController::class, 'delete'])->name('catalog.delete');
+
     Route::post('catalog/import', [ProductController::class, 'import'])->name('catalog.import');
     Route::post('catalog/export', [ProductController::class, 'export'])->name('catalog.export');
 
@@ -86,10 +91,9 @@ Route::middleware('auth:web')->group(function () {
     // Склад (МАФ)
     Route::get('product_sku', [ProductSKUController::class, 'index'])->name('product_sku.index');
     Route::get('product_sku/{product_sku}', [ProductSKUController::class, 'show'])->name('product_sku.show');
-//    Route::post('product_sku/store', [ProductSKUController::class, 'store'])->name('product_sku.store');
     Route::post('product_sku/update/{product_sku}', [ProductSKUController::class, 'update'])->name('product_sku.update');
 
-   // Склад заказы МАФ
+    // Склад заказы МАФ
     Route::get('maf_orders', [MafOrderController::class, 'index'])->name('maf_order.index');
     Route::get('maf_orders/{maf_order}', [MafOrderController::class, 'show'])->name('maf_order.show');
     Route::post('maf_orders/store', [MafOrderController::class, 'store'])->name('maf_order.store');