| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\PricingCode;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- class PricingCodeController extends Controller
- {
- protected array $data = [
- 'active' => 'spare_parts',
- 'title' => 'Справочник расшифровок',
- 'id' => 'pricing_codes',
- 'header' => [
- 'id' => 'ID',
- 'code' => 'Код',
- 'description' => 'Расшифровка',
- ],
- ];
- public function index(Request $request)
- {
- $q = PricingCode::query();
- // Поиск
- if ($request->has('search')) {
- $search = $request->get('search');
- $q->where(function ($query) use ($search) {
- $query->where('code', 'LIKE', '%' . $search . '%')
- ->orWhere('description', 'LIKE', '%' . $search . '%');
- });
- }
- $q->orderBy('code');
- $this->data['pricing_codes'] = $q->paginate(session('per_page', config('pagination.per_page')))->withQueryString();
- $this->data['search'] = $request->get('search');
- return view('pricing_codes.index', $this->data);
- }
- public function store(Request $request): RedirectResponse
- {
- $request->validate([
- 'type' => 'required|in:tsn_number,pricing_code',
- 'code' => 'required|string',
- 'description' => 'nullable|string',
- ]);
- // Проверяем уникальность комбинации type + code
- $exists = PricingCode::where('type', $request->type)
- ->where('code', $request->code)
- ->exists();
- if ($exists) {
- return redirect()->route('pricing_codes.index')
- ->with(['error' => 'Такой код уже существует для данного типа!']);
- }
- PricingCode::create($request->only(['type', 'code', 'description']));
- return redirect()->route('pricing_codes.index')
- ->with(['success' => 'Код расценки успешно добавлен!']);
- }
- public function update(Request $request, PricingCode $pricingCode): RedirectResponse
- {
- $request->validate([
- 'description' => 'nullable|string',
- ]);
- $pricingCode->update(['description' => $request->get('description')]);
- return redirect()->route('pricing_codes.index')
- ->with(['success' => 'Расшифровка успешно обновлена!']);
- }
- public function destroy(PricingCode $pricingCode): RedirectResponse
- {
- $pricingCode->delete();
- return redirect()->route('pricing_codes.index')
- ->with(['success' => 'Код расценки успешно удалён!']);
- }
- /**
- * API метод для получения расшифровки кода
- */
- public function getDescription(Request $request)
- {
- $type = $request->get('type');
- $code = $request->get('code');
- if (!$type || !$code) {
- return response()->json(['description' => null]);
- }
- $description = null;
- if ($type === 'tsn_number') {
- $description = PricingCode::getTsnDescription($code);
- } elseif ($type === 'pricing_code') {
- $description = PricingCode::getPricingCodeDescription($code);
- }
- return response()->json(['description' => $description]);
- }
- }
|