1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/Helpers/Subscription/SubscriptionCalculator.php

97 lines
2.9 KiB
PHP
Raw Normal View History

2021-10-21 04:03:41 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-21 04:03:41 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Subscription;
2021-10-21 06:08:46 +02:00
use App\Helpers\Invoice\ProRata;
2021-10-21 04:03:41 +02:00
use App\Models\Invoice;
use App\Models\Subscription;
2021-10-21 06:51:36 +02:00
use Illuminate\Support\Carbon;
2021-10-21 04:03:41 +02:00
/**
* SubscriptionCalculator.
*/
class SubscriptionCalculator
2021-10-21 04:03:41 +02:00
{
2021-10-21 06:08:46 +02:00
public Subscription $target_subscription;
2021-10-21 06:51:36 +02:00
public Invoice $invoice;
2021-10-21 06:08:46 +02:00
public function __construct(Subscription $target_subscription, Invoice $invoice)
2021-10-21 04:03:41 +02:00
{
2021-10-21 06:08:46 +02:00
$this->target_subscription = $target_subscription;
$this->invoice = $invoice;
2021-10-21 04:03:41 +02:00
}
/**
* Tests if the user is currently up
* to date with their payments for
* a given recurring invoice
*
2021-10-21 04:03:41 +02:00
* @return bool
*/
2024-01-14 05:05:00 +01:00
public function isPaidUp(): bool
2021-10-21 04:03:41 +02:00
{
2023-08-06 09:03:12 +02:00
$outstanding_invoices_exist = Invoice::query()->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
2021-10-21 06:08:46 +02:00
->where('subscription_id', $this->invoice->subscription_id)
->where('client_id', $this->invoice->client_id)
->where('balance', '>', 0)
->exists();
2021-10-21 04:03:41 +02:00
return ! $outstanding_invoices_exist;
2021-10-21 04:03:41 +02:00
}
2021-10-21 06:08:46 +02:00
public function calcUpgradePlan()
{
//set the starting refund amount
$refund_amount = 0;
2021-10-21 06:51:36 +02:00
$refund_invoice = false;
2021-10-21 06:08:46 +02:00
//are they paid up to date.
//yes - calculate refund
if ($this->isPaidUp()) {
2021-10-21 06:08:46 +02:00
$refund_invoice = $this->getRefundInvoice();
}
2021-10-21 06:08:46 +02:00
if ($refund_invoice) {
2023-08-03 06:30:14 +02:00
/** @var \App\Models\Subscription $subscription **/
2021-10-21 06:08:46 +02:00
$subscription = Subscription::find($this->invoice->subscription_id);
2024-01-14 05:05:00 +01:00
$pro_rata = new ProRata();
2021-10-21 06:08:46 +02:00
$to_date = $subscription->service()->getNextDateForFrequency(Carbon::parse($refund_invoice->date), $subscription->frequency_id);
2021-10-21 06:08:46 +02:00
$refund_amount = $pro_rata->refund($refund_invoice->amount, now(), $to_date, $subscription->frequency_id);
$charge_amount = $pro_rata->charge($this->target_subscription->price, now(), $to_date, $this->target_subscription->frequency_id);
return $charge_amount - $refund_amount;
2021-10-21 06:08:46 +02:00
}
//no - return full freight charge.
return $this->target_subscription->price;
}
public function executeUpgradePlan()
{
}
private function getRefundInvoice()
{
return Invoice::where('subscription_id', $this->invoice->subscription_id)
->where('client_id', $this->invoice->client_id)
->where('is_deleted', 0)
->orderBy('id', 'desc')
->first();
}
}