Working on Pro Rata Refunds

This commit is contained in:
David Bomba 2021-04-10 14:53:16 +10:00
parent 62401555cd
commit d91e8c438e
2 changed files with 49 additions and 1 deletions

View File

@ -220,6 +220,11 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_to_refund = $start_date->diffInDays($current_date); $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) public function createChangePlanInvoice($data)
@ -399,4 +404,38 @@ class SubscriptionService
return 1; 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;
}
}
} }

View File

@ -42,6 +42,15 @@ class DatesTest extends TestCase
$diff_in_days = $start_date->diffInDays($current_date); $diff_in_days = $start_date->diffInDays($current_date);
$this->assertEquals(19, $diff_in_days); $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);
} }
} }