Product.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }