OrderController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. 'name' => 'Название',
  24. 'user_id' => 'Менеджер',
  25. 'district_id' => 'Округ',
  26. 'area_id' => 'Район',
  27. 'object_address' => 'Адрес объекта',
  28. 'object_type_id' => 'Тип объекта',
  29. 'comment' => 'Комментарий',
  30. 'installation_date' => 'Дата выхода на монтаж',
  31. 'ready_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. 'name',
  41. 'comment',
  42. 'object_address',
  43. 'tg_group_name',
  44. 'tg_group_link',
  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, 'installation_date', 'ready_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. $quantities = $request->validated('quantity');
  92. unset($data['products']);
  93. if(isset($data['id'])) {
  94. $order = Order::query()->where('id', $data['id'])->first();
  95. $order->update($data);
  96. $order->refresh();
  97. } else {
  98. $data['order_status_id'] = Order::STATUS_NEW;
  99. $order = Order::query()->create($data);
  100. }
  101. // меняем список товаров заказа только если статус новый
  102. if($products && $quantities && ($order->order_status_id == 1)) {
  103. // remove from products_sku
  104. ProductSKU::query()->where('order_id', $order->id)->delete();
  105. foreach ($products as $key => $product) {
  106. for($i = 0; $i < $quantities[$key]; $i++) {
  107. ProductSKU::query()->create([
  108. 'order_id' => $order->id,
  109. 'product_id' => $product,
  110. 'status' => 'требуется'
  111. ]);
  112. }
  113. }
  114. }
  115. $order->refresh();
  116. $order->autoChangeStatus();
  117. return redirect()->route('order.show', $order);
  118. }
  119. /**
  120. * Display the specified resource.
  121. */
  122. public function show(Order $order)
  123. {
  124. $this->data['order'] = $order;
  125. return view('orders.show', $this->data);
  126. }
  127. /**
  128. * Show the form for editing the specified resource.
  129. */
  130. public function edit(Order $order)
  131. {
  132. $this->data['order'] = $order;
  133. return view('orders.edit', $this->data);
  134. }
  135. /**
  136. * Привязка товаров к заказу
  137. * @param Order $order
  138. * @return RedirectResponse
  139. */
  140. public function getMafToOrder(Order $order)
  141. {
  142. foreach ($order->products_sku as $product_sku) {
  143. // dont connect maf to order if already connected
  144. if($product_sku->maf_order) {
  145. continue;
  146. }
  147. $mafOrder = MafOrder::query()
  148. ->where('product_id', $product_sku->product_id)
  149. ->where('in_stock', '>' , 0)
  150. ->orderBy('created_at')
  151. ->first();
  152. $product_sku->update(['maf_order_id' => $mafOrder->id, 'status' => 'отгружен']);
  153. $mafOrder->decrement('in_stock');
  154. unset($mafOrder, $product_sku);
  155. }
  156. $order->autoChangeStatus();
  157. return redirect()->route('order.show', $order);
  158. }
  159. /**
  160. * @param Order $order
  161. * @return RedirectResponse
  162. */
  163. public function destroy(Order $order)
  164. {
  165. Order::query()->where('id', $order->id)->delete();
  166. ProductSKU::query()->where('order_id', $order->id)->delete();
  167. return redirect()->route('order.index');
  168. }
  169. public function revertMaf(Order $order)
  170. {
  171. foreach ($order->products_sku as $maf) {
  172. MafOrder::query()->where('id', $maf->maf_order_id)->increment('in_stock');
  173. $maf->update(['maf_order_id' => null, 'status' => 'требуется']);
  174. }
  175. return redirect()->route('order.show', $order);
  176. }
  177. public function moveMaf(Request $request)
  178. {
  179. $data = $request->validate([
  180. 'new_order_id' => 'required',
  181. 'ids' => 'required|json',
  182. ]);
  183. $ids = json_decode($data['ids'], true);
  184. $updated = [];
  185. foreach ($ids as $mafId) {
  186. $maf = ProductSKU::query()
  187. ->where('id', $mafId)
  188. ->first();
  189. if($maf) {
  190. $maf->update(['order_id' => $data['new_order_id']]);
  191. // todo update comment: add previous address
  192. $updated[] = $maf;
  193. }
  194. }
  195. return response()->json($updated);
  196. }
  197. public function search(Request $request): array
  198. {
  199. $ret = [];
  200. $s = $request->get('s');
  201. $searchFields = $this->data['searchFields'];
  202. $result = Order::query();
  203. if($s) {
  204. $result->where(function ($query) use ($searchFields, $s) {
  205. foreach ($searchFields as $searchField) {
  206. $query->orWhere($searchField, 'LIKE', '%' . $s . '%');
  207. }
  208. });
  209. }
  210. $result->orderBy('object_address');
  211. foreach ($result->get() as $p) {
  212. $ret[$p->id] = $p->common_name;
  213. }
  214. return $ret;
  215. }
  216. }