TransactionDetail.php 1.6KB

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