ProductSKU.php 1.9 KB

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