mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-13 22:54:25 +01:00
Working on tests for subscriptions
This commit is contained in:
parent
3de5665d94
commit
9f79b4b519
@ -34,7 +34,7 @@ class ProRata
|
|||||||
*/
|
*/
|
||||||
public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
||||||
{
|
{
|
||||||
$days = $from_date->diffInDays($to_date);
|
$days = $from_date->copy()->diffInDays($to_date);
|
||||||
$days_in_frequency = $this->getDaysInFrequency($frequency);
|
$days_in_frequency = $this->getDaysInFrequency($frequency);
|
||||||
|
|
||||||
return round( (($days/$days_in_frequency) * $amount),2);
|
return round( (($days/$days_in_frequency) * $amount),2);
|
||||||
@ -52,9 +52,12 @@ class ProRata
|
|||||||
*/
|
*/
|
||||||
public function charge(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
public function charge(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
|
||||||
{
|
{
|
||||||
$days = $from_date->diffInDays($to_date);
|
$days = $from_date->copy()->diffInDays($to_date);
|
||||||
$days_in_frequency = $this->getDaysInFrequency($frequency);
|
$days_in_frequency = $this->getDaysInFrequency($frequency);
|
||||||
|
nlog($from_date->format('Y-m-d'));
|
||||||
|
nlog($days);
|
||||||
|
nlog($days_in_frequency);
|
||||||
|
nlog($amount);
|
||||||
return round( (($days/$days_in_frequency) * $amount),2);
|
return round( (($days/$days_in_frequency) * $amount),2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ use App\Helpers\Invoice\ProRata;
|
|||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\RecurringInvoice;
|
use App\Models\RecurringInvoice;
|
||||||
use App\Models\Subscription;
|
use App\Models\Subscription;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SubscriptionCalculator.
|
* SubscriptionCalculator.
|
||||||
@ -24,7 +25,7 @@ class SubscriptionCalculator
|
|||||||
|
|
||||||
public Subscription $target_subscription;
|
public Subscription $target_subscription;
|
||||||
|
|
||||||
public Invoice $invoice
|
public Invoice $invoice;
|
||||||
|
|
||||||
public function __construct(Subscription $target_subscription, Invoice $invoice)
|
public function __construct(Subscription $target_subscription, Invoice $invoice)
|
||||||
{
|
{
|
||||||
@ -57,6 +58,8 @@ class SubscriptionCalculator
|
|||||||
//set the starting refund amount
|
//set the starting refund amount
|
||||||
$refund_amount = 0;
|
$refund_amount = 0;
|
||||||
|
|
||||||
|
$refund_invoice = false;
|
||||||
|
|
||||||
//are they paid up to date.
|
//are they paid up to date.
|
||||||
|
|
||||||
//yes - calculate refund
|
//yes - calculate refund
|
||||||
@ -68,7 +71,7 @@ class SubscriptionCalculator
|
|||||||
$subscription = Subscription::find($this->invoice->subscription_id);
|
$subscription = Subscription::find($this->invoice->subscription_id);
|
||||||
$pro_rata = new ProRata();
|
$pro_rata = new ProRata();
|
||||||
|
|
||||||
$to_date = $subscription->getNextDateForFrequency(Carbon::parse($refund_invoice->date), $subscription->frequency_id);
|
$to_date = $subscription->service()->getNextDateForFrequency(Carbon::parse($refund_invoice->date), $subscription->frequency_id);
|
||||||
|
|
||||||
$refund_amount = $pro_rata->refund($refund_invoice->amount, now(), $to_date, $subscription->frequency_id);
|
$refund_amount = $pro_rata->refund($refund_invoice->amount, now(), $to_date, $subscription->frequency_id);
|
||||||
|
|
||||||
@ -88,11 +91,9 @@ class SubscriptionCalculator
|
|||||||
|
|
||||||
private function getRefundInvoice()
|
private function getRefundInvoice()
|
||||||
{
|
{
|
||||||
|
|
||||||
return Invoice::where('subscription_id', $this->invoice->subscription_id)
|
return Invoice::where('subscription_id', $this->invoice->subscription_id)
|
||||||
->where('client_id', $this->invoice->client_id)
|
->where('client_id', $this->invoice->client_id)
|
||||||
->where('is_deleted', 0)
|
->where('is_deleted', 0)
|
||||||
->where('balance', '>', 0)
|
|
||||||
->orderBy('id', 'desc')
|
->orderBy('id', 'desc')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
|
@ -33,7 +33,8 @@ class SubscriptionFactory extends Factory
|
|||||||
public function definition()
|
public function definition()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY
|
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
||||||
|
'name' => $this->faker->company(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,11 @@
|
|||||||
*/
|
*/
|
||||||
namespace Tests\Unit;
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Helpers\Invoice\ProRata;
|
||||||
|
use App\Helpers\Subscription\SubscriptionCalculator;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Subscription;
|
use App\Models\Subscription;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Tests\MockUnitData;
|
use Tests\MockUnitData;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
@ -41,7 +44,13 @@ class SubscriptionsCalcTest extends TestCase
|
|||||||
'company_id' => $this->company->id,
|
'company_id' => $this->company->id,
|
||||||
'user_id' => $this->user->id,
|
'user_id' => $this->user->id,
|
||||||
'price' => 10,
|
'price' => 10,
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$target = Subscription::factory()->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'price' => 20,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$invoice = Invoice::factory()->create([
|
$invoice = Invoice::factory()->create([
|
||||||
@ -57,7 +66,7 @@ class SubscriptionsCalcTest extends TestCase
|
|||||||
'tax_name3' => '',
|
'tax_name3' => '',
|
||||||
'discount' => 0,
|
'discount' => 0,
|
||||||
'subscription_id' => $subscription->id,
|
'subscription_id' => $subscription->id,
|
||||||
'date' => now()
|
'date' => '2021-01-01',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$invoice = $invoice->calc()->getInvoice();
|
$invoice = $invoice->calc()->getInvoice();
|
||||||
@ -69,6 +78,33 @@ class SubscriptionsCalcTest extends TestCase
|
|||||||
$this->assertEquals(10, $invoice->amount);
|
$this->assertEquals(10, $invoice->amount);
|
||||||
$this->assertEquals(10, $invoice->balance);
|
$this->assertEquals(10, $invoice->balance);
|
||||||
|
|
||||||
|
$sub_calculator = new SubscriptionCalculator($target->fresh(), $invoice->fresh());
|
||||||
|
|
||||||
|
$this->assertFalse($sub_calculator->isPaidUp());
|
||||||
|
|
||||||
|
$invoice->service()->markPaid()->save();
|
||||||
|
|
||||||
|
$this->assertTrue($sub_calculator->isPaidUp());
|
||||||
|
|
||||||
|
$this->assertEquals(10, $invoice->amount);
|
||||||
|
$this->assertEquals(0, $invoice->balance);
|
||||||
|
|
||||||
|
$pro_rata = new ProRata();
|
||||||
|
|
||||||
|
$refund = $pro_rata->refund($invoice->amount, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
|
||||||
|
|
||||||
|
$this->assertEquals(1.61, $refund);
|
||||||
|
|
||||||
|
$pro_rata = new ProRata();
|
||||||
|
|
||||||
|
$upgrade = $pro_rata->charge($target->price, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
|
||||||
|
|
||||||
|
$this->assertEquals(3.23, $upgrade);
|
||||||
|
|
||||||
|
// $net_upgrade_price = $sub_calculator->calcUpgradePlan();
|
||||||
|
|
||||||
|
// $this->assertEquals(1.62, $net_upgrade_price);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user