File size: 1,325 Bytes
70ba896 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class RevenueTracking extends Model
{
protected $table = 'revenue_tracking';
protected $fillable = [
'order_id',
'amount',
'cost',
'profit_margin',
'product_name',
'quantity'
];
protected $casts = [
'amount' => 'decimal:2',
'cost' => 'decimal:2',
'profit_margin' => 'decimal:4'
];
// Relationships
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
// Calculate profit amount
public function getProfitAttribute()
{
return $this->amount - $this->cost;
}
// Calculate profit margin percentage
public function getProfitMarginPercentageAttribute()
{
if ($this->amount > 0) {
return (($this->amount - $this->cost) / $this->amount) * 100;
}
return 0;
}
// Scope for today's revenue
public function scopeToday($query)
{
return $query->whereDate('created_at', today());
}
// Scope for date range
public function scopeDateRange($query, $startDate, $endDate)
{
return $query->whereBetween('created_at', [$startDate, $endDate]);
}
}
|