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

Fixes for calculating pro rata refunds for subscriptions

This commit is contained in:
David Bomba 2023-01-19 13:20:05 +11:00
parent 4364f0d168
commit a382dbe1b6
2 changed files with 18 additions and 1 deletions

View File

@ -29,6 +29,8 @@ class UpdateOrCreateProduct implements ShouldQueue
public $company;
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*

View File

@ -331,6 +331,7 @@ class SubscriptionService
* We refund unused days left.
*
* @param Invoice $invoice
*
* @return float
*/
private function calculateProRataRefundForSubscription($invoice) :float
@ -338,6 +339,20 @@ class SubscriptionService
if(!$invoice || !$invoice->date || $invoice->status_id != Invoice::STATUS_PAID)
return 0;
/*Remove previous refunds from the calculation of the amount*/
$invoice->line_items = collect($invoice->line_items)->filter(function($item){
if($item->product_key == ctrans("texts.refund"))
{
return false;
}
return true;
})->toArray();
$amount = $invoice->calc()->getTotal();
$start_date = Carbon::parse($invoice->date);
$current_date = now();
@ -346,7 +361,7 @@ class SubscriptionService
$days_in_frequency = $this->getDaysInFrequency();
$pro_rata_refund = round((($days_in_frequency - $days_of_subscription_used)/$days_in_frequency) * $invoice->amount ,2);
$pro_rata_refund = round((($days_in_frequency - $days_of_subscription_used)/$days_in_frequency) * $amount ,2);
return max(0, $pro_rata_refund);