1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Subscription/UpgradePrice.php

102 lines
2.9 KiB
PHP
Raw Normal View History

2024-02-08 03:56:50 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Subscription;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Subscription;
use App\Models\RecurringInvoice;
use App\Services\AbstractService;
class UpgradePrice extends AbstractService
2024-02-13 05:25:18 +01:00
{
2024-02-08 03:56:50 +01:00
protected \App\Services\Subscription\SubscriptionStatus $status;
2024-02-08 08:42:38 +01:00
public float $upgrade_price = 0;
public float $refund = 0;
public float $outstanding_credit = 0;
2024-02-08 03:56:50 +01:00
public function __construct(protected RecurringInvoice $recurring_invoice, public Subscription $subscription)
{
}
2024-02-08 08:42:38 +01:00
public function run(): self
2024-02-08 03:56:50 +01:00
{
$this->status = $this->recurring_invoice
->subscription
->status($this->recurring_invoice);
2024-02-13 05:25:18 +01:00
if($this->status->is_in_good_standing) {
2024-02-08 08:42:38 +01:00
$this->calculateUpgrade();
2024-02-13 05:25:18 +01:00
} else {
2024-02-08 08:42:38 +01:00
$this->upgrade_price = $this->subscription->price;
2024-02-13 05:25:18 +01:00
}
2024-02-08 08:42:38 +01:00
return $this;
2024-02-13 05:25:18 +01:00
2024-02-08 03:56:50 +01:00
}
2024-02-08 08:42:38 +01:00
private function calculateUpgrade(): self
2024-02-08 03:56:50 +01:00
{
$ratio = $this->status->getProRataRatio();
$last_invoice = $this->recurring_invoice
->invoices()
->where('is_deleted', 0)
->where('is_proforma', 0)
->orderBy('id', 'desc')
->first();
2024-02-13 05:25:18 +01:00
2024-02-08 08:42:38 +01:00
$this->refund = $this->getRefundableAmount($last_invoice, $ratio);
$this->outstanding_credit = $this->getCredits();
2024-02-13 05:25:18 +01:00
2024-02-08 08:42:38 +01:00
nlog("{$this->subscription->price} - {$this->refund} - {$this->outstanding_credit}");
2024-02-08 03:56:50 +01:00
2024-02-08 08:42:38 +01:00
$this->upgrade_price = $this->subscription->price - $this->refund - $this->outstanding_credit;
2024-02-08 03:56:50 +01:00
2024-02-08 08:42:38 +01:00
return $this;
2024-02-08 03:56:50 +01:00
}
private function getRefundableAmount(?Invoice $invoice, float $ratio): float
{
2024-02-13 05:25:18 +01:00
if (!$invoice || !$invoice->date || $invoice->status_id != Invoice::STATUS_PAID || $ratio == 0) {
2024-02-08 03:56:50 +01:00
return 0;
2024-02-13 05:25:18 +01:00
}
2024-02-08 03:56:50 +01:00
2024-02-13 05:25:18 +01:00
return max(0, round(($invoice->paid_to_date * $ratio), 2));
2024-02-08 03:56:50 +01:00
}
private function getCredits(): float
{
$outstanding_credits = 0;
$use_credit_setting = $this->recurring_invoice->client->getSetting('use_credits_payment');
2024-02-13 05:25:18 +01:00
if($use_credit_setting) {
2024-02-08 03:56:50 +01:00
$outstanding_credits = Credit::query()
->where('client_id', $this->recurring_invoice->client_id)
->whereIn('status_id', [Credit::STATUS_SENT,Credit::STATUS_PARTIAL])
->where('is_deleted', 0)
->where('balance', '>', 0)
->sum('balance');
}
return $outstanding_credits;
}
2024-02-13 05:25:18 +01:00
}