1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

ProRata Refunds

This commit is contained in:
David Bomba 2021-10-21 13:03:41 +11:00
parent bcf34a6e62
commit dfa773d6b9
3 changed files with 113 additions and 7 deletions

View File

@ -11,13 +11,24 @@
namespace App\Helpers\Invoice;
use App\Models\Invoice;
use App\Models\RecurringInvoice;
use Carbon\Exceptions\InvalidFormatException;
use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Carbon;
class Refund
class ProRata
{
public function proRata(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
/**
* @param float $amount
* @param Carbon $from_date
* @param Carbon $to_date
* @param int $frequency
* @return float
*/
public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
{
$days = $from_date->diffInDays($to_date);
$days_in_frequency = $this->getDaysInFrequency($frequency);
@ -25,6 +36,53 @@ class Refund
return round( (($days/$days_in_frequency) * $amount),2);
}
/**
* Prepares the line items of an invoice
* to be pro rata refunded.
*
* @param Invoice $invoice
* @param bool $is_credit
* @return array
* @throws Exception
*/
public function refundItems(Invoice $invoice, $is_credit = false) :array
{
if(!$invoice)
return [];
$recurring_invoice = RecurringInvoice::find($invoice->recurring_id)->first();
if(!$recurring_invoice)
throw new \Exception("Invoice isn't attached to a recurring invoice");
/* depending on whether we are creating an invoice or a credit*/
$multiplier = $is_credit ? 1 : -1;
$start_date = Carbon::parse($invoice->date);
$line_items = [];
foreach($invoice->line_items as $item)
{
if($item->product_key != ctrans('texts.refund'))
{
$item->quantity = 1;
$item->cost = $this->refund($item->cost*$multiplier, $start_date, now(), $recurring_invoice->frequency_id);
$item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund') . ": ". $item->notes;
$line_items[] = $item;
}
}
return $line_items;
}
private function getDaysInFrequency($frequency)
{

View File

@ -0,0 +1,48 @@
<?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\Subscription;
use App\Models\Invoice;
use App\Models\RecurringInvoice;
use App\Models\Subscription;
/**
* SubscriptionCalculator.
*/
class SubscriptionCalculator
{
public Subscription $subscription;
public function __construct(Subscription $subscription)
{
$this->subscription = $subscription;
}
/**
* Tests if the user is currently up
* to date with their payments for
* a given recurring invoice
*
* @return bool
*/
public function isPaidUp(RecurringInvoice $recurring_invoice) :bool
{
$outstanding_invoices_exist = Invoice::whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('recurring_id', $recurring_invoice->id)
->where('balance', '>', 0)
->exists();
return ! $outstanding_invoices_exist;
}
}

View File

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