1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Refactor Refunds

This commit is contained in:
David Bomba 2021-10-21 12:40:59 +11:00
parent 17507fd258
commit bcf34a6e62
3 changed files with 67 additions and 97 deletions

View File

@ -0,0 +1,62 @@
<?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://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Invoice;
use App\Models\RecurringInvoice;
use Illuminate\Support\Carbon;
class Refund
{
public function proRata(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
{
$days = $from_date->diffInDays($to_date);
$days_in_frequency = $this->getDaysInFrequency($frequency);
return round( (($days/$days_in_frequency) * $amount),2);
}
private function getDaysInFrequency($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;
}
}
}

View File

@ -299,24 +299,6 @@ 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
@ -952,42 +934,6 @@ 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,
* 'quantity' => $this->quantity,

View File

@ -10,6 +10,7 @@
*/
namespace Tests\Unit;
use App\Helpers\Invoice\Refund;
use App\Models\RecurringInvoice;
use App\Utils\Ninja;
use Illuminate\Support\Carbon;
@ -28,7 +29,8 @@ class RefundUnitTest extends TestCase
public function testProRataRefundMonthly()
{
$refund = $this->proRataRefund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
$r = new Refund();
$refund = $r->proRata(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
$this->assertEquals(9.68, $refund);
@ -38,8 +40,9 @@ class RefundUnitTest extends TestCase
public function testProRataRefundYearly()
{
$r = new Refund();
$refund = $this->proRataRefund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
$refund = $r->proRata(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
$this->assertEquals(0.82, $refund);
}
@ -51,45 +54,4 @@ class RefundUnitTest extends TestCase
}
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;
}
}
}