Product.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 totalPrice(): 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. public function productPriceTxt(): Attribute
  53. {
  54. return Attribute::make(
  55. get: fn($value) => Price::format($this->product_price),
  56. );
  57. }
  58. public function installationPriceTxt(): Attribute
  59. {
  60. return Attribute::make(
  61. get: fn($value) => Price::format($this->installation_price),
  62. );
  63. }
  64. protected function totalPriceTxt(): Attribute
  65. {
  66. return Attribute::make(
  67. get: fn($value) => Price::format($this->total_price),
  68. );
  69. }
  70. public function commonName(): Attribute
  71. {
  72. return Attribute::make(
  73. get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
  74. // get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
  75. );
  76. }
  77. /**
  78. * @return BelongsToMany
  79. */
  80. public function orders(): BelongsToMany
  81. {
  82. return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
  83. }
  84. }