OrderController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\MafOrder;
  7. use App\Models\ObjectType;
  8. use App\Models\Order;
  9. use App\Models\OrderStatus;
  10. use App\Models\ProductSKU;
  11. use App\Models\Role;
  12. use App\Models\User;
  13. use Illuminate\Http\RedirectResponse;
  14. use Illuminate\Http\Request;
  15. class OrderController extends Controller
  16. {
  17. protected array $data = [
  18. 'active' => 'orders',
  19. 'title' => 'Заказы',
  20. 'id' => 'orders',
  21. 'header' => [
  22. 'id' => 'ID',
  23. 'user_id' => 'Менеджер',
  24. 'district_id' => 'Округ',
  25. 'area_id' => 'Район',
  26. 'object_address' => 'Адрес объекта',
  27. 'object_type_id' => 'Тип объекта',
  28. 'contract_date' => 'Дата договора',
  29. 'contract_number' => 'Номер договора',
  30. 'comment' => 'Комментарий',
  31. 'installation_date' => 'Дата выхода на монтаж',
  32. 'brigadier_id' => 'Бригадир',
  33. 'order_status_id' => 'Статус',
  34. 'tg_group_name' => 'Имя группы в ТГ',
  35. 'tg_group_link' => 'Ссылка на группу в ТГ',
  36. 'products_with_count' => 'МАФы',
  37. 'ready_to_mount' => 'Все МАФы на складе',
  38. ],
  39. 'searchFields' => [
  40. 'comment',
  41. 'object_address',
  42. 'tg_group_name',
  43. 'tg_group_link',
  44. 'contract_number',
  45. ],
  46. ];
  47. public function __construct()
  48. {
  49. $this->data['districts'] = District::query()->get()->pluck('name', 'id');
  50. $this->data['areas'] = Area::query()->get()->pluck('name', 'id');
  51. $this->data['objectTypes'] = ObjectType::query()->get()->pluck('name', 'id');
  52. $this->data['orderStatuses'] =OrderStatus::query()->get()->pluck('name', 'id');
  53. $this->data['brigadiers'] = User::query()->where('role', Role::BRIGADIER)->get()->pluck('name', 'id');
  54. $this->data['users'] = User::query()->whereIn('role', [Role::MANAGER, Role::ADMIN])->get()->pluck('name', 'id');
  55. }
  56. /**
  57. * Display a listing of the resource.
  58. */
  59. public function index(Request $request)
  60. {
  61. $model = new Order;
  62. // fill filters
  63. $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id', 'ready_to_mount');
  64. $this->createDateFilters($model, 'contract_date', 'installation_date');
  65. $this->data['ranges'] = [];
  66. $q = $model::query();
  67. $this->acceptFilters($q, $request);
  68. $this->acceptSearch($q, $request);
  69. $this->setSortAndOrderBy($model, $request);
  70. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  71. $this->data['orders'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
  72. foreach ($this->data['orders'] as $order) {
  73. $order->recalculateReadyToMount();
  74. }
  75. return view('orders.index', $this->data);
  76. }
  77. /**
  78. * Show the form for creating a new resource.
  79. */
  80. public function create()
  81. {
  82. return view('orders.edit', $this->data);
  83. }
  84. /**
  85. * Store a newly created resource in storage.
  86. */
  87. public function store(StoreOrderRequest $request)
  88. {
  89. $data = $request->validated();
  90. $products = $request->validated('products');
  91. $products_sku = $request->validated('products_sku');
  92. $quantities = $request->validated('quantity');
  93. unset($data['products']);
  94. if(isset($data['id'])) {
  95. $order = Order::query()->where('id', $data['id'])->first();
  96. $order->update($data);
  97. $order->refresh();
  98. } else {
  99. $data['order_status_id'] = Order::STATUS_NEW;
  100. $order = Order::query()->create($data);
  101. }
  102. // меняем список товаров заказа только если статус новый
  103. if($products && $quantities && ($order->order_status_id == 1)) {
  104. // remove from products_sku
  105. ProductSKU::query()->where('order_id', $order->id)->delete();
  106. foreach ($products as $key => $product) {
  107. for($i = 0; $i < $quantities[$key]; $i++) {
  108. ProductSKU::query()->create([
  109. 'order_id' => $order->id,
  110. 'product_id' => $product,
  111. 'status' => 'требуется'
  112. ]);
  113. }
  114. }
  115. }
  116. return redirect()->route('order.show', $order);
  117. }
  118. /**
  119. * Display the specified resource.
  120. */
  121. public function show(Order $order)
  122. {
  123. $this->data['order'] = $order;
  124. return view('orders.show', $this->data);
  125. }
  126. /**
  127. * Show the form for editing the specified resource.
  128. */
  129. public function edit(Order $order)
  130. {
  131. $this->data['order'] = $order;
  132. return view('orders.edit', $this->data);
  133. }
  134. /**
  135. * Привязка товаров к заказу
  136. * @param Order $order
  137. * @return RedirectResponse
  138. */
  139. public function getMafToOrder(Order $order)
  140. {
  141. foreach ($order->products_sku as $product_sku) {
  142. $mafOrder = MafOrder::query()
  143. ->where('product_id', $product_sku->product_id)
  144. ->where('in_stock', '>' , 0)
  145. ->orderBy('created_at')
  146. ->first();
  147. $product_sku->update(['maf_order_id' => $mafOrder->id, 'status' => 'отгружен']);
  148. $mafOrder->decrement('in_stock');
  149. unset($mafOrder, $product_sku);
  150. }
  151. $order->update(['order_status_id' => Order::STATUS_READY_TO_MOUNT]);
  152. return redirect()->route('order.show', $order);
  153. }
  154. /**
  155. * @param Order $order
  156. * @return RedirectResponse
  157. */
  158. public function destroy(Order $order)
  159. {
  160. Order::query()->where('id', $order->id)->delete();
  161. ProductSKU::query()->where('order_id', $order->id)->delete();
  162. return redirect()->route('order.index');
  163. }
  164. }