StoreSparePartRequest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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')) {
  42. $description = $this->input('tsn_number_description');
  43. // Сохраняем только если есть расшифровка
  44. if ($description) {
  45. \App\Models\PricingCode::createOrUpdate(
  46. \App\Models\PricingCode::TYPE_TSN_NUMBER,
  47. $this->input('tsn_number'),
  48. $description
  49. );
  50. }
  51. }
  52. // Сохраняем/обновляем расшифровку шифра расценки в справочник
  53. if ($this->filled('pricing_code')) {
  54. $description = $this->input('pricing_code_description');
  55. // Сохраняем только если есть расшифровка
  56. if ($description) {
  57. \App\Models\PricingCode::createOrUpdate(
  58. \App\Models\PricingCode::TYPE_PRICING_CODE,
  59. $this->input('pricing_code'),
  60. $description
  61. );
  62. }
  63. }
  64. }
  65. }