| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Requests\Order\StoreOrderRequest;
- use App\Models\Dictionary\Area;
- use App\Models\Dictionary\District;
- use App\Models\ObjectType;
- use App\Models\Order;
- use App\Models\OrderStatus;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Http\Request;
- class OrderController extends Controller
- {
- protected array $data = [
- 'active' => 'orders',
- 'title' => 'Заказы',
- 'id' => 'orders',
- 'header' => [
- 'id' => 'ID',
- 'user_id' => 'Менеджер',
- 'district_id' => 'Округ',
- 'area_id' => 'Район',
- 'object_address' => 'Адрес объекта',
- 'object_type_id' => 'Тип объекта',
- 'contract_date' => 'Дата договора',
- 'contract_number' => 'Номер договора',
- 'comment' => 'Комментарий',
- 'installation_date' => 'Дата выхода на монтаж',
- 'brigadier_id' => 'Бригадир',
- 'order_status_id' => 'Статус',
- 'tg_group_name' => 'Имя группы в ТГ',
- 'tg_group_link' => 'Ссылка на группу в ТГ',
- 'products-common_name' => 'МАФы',
- ],
- 'searchFields' => [
- 'comment',
- 'object_address',
- 'tg_group_name',
- 'tg_group_link',
- 'contract_number',
- ],
- ];
- public function __construct()
- {
- $this->data['districts'] = District::query()->get()->pluck('name', 'id');
- $this->data['areas'] = Area::query()->get()->pluck('name', 'id');
- $this->data['objectTypes'] = ObjectType::query()->get()->pluck('name', 'id');
- $this->data['orderStatuses'] =OrderStatus::query()->get()->pluck('name', 'id');
- $this->data['brigadiers'] = User::query()->where('role', Role::BRIGADIER)->get()->pluck('name', 'id');
- $this->data['users'] = User::query()->whereIn('role', [Role::MANAGER, Role::ADMIN])->get()->pluck('name', 'id');
- }
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $model = new Order;
- // fill filters
- $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id');
- $this->createDateFilters($model, 'contract_date', 'installation_date');
- $this->data['ranges'] = [];
- $q = $model::query();
- $this->acceptFilters($q, $request);
- $this->acceptSearch($q, $request);
- $this->setSortAndOrderBy($model, $request);
- $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
- $this->data['orders'] = $q->paginate()->withQueryString();
- return view('orders.index', $this->data);
- }
- /**
- * Show the form for creating a new resource.
- */
- public function create()
- {
- return view('orders.edit', $this->data);
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(StoreOrderRequest $request)
- {
- $data = $request->validated();
- $products = $request->validated('products');
- $quantities = $request->validated('quantity');
- unset($data['products']);
- if(isset($data['id'])) {
- $order = Order::query()->where('id', $data['id'])->first();
- $order->update($data);
- $order->refresh();
- } else {
- $order = Order::query()->create($data);
- }
- if($products && $quantities) {
- $order->products()->detach();
- foreach ($products as $key => $product) {
- if($quantities[$key] == 0) continue;
- $order->products()->attach($product, ['quantity' => $quantities[$key]]);
- }
- }
- return redirect()->route('order.show', $order);
- }
- /**
- * Display the specified resource.
- */
- public function show(Order $order)
- {
- $this->data['order'] = $order;
- return view('orders.show', $this->data);
- }
- /**
- * Show the form for editing the specified resource.
- */
- public function edit(Order $order)
- {
- $this->data['order'] = $order;
- return view('orders.edit', $this->data);
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(Request $request, string $id)
- {
- //
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- //
- }
- }
|