OrderController.php 7.8 KB

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