Product.php 3.5 KB

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