TransactionDetail.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Helpers\CurrencyFormat;
  4. use App\Models\Laundry;
  5. use App\Models\Transaction;
  6. use Illuminate\Database\Eloquent\Casts\Attribute;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. class TransactionDetail extends Model
  10. {
  11. use HasFactory, CurrencyFormat;
  12. protected $fillable = [
  13. 'price',
  14. 'discount',
  15. 'quantity',
  16. 'transaction_id',
  17. 'laundry_id',
  18. ];
  19. protected function price(): Attribute
  20. {
  21. return Attribute::make(
  22. get:fn($value) => $this->setRupiahFormat($value, 2, true)
  23. );
  24. }
  25. protected function discount(): Attribute
  26. {
  27. return Attribute::make(
  28. get:fn($value) => "{$value}%"
  29. );
  30. }
  31. public function transaction()
  32. {
  33. return $this->belongsTo(Transaction::class);
  34. }
  35. public function laundry()
  36. {
  37. return $this->belongsTo(Laundry::class);
  38. }
  39. public function totalPrice()
  40. {
  41. $price = $this->getRawOriginal('price') * $this->quantity;
  42. $totalPrice = $price - $price * ($this->getRawOriginal('discount') / 100);
  43. return $totalPrice;
  44. }
  45. public function totalPriceAsString()
  46. {
  47. return $this->setRupiahFormat($this->totalPrice());
  48. }
  49. public function totalPriceAsFullString()
  50. {
  51. return $this->setRupiahFormat($this->totalPrice(), 2, true);
  52. }
  53. }