PricingCodeController.php 4.9 KB

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