Product.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Models;
  3. use App\Helpers\Price;
  4. use Illuminate\Database\Eloquent\Casts\Attribute;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Illuminate\Support\Facades\DB;
  9. class Product extends Model
  10. {
  11. use SoftDeletes;
  12. const DEFAULT_SORT_BY = 'article';
  13. protected $fillable = [
  14. 'year',
  15. 'name_tz',
  16. 'type_tz',
  17. 'nomenclature_number',
  18. 'sizes',
  19. 'manufacturer',
  20. 'unit',
  21. 'type',
  22. 'price_status',
  23. 'product_price',
  24. 'installation_price',
  25. 'service_price',
  26. 'total_price',
  27. 'manufacturer_name',
  28. 'article',
  29. 'note',
  30. ];
  31. protected $appends = ['product_price', 'installation_price', 'service_price', 'total_price',
  32. 'product_price_txt', 'installation_price_txt', 'service_price_txt', 'total_price_txt',];
  33. protected function productPrice(): Attribute
  34. {
  35. return Attribute::make(
  36. get: fn(int $value) => (float) $value / 100,
  37. set: fn (float $value) => (int) ($value * 100),
  38. );
  39. }
  40. protected function installationPrice(): Attribute
  41. {
  42. return Attribute::make(
  43. get: fn(int $value) => (float) $value / 100,
  44. set: fn (float $value) => (int) ($value * 100),
  45. );
  46. }
  47. protected function servicePrice(): Attribute
  48. {
  49. return Attribute::make(
  50. get: fn(int $value) => (float) $value / 100,
  51. set: fn (float $value) => (int) ($value * 100),
  52. );
  53. }
  54. protected function totalPrice(): Attribute
  55. {
  56. return Attribute::make(
  57. get: fn(int $value) => (float) $value / 100,
  58. set: fn (float $value) => (int) ($value * 100),
  59. );
  60. }
  61. public function productPriceTxt(): Attribute
  62. {
  63. return Attribute::make(
  64. get: fn($value) => Price::format($this->product_price),
  65. );
  66. }
  67. public function installationPriceTxt(): Attribute
  68. {
  69. return Attribute::make(
  70. get: fn($value) => Price::format($this->installation_price),
  71. );
  72. }
  73. protected function servicePriceTxt(): Attribute
  74. {
  75. return Attribute::make(
  76. get: fn($value) => Price::format($this->service_price),
  77. );
  78. }
  79. protected function totalPriceTxt(): Attribute
  80. {
  81. return Attribute::make(
  82. get: fn($value) => Price::format($this->total_price),
  83. );
  84. }
  85. public function commonName(): Attribute
  86. {
  87. return Attribute::make(
  88. get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  89. );
  90. }
  91. /**
  92. * @return BelongsToMany
  93. */
  94. public function orders(): BelongsToMany
  95. {
  96. return $this->belongsToMany(Order::class, 'order_product');
  97. }
  98. }