Product.php 3.8 KB

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