mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Working on pro rata refunds
This commit is contained in:
parent
ccaa5c1d31
commit
62401555cd
@ -14,6 +14,7 @@ namespace App\Http\Controllers\ClientPortal;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ClientPortal\Subscriptions\ShowPlanSwitchRequest;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Models\Subscription;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@ -23,15 +24,20 @@ class SubscriptionPlanSwitchController extends Controller
|
||||
* Show the page for switching between plans.
|
||||
*
|
||||
* @param ShowPlanSwitchRequest $request
|
||||
* @param Subscription $subscription
|
||||
* @param RecurringInvoice $recurring_invoice
|
||||
* @param string $target
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function index(ShowPlanSwitchRequest $request, Subscription $subscription, Subscription $target)
|
||||
public function index(ShowPlanSwitchRequest $request, RecurringInvoice $recurring_invoice, Subscription $target)
|
||||
{
|
||||
//calculate whether a payment is required or whether we pass through a credit for this.
|
||||
|
||||
$amount = $recurring_invoice->subscription->service()->calculateUpgradePrice($recurring_invoice, $target);
|
||||
|
||||
return render('subscriptions.switch', [
|
||||
'subscription' => $subscription,
|
||||
'target' => $target,
|
||||
'amount' => $amount,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -297,7 +297,6 @@ class InvoiceService
|
||||
|
||||
public function deletePdf()
|
||||
{
|
||||
nlog("delete PDF");
|
||||
//UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
|
||||
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
|
||||
|
||||
|
@ -33,6 +33,7 @@ use App\Utils\Ninja;
|
||||
use App\Utils\Traits\CleanLineItems;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\SubscriptionHooker;
|
||||
use Carbon\Carbon;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
class SubscriptionService
|
||||
@ -44,14 +45,15 @@ class SubscriptionService
|
||||
/** @var subscription */
|
||||
private $subscription;
|
||||
|
||||
/** @var client_subscription */
|
||||
// private $client_subscription;
|
||||
|
||||
public function __construct(Subscription $subscription)
|
||||
{
|
||||
$this->subscription = $subscription;
|
||||
}
|
||||
|
||||
/*
|
||||
Performs the initial purchase of a
|
||||
one time or recurring product
|
||||
*/
|
||||
public function completePurchase(PaymentHash $payment_hash)
|
||||
{
|
||||
|
||||
@ -181,9 +183,72 @@ class SubscriptionService
|
||||
return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
||||
}
|
||||
|
||||
public function calculateUpgradePrice(RecurringInvoice $recurring_invoice, Subscription $target)
|
||||
{
|
||||
//calculate based on daily prices
|
||||
|
||||
$current_amount = $recurring_invoice->amount;
|
||||
$currency_frequency = $recurring_invoice->frequency_id;
|
||||
|
||||
$outstanding = $recurring_invoice->invoices
|
||||
->where('is_deleted', 0)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('balance', '>', 0);
|
||||
|
||||
$outstanding_amounts = $outstanding->sum('balance');
|
||||
$outstanding_invoices = $outstanding->get();
|
||||
|
||||
if ($outstanding->count() == 0){
|
||||
//nothing outstanding
|
||||
}
|
||||
elseif ($outstanding_amounts > $current_amount){
|
||||
//user has multiple amounts outstanding
|
||||
}
|
||||
elseif ($outstanding_amounts < $current_amount && $outstanding_amounts > 0) {
|
||||
//user is changing plan mid frequency cycle
|
||||
}
|
||||
|
||||
// $current_plan_price = $this->subscription
|
||||
}
|
||||
|
||||
private function calculateProRataRefund($invoice)
|
||||
{
|
||||
//determine the start date
|
||||
|
||||
$start_date = Carbon::parse($invoice->date);
|
||||
|
||||
$current_date = now();
|
||||
|
||||
$days_to_refund = $start_date->diffInDays($current_date);
|
||||
}
|
||||
|
||||
public function createChangePlanInvoice($data)
|
||||
{
|
||||
//Data array structure
|
||||
/**
|
||||
* [
|
||||
* 'subscription' => Subscription::class,
|
||||
* 'target' => Subscription::class
|
||||
* ]
|
||||
*/
|
||||
|
||||
//logic
|
||||
|
||||
// Is the user paid up to date? ie are there any outstanding invoices for this subscription
|
||||
|
||||
// User in arrears.
|
||||
|
||||
|
||||
// User paid up to date (in credit!)
|
||||
|
||||
//generate credit amount.
|
||||
//
|
||||
//generate new billable amount
|
||||
//
|
||||
|
||||
//if billable amount is LESS than 0 -> generate a credit and pass through.
|
||||
//
|
||||
//if billable amoun is GREATER than 0 -> gener
|
||||
return Invoice::where('status_id', Invoice::STATUS_SENT)->first();
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@
|
||||
|
||||
<div class="flex mt-4">
|
||||
@foreach($invoice->subscription->service()->getPlans() as $subscription)
|
||||
<a href="{{ route('client.subscription.plan_switch', ['subscription' => $invoice->subscription->hashed_id, 'target' => $subscription->hashed_id]) }}" class="border rounded px-5 py-2 hover:border-gray-800 text-sm cursor-pointer">{{ $subscription->name }}</a>
|
||||
<a href="{{ route('client.subscription.plan_switch', ['recurring_invoice' => $invoice->hashed_id, 'target' => $subscription->hashed_id]) }}" class="border rounded px-5 py-2 hover:border-gray-800 text-sm cursor-pointer">{{ $subscription->name }}</a>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
|
@ -72,7 +72,7 @@ Route::group(['middleware' => ['auth:contact', 'locale', 'check_client_existence
|
||||
Route::get('documents/{document}/download', 'ClientPortal\DocumentController@download')->name('documents.download');
|
||||
Route::resource('documents', 'ClientPortal\DocumentController')->only(['index', 'show']);
|
||||
|
||||
Route::get('subscriptions/{subscription}/plan_switch/{target}', 'ClientPortal\SubscriptionPlanSwitchController@index')->name('subscription.plan_switch');
|
||||
Route::get('subscriptions/{recurring_invoice}/plan_switch/{target}', 'ClientPortal\SubscriptionPlanSwitchController@index')->name('subscription.plan_switch');
|
||||
|
||||
Route::resource('subscriptions', 'ClientPortal\SubscriptionController')->only(['index']);
|
||||
|
||||
|
47
tests/Unit/DatesTest.php
Normal file
47
tests/Unit/DatesTest.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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://opensource.org/licenses/AAL
|
||||
*/
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Helpers\Invoice\InvoiceSum;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
class DatesTest extends TestCase
|
||||
{
|
||||
use MockAccountData;
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// $this->makeTestData();
|
||||
|
||||
}
|
||||
|
||||
public function testDaysDiff()
|
||||
{
|
||||
$string_date = '2021-06-01';
|
||||
|
||||
$start_date = Carbon::parse($string_date);
|
||||
$current_date = Carbon::parse('2021-06-20');
|
||||
|
||||
$diff_in_days = $start_date->diffInDays($current_date);
|
||||
|
||||
$this->assertEquals(19, $diff_in_days);
|
||||
;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user