Kaynağa Gözat

pagination, remove orders, short maf list in order list

Alexander Musikhin 7 ay önce
ebeveyn
işleme
54c0d23b0e

+ 3 - 0
.env.example

@@ -44,3 +44,6 @@ REDIS_PASSWORD=sOmE_sEcUrE_pAsS
 REDIS_PORT=6379
 
 JWT_SECRET=c05c4346dc03362dabbf94c18a1befe78e9b301837f3163e43a57201d9cc09cb
+
+# Default pagination limit
+PAGINATION_LIMIT=2000

+ 1 - 1
app/Http/Controllers/MafOrderController.php

@@ -47,7 +47,7 @@ class MafOrderController extends Controller
 
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
 
-        $this->data['maf_orders'] = $q->paginate()->withQueryString();
+        $this->data['maf_orders'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
         return view('maf_orders.index', $this->data);
     }
 

+ 13 - 2
app/Http/Controllers/OrderController.php

@@ -36,7 +36,7 @@ class OrderController extends Controller
             'order_status_id'           => 'Статус',
             'tg_group_name'             => 'Имя группы в ТГ',
             'tg_group_link'             => 'Ссылка на группу в ТГ',
-            'products-common_name'      => 'МАФы',
+            'products_with_count'       => 'МАФы',
             'ready_to_mount'            => 'Все МАФы на складе',
         ],
         'searchFields' => [
@@ -77,7 +77,7 @@ class OrderController extends Controller
         $this->setSortAndOrderBy($model, $request);
 
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
-        $this->data['orders'] = $q->paginate()->withQueryString();
+        $this->data['orders'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
 
         foreach ($this->data['orders'] as $order) {
             $order->recalculateReadyToMount();
@@ -172,4 +172,15 @@ class OrderController extends Controller
         $order->update(['order_status_id' => Order::STATUS_READY_TO_MOUNT]);
         return redirect()->route('order.show', $order);
     }
+
+    /**
+     * @param Order $order
+     * @return RedirectResponse
+     */
+    public function destroy(Order $order)
+    {
+        Order::query()->where('id', $order->id)->delete();
+        ProductSKU::query()->where('order_id', $order->id)->delete();
+        return redirect()->route('order.index');
+    }
 }

+ 1 - 1
app/Http/Controllers/ProductController.php

@@ -60,7 +60,7 @@ class ProductController extends Controller
         $this->setSortAndOrderBy($model, $request);
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
 
-        $this->data['products'] = $q->paginate()->withQueryString();
+        $this->data['products'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
         return view('catalog.index', $this->data);
     }
 

+ 1 - 1
app/Http/Controllers/ProductSKUController.php

@@ -67,7 +67,7 @@ class ProductSKUController extends Controller
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
 
 //        dump($q->toRawSql());
-        $this->data['products_sku'] = $q->paginate()->withQueryString();
+        $this->data['products_sku'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
         return view('products_sku.index', $this->data);
     }
 

+ 1 - 1
app/Http/Controllers/ResponsibleController.php

@@ -46,7 +46,7 @@ class ResponsibleController extends Controller
         $this->setSortAndOrderBy($model, $request);
 
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
-        $this->data['responsibles'] = $q->paginate()->withQueryString();
+        $this->data['responsibles'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
 
         return view('responsibles.index', $this->data);
     }

+ 1 - 1
app/Http/Controllers/UserController.php

@@ -48,7 +48,7 @@ class UserController extends Controller
         $this->setSortAndOrderBy($model, $request);
 
         $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
-        $this->data['users'] = $q->paginate()->withQueryString();
+        $this->data['users'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
 
         return view('users.index', $this->data);
     }

+ 35 - 6
app/Models/Order.php

@@ -41,7 +41,7 @@ class Order extends Model
         'ready_to_mount',
     ];
 
-    public $appends = ['common_name'];
+    public $appends = ['common_name', 'products_with_count'];
 
     public function products_sku(): HasMany
     {
@@ -50,13 +50,15 @@ class Order extends Model
 
     public function products(): HasManyThrough
     {
-        return $this->hasManyThrough(Product::class, ProductSKU::class, 'order_id', 'id');
+        return $this->hasManyThrough(
+            Product::class,
+            ProductSKU::class,
+            'order_id',
+            'id',
+            'id',
+            'product_id');
     }
 
-    public function maf_orders(): BelongsTo
-    {
-        return $this->belongsTo(MafOrder::class, 'maf_order_id');
-    }
 
     public function user(): BelongsTo
     {
@@ -128,5 +130,32 @@ class Order extends Model
             get: fn($value) => (string) $this->district->shortname . ', ' . $this->area->name . ', ' . $this->object_address,
         );
     }
+
+    public function productsWithCount(): Attribute
+    {
+        $products = $this->products;
+        $ret = [];
+
+        foreach ($products as $product) {
+            if(isset($ret[$product->id])) {
+                $ret[$product->id]['count'] += 1;
+            } else {
+                $ret[$product->id] = [
+                    'name' => $product->common_name,
+                    'count' => 1,
+                ];
+            }
+        }
+        $s = '';
+        foreach ($ret as $product) {
+            $s .= $product['name'] . ' (' . $product['count'] . ')<br>';
+        }
+
+        return Attribute::make(
+            get: fn($value) => (string) $s,
+        );
+    }
+
+
 }
 

+ 2 - 1
app/Models/Product.php

@@ -84,7 +84,8 @@ class Product extends Model
     public function commonName(): Attribute
     {
         return Attribute::make(
-            get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
+            get: fn($value) =>  $this->article . ', №' . $this->nomenclature_number,
+//            get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
         );
     }
 

+ 5 - 0
config/pagination.php

@@ -0,0 +1,5 @@
+<?php
+
+return [
+    'per_page' => env('PAGINATION_LIMIT', 1000),
+];

+ 2 - 2
resources/views/orders/edit.blade.php

@@ -42,8 +42,8 @@
             <div class="col-xxl-6">
                 <h4>МАФ</h4>
                 <div>
-                    <input type="text" class="form-control mb-2" @disabled($order->order_status_id ?? 0 > 1) placeholder="Поиск номенклатуры" id="search_maf">
-                    <select id="select_maf" class="form-select mb-3" multiple @disabled($order->order_status_id ?? 0 > 1)></select>
+                    <input type="text" class="form-control mb-2" @disabled(($order->order_status_id ?? 0) > 1) placeholder="Поиск номенклатуры" id="search_maf">
+                    <select id="select_maf" class="form-select mb-3" multiple @disabled(($order->order_status_id ?? 0) > 1)></select>
                 </div>
 
                 <div id="selected_maf">

+ 7 - 0
resources/views/orders/show.blade.php

@@ -12,6 +12,13 @@
                     <a href="{{ route('order.get-maf', $order) }}" class="btn btn-primary @disabled($order->ready_to_mount == 'Нет' )" >Привязать МАФы к заказу</a>
                 @endif
                 <a href="{{ route('order.edit', $order) }}" class="btn btn-primary">Редактировать</a>
+                @if(hasRole('admin') && ($order->order_status_id == App\Models\Order::STATUS_NEW))
+                        <a href="#" onclick="if(confirm('Удалить площадку?')) $('form#destroy').submit();" class="btn btn-danger">Удалить</a>
+                    <form action="{{ route('order.destroy', $order) }}" method="post" class="d-none" id="destroy">
+                        @csrf
+                        @method('DELETE')
+                    </form>
+                @endif
             </div>
         </div>
         <div class="row">

+ 3 - 2
resources/views/partials/table.blade.php

@@ -65,9 +65,10 @@
                                     {!! $string->$rel->$field !!}
                                 @endif
                             @else
-                                <ul class="small mb-0">
+
+                                <ul class="small mb-0 list-group list-group-flush bg-secondary">
                                     @foreach($string->$rel ?? [] as $item)
-                                        <li>
+                                        <li class="list-group-item py-0 bg-body-secondary">
                                             {!! $item->$field !!}
                                         </li>
                                     @endforeach

+ 1 - 1
routes/web.php

@@ -58,7 +58,7 @@ Route::middleware('auth:web')->group(function () {
     Route::post('order/store', [OrderController::class, 'store'])->name('order.store');
     Route::post('order/{order}/store', [OrderController::class, 'store'])->name('order.update');
     Route::get('order/{order}/get-maf', [OrderController::class, 'getMafToOrder'])->name('order.get-maf');
-    Route::get('order/destroy', [OrderController::class, 'destroy'])->name('order.destroy');
+    Route::delete('order/{order}', [OrderController::class, 'destroy'])->name('order.destroy')->middleware('role:' . Role::ADMIN);
 
     // Склад (МАФ)
     Route::get('product_sku', [ProductSKUController::class, 'index'])->name('product_sku.index');

+ 2 - 1
todo.md

@@ -9,7 +9,8 @@
 - [x] Добавить ответственных и привязать к району: ФИО, телефон
 - [x] заказы (площадки) - создание, отображение таблицы
 - [x] просмотр заказа, добавление товаров в заказ
-- [ ] вынести пагинацию в настройки енв
+- [x] вынести пагинацию в настройки енв
+- [x] удаление площадки
 - [ ] перенос мафов
 - [ ] вкладка договоры
 - [ ] формирование документов на монтаж, сдачу