Эх сурвалжийг харах

added segregation by year

Alexander Musikhin 7 сар өмнө
parent
commit
156c9bd2b1

+ 9 - 7
app/Http/Controllers/Controller.php

@@ -70,13 +70,15 @@ class Controller extends BaseController
     protected function createFilters(Model $model, string ...$columns): void
     protected function createFilters(Model $model, string ...$columns): void
     {
     {
         foreach ($columns as $column) {
         foreach ($columns as $column) {
-            $uniqueValues =  DB::table(($model)->getTable())
-                ->select($column)
-                ->distinct()
-                ->whereNull('deleted_at')
-                ->get()
-                ->pluck($column)
-                ->toArray();
+//            $uniqueValues =  DB::table(($model)->getTable())
+//                ->select($column)
+//                ->distinct()
+//                ->whereNull('deleted_at')
+//                ->get()
+//                ->pluck($column)
+//                ->toArray();
+            $uniqueValues = $model::query()->distinct()->get($column)->pluck($column)->toArray();
+
             $result = [];
             $result = [];
             foreach ($uniqueValues as $val){
             foreach ($uniqueValues as $val){
                 if(str_ends_with($column, '_id')) {
                 if(str_ends_with($column, '_id')) {

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

@@ -19,7 +19,7 @@ class ProductController extends Controller
         'id'        => 'products',
         'id'        => 'products',
         'header'    => [
         'header'    => [
             'id'                        => 'ID',
             'id'                        => 'ID',
-            'year'                      => 'Год',
+//            'year'                      => 'Год',
             'article'                   => 'Артикул',
             'article'                   => 'Артикул',
             'nomenclature_number'       => 'Номер номенклатуры',
             'nomenclature_number'       => 'Номер номенклатуры',
             'manufacturer'              => 'Производитель',
             'manufacturer'              => 'Производитель',
@@ -77,7 +77,6 @@ class ProductController extends Controller
     {
     {
         // validate data
         // validate data
         $request->validate([
         $request->validate([
-            'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
             'import_file' => 'file',
             'import_file' => 'file',
         ]);
         ]);
 
 
@@ -86,7 +85,7 @@ class ProductController extends Controller
         Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
         Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
 
 
         // dispatch job
         // dispatch job
-        ImportCatalog::dispatch($path, $request->year, $request->user()->id);
+        ImportCatalog::dispatch($path, year(), $request->user()->id);
         Log::info('ImportCatalog job created!');
         Log::info('ImportCatalog job created!');
         return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
         return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
     }
     }

+ 18 - 0
app/Models/MafOrder.php

@@ -2,14 +2,31 @@
 
 
 namespace App\Models;
 namespace App\Models;
 
 
+use App\Models\Scopes\YearScope;
+use Illuminate\Database\Eloquent\Attributes\ScopedBy;
+use Illuminate\Database\Eloquent\Casts\Attribute;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Database\Eloquent\SoftDeletes;
 
 
+#[ScopedBy([YearScope::class])]
 class MafOrder extends Model
 class MafOrder extends Model
 {
 {
     use SoftDeletes;
     use SoftDeletes;
+
+// set year attribute to current selected year
+    protected static function boot(): void
+    {
+        parent::boot();
+        static::creating(function($attributes) {
+            if(!isset($attributes->year)) {
+                $attributes->year = year();
+            }
+        });
+    }
+
     protected $fillable = [
     protected $fillable = [
+        'year',
         'order_number',
         'order_number',
         'user_id',
         'user_id',
         'product_id',
         'product_id',
@@ -28,4 +45,5 @@ class MafOrder extends Model
     {
     {
         return $this->belongsTo(Product::class);
         return $this->belongsTo(Product::class);
     }
     }
+
 }
 }

+ 15 - 0
app/Models/Order.php

@@ -5,6 +5,8 @@ namespace App\Models;
 use App\Helpers\Price;
 use App\Helpers\Price;
 use App\Models\Dictionary\Area;
 use App\Models\Dictionary\Area;
 use App\Models\Dictionary\District;
 use App\Models\Dictionary\District;
+use App\Models\Scopes\YearScope;
+use Illuminate\Database\Eloquent\Attributes\ScopedBy;
 use Illuminate\Database\Eloquent\Casts\Attribute;
 use Illuminate\Database\Eloquent\Casts\Attribute;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -14,6 +16,7 @@ use Illuminate\Database\Eloquent\Relations\HasManyThrough;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\DB;
 
 
+#[ScopedBy([YearScope::class])]
 class Order extends Model
 class Order extends Model
 {
 {
     use SoftDeletes;
     use SoftDeletes;
@@ -24,7 +27,19 @@ class Order extends Model
     const STATUS_READY_TO_HAND_OVER = 3;
     const STATUS_READY_TO_HAND_OVER = 3;
     const STATUS_HANDED_OVER = 4;
     const STATUS_HANDED_OVER = 4;
 
 
+    // set year attribute to current selected year
+    protected static function boot(): void
+    {
+        parent::boot();
+        static::creating(function($attributes) {
+            if(!isset($attributes->year)) {
+                $attributes->year = year();
+            }
+        });
+    }
+
     protected $fillable = [
     protected $fillable = [
+        'year',
         'user_id',
         'user_id',
         'district_id',
         'district_id',
         'area_id',
         'area_id',

+ 11 - 0
app/Models/Product.php

@@ -35,6 +35,17 @@ class Product extends Model
         'note',
         'note',
     ];
     ];
 
 
+    // set year attribute to current selected year
+    protected static function boot(): void
+    {
+        parent::boot();
+        static::creating(function($attributes) {
+            if(!isset($attributes->year)) {
+                $attributes->year = year();
+            }
+        });
+    }
+
     protected $appends = ['product_price', 'installation_price', 'total_price',
     protected $appends = ['product_price', 'installation_price', 'total_price',
                           'product_price_txt', 'installation_price_txt', 'total_price_txt',];
                           'product_price_txt', 'installation_price_txt', 'total_price_txt',];
     protected function productPrice(): Attribute
     protected function productPrice(): Attribute

+ 15 - 0
app/Models/ProductSKU.php

@@ -2,16 +2,20 @@
 
 
 namespace App\Models;
 namespace App\Models;
 
 
+use App\Models\Scopes\YearScope;
+use Illuminate\Database\Eloquent\Attributes\ScopedBy;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Database\Eloquent\SoftDeletes;
 
 
+#[ScopedBy([YearScope::class])]
 class ProductSKU extends Model
 class ProductSKU extends Model
 {
 {
     use SoftDeletes;
     use SoftDeletes;
 
 
     const DEFAULT_SORT_BY = 'created_at';
     const DEFAULT_SORT_BY = 'created_at';
     protected $fillable = [
     protected $fillable = [
+        'year',
         'product_id',
         'product_id',
         'order_id',
         'order_id',
         'maf_order_id',
         'maf_order_id',
@@ -27,6 +31,17 @@ class ProductSKU extends Model
     ];
     ];
     public $table = 'products_sku';
     public $table = 'products_sku';
 
 
+    // set year attribute to current selected year
+    protected static function boot(): void
+    {
+        parent::boot();
+        static::creating(function($attributes) {
+            if(!isset($attributes->year)) {
+                $attributes->year = year();
+            }
+        });
+    }
+
     /**
     /**
      * @return BelongsTo
      * @return BelongsTo
      */
      */

+ 1 - 1
app/Models/Scopes/YearScope.php

@@ -13,6 +13,6 @@ class YearScope implements Scope
      */
      */
     public function apply(Builder $builder, Model $model): void
     public function apply(Builder $builder, Model $model): void
     {
     {
-        $builder->where('year', year());
+        $builder->where($model->getTable() . '.year', year());
     }
     }
 }
 }

+ 1 - 0
database/migrations/2025_03_24_153700_create_orders_table.php

@@ -13,6 +13,7 @@ return new class extends Migration
     {
     {
         Schema::create('orders', function (Blueprint $table) {
         Schema::create('orders', function (Blueprint $table) {
             $table->id();
             $table->id();
+            $table->unsignedInteger('year');
             $table->foreignId('user_id')->constrained('users')->restrictOnDelete();                 // менеджер
             $table->foreignId('user_id')->constrained('users')->restrictOnDelete();                 // менеджер
             $table->foreignId('district_id')->constrained('districts')->restrictOnDelete();         // округ
             $table->foreignId('district_id')->constrained('districts')->restrictOnDelete();         // округ
             $table->foreignId('area_id')->constrained('areas')->restrictOnDelete();                 // район
             $table->foreignId('area_id')->constrained('areas')->restrictOnDelete();                 // район

+ 1 - 0
database/migrations/2025_04_05_231749_create_maf_orders_table.php

@@ -13,6 +13,7 @@ return new class extends Migration
     {
     {
         Schema::create('maf_orders', function (Blueprint $table) {
         Schema::create('maf_orders', function (Blueprint $table) {
             $table->id();
             $table->id();
+            $table->unsignedInteger('year');
             $table->string('order_number')->nullable();
             $table->string('order_number')->nullable();
             $table->foreignId('user_id')->constrained('users');
             $table->foreignId('user_id')->constrained('users');
             $table->foreignId('product_id')->constrained('products');
             $table->foreignId('product_id')->constrained('products');

+ 1 - 0
database/migrations/2025_04_11_145335_create_products_sku_table.php

@@ -15,6 +15,7 @@ return new class extends Migration
     {
     {
         Schema::create('products_sku', function (Blueprint $table) {
         Schema::create('products_sku', function (Blueprint $table) {
             $table->id();
             $table->id();
+            $table->unsignedInteger('year');
             $table->foreignId('product_id')                             // id продукта
             $table->foreignId('product_id')                             // id продукта
                 ->constrained('products')
                 ->constrained('products')
                 ->restrictOnDelete();
                 ->restrictOnDelete();

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

@@ -42,7 +42,6 @@
                 <div class="modal-body">
                 <div class="modal-body">
                     <form action="{{ route('catalog.import') }}" method="post" enctype="multipart/form-data">
                     <form action="{{ route('catalog.import') }}" method="post" enctype="multipart/form-data">
                         @csrf
                         @csrf
-                        @include('partials.input', ['title' => 'Год', 'name' => 'year', 'type' => 'number', 'min' => 2000, 'max' => (int) date('Y') + 1, 'value' => date('Y')])
                         @include('partials.input', ['title' => 'XLSX файл', 'name' => 'import_file', 'type' => 'file'])
                         @include('partials.input', ['title' => 'XLSX файл', 'name' => 'import_file', 'type' => 'file'])
                         @include('partials.submit', ['name' => 'Импорт'])
                         @include('partials.submit', ['name' => 'Импорт'])
                     </form>
                     </form>