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_code' => 'nullable|string',
  31. 'pricing_code_description' => 'nullable|string',
  32. 'min_stock' => 'nullable|integer|min:0',
  33. ];
  34. }
  35. /**
  36. * Обработка данных после валидации
  37. */
  38. protected function passedValidation()
  39. {
  40. // Сохраняем расшифровку № по ТСН в справочник, если указана
  41. if ($this->filled('tsn_number') && $this->filled('tsn_number_description')) {
  42. \App\Models\PricingCode::createOrUpdate(
  43. \App\Models\PricingCode::TYPE_TSN_NUMBER,
  44. $this->input('tsn_number'),
  45. $this->input('tsn_number_description')
  46. );
  47. }
  48. // Сохраняем расшифровку шифра расценки в справочник, если указана
  49. if ($this->filled('pricing_code') && $this->filled('pricing_code_description')) {
  50. \App\Models\PricingCode::createOrUpdate(
  51. \App\Models\PricingCode::TYPE_PRICING_CODE,
  52. $this->input('pricing_code'),
  53. $this->input('pricing_code_description')
  54. );
  55. }
  56. }
  57. }