소스 검색

Убрать скоуп по году в запчастях (заказы, каталог)

- Удалить YearScope из SparePartOrder и Reservation моделей
- Сделать поле year в spare_part_orders nullable
- Убрать автоустановку года в boot() методе
- Убрать фильтр по году из SparePartOrderController
- Убрать столбец года из таблицы заказов запчастей
- Обновить миграцию для переиндексирования без года
- Обновить тесты согласно новому поведению

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Alexander Musikhin 1 일 전
부모
커밋
b9e21ecaff

+ 1 - 2
app/Http/Controllers/SparePartOrderController.php

@@ -19,7 +19,6 @@ class SparePartOrderController extends Controller
         'id' => 'spare_part_orders',
         'header' => [
             'id' => 'ID',
-            'year' => 'Год',
             'article' => 'Артикул',
             'source_text' => 'Источник заказа',
             'status_name' => 'Статус',
@@ -49,7 +48,7 @@ class SparePartOrderController extends Controller
         $model = new SparePartOrdersView();
 
         // Фильтры
-        $this->createFilters($model, 'year', 'article');
+        $this->createFilters($model, 'article');
         $this->createRangeFilters($model, 'ordered_quantity', 'available_qty');
         $this->createDateFilters($model, 'created_at');
 

+ 1 - 2
app/Models/Reservation.php

@@ -2,7 +2,6 @@
 
 namespace App\Models;
 
-use App\Models\Scopes\YearScope;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -56,7 +55,7 @@ class Reservation extends Model
 
     public function sparePartOrder(): BelongsTo
     {
-        return $this->belongsTo(SparePartOrder::class)->withoutGlobalScope(YearScope::class);
+        return $this->belongsTo(SparePartOrder::class);
     }
 
     public function reclamation(): BelongsTo

+ 0 - 8
app/Models/SparePartOrder.php

@@ -2,8 +2,6 @@
 
 namespace App\Models;
 
-use App\Models\Scopes\YearScope;
-use Illuminate\Database\Eloquent\Attributes\ScopedBy;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -22,7 +20,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * 2. in_stock - получено на склад
  * 3. shipped - полностью отгружено (available_qty = 0)
  */
-#[ScopedBy([YearScope::class])]
 class SparePartOrder extends Model
 {
     use HasFactory, SoftDeletes;
@@ -40,7 +37,6 @@ class SparePartOrder extends Model
     const DEFAULT_SORT_BY = 'created_at';
 
     protected $fillable = [
-        'year',
         'spare_part_id',
         'source_text',
         'sourceable_id',
@@ -57,7 +53,6 @@ class SparePartOrder extends Model
         'with_documents' => 'boolean',
         'ordered_quantity' => 'integer',
         'available_qty' => 'integer',
-        'year' => 'integer',
     ];
 
     protected static function boot(): void
@@ -65,9 +60,6 @@ class SparePartOrder extends Model
         parent::boot();
 
         static::creating(function ($model) {
-            if (!isset($model->year)) {
-                $model->year = year();
-            }
             if (!isset($model->available_qty)) {
                 $model->available_qty = $model->ordered_quantity;
             }

+ 0 - 1
app/Models/SparePartOrdersView.php

@@ -22,7 +22,6 @@ class SparePartOrdersView extends Model
         'with_documents' => 'boolean',
         'ordered_quantity' => 'integer',
         'available_qty' => 'integer',
-        'year' => 'integer',
     ];
 
     /**

+ 0 - 1
database/factories/SparePartOrderFactory.php

@@ -19,7 +19,6 @@ class SparePartOrderFactory extends Factory
         $quantity = fake()->numberBetween(5, 100);
 
         return [
-            'year' => (int) date('Y'),
             'spare_part_id' => SparePart::factory(),
             'source_text' => fake()->company(),
             'status' => SparePartOrder::STATUS_IN_STOCK,

+ 34 - 0
database/migrations/2026_01_28_221659_remove_year_scope_from_spare_part_orders.php

@@ -0,0 +1,34 @@
+<?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('spare_part_orders', function (Blueprint $table) {
+            // Удаляем индекс с year и recreate без него
+            $table->dropIndex(['year', 'spare_part_id', 'status']);
+            // Добавляем индекс без year
+            $table->index(['spare_part_id', 'status']);
+            // Делаем year nullable для совместимости
+            $table->unsignedInteger('year')->nullable()->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('spare_part_orders', function (Blueprint $table) {
+            $table->dropIndex(['spare_part_id', 'status']);
+            $table->index(['year', 'spare_part_id', 'status']);
+        });
+    }
+};

+ 2 - 3
tests/Unit/Models/SparePartOrderTest.php

@@ -47,7 +47,7 @@ class SparePartOrderTest extends TestCase
         $this->assertEquals(15, $order->available_qty);
     }
 
-    public function test_year_defaults_to_current_year_on_create(): void
+    public function test_year_can_be_null_on_create(): void
     {
         $sparePart = SparePart::factory()->create();
         $user = User::factory()->create();
@@ -60,7 +60,7 @@ class SparePartOrderTest extends TestCase
             'with_documents' => false,
         ]);
 
-        $this->assertEquals((int) date('Y'), $order->year);
+        $this->assertNull($order->year);
     }
 
     public function test_reserved_qty_calculates_active_reservations(): void
@@ -278,7 +278,6 @@ class SparePartOrderTest extends TestCase
         $this->assertIsBool($order->with_documents);
         $this->assertIsInt($order->ordered_quantity);
         $this->assertIsInt($order->available_qty);
-        $this->assertIsInt($order->year);
     }
 
     public function test_remaining_quantity_backward_compatibility(): void