chore: fix mrr calculation and grid import
This commit is contained in:
@@ -473,4 +473,79 @@ class Subscription extends Model
|
||||
|
||||
return $processedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Monthly Recurring Revenue (MRR) for this subscription
|
||||
*/
|
||||
public function calculateMRR(): float
|
||||
{
|
||||
// Only active and trialing subscriptions contribute to MRR
|
||||
if (! in_array($this->status, ['active', 'trialing'])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check if subscription has ended
|
||||
if ($this->ends_at && $this->ends_at->isPast()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get the plan's MRR calculation
|
||||
if ($this->plan) {
|
||||
return $this->plan->calculateMRR();
|
||||
}
|
||||
|
||||
// Fallback: try to calculate from legacy data or provider data
|
||||
$price = $this->getLegacyPrice();
|
||||
if ($price > 0) {
|
||||
$cycleDays = $this->getLegacyBillingCycleDays();
|
||||
|
||||
return ($price / $cycleDays) * 30;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get price from legacy data or provider data
|
||||
*/
|
||||
private function getLegacyPrice(): float
|
||||
{
|
||||
// Try to get price from plan first
|
||||
if ($this->plan && $this->plan->price) {
|
||||
return (float) $this->plan->price;
|
||||
}
|
||||
|
||||
// Try provider data
|
||||
if ($this->provider_data && isset($this->provider_data['plan_details']['price'])) {
|
||||
return (float) $this->provider_data['plan_details']['price'];
|
||||
}
|
||||
|
||||
// Try legacy stripe_price field
|
||||
if ($this->stripe_price) {
|
||||
// This would need additional logic to get price from Stripe
|
||||
// For now, return 0 as we can't easily get the amount
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get billing cycle days from legacy data
|
||||
*/
|
||||
private function getLegacyBillingCycleDays(): int
|
||||
{
|
||||
// Try to get from plan first
|
||||
if ($this->plan && $this->plan->billing_cycle_days) {
|
||||
return (int) $this->plan->billing_cycle_days;
|
||||
}
|
||||
|
||||
// Try provider data
|
||||
if ($this->provider_data && isset($this->provider_data['plan_details']['billing_cycle_days'])) {
|
||||
return (int) $this->provider_data['plan_details']['billing_cycle_days'];
|
||||
}
|
||||
|
||||
// Fallback to legacy monthly_billing
|
||||
return $this->plan && $this->plan->monthly_billing ? 30 : 365;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user