Product.php 2.4 KB

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