OrderController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\Order\StoreOrderRequest;
  4. use App\Models\Dictionary\Area;
  5. use App\Models\Dictionary\District;
  6. use App\Models\ObjectType;
  7. use App\Models\Order;
  8. use App\Models\OrderStatus;
  9. use App\Models\Role;
  10. use App\Models\User;
  11. use Illuminate\Http\Request;
  12. class OrderController extends Controller
  13. {
  14. protected array $data = [
  15. 'active' => 'orders',
  16. 'title' => 'Заказы',
  17. 'id' => 'orders',
  18. 'header' => [
  19. 'id' => 'ID',
  20. 'user_id' => 'Менеджер',
  21. 'district_id' => 'Округ',
  22. 'area_id' => 'Район',
  23. 'object_address' => 'Адрес объекта',
  24. 'object_type_id' => 'Тип объекта',
  25. 'contract_date' => 'Дата договора',
  26. 'contract_number' => 'Номер договора',
  27. 'comment' => 'Комментарий',
  28. 'installation_date' => 'Дата выхода на монтаж',
  29. 'brigadier_id' => 'Бригадир',
  30. 'order_status_id' => 'Статус',
  31. 'tg_group_name' => 'Имя группы в ТГ',
  32. 'tg_group_link' => 'Ссылка на группу в ТГ',
  33. 'products-common_name' => 'МАФы',
  34. ],
  35. 'searchFields' => [
  36. 'comment',
  37. 'object_address',
  38. 'tg_group_name',
  39. 'tg_group_link',
  40. 'contract_number',
  41. ],
  42. ];
  43. public function __construct()
  44. {
  45. $this->data['districts'] = District::query()->get()->pluck('name', 'id');
  46. $this->data['areas'] = Area::query()->get()->pluck('name', 'id');
  47. $this->data['objectTypes'] = ObjectType::query()->get()->pluck('name', 'id');
  48. $this->data['orderStatuses'] =OrderStatus::query()->get()->pluck('name', 'id');
  49. $this->data['brigadiers'] = User::query()->where('role', Role::BRIGADIER)->get()->pluck('name', 'id');
  50. $this->data['users'] = User::query()->whereIn('role', [Role::MANAGER, Role::ADMIN])->get()->pluck('name', 'id');
  51. }
  52. /**
  53. * Display a listing of the resource.
  54. */
  55. public function index(Request $request)
  56. {
  57. $model = new Order;
  58. // fill filters
  59. $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id');
  60. $this->createDateFilters($model, 'contract_date', 'installation_date');
  61. $this->data['ranges'] = [];
  62. $q = $model::query();
  63. $this->acceptFilters($q, $request);
  64. $this->acceptSearch($q, $request);
  65. $this->setSortAndOrderBy($model, $request);
  66. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  67. $this->data['orders'] = $q->paginate()->withQueryString();
  68. return view('orders.index', $this->data);
  69. }
  70. /**
  71. * Show the form for creating a new resource.
  72. */
  73. public function create()
  74. {
  75. return view('orders.edit', $this->data);
  76. }
  77. /**
  78. * Store a newly created resource in storage.
  79. */
  80. public function store(StoreOrderRequest $request)
  81. {
  82. $data = $request->validated();
  83. $products = $request->validated('products');
  84. $quantities = $request->validated('quantity');
  85. unset($data['products']);
  86. if(isset($data['id'])) {
  87. $order = Order::query()->where('id', $data['id'])->first();
  88. $order->update($data);
  89. $order->refresh();
  90. } else {
  91. $order = Order::query()->create($data);
  92. }
  93. if($products && $quantities) {
  94. $order->products()->detach();
  95. foreach ($products as $key => $product) {
  96. if($quantities[$key] == 0) continue;
  97. $order->products()->attach($product, ['quantity' => $quantities[$key]]);
  98. }
  99. }
  100. return redirect()->route('order.show', $order);
  101. }
  102. /**
  103. * Display the specified resource.
  104. */
  105. public function show(Order $order)
  106. {
  107. $this->data['order'] = $order;
  108. return view('orders.show', $this->data);
  109. }
  110. /**
  111. * Show the form for editing the specified resource.
  112. */
  113. public function edit(Order $order)
  114. {
  115. $this->data['order'] = $order;
  116. return view('orders.edit', $this->data);
  117. }
  118. /**
  119. * Update the specified resource in storage.
  120. */
  121. public function update(Request $request, string $id)
  122. {
  123. //
  124. }
  125. /**
  126. * Remove the specified resource from storage.
  127. */
  128. public function destroy(string $id)
  129. {
  130. //
  131. }
  132. }