Product.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 'article',
  16. 'name_tz',
  17. 'type_tz',
  18. 'nomenclature_number',
  19. 'sizes',
  20. 'manufacturer',
  21. 'unit',
  22. 'type',
  23. 'product_price',
  24. 'installation_price',
  25. 'total_price',
  26. 'manufacturer_name',
  27. 'note',
  28. ];
  29. protected $appends = ['product_price', 'installation_price', 'total_price',
  30. 'product_price_txt', 'installation_price_txt', 'total_price_txt',];
  31. protected function productPrice(): Attribute
  32. {
  33. return Attribute::make(
  34. get: fn(int $value) => (float) $value / 100,
  35. set: fn (float $value) => (int) ($value * 100),
  36. );
  37. }
  38. protected function installationPrice(): Attribute
  39. {
  40. return Attribute::make(
  41. get: fn(int $value) => (float) $value / 100,
  42. set: fn (float $value) => (int) ($value * 100),
  43. );
  44. }
  45. protected function servicePrice(): Attribute
  46. {
  47. return Attribute::make(
  48. get: fn(int $value) => (float) $value / 100,
  49. set: fn (float $value) => (int) ($value * 100),
  50. );
  51. }
  52. protected function totalPrice(): Attribute
  53. {
  54. return Attribute::make(
  55. get: fn(int $value) => (float) $value / 100,
  56. set: fn (float $value) => (int) ($value * 100),
  57. );
  58. }
  59. public function productPriceTxt(): Attribute
  60. {
  61. return Attribute::make(
  62. get: fn($value) => Price::format($this->product_price),
  63. );
  64. }
  65. public function installationPriceTxt(): Attribute
  66. {
  67. return Attribute::make(
  68. get: fn($value) => Price::format($this->installation_price),
  69. );
  70. }
  71. protected function totalPriceTxt(): Attribute
  72. {
  73. return Attribute::make(
  74. get: fn($value) => Price::format($this->total_price),
  75. );
  76. }
  77. public function commonName(): Attribute
  78. {
  79. return Attribute::make(
  80. get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  81. );
  82. }
  83. /**
  84. * @return BelongsToMany
  85. */
  86. public function orders(): BelongsToMany
  87. {
  88. return $this->belongsToMany(Order::class, 'order_product');
  89. }
  90. }