Product.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. class Product extends Model
  8. {
  9. use SoftDeletes;
  10. const DEFAULT_SORT_BY = 'article';
  11. protected $fillable = [
  12. 'year',
  13. 'name_tz',
  14. 'type_tz',
  15. 'nomenclature_number',
  16. 'sizes',
  17. 'manufacturer',
  18. 'unit',
  19. 'type',
  20. 'price_status',
  21. 'product_price',
  22. 'installation_price',
  23. 'service_price',
  24. 'total_price',
  25. 'manufacturer_name',
  26. 'article',
  27. 'note',
  28. ];
  29. protected $appends = ['product_price', 'installation_price', 'service_price', 'total_price',
  30. 'product_price_txt', 'installation_price_txt', 'service_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 servicePriceTxt(): Attribute
  72. {
  73. return Attribute::make(
  74. get: fn($value) => Price::format($this->service_price),
  75. );
  76. }
  77. protected function totalPriceTxt(): Attribute
  78. {
  79. return Attribute::make(
  80. get: fn($value) => Price::format($this->total_price),
  81. );
  82. }
  83. }