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