OrderController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\Order\StoreOrderRequest;
  4. use App\Models\Brigadier;
  5. use App\Models\Dictionary\Area;
  6. use App\Models\Dictionary\District;
  7. use App\Models\ObjectType;
  8. use App\Models\Order;
  9. use App\Models\OrderStatus;
  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. ],
  34. 'searchFields' => [
  35. 'comment',
  36. 'object_address',
  37. 'tg_group_name',
  38. 'tg_group_link',
  39. 'contract_number',
  40. ],
  41. ];
  42. public function __construct()
  43. {
  44. $this->data['districts'] = District::query()->get()->pluck('name', 'id');
  45. $this->data['areas'] = Area::query()->get()->pluck('name', 'id');
  46. $this->data['objectTypes'] = ObjectType::query()->get()->pluck('name', 'id');
  47. $this->data['orderStatuses'] =OrderStatus::query()->get()->pluck('name', 'id');
  48. $this->data['brigadiers'] = Brigadier::query()->get()->pluck('name', 'id');
  49. $this->data['users'] = User::query()->get()->pluck('name', 'id');
  50. }
  51. /**
  52. * Display a listing of the resource.
  53. */
  54. public function index(Request $request)
  55. {
  56. $model = new Order;
  57. // fill filters
  58. $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id');
  59. $this->createDateFilters($model, 'contract_date', 'installation_date');
  60. $this->data['ranges'] = [];
  61. $q = $model::query();
  62. $this->acceptFilters($q, $request);
  63. $this->acceptSearch($q, $request);
  64. $this->setSortAndOrderBy($model, $request);
  65. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  66. $this->data['orders'] = $q->paginate()->withQueryString();
  67. return view('orders.index', $this->data);
  68. }
  69. /**
  70. * Show the form for creating a new resource.
  71. */
  72. public function create()
  73. {
  74. return view('orders.edit', $this->data);
  75. }
  76. /**
  77. * Store a newly created resource in storage.
  78. */
  79. public function store(StoreOrderRequest $request)
  80. {
  81. $data = $request->validated();
  82. $products = $request->validated('products');
  83. $quantities = $request->validated('quantity');
  84. unset($data['products']);
  85. if(isset($data['id'])) {
  86. $order = Order::query()->where('id', $data['id'])->first();
  87. $order->update($data);
  88. $order->refresh();
  89. } else {
  90. $order = Order::query()->create($data);
  91. }
  92. if($products && $quantities) {
  93. $order->products()->detach();
  94. foreach ($products as $key => $product) {
  95. if($quantities[$key] == 0) continue;
  96. $order->products()->attach($product, ['quantity' => $quantities[$key]]);
  97. }
  98. }
  99. return redirect()->route('order.index');
  100. }
  101. /**
  102. * Display the specified resource.
  103. */
  104. public function show(Order $order)
  105. {
  106. $this->data['order'] = $order;
  107. return view('orders.edit', $this->data);
  108. }
  109. /**
  110. * Show the form for editing the specified resource.
  111. */
  112. public function edit(string $id)
  113. {
  114. //
  115. }
  116. /**
  117. * Update the specified resource in storage.
  118. */
  119. public function update(Request $request, string $id)
  120. {
  121. //
  122. }
  123. /**
  124. * Remove the specified resource from storage.
  125. */
  126. public function destroy(string $id)
  127. {
  128. //
  129. }
  130. }