ProductSKU.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Scopes\YearScope;
  4. use Illuminate\Database\Eloquent\Attributes\ScopedBy;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. #[ScopedBy([YearScope::class])]
  9. class ProductSKU extends Model
  10. {
  11. use SoftDeletes;
  12. const DEFAULT_SORT_BY = 'created_at';
  13. protected $fillable = [
  14. 'year',
  15. 'product_id',
  16. 'order_id',
  17. 'maf_order_id',
  18. 'status', // needs - нуждается в данном товаре, related - привязана к MafOrder
  19. 'rfid',
  20. 'factory_number',
  21. 'manufacture_date',
  22. 'service_life',
  23. 'certificate_number',
  24. 'certificate_date',
  25. 'certificate_issuer',
  26. 'certificate_type',
  27. ];
  28. public $table = 'products_sku';
  29. // set year attribute to current selected year
  30. protected static function boot(): void
  31. {
  32. parent::boot();
  33. static::creating(function($attributes) {
  34. if(!isset($attributes->year)) {
  35. $attributes->year = year();
  36. }
  37. });
  38. }
  39. /**
  40. * @return BelongsTo
  41. */
  42. public function product(): BelongsTo
  43. {
  44. return $this->belongsTo(Product::class, 'product_id', 'id');
  45. }
  46. /**
  47. * @return BelongsTo
  48. */
  49. public function order(): BelongsTo
  50. {
  51. return $this->belongsTo(Order::class, 'order_id', 'id');
  52. }
  53. /**
  54. * @return BelongsTo
  55. */
  56. public function maf_order(): BelongsTo
  57. {
  58. return $this->belongsTo(MafOrder::class, 'maf_order_id', 'id');
  59. }
  60. }