Răsfoiți Sursa

created orders view, model and refactor controller to view

Alexander Musikhin 6 luni în urmă
părinte
comite
325d6386e4

+ 10 - 9
app/Http/Controllers/OrderController.php

@@ -13,6 +13,7 @@ use App\Models\MafOrder;
 use App\Models\ObjectType;
 use App\Models\Order;
 use App\Models\OrderStatus;
+use App\Models\OrderView;
 use App\Models\ProductSKU;
 use App\Models\Role;
 use App\Models\User;
@@ -30,16 +31,16 @@ class OrderController extends Controller
         'header'    => [
             'id'                        => 'ID',
             'name'                      => 'Название',
-            'user_id'                   => 'Менеджер',
-            'district_id'               => 'Округ',
-            'area_id'                   => 'Район',
+            'user_name'                 => 'Менеджер',
+            'district_name'             => 'Округ',
+            'area_name'                 => 'Район',
             'object_address'            => 'Адрес объекта',
-            'object_type_id'            => 'Тип объекта',
+            'object_type_name'          => 'Тип объекта',
             'comment'                   => 'Комментарий',
             'installation_date'         => 'Дата выхода на монтаж',
             'ready_date'                => 'Дата готовности площадки',
-            'brigadier_id'              => 'Бригадир',
-            'order_status_id'           => 'Статус',
+            'brigadier_name'            => 'Бригадир',
+            'order_status_name'         => 'Статус',
             'tg_group_name'             => 'Имя группы в ТГ',
             'tg_group_link'             => 'Ссылка на группу в ТГ',
             'products_with_count'       => 'МАФы',
@@ -71,9 +72,9 @@ class OrderController extends Controller
     public function index(Request $request)
     {
         session(['gp_orders' => $request->all()]);
-        $model = new Order;
+        $model = new OrderView;
         // fill filters
-        $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id', 'ready_to_mount');
+        $this->createFilters($model, 'user_name', 'district_name', 'area_name', 'object_type_name', 'brigadier_name', 'order_status_name', 'ready_to_mount');
         $this->createDateFilters($model, 'installation_date', 'ready_date');
         $this->data['ranges'] = [];
 
@@ -87,7 +88,7 @@ class OrderController extends Controller
         $this->data['orders'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
 
         foreach ($this->data['orders'] as $order) {
-            $order->recalculateReadyToMount();
+            Order::where('id', $order->id)->first()->recalculateReadyToMount();
         }
 
         return view('orders.index', $this->data);

+ 58 - 0
app/Models/OrderView.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Casts\Attribute;
+use Illuminate\Database\Eloquent\Model;
+
+class OrderView extends Model
+{
+    const DEFAULT_SORT_BY = 'created_at';
+
+
+    protected $table = 'orders_view';
+    public $appends = ['common_name', 'products_with_count'];
+
+
+    public function commonName(): Attribute
+    {
+        return Attribute::make(
+            get: fn($value) => (string) $this->object_address . ', ' . $this->area_name . ', ' . $this->district_name,
+        );
+    }
+
+    public function productsWithCount(): Attribute
+    {
+        $products = ProductSKU::query()->where('order_id', $this->id)->get(); //$this->products_sku;
+        $ret = [];
+
+        foreach ($products as $product) {
+            if(isset($ret[$product->product->id])) {
+                $ret[$product->product->id]['count'] += 1;
+            } else {
+                $ret[$product->product->id] = [
+                    'name' => $product->product->article,
+                    'count' => 1,
+                ];
+            }
+            if($product->maf_order?->order_number) {
+
+                $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
+            }
+        }
+        $s = '';
+        $openTag = '<div>';
+        $closeTag = '</div>';
+
+        foreach ($ret as $product) {
+
+            $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
+            $s .= $openTag . $product['name'] . ' - ' . $product['count'] . $order_numbers . $closeTag;
+        }
+
+        return Attribute::make(
+            get: fn($value) => (string) $s,
+        );
+    }
+
+}

+ 39 - 0
database/migrations/2025_05_29_183423_create_orders_view_table.php

@@ -0,0 +1,39 @@
+<?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
+    {
+        $sql = "CREATE OR REPLACE VIEW orders_view AS
+        SELECT o.*, u.name as user_name, 
+            d.shortname as district_name, 
+            a.name as area_name, 
+            ot.name as object_type_name, 
+            u2.name as brigadier_name, 
+            os.name as order_status_name
+          FROM orders o
+            LEFT JOIN users u ON u.id = o.user_id
+            LEFT JOIN districts d ON d.id = o.district_id
+            LEFT JOIN areas as a ON a.id = o.area_id
+            LEFT JOIN object_types ot ON ot.id = o.object_type_id
+            LEFT JOIN users u2 ON u2.id = o.brigadier_id
+            LEFT JOIN order_statuses os ON os.id = o.order_status_id
+        ";
+        DB::unprepared($sql);
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        DB::unprepared("DROP VIEW IF EXISTS orders_view");
+    }
+};