Product.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\SoftDeletes;
  12. #[ScopedBy([YearScope::class])]
  13. class Product extends Model
  14. {
  15. use SoftDeletes;
  16. const DEFAULT_SORT_BY = 'article';
  17. protected $fillable = [
  18. 'year',
  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. 'note',
  32. 'certificate_id',
  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 = [
  45. 'product_price',
  46. 'installation_price',
  47. 'total_price',
  48. 'product_price_txt',
  49. 'installation_price_txt',
  50. 'total_price_txt',
  51. 'image'
  52. ];
  53. protected function productPrice(): 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 installationPrice(): 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. protected function totalPrice(): Attribute
  68. {
  69. return Attribute::make(
  70. get: fn(int $value) => (float) $value / 100,
  71. set: fn (float $value) => (int) ($value * 100),
  72. );
  73. }
  74. public function productPriceTxt(): Attribute
  75. {
  76. return Attribute::make(
  77. get: fn($value) => Price::format($this->product_price),
  78. );
  79. }
  80. public function installationPriceTxt(): Attribute
  81. {
  82. return Attribute::make(
  83. get: fn($value) => Price::format($this->installation_price),
  84. );
  85. }
  86. protected function totalPriceTxt(): Attribute
  87. {
  88. return Attribute::make(
  89. get: fn($value) => Price::format($this->total_price),
  90. );
  91. }
  92. public function commonName(): Attribute
  93. {
  94. return Attribute::make(
  95. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  96. );
  97. }
  98. /**
  99. * @return BelongsToMany
  100. */
  101. public function orders(): BelongsToMany
  102. {
  103. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  104. }
  105. public function certificate(): BelongsTo
  106. {
  107. return $this->belongsTo(File::class, 'certificate_id', 'id');
  108. }
  109. public function maf_orders(): HasMany
  110. {
  111. return $this->hasMany(MafOrder::class, 'product_id', 'id');
  112. }
  113. public function hasRelations(): bool
  114. {
  115. if($this->maf_orders && ($this->maf_orders->count() > 0)) {
  116. return true;
  117. }
  118. if($this->orders && ($this->orders->count() > 0)) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. public function image(): Attribute
  124. {
  125. if(file_exists(public_path() . '/images/main/' . $this->article . '.0000.0000.jpg')) {
  126. $path = url('/images/main/' . $this->article . '.0000.0000.jpg');
  127. } else {
  128. $path = '';
  129. }
  130. return Attribute::make(
  131. get: fn() => $path,
  132. );
  133. }
  134. }