mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
Tests for pro rata refunds
This commit is contained in:
parent
5579cee330
commit
17507fd258
@ -299,6 +299,24 @@ class SubscriptionService
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the pro rata refund between two dates
|
||||
* using a daily calculation.
|
||||
*
|
||||
* @param float $amount
|
||||
* @param Carbon $from_date
|
||||
* @param Carbon $to_date
|
||||
* @param int $frequency The billing interval
|
||||
* @return float
|
||||
*/
|
||||
private function proRataRefund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
||||
{
|
||||
$days = $from_date->diffInDays($to_date);
|
||||
$days_in_frequency = $this->getDaysInFrequencySpecific($frequency);
|
||||
|
||||
return round( ($days/$days_in_frequency) * $amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns refundable set of line items
|
||||
* transformed for direct injection into
|
||||
@ -934,6 +952,41 @@ class SubscriptionService
|
||||
|
||||
}
|
||||
|
||||
private function getDaysInFrequencySpecific($frequency)
|
||||
{
|
||||
|
||||
switch ($frequency) {
|
||||
case RecurringInvoice::FREQUENCY_DAILY:
|
||||
return 1;
|
||||
case RecurringInvoice::FREQUENCY_WEEKLY:
|
||||
return 7;
|
||||
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
||||
return 14;
|
||||
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
||||
return now()->diffInDays(now()->addWeeks(4));
|
||||
case RecurringInvoice::FREQUENCY_MONTHLY:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow());
|
||||
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(2));
|
||||
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(3));
|
||||
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(4));
|
||||
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(6));
|
||||
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
||||
return now()->diffInDays(now()->addYear());
|
||||
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
||||
return now()->diffInDays(now()->addYears(2));
|
||||
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
||||
return now()->diffInDays(now()->addYears(3));
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 'email' => $this->email ?? $this->contact->email,
|
||||
|
95
tests/Unit/RefundUnitTest.php
Normal file
95
tests/Unit/RefundUnitTest.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Utils\Ninja;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
class RefundUnitTest extends TestCase
|
||||
{
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testProRataRefundMonthly()
|
||||
{
|
||||
$refund = $this->proRataRefund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
|
||||
|
||||
$this->assertEquals(9.68, $refund);
|
||||
|
||||
$this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
|
||||
|
||||
}
|
||||
|
||||
public function testProRataRefundYearly()
|
||||
{
|
||||
|
||||
$refund = $this->proRataRefund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
|
||||
|
||||
$this->assertEquals(0.82, $refund);
|
||||
}
|
||||
|
||||
public function testDiffInDays()
|
||||
{
|
||||
|
||||
$this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
|
||||
|
||||
}
|
||||
|
||||
private function proRataRefund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
||||
{
|
||||
$days = $from_date->diffInDays($to_date);
|
||||
$days_in_frequency = $this->getDaysInFrequencySpecific($frequency);
|
||||
|
||||
return round( (($days/$days_in_frequency) * $amount),2);
|
||||
}
|
||||
|
||||
private function getDaysInFrequencySpecific($frequency)
|
||||
{
|
||||
|
||||
switch ($frequency) {
|
||||
case RecurringInvoice::FREQUENCY_DAILY:
|
||||
return 1;
|
||||
case RecurringInvoice::FREQUENCY_WEEKLY:
|
||||
return 7;
|
||||
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
||||
return 14;
|
||||
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
||||
return now()->diffInDays(now()->addWeeks(4));
|
||||
case RecurringInvoice::FREQUENCY_MONTHLY:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow());
|
||||
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(2));
|
||||
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(3));
|
||||
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(4));
|
||||
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
||||
return now()->diffInDays(now()->addMonthNoOverflow(6));
|
||||
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
||||
return now()->diffInDays(now()->addYear());
|
||||
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
||||
return now()->diffInDays(now()->addYears(2));
|
||||
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
||||
return now()->diffInDays(now()->addYears(3));
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user