Sfoglia il codice sorgente

schedules edit and create custom

Alexander Musikhin 2 mesi fa
parent
commit
1110abc303

+ 34 - 0
app/Http/Controllers/ScheduleController.php

@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
 
 use App\Helpers\DateHelper;
 use App\Http\Requests\CreateScheduleFromOrderRequest;
+use App\Http\Requests\CreateScheduleRequest;
+use App\Http\Requests\UpdateScheduleRequest;
 use App\Models\Order;
 use App\Models\Schedule;
 use Illuminate\Http\Request;
@@ -81,4 +83,36 @@ class ScheduleController extends Controller
 
     }
 
+    public function update(UpdateScheduleRequest $request)
+    {
+        $validated = $request->validated();
+        Schedule::query()
+            ->where('id', $validated['id'])
+            ->update($validated);
+        return redirect()->back();
+    }
+
+    public function create(CreateScheduleRequest $request)
+    {
+        $validated = $request->validated();
+        $order = Order::query()
+            ->where('id', $validated['order_id'])
+            ->first();
+        Schedule::query()
+            ->create([
+                'order_id' => $validated['order_id'],
+                'installation_date' => $validated['installation_date'],
+                'manual' => true,
+                'district_id' => $order->district_id,
+                'area_id' => $order->area_id,
+                'object_address' => $order->object_address,
+                'object_type_id' => $order->object_type_id,
+                'mafs' => $order->productsWithCount,
+                'mafs_count' => $order->products_sku()->count(),
+                'brigadier_id' => $order->brigadier_id,
+                'comment'   => $validated['comment'],
+            ]);
+        return redirect()->back();
+    }
+
 }

+ 30 - 0
app/Http/Requests/CreateScheduleRequest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class CreateScheduleRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return auth()->check();
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+     */
+    public function rules(): array
+    {
+        return [
+            'order_id'              => 'required|integer|exists:orders,id',
+            'installation_date'     => 'required|date',
+            'comment'               => 'nullable',
+        ];
+    }
+}

+ 30 - 0
app/Http/Requests/UpdateScheduleRequest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateScheduleRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return auth()->check();
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+     */
+    public function rules(): array
+    {
+        return [
+            'id'                => 'required|exists:schedules,id',
+            'installation_date' => 'required|date',
+            'comment'           => 'required|string',
+        ];
+    }
+}

+ 131 - 30
resources/views/schedule/index.blade.php

@@ -15,7 +15,8 @@
                 </div>
                 <label class="p-2 d-block small mt-1">Неделя №</label>
                 <div class="p-2">
-                    <input type="number" style="width: 3rem" value="{{ $weekNumber }}" class="form-control form-control-sm"
+                    <input type="number" style="width: 3rem" value="{{ $weekNumber }}"
+                           class="form-control form-control-sm"
                            onchange="document.location = '{{ route('schedule.index') }}?week='+this.value"
                            min="1" max="53" title="№ недели">
                 </div>
@@ -43,50 +44,116 @@
     <div class="schedule">
         <table class="table schedules">
             <thead>
-                <tr>
-                    <th class="text-center vertical">День недели</th>
-                    <th class="text-center vertical">Дата</th>
-                    <th>Код адр</th>
-                    <th>Округ</th>
-                    <th>Район</th>
-                    <th>Адрес</th>
-                    <th>Тип объекта</th>
-                    <th>Артикулы МАФ</th>
-                    <th>Кол-во позиций</th>
-                    <th>Бригадир</th>
-                    <th>Примечание</th>
-                </tr>
+            <tr>
+                <th class="text-center vertical">День недели</th>
+                <th class="text-center vertical">Дата</th>
+                <th>Код адр</th>
+                <th>Округ</th>
+                <th>Район</th>
+                <th>Адрес</th>
+                <th>Тип объекта</th>
+                <th>Артикулы МАФ</th>
+                <th>Кол-во позиций</th>
+                <th>Бригадир</th>
+                <th>Примечание</th>
+                <th></th>
+            </tr>
             </thead>
             <tbody>
             @foreach($schedules as $dow => $schs)
                 <tr>
-                    <td rowspan="{{ ($schs) ? count($schs) : '1' }}" class="vertical">{{ \App\Helpers\DateHelper::getHumanDayOfWeek($dow) }}</td>
-                    <td rowspan="{{ ($schs) ? count($schs) : '1' }}" class="vertical">{{ \App\Helpers\DateHelper::getHumanDate($dow) }}</td>
+                    <td rowspan="{{ ($schs) ? count($schs) : '1' }}"
+                        class="vertical">{{ \App\Helpers\DateHelper::getHumanDayOfWeek($dow) }}
+                        <i class="bi bi-calendar-plus text-primary ms-2 createSchedule"
+                           title="Новая запись" data-schedule-date="{{ $dow }}"></i>
+                    </td>
+                    <td rowspan="{{ ($schs) ? count($schs) : '1' }}"
+                        class="vertical">{{ \App\Helpers\DateHelper::getHumanDate($dow) }}</td>
                     @if($schs)
                         @foreach($schs as $schedule)
                             {!! (!$loop->first) ? '<tr>':'' !!}
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->order_id }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->district->shortname }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->area->name }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->object_address }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->objectType->name }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{!! $schedule->mafs !!}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->mafs_count }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->brigadier->name }}</td>
-                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">{{ $schedule->comment }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->order_id }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->district->shortname }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->area->name }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->object_address }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->objectType->name }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{!! $schedule->mafs !!}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->mafs_count }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}"
+                                class="align-middle">{{ $schedule->brigadier->name }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle comment-{{ $schedule->id }}">{{ $schedule->comment }}</td>
+                            <td style="background: {{ $schedule->brigadier->color }}" class="align-middle">
+                                <i class="bi bi-pencil ms-3 cursor-pointer text-primary editSchedule"
+                                   data-schedule-date="{{ $schedule->installation_date }}" data-schedule-id="{{ $schedule->id }}"></i>
+                            </td>
                             </tr>
                         @endforeach
-                    @endif
-                    {!! (!$schs) ? '</tr>': '' !!}
+                     @endif
+            {!! (!$schs) ? '</tr>': '' !!}
 
             @endforeach
 
             </tbody>
         </table>
 
+    </div>
 
+    <!-- Модальное окно редактирования графика -->
+    <div class="modal fade" id="copySchedule" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
+        <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h1 class="modal-title fs-5" id="addModalLabel">Изменить запись в графике</h1>
+                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
+                </div>
+                <div class="modal-body">
+                    <form action="{{ route('schedule.update') }}" method="post" id="scheduleEditForm">
+                        @csrf
+                        <input type="hidden" name="id" value="0">
+                        <div>
+                            <input type="date" name="installation_date" value="" class="form-control form-control-sm mb-3">
+                            <textarea name="comment" placeholder="Комментарий для графика"
+                                      class="form-control form-control-sm mb-3"></textarea>
+                            <button type="submit" class="btn btn-primary btn-sm">Обновить</button>
+                        </div>
+                    </form>
+                </div>
+            </div>
+        </div>
     </div>
 
+    <!-- Модальное окно создания графика -->
+    <div class="modal fade" id="createSchedule" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
+        <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h1 class="modal-title fs-5" id="addModalLabel">Изменить запись в графике</h1>
+                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
+                </div>
+                <div class="modal-body">
+                    <form action="{{ route('schedule.create') }}" method="post" id="scheduleCreateForm">
+                        @csrf
+                        <input type="text" class="form-control mb-2" placeholder="Поиск площадки" id="search_order">
+                        <select id="select_order" name="order_id" class="form-select mb-3" size="7" multiple
+                                required></select>
+                        <div>
+                            <input type="date" name="installation_date" value="" class="form-control form-control-sm mb-3">
+                            <textarea name="comment" placeholder="Комментарий для графика"
+                                      class="form-control form-control-sm mb-3"></textarea>
+                            <button type="submit" class="btn btn-primary btn-sm">Создать</button>
+                        </div>
+                    </form>
+                </div>
+            </div>
+        </div>
+    </div>
 
     @if($errors->any())
         @dump($errors)
@@ -94,18 +161,52 @@
 @endsection
 
 @push('scripts')
+    <script type="module">
+        $('.editSchedule').on('click', function () {
+            let scheduleId = $(this).attr('data-schedule-id');
+            let scheduleDate = $(this).attr('data-schedule-date');
+            let scheduleComment = $('.comment-'+scheduleId).text();
+            $('form#scheduleEditForm input[name=id]').val(scheduleId);
+            $('form#scheduleEditForm input[name=installation_date]').val(scheduleDate);
+            $('form#scheduleEditForm textarea').text(scheduleComment);
+            let myModalEditSchedule = new bootstrap.Modal(document.getElementById("copySchedule"), {});
+            myModalEditSchedule.show();
+        });
+
+        $('.createSchedule').on('click', function () {
+            let scheduleDate = $(this).attr('data-schedule-date');
+
+            $('form#scheduleCreateForm input[name=installation_date]').val(scheduleDate);
+            $('form#scheduleCreateForm textarea').text('');
+
+            let myModalCreateSchedule = new bootstrap.Modal(document.getElementById("createSchedule"), {});
+            myModalCreateSchedule.show();
+        });
+
+        // select order
+        $('#search_order').on('keyup', function () {
+            // search products on backend
+            $.get('{{ route('order.search') }}?s=' + $(this).val(),
+                function (data) {
+                    $('#select_order').children().remove()
+                    $.each(data, function (id, name) {
+                        $('#select_order').append('<option value=\'' + id + '\'>' + name + '</option>');
+                    });
+                }
+            );
+        }).trigger('keyup');
+    </script>
     <script type="text/javascript">
         function getWeekNumber(d) {
             // Copy date so don't modify original
             d = new Date(Date.parse(d));
             // Set to nearest Thursday: current date + 4 - current day number
             // Make Sunday's day number 7
-            d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
+            d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
             // Get first day of year
-            let yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
+            let yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
             // Calculate and return week number
             return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
         }
-
     </script>
 @endpush

+ 2 - 0
routes/web.php

@@ -147,6 +147,8 @@ Route::middleware('auth:web')->group(function () {
 
     Route::get('schedule', [ScheduleController::class, 'index'])->name('schedule.index');
     Route::post('schedule/create_from_order', [ScheduleController::class, 'createFromOrder'])->name('schedule.create-from-order');
+    Route::post('schedule/update', [ScheduleController::class, 'update'])->name('schedule.update');
+    Route::post('schedule/create', [ScheduleController::class, 'create'])->name('schedule.create');
 
     // ajax search products
     Route::get('product/search', [ProductController::class, 'search'])->name('product.search');