12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Helpers\CurrencyFormat;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Casts\Attribute;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. class Mutation extends Model
  9. {
  10. use HasFactory, CurrencyFormat;
  11. protected $fillable = [
  12. 'type',
  13. 'amount',
  14. 'outlet_id',
  15. 'transaction_id',
  16. 'expense_id',
  17. ];
  18. public function createdAt(): Attribute
  19. {
  20. return Attribute::make(
  21. get:fn($value) => Carbon::parse($value)->translatedFormat('l d/m/Y')
  22. );
  23. }
  24. public function amount(): Attribute
  25. {
  26. return Attribute::make(
  27. get:fn($value) => $this->transaction_id ? $this->setRupiahFormat($value, 2, true)
  28. : '- ' . $this->setRupiahFormat($value, 2, true)
  29. );
  30. }
  31. public function type(): Attribute
  32. {
  33. return Attribute::make(
  34. get:fn($value) => $value == 1 ? __('words.income') : __('words.expense')
  35. );
  36. }
  37. public function outlet()
  38. {
  39. return $this->belongsTo(Outlet::class);
  40. }
  41. public function scopeFilter($query, array $filters)
  42. {
  43. $query->when($filters['startDate'] ?? null, function ($query, $date) {
  44. $query->whereDate('created_at', '>=', $date);
  45. })->when($filters['endDate'] ?? null, function ($query, $date) {
  46. $query->whereDate('created_at', '<=', $date);
  47. })->when($filters['outlet'] ?? null, function ($query, $outlet) {
  48. $query->where('outlet_id', $outlet);
  49. });
  50. }
  51. }