Product.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. ];
  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 certificate(): BelongsTo
  108. {
  109. return $this->belongsTo(File::class, 'certificate_id', 'id');
  110. }
  111. public function maf_orders(): HasMany
  112. {
  113. return $this->hasMany(MafOrder::class, 'product_id', 'id');
  114. }
  115. public function hasRelations(): bool
  116. {
  117. if($this->maf_orders && ($this->maf_orders->count() > 0)) {
  118. return true;
  119. }
  120. if($this->orders && ($this->orders->count() > 0)) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. public function image(): Attribute
  126. {
  127. if(file_exists(public_path() . '/images/main/' . $this->article . '.0000.0000.jpg')) {
  128. $path = url('/images/main/' . $this->article . '.0000.0000.jpg');
  129. } else {
  130. $path = '';
  131. }
  132. return Attribute::make(
  133. get: fn() => $path,
  134. );
  135. }
  136. }