Product.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\SoftDeletes;
  7. use Illuminate\Support\Facades\DB;
  8. class Product extends Model
  9. {
  10. use SoftDeletes;
  11. const DEFAULT_SORT_BY = 'article';
  12. protected $fillable = [
  13. 'year',
  14. 'name_tz',
  15. 'type_tz',
  16. 'nomenclature_number',
  17. 'sizes',
  18. 'manufacturer',
  19. 'unit',
  20. 'type',
  21. 'price_status',
  22. 'product_price',
  23. 'installation_price',
  24. 'service_price',
  25. 'total_price',
  26. 'manufacturer_name',
  27. 'article',
  28. 'note',
  29. ];
  30. protected $appends = ['product_price', 'installation_price', 'service_price', 'total_price',
  31. 'product_price_txt', 'installation_price_txt', 'service_price_txt', 'total_price_txt',];
  32. protected function productPrice(): Attribute
  33. {
  34. return Attribute::make(
  35. get: fn(int $value) => (float) $value / 100,
  36. set: fn (float $value) => (int) ($value * 100),
  37. );
  38. }
  39. protected function installationPrice(): Attribute
  40. {
  41. return Attribute::make(
  42. get: fn(int $value) => (float) $value / 100,
  43. set: fn (float $value) => (int) ($value * 100),
  44. );
  45. }
  46. protected function servicePrice(): Attribute
  47. {
  48. return Attribute::make(
  49. get: fn(int $value) => (float) $value / 100,
  50. set: fn (float $value) => (int) ($value * 100),
  51. );
  52. }
  53. protected function totalPrice(): Attribute
  54. {
  55. return Attribute::make(
  56. get: fn(int $value) => (float) $value / 100,
  57. set: fn (float $value) => (int) ($value * 100),
  58. );
  59. }
  60. public function productPriceTxt(): Attribute
  61. {
  62. return Attribute::make(
  63. get: fn($value) => Price::format($this->product_price),
  64. );
  65. }
  66. public function installationPriceTxt(): Attribute
  67. {
  68. return Attribute::make(
  69. get: fn($value) => Price::format($this->installation_price),
  70. );
  71. }
  72. protected function servicePriceTxt(): Attribute
  73. {
  74. return Attribute::make(
  75. get: fn($value) => Price::format($this->service_price),
  76. );
  77. }
  78. protected function totalPriceTxt(): Attribute
  79. {
  80. return Attribute::make(
  81. get: fn($value) => Price::format($this->total_price),
  82. );
  83. }
  84. /**
  85. * @param $column
  86. * @return array
  87. */
  88. public static function getFilters($column): array
  89. {
  90. return DB::table('products')
  91. ->select($column)
  92. ->distinct()
  93. ->get()
  94. ->pluck($column)
  95. ->toArray();
  96. }
  97. }