ContractorInstallationPrice.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Models;
  3. use App\Helpers\Price;
  4. use Illuminate\Database\Eloquent\Casts\Attribute;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. class ContractorInstallationPrice extends Model
  9. {
  10. use HasFactory;
  11. protected $fillable = [
  12. 'contractor_id',
  13. 'product_id',
  14. 'catalog_year',
  15. 'name_in_spec',
  16. 'price',
  17. ];
  18. protected $appends = [
  19. 'price_txt',
  20. ];
  21. public function contractor(): BelongsTo
  22. {
  23. return $this->belongsTo(Contractor::class);
  24. }
  25. public function product(): BelongsTo
  26. {
  27. return $this->belongsTo(Product::class, 'product_id')->withoutGlobalScope(\App\Models\Scopes\YearScope::class)->withTrashed();
  28. }
  29. protected function price(): Attribute
  30. {
  31. return Attribute::make(
  32. get: fn(int|null $value) => (float) ($value ?? 0) / 100,
  33. set: fn(float|int|string|null $value) => (int) round(((float) ($value ?? 0)) * 100),
  34. );
  35. }
  36. protected function priceTxt(): Attribute
  37. {
  38. return Attribute::make(
  39. get: fn() => Price::format($this->price),
  40. );
  41. }
  42. }