StoreSparePartRequest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class StoreSparePartRequest extends FormRequest
  5. {
  6. /**
  7. * Determine if the user is authorized to make this request.
  8. */
  9. public function authorize(): bool
  10. {
  11. return hasRole('admin');
  12. }
  13. /**
  14. * Get the validation rules that apply to the request.
  15. *
  16. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  17. */
  18. public function rules(): array
  19. {
  20. return [
  21. 'article' => 'required|string|max:255',
  22. 'used_in_maf' => 'nullable|string|max:255',
  23. 'product_id' => 'nullable|exists:products,id',
  24. 'note' => 'nullable|string',
  25. 'purchase_price' => 'nullable|numeric|min:0',
  26. 'customer_price' => 'nullable|numeric|min:0',
  27. 'expertise_price' => 'nullable|numeric|min:0',
  28. 'tsn_number' => 'nullable|string|max:255',
  29. 'tsn_number_description' => 'nullable|string',
  30. 'pricing_codes' => 'nullable|array',
  31. 'pricing_codes.*' => 'nullable|string|max:255',
  32. 'pricing_codes_descriptions' => 'nullable|array',
  33. 'pricing_codes_descriptions.*' => 'nullable|string',
  34. 'min_stock' => 'nullable|integer|min:0',
  35. ];
  36. }
  37. /**
  38. * Обработка данных после валидации
  39. */
  40. protected function passedValidation()
  41. {
  42. // Сохраняем/обновляем расшифровку № по ТСН в справочник
  43. if ($this->filled('tsn_number')) {
  44. $description = $this->input('tsn_number_description');
  45. // Сохраняем только если есть расшифровка
  46. if ($description) {
  47. \App\Models\PricingCode::createOrUpdate(
  48. \App\Models\PricingCode::TYPE_TSN_NUMBER,
  49. $this->input('tsn_number'),
  50. $description
  51. );
  52. }
  53. }
  54. // Примечание: сохранение pricing_codes и их расшифровок
  55. // происходит в контроллере через syncPricingCodes()
  56. }
  57. }