OrderController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. 'comment' => 'Комментарий',
  29. 'installation_date' => 'Дата выхода на монтаж',
  30. 'ready_date' => 'Дата готовности площадки',
  31. 'brigadier_id' => 'Бригадир',
  32. 'order_status_id' => 'Статус',
  33. 'tg_group_name' => 'Имя группы в ТГ',
  34. 'tg_group_link' => 'Ссылка на группу в ТГ',
  35. 'products_with_count' => 'МАФы',
  36. 'ready_to_mount' => 'Все МАФы на складе',
  37. ],
  38. 'searchFields' => [
  39. 'comment',
  40. 'object_address',
  41. 'tg_group_name',
  42. 'tg_group_link',
  43. ],
  44. ];
  45. public function __construct()
  46. {
  47. $this->data['districts'] = District::query()->get()->pluck('name', 'id');
  48. $this->data['areas'] = Area::query()->get()->pluck('name', 'id');
  49. $this->data['objectTypes'] = ObjectType::query()->get()->pluck('name', 'id');
  50. $this->data['orderStatuses'] =OrderStatus::query()->get()->pluck('name', 'id');
  51. $this->data['brigadiers'] = User::query()->where('role', Role::BRIGADIER)->get()->pluck('name', 'id');
  52. $this->data['users'] = User::query()->whereIn('role', [Role::MANAGER, Role::ADMIN])->get()->pluck('name', 'id');
  53. }
  54. /**
  55. * Display a listing of the resource.
  56. */
  57. public function index(Request $request)
  58. {
  59. $model = new Order;
  60. // fill filters
  61. $this->createFilters($model, 'user_id', 'district_id', 'area_id', 'object_type_id', 'brigadier_id', 'order_status_id', 'ready_to_mount');
  62. $this->createDateFilters($model, 'installation_date', 'ready_date');
  63. $this->data['ranges'] = [];
  64. $q = $model::query();
  65. $this->acceptFilters($q, $request);
  66. $this->acceptSearch($q, $request);
  67. $this->setSortAndOrderBy($model, $request);
  68. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  69. $this->data['orders'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
  70. foreach ($this->data['orders'] as $order) {
  71. $order->recalculateReadyToMount();
  72. }
  73. return view('orders.index', $this->data);
  74. }
  75. /**
  76. * Show the form for creating a new resource.
  77. */
  78. public function create()
  79. {
  80. return view('orders.edit', $this->data);
  81. }
  82. /**
  83. * Store a newly created resource in storage.
  84. */
  85. public function store(StoreOrderRequest $request)
  86. {
  87. $data = $request->validated();
  88. $products = $request->validated('products');
  89. $quantities = $request->validated('quantity');
  90. unset($data['products']);
  91. if(isset($data['id'])) {
  92. $order = Order::query()->where('id', $data['id'])->first();
  93. $order->update($data);
  94. $order->refresh();
  95. } else {
  96. $data['order_status_id'] = Order::STATUS_NEW;
  97. $order = Order::query()->create($data);
  98. }
  99. // меняем список товаров заказа только если статус новый
  100. if($products && $quantities && ($order->order_status_id == 1)) {
  101. // remove from products_sku
  102. ProductSKU::query()->where('order_id', $order->id)->delete();
  103. foreach ($products as $key => $product) {
  104. for($i = 0; $i < $quantities[$key]; $i++) {
  105. ProductSKU::query()->create([
  106. 'order_id' => $order->id,
  107. 'product_id' => $product,
  108. 'status' => 'требуется'
  109. ]);
  110. }
  111. }
  112. }
  113. $order->refresh();
  114. $order->autoChangeStatus();
  115. return redirect()->route('order.show', $order);
  116. }
  117. /**
  118. * Display the specified resource.
  119. */
  120. public function show(Order $order)
  121. {
  122. $this->data['order'] = $order;
  123. return view('orders.show', $this->data);
  124. }
  125. /**
  126. * Show the form for editing the specified resource.
  127. */
  128. public function edit(Order $order)
  129. {
  130. $this->data['order'] = $order;
  131. return view('orders.edit', $this->data);
  132. }
  133. /**
  134. * Привязка товаров к заказу
  135. * @param Order $order
  136. * @return RedirectResponse
  137. */
  138. public function getMafToOrder(Order $order)
  139. {
  140. foreach ($order->products_sku as $product_sku) {
  141. // dont connect maf to order if already connected
  142. if($product_sku->maf_order) {
  143. continue;
  144. }
  145. $mafOrder = MafOrder::query()
  146. ->where('product_id', $product_sku->product_id)
  147. ->where('in_stock', '>' , 0)
  148. ->orderBy('created_at')
  149. ->first();
  150. $product_sku->update(['maf_order_id' => $mafOrder->id, 'status' => 'отгружен']);
  151. $mafOrder->decrement('in_stock');
  152. unset($mafOrder, $product_sku);
  153. }
  154. $order->autoChangeStatus();
  155. return redirect()->route('order.show', $order);
  156. }
  157. /**
  158. * @param Order $order
  159. * @return RedirectResponse
  160. */
  161. public function destroy(Order $order)
  162. {
  163. Order::query()->where('id', $order->id)->delete();
  164. ProductSKU::query()->where('order_id', $order->id)->delete();
  165. return redirect()->route('order.index');
  166. }
  167. public function revertMaf(Order $order)
  168. {
  169. foreach ($order->products_sku as $maf) {
  170. MafOrder::query()->where('id', $maf->maf_order_id)->increment('in_stock');
  171. $maf->update(['maf_order_id' => null, 'status' => 'требуется']);
  172. }
  173. return redirect()->route('order.show', $order);
  174. }
  175. public function moveMaf(Request $request)
  176. {
  177. $data = $request->validate([
  178. 'new_order_id' => 'required',
  179. 'ids' => 'required|json',
  180. ]);
  181. $ids = json_decode($data['ids'], true);
  182. $updated = [];
  183. foreach ($ids as $mafId) {
  184. $maf = ProductSKU::query()
  185. ->where('id', $mafId)
  186. ->first();
  187. if($maf) {
  188. $maf->update(['order_id' => $data['new_order_id']]);
  189. // todo update comment: add previous address
  190. $updated[] = $maf;
  191. }
  192. }
  193. return response()->json($updated);
  194. }
  195. public function search(Request $request): array
  196. {
  197. $ret = [];
  198. $s = $request->get('s');
  199. $searchFields = $this->data['searchFields'];
  200. $result = Order::query();
  201. if($s) {
  202. $result->where(function ($query) use ($searchFields, $s) {
  203. foreach ($searchFields as $searchField) {
  204. $query->orWhere($searchField, 'LIKE', '%' . $s . '%');
  205. }
  206. });
  207. }
  208. $result->orderBy('object_address');
  209. foreach ($result->get() as $p) {
  210. $ret[$p->id] = $p->common_name;
  211. }
  212. return $ret;
  213. }
  214. }