Product.php 4.1 KB

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