PricingCodeController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. 'code' => 'Код',
  15. 'description' => 'Расшифровка',
  16. ],
  17. ];
  18. public function index(Request $request)
  19. {
  20. $q = PricingCode::query();
  21. // Поиск
  22. if ($request->has('search')) {
  23. $search = $request->get('search');
  24. $q->where(function ($query) use ($search) {
  25. $query->where('code', 'LIKE', '%' . $search . '%')
  26. ->orWhere('description', 'LIKE', '%' . $search . '%');
  27. });
  28. }
  29. $q->orderBy('code');
  30. $this->data['pricing_codes'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
  31. $this->data['search'] = $request->get('search');
  32. return view('pricing_codes.index', $this->data);
  33. }
  34. public function store(Request $request): RedirectResponse
  35. {
  36. $request->validate([
  37. 'type' => 'required|in:tsn_number,pricing_code',
  38. 'code' => 'required|string',
  39. 'description' => 'nullable|string',
  40. ]);
  41. // Проверяем уникальность комбинации type + code
  42. $exists = PricingCode::where('type', $request->type)
  43. ->where('code', $request->code)
  44. ->exists();
  45. if ($exists) {
  46. return redirect()->route('pricing_codes.index')
  47. ->with(['error' => 'Такой код уже существует для данного типа!']);
  48. }
  49. PricingCode::create($request->only(['type', 'code', 'description']));
  50. return redirect()->route('pricing_codes.index')
  51. ->with(['success' => 'Код расценки успешно добавлен!']);
  52. }
  53. public function update(Request $request, PricingCode $pricingCode): RedirectResponse
  54. {
  55. $request->validate([
  56. 'description' => 'nullable|string',
  57. ]);
  58. $pricingCode->update(['description' => $request->get('description')]);
  59. return redirect()->route('pricing_codes.index')
  60. ->with(['success' => 'Расшифровка успешно обновлена!']);
  61. }
  62. public function destroy(PricingCode $pricingCode): RedirectResponse
  63. {
  64. $pricingCode->delete();
  65. return redirect()->route('pricing_codes.index')
  66. ->with(['success' => 'Код расценки успешно удалён!']);
  67. }
  68. /**
  69. * API метод для получения расшифровки кода
  70. */
  71. public function getDescription(Request $request)
  72. {
  73. $type = $request->get('type');
  74. $code = $request->get('code');
  75. if (!$type || !$code) {
  76. return response()->json(['description' => null]);
  77. }
  78. $description = null;
  79. if ($type === 'tsn_number') {
  80. $description = PricingCode::getTsnDescription($code);
  81. } elseif ($type === 'pricing_code') {
  82. $description = PricingCode::getPricingCodeDescription($code);
  83. }
  84. return response()->json(['description' => $description]);
  85. }
  86. }