Product.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 'passport_name',
  34. 'statement_name',
  35. 'service_life',
  36. 'certificate_number',
  37. 'certificate_date',
  38. 'certificate_issuer',
  39. 'certificate_type',
  40. 'weight',
  41. 'volume',
  42. 'places',
  43. ];
  44. // set year attribute to current selected year
  45. protected static function boot(): void
  46. {
  47. parent::boot();
  48. static::creating(function($attributes) {
  49. if(!isset($attributes->year)) {
  50. $attributes->year = year();
  51. }
  52. });
  53. }
  54. protected $appends = [
  55. 'product_price',
  56. 'installation_price',
  57. 'total_price',
  58. 'product_price_txt',
  59. 'installation_price_txt',
  60. 'total_price_txt',
  61. 'image'
  62. ];
  63. protected function productPrice(): Attribute
  64. {
  65. return Attribute::make(
  66. get: fn(int|null $value) => (float) $value / 100,
  67. set: fn (float $value) => (int) ($value * 100),
  68. );
  69. }
  70. protected function installationPrice(): Attribute
  71. {
  72. return Attribute::make(
  73. get: fn(int|null $value) => (float) $value / 100,
  74. set: fn (float $value) => (int) ($value * 100),
  75. );
  76. }
  77. protected function totalPrice(): Attribute
  78. {
  79. return Attribute::make(
  80. get: fn(int|null $value) => (float) $value / 100,
  81. set: fn (float $value) => (int) ($value * 100),
  82. );
  83. }
  84. public function productPriceTxt(): Attribute
  85. {
  86. return Attribute::make(
  87. get: fn($value) => Price::format($this->product_price),
  88. );
  89. }
  90. public function installationPriceTxt(): Attribute
  91. {
  92. return Attribute::make(
  93. get: fn($value) => Price::format($this->installation_price),
  94. );
  95. }
  96. protected function totalPriceTxt(): Attribute
  97. {
  98. return Attribute::make(
  99. get: fn($value) => Price::format($this->total_price),
  100. );
  101. }
  102. public function commonName(): Attribute
  103. {
  104. return Attribute::make(
  105. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  106. );
  107. }
  108. /**
  109. * @return BelongsToMany
  110. */
  111. public function orders(): BelongsToMany
  112. {
  113. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  114. }
  115. public function certificate(): BelongsTo
  116. {
  117. return $this->belongsTo(File::class, 'certificate_id', 'id');
  118. }
  119. public function maf_orders(): HasMany
  120. {
  121. return $this->hasMany(MafOrder::class, 'product_id', 'id');
  122. }
  123. public function hasRelations(): bool
  124. {
  125. if($this->maf_orders && ($this->maf_orders->count() > 0)) {
  126. return true;
  127. }
  128. if($this->orders && ($this->orders->count() > 0)) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public function image(): Attribute
  134. {
  135. if(file_exists(public_path() . '/images/main/' . $this->article . '.0000.0000.jpg')) {
  136. $path = url('/images/main/' . $this->article . '.0000.0000.jpg');
  137. } else {
  138. $path = '';
  139. }
  140. return Attribute::make(
  141. get: fn() => $path,
  142. );
  143. }
  144. }