Product.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 'manufacturer',
  24. 'unit',
  25. 'type',
  26. 'product_price',
  27. 'installation_price',
  28. 'total_price',
  29. 'manufacturer_name',
  30. 'note',
  31. ];
  32. // set year attribute to current selected year
  33. protected static function boot(): void
  34. {
  35. parent::boot();
  36. static::creating(function($attributes) {
  37. if(!isset($attributes->year)) {
  38. $attributes->year = year();
  39. }
  40. });
  41. }
  42. protected $appends = ['product_price', 'installation_price', 'total_price',
  43. 'product_price_txt', 'installation_price_txt', 'total_price_txt',];
  44. protected function productPrice(): Attribute
  45. {
  46. return Attribute::make(
  47. get: fn(int $value) => (float) $value / 100,
  48. set: fn (float $value) => (int) ($value * 100),
  49. );
  50. }
  51. protected function installationPrice(): Attribute
  52. {
  53. return Attribute::make(
  54. get: fn(int $value) => (float) $value / 100,
  55. set: fn (float $value) => (int) ($value * 100),
  56. );
  57. }
  58. protected function totalPrice(): Attribute
  59. {
  60. return Attribute::make(
  61. get: fn(int $value) => (float) $value / 100,
  62. set: fn (float $value) => (int) ($value * 100),
  63. );
  64. }
  65. public function productPriceTxt(): Attribute
  66. {
  67. return Attribute::make(
  68. get: fn($value) => Price::format($this->product_price),
  69. );
  70. }
  71. public function installationPriceTxt(): Attribute
  72. {
  73. return Attribute::make(
  74. get: fn($value) => Price::format($this->installation_price),
  75. );
  76. }
  77. protected function totalPriceTxt(): Attribute
  78. {
  79. return Attribute::make(
  80. get: fn($value) => Price::format($this->total_price),
  81. );
  82. }
  83. public function commonName(): Attribute
  84. {
  85. return Attribute::make(
  86. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  87. // get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  88. );
  89. }
  90. /**
  91. * @return BelongsToMany
  92. */
  93. public function orders(): BelongsToMany
  94. {
  95. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  96. }
  97. }