Browse Source

updated orders: maf get and revert for all mafs of order

Alexander Musikhin 7 tháng trước cách đây
mục cha
commit
f4b9f8c836

+ 5 - 17
app/Http/Controllers/OrderController.php

@@ -185,26 +185,14 @@ class OrderController extends Controller
         return redirect()->route('order.index');
     }
 
-    public function revertMaf(Request $request, Order $order)
+    public function revertMaf(Order $order)
     {
-        $ids = $request->validate([
-            'ids' => 'required|json',
-        ]);
-        $ids = json_decode($ids['ids'], true);
-        $updated = [];
-        foreach ($ids as $mafId) {
-            $maf = ProductSKU::query()
-                ->where('order_id', $order->id)
-                ->where('id', $mafId)
-                ->first();
-            if($maf) {
-                MafOrder::query()->where('id', $maf->maf_order_id)->increment('in_stock');
-                $maf->update(['maf_order_id' => null, 'status' => 'требуется']);
-                $updated[] = $maf;
-            }
+        foreach ($order->products_sku as $maf) {
+            MafOrder::query()->where('id', $maf->maf_order_id)->increment('in_stock');
+            $maf->update(['maf_order_id' => null, 'status' => 'требуется']);
         }
 
-        return response()->json($updated);
+        return redirect()->route('order.show', $order);
     }
 
     public function moveMaf(Request $request)

+ 15 - 13
resources/views/orders/show.blade.php

@@ -8,12 +8,15 @@
                 <h3>Площадка {{ $order->object_address }}</h3>
             </div>
             <div class="col-6 text-end">
-                @if($order->order_status_id == App\Models\Order::STATUS_NEW)
-                    <a href="{{ route('order.get-maf', $order) }}" class="btn btn-primary @disabled($order->ready_to_mount == 'Нет' )" >Привязать МАФы к заказу</a>
-                @endif
+                <a href="{{ route('order.get-maf', $order) }}"
+                   class="btn btn-primary @disabled($order->ready_to_mount == 'Нет' )">Привязать МАФы</a>
+
+                <a href="{{ route('order.revert-maf', $order) }}"
+                   class="btn btn-primary">Отвязать МАФы</a>
                 <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>
+                    <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')
@@ -82,7 +85,7 @@
                                     <td>{{ $p->manufacture_date }}</td>
                                     <td>{{ $p->service_life }}</td>
                                     <td class="text-center">
-                                        @if($order->order_status_id > 1)
+                                        @if($p->maf_order?->order_number)
                                             <i class="bi bi-check-all text-success fw-bold"></i>
                                         @else
                                             @if($needs[$p->product_id]['sku']-- > 0)
@@ -97,14 +100,13 @@
                             </tbody>
                         </table>
                         <div>
-                            <button class="btn btn-primary btn-sm revert-maf">Вернуть на склад</button>
+                            <button class="btn btn-primary btn-sm ">Перенести МАФы</button>
                         </div>
                     @endif
                 </div>
 
             </div>
         </div>
-
     </div>
 
 @endsection
@@ -118,8 +120,8 @@
 
         $('.revert-maf').on('click', function () {
             let ids = Array();
-            $('.check-maf').each(function (){
-                if($(this).prop('checked')) {
+            $('.check-maf').each(function () {
+                if ($(this).prop('checked')) {
                     ids.push($(this).attr('data-maf-id'));
                 }
             });
@@ -129,10 +131,10 @@
                     '_token': $('meta[name=csrf-token]').attr('content'),
                     ids: JSON.stringify(ids),
                 },
-                function (){
-                        // console.log(data);
-                        location.reload();
-                    }
+                function () {
+                    // console.log(data);
+                    location.reload();
+                }
             );
         });
 

+ 2 - 2
routes/web.php

@@ -66,10 +66,10 @@ Route::middleware('auth:web')->group(function () {
     Route::get('order/edit/{order}', [OrderController::class, 'edit'])->name('order.edit');
     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::delete('order/{order}', [OrderController::class, 'destroy'])->name('order.destroy')->middleware('role:' . Role::ADMIN);
 
-    Route::post('order/revert-maf/{order}', [OrderController::class, 'revertMaf'])->name('order.revert-maf');
+    Route::get('order/{order}/get-maf', [OrderController::class, 'getMafToOrder'])->name('order.get-maf');
+    Route::get('order/revert-maf/{order}', [OrderController::class, 'revertMaf'])->name('order.revert-maf');
     Route::post('order/move-maf', [OrderController::class, 'moveMaf'])->name('order.move-maf');