Product.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\BelongsToMany;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. use Illuminate\Support\Facades\DB;
  11. #[ScopedBy([YearScope::class])]
  12. class Product extends Model
  13. {
  14. use SoftDeletes;
  15. const DEFAULT_SORT_BY = 'article';
  16. protected $fillable = [
  17. 'year',
  18. 'article',
  19. 'name_tz',
  20. 'type_tz',
  21. 'nomenclature_number',
  22. 'sizes',
  23. 'type',
  24. 'product_price',
  25. 'installation_price',
  26. 'total_price',
  27. 'manufacturer_name',
  28. 'note',
  29. ];
  30. // set year attribute to current selected year
  31. protected static function boot(): void
  32. {
  33. parent::boot();
  34. static::creating(function($attributes) {
  35. if(!isset($attributes->year)) {
  36. $attributes->year = year();
  37. }
  38. });
  39. }
  40. protected $appends = ['product_price', 'installation_price', 'total_price',
  41. 'product_price_txt', 'installation_price_txt', 'total_price_txt',];
  42. protected function productPrice(): Attribute
  43. {
  44. return Attribute::make(
  45. get: fn(int $value) => (float) $value / 100,
  46. set: fn (float $value) => (int) ($value * 100),
  47. );
  48. }
  49. protected function installationPrice(): Attribute
  50. {
  51. return Attribute::make(
  52. get: fn(int $value) => (float) $value / 100,
  53. set: fn (float $value) => (int) ($value * 100),
  54. );
  55. }
  56. protected function totalPrice(): Attribute
  57. {
  58. return Attribute::make(
  59. get: fn(int $value) => (float) $value / 100,
  60. set: fn (float $value) => (int) ($value * 100),
  61. );
  62. }
  63. public function productPriceTxt(): Attribute
  64. {
  65. return Attribute::make(
  66. get: fn($value) => Price::format($this->product_price),
  67. );
  68. }
  69. public function installationPriceTxt(): Attribute
  70. {
  71. return Attribute::make(
  72. get: fn($value) => Price::format($this->installation_price),
  73. );
  74. }
  75. protected function totalPriceTxt(): Attribute
  76. {
  77. return Attribute::make(
  78. get: fn($value) => Price::format($this->total_price),
  79. );
  80. }
  81. public function commonName(): Attribute
  82. {
  83. return Attribute::make(
  84. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  85. // get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  86. );
  87. }
  88. /**
  89. * @return BelongsToMany
  90. */
  91. public function orders(): BelongsToMany
  92. {
  93. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  94. }
  95. }