Product.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. protected $fillable = [
  11. 'year',
  12. 'name_tz',
  13. 'type_tz',
  14. 'nomenclature_number',
  15. 'sizes',
  16. 'manufacturer',
  17. 'unit',
  18. 'type',
  19. 'price_status',
  20. 'product_price',
  21. 'installation_price',
  22. 'service_price',
  23. 'total_price',
  24. 'manufacturer_name',
  25. 'article',
  26. 'note',
  27. ];
  28. protected $appends = ['product_price', 'installation_price', 'service_price', 'total_price',
  29. 'product_price_txt', 'installation_price_txt', 'service_price_txt', 'total_price_txt',];
  30. protected function productPrice(): Attribute
  31. {
  32. return Attribute::make(
  33. get: fn(int $value) => (float) $value / 100,
  34. set: fn (float $value) => (int) ($value * 100),
  35. );
  36. }
  37. protected function installationPrice(): Attribute
  38. {
  39. return Attribute::make(
  40. get: fn(int $value) => (float) $value / 100,
  41. set: fn (float $value) => (int) ($value * 100),
  42. );
  43. }
  44. protected function servicePrice(): Attribute
  45. {
  46. return Attribute::make(
  47. get: fn(int $value) => (float) $value / 100,
  48. set: fn (float $value) => (int) ($value * 100),
  49. );
  50. }
  51. protected function totalPrice(): Attribute
  52. {
  53. return Attribute::make(
  54. get: fn(int $value) => (float) $value / 100,
  55. set: fn (float $value) => (int) ($value * 100),
  56. );
  57. }
  58. public function productPriceTxt(): Attribute
  59. {
  60. return Attribute::make(
  61. get: fn($value) => Price::format($this->product_price),
  62. );
  63. }
  64. public function installationPriceTxt(): Attribute
  65. {
  66. return Attribute::make(
  67. get: fn($value) => Price::format($this->installation_price),
  68. );
  69. }
  70. protected function servicePriceTxt(): Attribute
  71. {
  72. return Attribute::make(
  73. get: fn($value) => Price::format($this->service_price),
  74. );
  75. }
  76. protected function totalPriceTxt(): Attribute
  77. {
  78. return Attribute::make(
  79. get: fn($value) => Price::format($this->total_price),
  80. );
  81. }
  82. }