diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 44ff80daf285..5442488dbc39 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -220,6 +220,11 @@ class SubscriptionService $current_date = now(); $days_to_refund = $start_date->diffInDays($current_date); + $days_in_frequency = $this->getDaysInFrequency(); + + $pro_rata_refund = round(($days_to_refund/$days_in_frequency) * $invoice->amount ,2); + + return $pro_rata_refund; } public function createChangePlanInvoice($data) @@ -399,4 +404,38 @@ class SubscriptionService return 1; } + + private function getDaysInFrequency() + { + + switch ($this->subscription->frequency_id) { + case self::FREQUENCY_DAILY: + return 1; + case self::FREQUENCY_WEEKLY: + return 7; + case self::FREQUENCY_TWO_WEEKS: + return 14; + case self::FREQUENCY_FOUR_WEEKS: + return now()->diffInDays(now()->addWeeks(4)); + case self::FREQUENCY_MONTHLY: + return now()->diffInDays(now()->addMonthNoOverflow()); + case self::FREQUENCY_TWO_MONTHS: + return now()->diffInDays(now()->addMonthNoOverflow(2)); + case self::FREQUENCY_THREE_MONTHS: + return now()->diffInDays(now()->addMonthNoOverflow(3)); + case self::FREQUENCY_FOUR_MONTHS: + return now()->diffInDays(now()->addMonthNoOverflow(4)); + case self::FREQUENCY_SIX_MONTHS: + return now()->diffInDays(now()->addMonthNoOverflow(6)); + case self::FREQUENCY_ANNUALLY: + return now()->diffInDays(now()->addYear()); + case self::FREQUENCY_TWO_YEARS: + return now()->diffInDays(now()->addYears(2)); + case self::FREQUENCY_THREE_YEARS: + return now()->diffInDays(now()->addYears(3)); + default: + return 0; + } + + } } diff --git a/tests/Unit/DatesTest.php b/tests/Unit/DatesTest.php index c76f7aca980e..ebf6e1dd17b2 100644 --- a/tests/Unit/DatesTest.php +++ b/tests/Unit/DatesTest.php @@ -42,6 +42,15 @@ class DatesTest extends TestCase $diff_in_days = $start_date->diffInDays($current_date); $this->assertEquals(19, $diff_in_days); -; + + } + + public function testDiffInDaysRange() + { + $now = Carbon::parse('2020-01-01'); + + $x = now()->diffInDays(now()->addDays(7)); + + $this->assertEquals(7, $x); } }