PricingCodeController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $sortBy = $request->get('sortBy', 'code');
  23. $allowedSort = ['id', 'type', 'code', 'description'];
  24. if (!in_array($sortBy, $allowedSort, true)) {
  25. $sortBy = 'code';
  26. }
  27. $orderBy = $request->get('order') === 'desc' ? 'desc' : 'asc';
  28. $q = PricingCode::query();
  29. // Поиск
  30. $search = $request->get('s', $request->get('search'));
  31. if (!empty($search)) {
  32. $q->where(function ($query) use ($search) {
  33. $query->where('code', 'LIKE', '%' . $search . '%')
  34. ->orWhere('description', 'LIKE', '%' . $search . '%')
  35. ->orWhere('type', 'LIKE', '%' . $search . '%');
  36. });
  37. }
  38. $q->orderBy($sortBy, $orderBy);
  39. $this->data['pricing_codes'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
  40. $this->data['sortBy'] = $sortBy;
  41. $this->data['orderBy'] = $orderBy;
  42. $this->data['searchFields'] = ['type', 'code', 'description'];
  43. $this->data['search'] = $search;
  44. return view('pricing_codes.index', $this->data);
  45. }
  46. public function store(Request $request): RedirectResponse
  47. {
  48. $request->validate([
  49. 'type' => 'required|in:tsn_number,pricing_code',
  50. 'code' => 'required|string',
  51. 'description' => 'nullable|string',
  52. ]);
  53. // Проверяем уникальность комбинации type + code
  54. $exists = PricingCode::where('type', $request->type)
  55. ->where('code', $request->code)
  56. ->exists();
  57. if ($exists) {
  58. return redirect()->route('pricing_codes.index')
  59. ->with(['error' => 'Такой код уже существует для данного типа!']);
  60. }
  61. PricingCode::create($request->only(['type', 'code', 'description']));
  62. return redirect()->route('pricing_codes.index')
  63. ->with(['success' => 'Код расценки успешно добавлен!']);
  64. }
  65. public function update(Request $request, PricingCode $pricingCode): RedirectResponse
  66. {
  67. $request->validate([
  68. 'description' => 'nullable|string',
  69. ]);
  70. $pricingCode->update(['description' => $request->get('description')]);
  71. return redirect()->route('pricing_codes.index')
  72. ->with(['success' => 'Расшифровка успешно обновлена!']);
  73. }
  74. public function destroy(PricingCode $pricingCode): RedirectResponse
  75. {
  76. $pricingCode->delete();
  77. return redirect()->route('pricing_codes.index')
  78. ->with(['success' => 'Код расценки успешно удалён!']);
  79. }
  80. /**
  81. * API метод для получения расшифровки кода
  82. */
  83. public function getDescription(Request $request)
  84. {
  85. $type = $request->get('type');
  86. $code = $request->get('code');
  87. if (!$type || !$code) {
  88. return response()->json(['description' => null]);
  89. }
  90. $description = null;
  91. if ($type === 'tsn_number') {
  92. $description = PricingCode::getTsnDescription($code);
  93. } elseif ($type === 'pricing_code') {
  94. $description = PricingCode::getPricingCodeDescription($code);
  95. }
  96. return response()->json(['description' => $description]);
  97. }
  98. /**
  99. * API метод для поиска кодов (autocomplete)
  100. */
  101. public function search(Request $request)
  102. {
  103. $type = $request->get('type');
  104. $query = $request->get('query', '');
  105. if (!$type) {
  106. return response()->json([]);
  107. }
  108. $codes = PricingCode::where('type', $type)
  109. ->where(function ($q) use ($query) {
  110. $q->where('code', 'LIKE', '%' . $query . '%')
  111. ->orWhere('description', 'LIKE', '%' . $query . '%');
  112. })
  113. ->orderBy('code')
  114. ->limit(20)
  115. ->get(['code', 'description']);
  116. return response()->json($codes);
  117. }
  118. }