Product.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Models;
  3. use App\Helpers\Price;
  4. use App\Models\Scopes\YearScope;
  5. use Illuminate\Database\Eloquent\Attributes\ScopedBy;
  6. use Illuminate\Database\Eloquent\Casts\Attribute;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\Relations\HasOne;
  12. use Illuminate\Database\Eloquent\SoftDeletes;
  13. use Illuminate\Support\Facades\DB;
  14. #[ScopedBy([YearScope::class])]
  15. class Product extends Model
  16. {
  17. use SoftDeletes;
  18. const DEFAULT_SORT_BY = 'article';
  19. protected $fillable = [
  20. 'year',
  21. 'article',
  22. 'name_tz',
  23. 'type_tz',
  24. 'nomenclature_number',
  25. 'sizes',
  26. 'manufacturer',
  27. 'unit',
  28. 'type',
  29. 'product_price',
  30. 'installation_price',
  31. 'total_price',
  32. 'manufacturer_name',
  33. 'note',
  34. ];
  35. // set year attribute to current selected year
  36. protected static function boot(): void
  37. {
  38. parent::boot();
  39. static::creating(function($attributes) {
  40. if(!isset($attributes->year)) {
  41. $attributes->year = year();
  42. }
  43. });
  44. }
  45. protected $appends = ['product_price', 'installation_price', 'total_price',
  46. 'product_price_txt', 'installation_price_txt', 'total_price_txt',];
  47. protected function productPrice(): Attribute
  48. {
  49. return Attribute::make(
  50. get: fn(int $value) => (float) $value / 100,
  51. set: fn (float $value) => (int) ($value * 100),
  52. );
  53. }
  54. protected function installationPrice(): Attribute
  55. {
  56. return Attribute::make(
  57. get: fn(int $value) => (float) $value / 100,
  58. set: fn (float $value) => (int) ($value * 100),
  59. );
  60. }
  61. protected function totalPrice(): Attribute
  62. {
  63. return Attribute::make(
  64. get: fn(int $value) => (float) $value / 100,
  65. set: fn (float $value) => (int) ($value * 100),
  66. );
  67. }
  68. public function productPriceTxt(): Attribute
  69. {
  70. return Attribute::make(
  71. get: fn($value) => Price::format($this->product_price),
  72. );
  73. }
  74. public function installationPriceTxt(): Attribute
  75. {
  76. return Attribute::make(
  77. get: fn($value) => Price::format($this->installation_price),
  78. );
  79. }
  80. protected function totalPriceTxt(): Attribute
  81. {
  82. return Attribute::make(
  83. get: fn($value) => Price::format($this->total_price),
  84. );
  85. }
  86. public function commonName(): Attribute
  87. {
  88. return Attribute::make(
  89. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  90. // get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  91. );
  92. }
  93. /**
  94. * @return BelongsToMany
  95. */
  96. public function orders(): BelongsToMany
  97. {
  98. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  99. }
  100. public function maf_orders(): HasMany
  101. {
  102. return $this->hasMany(MafOrder::class, 'product_id', 'id');
  103. }
  104. public function hasRelations(): bool
  105. {
  106. if($this->maf_orders && ($this->maf_orders->count() > 0)) {
  107. return true;
  108. }
  109. if($this->orders && ($this->orders->count() > 0)) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. }