Jelajahi Sumber

adding goods to order

Alexander Musikhin 8 bulan lalu
induk
melakukan
151b0ed135

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

@@ -11,6 +11,7 @@ use App\Models\Dictionary\District;
 use App\Models\ObjectType;
 use App\Models\Order;
 use App\Models\OrderStatus;
+use App\Models\Product;
 use App\Models\User;
 use Illuminate\Http\Request;
 use Illuminate\Support\Str;
@@ -181,6 +182,7 @@ class OrderController extends Controller
     public function show(Order $order)
     {
         $this->data['order'] = $order;
+        $this->data['products'] = Product::get()->pluck('manufacturer_name', 'article')->toArray();
         return view('orders.edit', $this->data);
     }
 

+ 34 - 9
app/Http/Controllers/ProductController.php

@@ -37,7 +37,14 @@ class ProductController extends Controller
             'total_price_txt'           => 'Итоговая цена',
             'note'                      => 'Примечания',
             'created_at'                => 'Дата создания',
-        ]
+        ],
+        'searchFields' =>  [
+            'nomenclature_number',
+            'article',
+            'name_tz',
+            'manufacturer_name',
+            'note',
+        ],
     ];
 
     public function index(Request $request)
@@ -84,14 +91,6 @@ class ProductController extends Controller
             ]
         ];
 
-        $this->data['searchFields'] = [
-            'nomenclature_number',
-            'article',
-            'name_tz',
-            'manufacturer_name',
-            'note',
-        ];
-
         // fill filters
         $this->data['filters'] = $filters;
         $this->data['dates'] = $dates;
@@ -202,4 +201,30 @@ class ProductController extends Controller
 
         return redirect()->route('catalog.index')->with(['success' => 'Задача экспорта успешно создана!']);
     }
+
+    /**
+     * @param Request $request
+     * @return array
+     */
+    public function search(Request $request): array
+    {
+        $s = $request->get('s');
+        $searchFields = $this->data['searchFields'];
+        $ret = [];
+        if($s && strlen($s > 1)) {
+            $result = Product::query()->where(function ($query) use ($searchFields, $s) {
+                foreach ($searchFields as $searchField) {
+                    $query->orWhere($searchField, 'LIKE', '%' . $s . '%');
+                }
+            });
+
+            foreach ($result->get() as $p) {
+                $string = '';
+                foreach ($searchFields as $searchField)
+                    $string .= $p->$searchField . ' / ';
+                $ret[$p->id] = $string;
+            }
+        }
+        return $ret;
+    }
 }

+ 33 - 5
resources/views/orders/edit.blade.php

@@ -3,8 +3,11 @@
 @section('content')
 
     <div class="px-3">
-        <div class="col-xxl-6 offset-xxl-2">
-            <form action="{{ route('order.store') }}" method="post">
+
+        <form class="row" action="{{ route('order.store') }}" method="post">
+            <div class="col-xxl-6">
+                <h4>Общая информация об объекте</h4>
+
                 @csrf
 
                 @if(isset($order))
@@ -37,9 +40,19 @@
 
                 @include('partials.input', ['name' => 'tg_group_link', 'title' => 'Ссылка на группу в ТГ', 'value' => $order->tg_group_link ?? old('tg_group_link')])
 
+            </div>
+            <div class="col-xxl-6">
+                <h4>МАФ</h4>
+
+                @include('partials.input', ['name' => 'search_maf', 'title' => 'Поиск МАФ', 'value' => '', 'placeholder' => 'Артикул или номер номенклатуры', 'datalist' => []])
+
+
+            </div>
+            <div class="col-12 text-end">
                 @include('partials.submit')
-            </form>
-        </div>
+            </div>
+        </form>
+
     </div>
 
     @if($errors->any())
@@ -50,11 +63,26 @@
 
 @push('scripts')
     <script type="module">
+        $('#search_maf').on('keyup', function () {
+            // search products on backend
+            $.get('{{ route('product.search') }}?s=' + $(this).val(),
+                function (data) {
+                    $('#dl-search_maf').children().remove();
+                    $.each(data, function (id, name) {
+                        $('#dl-search_maf').append('<option value=\'' + name + '\'>' + id + '</option>');
+                    });
+                    console.log(data);
+                }
+            );
+        })
+
+
+
         $('#district_id').on('change', function () {
             // load areas of selected district
             console.log($(this).val());
 
-            $.get('{{ route('area.ajax-get-areas-by-district') }}/'+$(this).val(),
+            $.get('{{ route('area.ajax-get-areas-by-district') }}/' + $(this).val(),
                 function (data) {
                     $('#area_id').children().remove();
                     $.each(data, function (id, name) {

+ 4 - 0
routes/web.php

@@ -48,6 +48,10 @@ Route::middleware('auth:web')->group(function () {
     Route::post('order/{order}/store', [OrderController::class, 'store'])->name('order.update');
     Route::get('order/destroy', [OrderController::class, 'destroy'])->name('order.destroy');
 
+    // ajax search products
+    Route::get('product/search', [ProductController::class, 'search'])->name('product.search');
+
+
     // ajax get areas by district
     Route::get('areas/{district_id?}', [AreaController::class, 'ajaxGetAreasByDistrict'])->name('area.ajax-get-areas-by-district');