StoreProductRequest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Services\Access\AccessService;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. class StoreProductRequest extends FormRequest
  6. {
  7. /**
  8. * Determine if the user is authorized to make this request.
  9. */
  10. public function authorize(): bool
  11. {
  12. if (!auth()->check()) {
  13. return false;
  14. }
  15. if (!$this->routeIs('catalog.store')) {
  16. return true;
  17. }
  18. $requiredFields = [
  19. 'article',
  20. 'name_tz',
  21. 'type_tz',
  22. 'nomenclature_number',
  23. 'sizes',
  24. 'manufacturer',
  25. 'unit',
  26. 'type',
  27. 'product_price',
  28. 'installation_price',
  29. 'total_price',
  30. 'manufacturer_name',
  31. ];
  32. $access = app(AccessService::class);
  33. foreach ($requiredFields as $field) {
  34. if (!$access->canUpdateField($this->user(), 'catalog', $field)) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. /**
  41. * Get the validation rules that apply to the request.
  42. *
  43. * @return array
  44. */
  45. public function rules(): array
  46. {
  47. $rules = [
  48. 'article' => 'required|string',
  49. 'name_tz' => 'required|string',
  50. 'type_tz' => 'required|string',
  51. 'nomenclature_number' => 'required|string',
  52. 'sizes' => 'required|string',
  53. 'manufacturer' => 'required|string',
  54. 'unit' => 'required|string',
  55. 'type' => 'required|string',
  56. 'product_price' => 'required|numeric',
  57. 'installation_price' => 'required|numeric',
  58. 'total_price' => 'required|numeric',
  59. 'manufacturer_name' => 'required|string',
  60. 'note' => 'nullable|string',
  61. 'passport_name' => 'nullable|string',
  62. 'statement_name' => 'nullable|string',
  63. 'service_life' => 'nullable|string',
  64. 'certificate_number' => 'nullable|string',
  65. 'certificate_date' => 'nullable|string',
  66. 'certificate_issuer' => 'nullable|string',
  67. 'certificate_type' => 'nullable|string',
  68. 'weight' => 'nullable|numeric',
  69. 'volume' => 'nullable|nullable',
  70. 'places' => 'nullable|integer',
  71. ];
  72. $access = app(AccessService::class);
  73. foreach (array_keys($rules) as $field) {
  74. if (!$access->canUpdateField($this->user(), 'catalog', $field)) {
  75. unset($rules[$field]);
  76. }
  77. }
  78. return $rules;
  79. }
  80. }