mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Sub v2
This commit is contained in:
parent
735cc552af
commit
4901b31c47
@ -11,7 +11,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\Subscription\PaymentLinkService;
|
||||
use App\Services\Subscription\SubscriptionService;
|
||||
use App\Services\Subscription\SubscriptionStatus;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@ -132,6 +134,16 @@ class Subscription extends BaseModel
|
||||
return new SubscriptionService($this);
|
||||
}
|
||||
|
||||
public function link_service(): PaymentLinkService
|
||||
{
|
||||
return new PaymentLinkService($this);
|
||||
}
|
||||
|
||||
public function status(RecurringInvoice $recurring_invoice): SubscriptionStatus
|
||||
{
|
||||
return (new SubscriptionStatus($this, $recurring_invoice))->run();
|
||||
}
|
||||
|
||||
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
|
81
app/Services/Subscription/PaymentLinkService.php
Normal file
81
app/Services/Subscription/PaymentLinkService.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?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\PaymentHash;
|
||||
use App\Models\Subscription;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\RecurringInvoice;
|
||||
|
||||
class PaymentLinkService
|
||||
{
|
||||
|
||||
public const WHITE_LABEL = 4316;
|
||||
|
||||
public function __construct(public Subscription $subscription)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* CompletePurchase
|
||||
*
|
||||
* Perform the initial purchase of a one time
|
||||
* or recurring product
|
||||
*
|
||||
* @param PaymentHash $payment_hash
|
||||
* @return Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function completePurchase(PaymentHash $payment_hash): \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* isEligible
|
||||
* ["message" => "Success", "status_code" => 200];
|
||||
* @param ClientContact $contact
|
||||
* @return array{"message": string, "status_code": int}
|
||||
*/
|
||||
public function isEligible(ClientContact $contact): array
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/* Starts the process to create a trial
|
||||
- we create a recurring invoice, which has its next_send_date as now() + trial_duration
|
||||
- we then hit the client API end point to advise the trial payload
|
||||
- we then return the user to either a predefined user endpoint, OR we return the user to the recurring invoice page.
|
||||
|
||||
* startTrial
|
||||
*
|
||||
* @param array $data{contact_id: int, client_id: int, bundle: \Illuminate\Support\Collection, coupon?: string, }
|
||||
* @return Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function startTrial(array $data): \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateUpdatePriceV2
|
||||
*
|
||||
* Need to change the naming of the method
|
||||
*
|
||||
* @param RecurringInvoice $recurring_invoice - The Current Recurring Invoice for the subscription.
|
||||
* @param Subscription $target - The new target subscription to move to
|
||||
* @return float - the upgrade price
|
||||
*/
|
||||
public function calculateUpgradePriceV2(RecurringInvoice $recurring_invoice, Subscription $target): ?float
|
||||
{
|
||||
return (new UpgradePrice($recurring_invoice, $target))->run();
|
||||
}
|
||||
}
|
@ -121,7 +121,7 @@ class ProRata extends AbstractService
|
||||
*/
|
||||
private function checkRefundPeriod(): self
|
||||
{
|
||||
if(!$this->subscription->refund_period || $this->subscription->refund_period === 0)
|
||||
if(!$this->recurring_invoice->subscription->refund_period || $this->recurring_invoice->subscription->refund_period === 0)
|
||||
return $this->setRefundable(false);
|
||||
|
||||
$primary_invoice = $this->recurring_invoice
|
||||
@ -133,7 +133,7 @@ class ProRata extends AbstractService
|
||||
|
||||
if($primary_invoice &&
|
||||
$primary_invoice->status_id == Invoice::STATUS_PAID &&
|
||||
Carbon::parse($primary_invoice->date)->addSeconds($this->subscription->refund_period)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset()))
|
||||
Carbon::parse($primary_invoice->date)->addSeconds($this->recurring_invoice->subscription->refund_period)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset()))
|
||||
){
|
||||
return $this->setRefundable(true);
|
||||
}
|
||||
@ -213,7 +213,7 @@ class ProRata extends AbstractService
|
||||
private function isInTrialPeriod(): self
|
||||
{
|
||||
|
||||
if(!$this->subscription->trial_enabled)
|
||||
if(!$this->recurring_invoice->subscription->trial_enabled)
|
||||
return $this->setIsTrial(false);
|
||||
|
||||
$primary_invoice = $this->recurring_invoice
|
||||
@ -223,7 +223,7 @@ class ProRata extends AbstractService
|
||||
->orderBy('id', 'asc')
|
||||
->first();
|
||||
|
||||
if($primary_invoice && Carbon::parse($primary_invoice->date)->addSeconds($this->subscription->trial_duration)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset())))
|
||||
if($primary_invoice && Carbon::parse($primary_invoice->date)->addSeconds($this->recurring_invoice->subscription->trial_duration)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset())))
|
||||
return $this->setIsTrial(true);
|
||||
|
||||
$this->setIsTrial(false);
|
||||
|
169
app/Services/Subscription/SubscriptionStatus.php
Normal file
169
app/Services/Subscription/SubscriptionStatus.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?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\Invoice;
|
||||
use App\Models\Subscription;
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Services\AbstractService;
|
||||
|
||||
class SubscriptionStatus extends AbstractService
|
||||
{
|
||||
public function __construct(public Subscription $subscription, protected RecurringInvoice $recurring_invoice) {}
|
||||
|
||||
/** @var bool $is_trial */
|
||||
public bool $is_trial = false;
|
||||
|
||||
/** @var bool $is_refundable */
|
||||
public bool $is_refundable = false;
|
||||
|
||||
/** @var bool $is_in_good_standing */
|
||||
public bool $is_in_good_standing = false;
|
||||
|
||||
public function run(): self
|
||||
{
|
||||
$this->checkTrial()
|
||||
->checkRefundable()
|
||||
->checkInGoodStanding();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProRataRatio():float
|
||||
{
|
||||
//calculate how much used.
|
||||
|
||||
$primary_invoice = $this->recurring_invoice
|
||||
->invoices()
|
||||
->where('is_deleted', 0)
|
||||
->where('is_proforma', 0)
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
|
||||
if(!$primary_invoice)
|
||||
return 0;
|
||||
|
||||
$subscription_start_date = Carbon::parse($primary_invoice->date)->startOfDay();
|
||||
$subscription_interval_end_date = Carbon::parse($this->recurring_invoice->next_send_date_client);
|
||||
|
||||
$seconds_of_subscription_used = $subscription_start_date->diffInDays(now());
|
||||
$total_seconds_in_subscription_interval = $subscription_start_date->diffInDays($subscription_interval_end_date);
|
||||
|
||||
return $seconds_of_subscription_used / $total_seconds_in_subscription_interval;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* checkInGoodStanding
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function checkInGoodStanding(): self
|
||||
{
|
||||
|
||||
$this->is_in_good_standing = $this->recurring_invoice
|
||||
->invoices()
|
||||
->where('is_deleted', 0)
|
||||
->where('is_proform', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PAID])
|
||||
->where('balance', '>', 0)
|
||||
->doesntExist();
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* checkTrial
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function checkTrial(): self
|
||||
{
|
||||
|
||||
if(!$this->subscription->trial_enabled)
|
||||
$this->setIsTrial(false);
|
||||
|
||||
$primary_invoice = $this->recurring_invoice
|
||||
->invoices()
|
||||
->where('is_deleted', 0)
|
||||
->where('is_proforma', 0)
|
||||
->orderBy('id', 'asc')
|
||||
->first();
|
||||
|
||||
if($primary_invoice && Carbon::parse($primary_invoice->date)->addSeconds($this->subscription->trial_duration)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset()))) {
|
||||
return $this->setIsTrial(true);
|
||||
}
|
||||
|
||||
$this->setIsTrial(false);
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this subscription
|
||||
* is eligible for a refund.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function checkRefundable(): self
|
||||
{
|
||||
if(!$this->recurring_invoice->subscription->refund_period || $this->recurring_invoice->subscription->refund_period === 0)
|
||||
return $this->setRefundable(false);
|
||||
|
||||
$primary_invoice = $this->recurring_invoice
|
||||
->invoices()
|
||||
->where('is_deleted', 0)
|
||||
->where('is_proforma', 0)
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
|
||||
if($primary_invoice &&
|
||||
$primary_invoice->status_id == Invoice::STATUS_PAID &&
|
||||
Carbon::parse($primary_invoice->date)->addSeconds($this->recurring_invoice->subscription->refund_period)->lte(now()->startOfDay()->addSeconds($primary_invoice->client->timezone_offset()))
|
||||
){
|
||||
return $this->setRefundable(true);
|
||||
}
|
||||
|
||||
return $this->setRefundable(false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* setRefundable
|
||||
*
|
||||
* @param bool $refundable
|
||||
* @return self
|
||||
*/
|
||||
private function setRefundable(bool $refundable): self
|
||||
{
|
||||
$this->is_refundable = $refundable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the is_trial flag
|
||||
*
|
||||
* @param bool $is_trial
|
||||
* @return self
|
||||
*/
|
||||
private function setIsTrial(bool $is_trial): self
|
||||
{
|
||||
$this->is_trial = $is_trial;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
91
app/Services/Subscription/UpgradePrice.php
Normal file
91
app/Services/Subscription/UpgradePrice.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?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
|
||||
{
|
||||
protected \App\Services\Subscription\SubscriptionStatus $status;
|
||||
|
||||
public function __construct(protected RecurringInvoice $recurring_invoice, public Subscription $subscription)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(): float
|
||||
{
|
||||
|
||||
$this->status = $this->recurring_invoice
|
||||
->subscription
|
||||
->status($this->recurring_invoice);
|
||||
|
||||
if($this->status->is_trial || !$this->status->is_in_good_standing)
|
||||
return $this->subscription->price;
|
||||
|
||||
if($this->status->is_in_good_standing)
|
||||
return $this->calculateUpgrade();
|
||||
|
||||
}
|
||||
|
||||
private function calculateUpgrade(): float
|
||||
{
|
||||
$ratio = $this->status->getProRataRatio();
|
||||
|
||||
$last_invoice = $this->recurring_invoice
|
||||
->invoices()
|
||||
->where('is_deleted', 0)
|
||||
->where('is_proforma', 0)
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
|
||||
$refund = $this->getRefundableAmount($last_invoice, $ratio);
|
||||
$outstanding_credit = $this->getCredits();
|
||||
|
||||
nlog("{$this->subscription->price} - {$refund} - {$outstanding_credit}");
|
||||
|
||||
return $this->subscription->price - $refund - $outstanding_credit;
|
||||
|
||||
}
|
||||
|
||||
private function getRefundableAmount(?Invoice $invoice, float $ratio): float
|
||||
{
|
||||
if (!$invoice || !$invoice->date || $invoice->status_id != Invoice::STATUS_PAID || $ratio == 0)
|
||||
return 0;
|
||||
|
||||
return max(0, round(($invoice->paid_to_date*$ratio),2));
|
||||
}
|
||||
|
||||
private function getCredits(): float
|
||||
{
|
||||
$outstanding_credits = 0;
|
||||
|
||||
$use_credit_setting = $this->recurring_invoice->client->getSetting('use_credits_payment');
|
||||
|
||||
if($use_credit_setting){
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user