Product.php 4.0 KB

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