PricingCodeController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\PricingCode;
  4. use Illuminate\Http\RedirectResponse;
  5. use Illuminate\Http\Request;
  6. class PricingCodeController extends Controller
  7. {
  8. protected array $data = [
  9. 'active' => 'spare_parts',
  10. 'title' => 'Справочник расшифровок',
  11. 'id' => 'pricing_codes',
  12. 'header' => [
  13. 'id' => 'ID',
  14. 'type' => 'Тип',
  15. 'code' => 'Код',
  16. 'description' => 'Расшифровка',
  17. 'actions' => 'Действия',
  18. ],
  19. ];
  20. public function index(Request $request)
  21. {
  22. $this->setSortAndOrderBy(new PricingCode(), $request);
  23. $sortBy = $request->get('sortBy', 'code');
  24. $allowedSort = ['id', 'type', 'code', 'description'];
  25. if (!in_array($sortBy, $allowedSort, true)) {
  26. $sortBy = 'code';
  27. }
  28. $orderBy = $request->get('order') === 'desc' ? 'desc' : 'asc';
  29. $q = PricingCode::query();
  30. // Поиск
  31. $search = $request->get('s', $request->get('search'));
  32. if (!empty($search)) {
  33. $q->where(function ($query) use ($search) {
  34. $query->where('code', 'LIKE', '%' . $search . '%')
  35. ->orWhere('description', 'LIKE', '%' . $search . '%')
  36. ->orWhere('type', 'LIKE', '%' . $search . '%');
  37. });
  38. }
  39. $q->orderBy($sortBy, $orderBy);
  40. if ($sortBy !== 'id') {
  41. $q->orderBy('id', $orderBy);
  42. }
  43. $this->data['pricing_codes'] = $q->paginate($this->data['per_page'])->withQueryString();
  44. $this->data['sortBy'] = $sortBy;
  45. $this->data['orderBy'] = $orderBy;
  46. $this->data['searchFields'] = ['type', 'code', 'description'];
  47. $this->data['search'] = $search;
  48. return view('pricing_codes.index', $this->data);
  49. }
  50. public function store(Request $request): RedirectResponse
  51. {
  52. $request->validate([
  53. 'type' => 'required|in:tsn_number,pricing_code',
  54. 'code' => 'required|string',
  55. 'description' => 'nullable|string',
  56. ]);
  57. // Проверяем уникальность комбинации type + code
  58. $exists = PricingCode::where('type', $request->type)
  59. ->where('code', $request->code)
  60. ->exists();
  61. if ($exists) {
  62. return redirect()->route('pricing_codes.index')
  63. ->with(['error' => 'Такой код уже существует для данного типа!']);
  64. }
  65. PricingCode::create($request->only(['type', 'code', 'description']));
  66. return redirect()->route('pricing_codes.index')
  67. ->with(['success' => 'Код расценки успешно добавлен!']);
  68. }
  69. public function update(Request $request, PricingCode $pricingCode): RedirectResponse
  70. {
  71. $request->validate([
  72. 'description' => 'nullable|string',
  73. ]);
  74. $pricingCode->update(['description' => $request->get('description')]);
  75. return redirect()->route('pricing_codes.index')
  76. ->with(['success' => 'Расшифровка успешно обновлена!']);
  77. }
  78. public function destroy(PricingCode $pricingCode): RedirectResponse
  79. {
  80. $pricingCode->delete();
  81. return redirect()->route('pricing_codes.index')
  82. ->with(['success' => 'Код расценки успешно удалён!']);
  83. }
  84. /**
  85. * API метод для получения расшифровки кода
  86. */
  87. public function getDescription(Request $request)
  88. {
  89. $type = $request->get('type');
  90. $code = $request->get('code');
  91. if (!$type || !$code) {
  92. return response()->json(['description' => null]);
  93. }
  94. $description = null;
  95. if ($type === 'tsn_number') {
  96. $description = PricingCode::getTsnDescription($code);
  97. } elseif ($type === 'pricing_code') {
  98. $description = PricingCode::getPricingCodeDescription($code);
  99. }
  100. return response()->json(['description' => $description]);
  101. }
  102. /**
  103. * API метод для поиска кодов (autocomplete)
  104. */
  105. public function search(Request $request)
  106. {
  107. $type = $request->get('type');
  108. $query = $request->get('query', '');
  109. if (!$type) {
  110. return response()->json([]);
  111. }
  112. $codes = PricingCode::where('type', $type)
  113. ->where(function ($q) use ($query) {
  114. $q->where('code', 'LIKE', '%' . $query . '%')
  115. ->orWhere('description', 'LIKE', '%' . $query . '%');
  116. })
  117. ->orderBy('code')
  118. ->limit(20)
  119. ->get(['code', 'description']);
  120. return response()->json($codes);
  121. }
  122. }