1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/tests/Unit/SubscriptionsCalcTest.php

107 lines
2.8 KiB
PHP
Raw Normal View History

2021-10-21 06:08:46 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-10-21 12:44:18 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-10-21 06:08:46 +02:00
*/
2021-10-21 12:44:18 +02:00
2021-10-21 06:08:46 +02:00
namespace Tests\Unit;
2021-10-21 06:51:36 +02:00
use App\Helpers\Invoice\ProRata;
use App\Helpers\Subscription\SubscriptionCalculator;
2021-10-21 06:08:46 +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 06:08:46 +02:00
use Tests\MockUnitData;
use Tests\TestCase;
/**
* @test
*/
class SubscriptionsCalcTest extends TestCase
{
use MockUnitData;
/**
* Important consideration with Base64
* encoding checks.
*
* No method can guarantee against false positives.
*/
2021-10-21 12:44:18 +02:00
public function setUp(): void
2021-10-21 06:08:46 +02:00
{
parent::setUp();
$this->makeTestData();
}
public function testCalcUpgradePrice()
{
2021-10-21 12:44:18 +02:00
2021-10-21 06:08:46 +02:00
$subscription = Subscription::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'price' => 10,
2021-10-21 06:51:36 +02:00
]);
2021-10-21 06:08:46 +02:00
2021-10-21 06:51:36 +02:00
$target = Subscription::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'price' => 20,
2021-10-21 06:08:46 +02:00
]);
$invoice = Invoice::factory()->create([
'line_items' => $this->buildLineItems(),
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'tax_rate1' => 0,
'tax_name1' => '',
'tax_rate2' => 0,
'tax_name2' => '',
'tax_rate3' => 0,
'tax_name3' => '',
'discount' => 0,
'subscription_id' => $subscription->id,
2021-10-21 06:51:36 +02:00
'date' => '2021-01-01',
2021-10-21 12:44:18 +02:00
]);
2021-10-21 06:08:46 +02:00
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(10, $invoice->amount);
$invoice->service()->markSent()->save();
$this->assertEquals(10, $invoice->amount);
$this->assertEquals(10, $invoice->balance);
2021-10-21 06:51:36 +02:00
$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);
2021-10-21 12:44:18 +02:00
$pro_rata = new ProRata;
2021-10-21 06:51:36 +02:00
$refund = $pro_rata->refund($invoice->amount, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
2022-01-30 01:34:40 +01:00
// $this->assertEquals(1.61, $refund);
2021-10-21 06:51:36 +02:00
2021-10-21 12:44:18 +02:00
$pro_rata = new ProRata;
2021-10-21 06:51:36 +02:00
$upgrade = $pro_rata->charge($target->price, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
2022-01-30 01:34:40 +01:00
// $this->assertEquals(3.23, $upgrade);
2021-10-21 06:51:36 +02:00
2021-10-21 06:08:46 +02:00
}
2021-10-21 12:44:18 +02:00
}