2022-11-17 07:38:27 +01: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)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
2022-11-17 07:38:27 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use Tests\MockAccountData;
|
2023-04-27 02:19:55 +02:00
|
|
|
use App\Jobs\Util\ReminderJob;
|
|
|
|
use App\DataMapper\InvoiceItem;
|
|
|
|
use App\DataMapper\FeesAndLimits;
|
|
|
|
use App\DataMapper\CompanySettings;
|
2023-10-11 01:25:21 +02:00
|
|
|
use App\Factory\InvoiceItemFactory;
|
|
|
|
use App\Factory\ClientGatewayTokenFactory;
|
2023-04-27 02:19:55 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2022-11-17 07:38:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class LateFeeTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
public $faker;
|
|
|
|
|
2023-04-27 02:56:30 +02:00
|
|
|
public $account;
|
|
|
|
|
|
|
|
public $company;
|
|
|
|
|
|
|
|
public $client;
|
|
|
|
|
2022-11-17 07:38:27 +01:00
|
|
|
protected function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
2022-11-17 07:38:27 +01:00
|
|
|
$this->makeTestData();
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildData($settings)
|
|
|
|
{
|
|
|
|
$this->account = Account::factory()->create([
|
|
|
|
'hosted_client_count' => 1000,
|
|
|
|
'hosted_company_count' => 1000,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->account->num_users = 3;
|
|
|
|
$this->account->save();
|
|
|
|
|
|
|
|
$this->user = User::factory()->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'confirmation_code' => 'xyz123',
|
|
|
|
'email' => $this->faker->unique()->safeEmail(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->company = Company::factory()->create([
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'settings' => $settings,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->company->settings = $settings;
|
|
|
|
$this->company->save();
|
|
|
|
|
|
|
|
$settings = new \stdClass;
|
|
|
|
$settings->currency_id = '1';
|
|
|
|
|
2023-04-27 02:40:57 +02:00
|
|
|
$client = Client::factory()->create([
|
2023-04-27 02:19:55 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_deleted' => 0,
|
|
|
|
'settings' => $settings,
|
|
|
|
]);
|
2023-04-27 02:40:57 +02:00
|
|
|
|
|
|
|
return $client;
|
2023-04-27 02:19:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
public function testModelBehaviourInsideMap()
|
|
|
|
{
|
|
|
|
$i = Invoice::factory()->count(5)
|
|
|
|
->create([
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_name3' => '',
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'discount' => 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$i->each(function($invoice){
|
|
|
|
$this->assertGreaterThan(1, count($invoice->line_items));
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertCount(5, $i);
|
|
|
|
|
|
|
|
$invoices = $i->map(function ($invoice) {
|
|
|
|
$invoice->service()->removeUnpaidGatewayFees();
|
|
|
|
return $invoice;
|
|
|
|
});
|
|
|
|
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
$this->assertGreaterThan(1, count($invoice->line_items));
|
|
|
|
});
|
|
|
|
|
|
|
|
$ids = $invoices->pluck('id');
|
|
|
|
|
|
|
|
$invoices = $i->map(function ($invoice) {
|
|
|
|
|
|
|
|
$line_items = $invoice->line_items;
|
|
|
|
|
|
|
|
$item = new InvoiceItem;
|
|
|
|
$item->type_id = '3';
|
|
|
|
$item->product_key = trans('texts.fee');
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = new InvoiceItem;
|
|
|
|
$item->type_id = '5';
|
|
|
|
$item->product_key = trans('texts.fee');
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
$invoice->line_items = $line_items;
|
|
|
|
$invoice->saveQuietly();
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
});
|
|
|
|
|
|
|
|
$invoices = Invoice::whereIn('id', $ids)->cursor()->map(function ($invoice){
|
|
|
|
nlog("line item count = ".count($invoice->line_items));
|
|
|
|
$this->assertGreaterThan(0, count($invoice->line_items));
|
|
|
|
|
|
|
|
$invoice->service()->removeUnpaidGatewayFees();
|
|
|
|
$invoice = $invoice->fresh();
|
|
|
|
$this->assertGreaterThan(0, count($invoice->line_items));
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
});
|
|
|
|
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
nlog($invoice->line_items);
|
|
|
|
$this->assertGreaterThan(0, count($invoice->line_items));
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCollectionPassesIsArray()
|
|
|
|
{
|
|
|
|
$line_items = collect($this->invoice->line_items);
|
|
|
|
$this->assertTrue(is_array($this->invoice->line_items));
|
|
|
|
$this->assertTrue(is_iterable($line_items));
|
|
|
|
$this->assertFalse(is_array($line_items));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLineItemResiliency()
|
|
|
|
{
|
|
|
|
$line_count = count($this->invoice->line_items);
|
|
|
|
$this->assertGreaterThan(0, $line_count);
|
|
|
|
|
|
|
|
$this->invoice->service()->removeUnpaidGatewayFees();
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->fresh();
|
|
|
|
|
|
|
|
$this->assertCount($line_count, $this->invoice->line_items);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCollectionAsLineItemArray()
|
|
|
|
{
|
|
|
|
|
|
|
|
$i = Invoice::factory()->create([
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_name3' => '',
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'discount' => 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
$item->type_id = '1';
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = new InvoiceItem;
|
|
|
|
$item->type_id = '5';
|
|
|
|
$item->product_key = trans('texts.fee');
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 1;
|
|
|
|
$item->type_id = '3';
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$i->line_items = $line_items;
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($line_items));
|
|
|
|
|
|
|
|
$i = $i->calc()->getInvoice();
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($i->line_items));
|
|
|
|
$this->assertEquals(21, $i->amount);
|
|
|
|
|
|
|
|
// $invoice_items = collect($invoice_items)->filter(function ($item) {
|
|
|
|
// return $item->type_id != '3';
|
|
|
|
// });
|
|
|
|
|
|
|
|
// $this->invoice->line_items = $invoice_items;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-11 01:25:21 +02:00
|
|
|
public function testLateFeeRemovals()
|
|
|
|
{
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$data[1]['min_limit'] = -1;
|
|
|
|
$data[1]['max_limit'] = -1;
|
|
|
|
$data[1]['fee_amount'] = 0.00;
|
|
|
|
$data[1]['fee_percent'] = 1;
|
|
|
|
$data[1]['fee_tax_name1'] = '';
|
|
|
|
$data[1]['fee_tax_rate1'] = 0;
|
|
|
|
$data[1]['fee_tax_name2'] = '';
|
|
|
|
$data[1]['fee_tax_rate2'] = 0;
|
|
|
|
$data[1]['fee_tax_name3'] = '';
|
|
|
|
$data[1]['fee_tax_rate3'] = 0;
|
|
|
|
$data[1]['adjust_fee_percent'] = false;
|
|
|
|
$data[1]['fee_cap'] = 0;
|
|
|
|
$data[1]['is_enabled'] = true;
|
|
|
|
|
|
|
|
$cg = new \App\Models\CompanyGateway;
|
|
|
|
$cg->company_id = $this->company->id;
|
|
|
|
$cg->user_id = $this->user->id;
|
|
|
|
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
|
|
|
|
$cg->require_cvv = true;
|
|
|
|
$cg->require_billing_address = true;
|
|
|
|
$cg->require_shipping_address = true;
|
|
|
|
$cg->update_details = true;
|
|
|
|
$cg->config = encrypt(config('ninja.testvars.stripe'));
|
|
|
|
$cg->fees_and_limits = $data;
|
|
|
|
$cg->save();
|
|
|
|
|
|
|
|
$cgt = ClientGatewayTokenFactory::create($this->company->id);
|
|
|
|
$cgt->client_id = $this->client->id;
|
|
|
|
$cgt->token = '';
|
|
|
|
$cgt->gateway_customer_reference = '';
|
|
|
|
$cgt->gateway_type_id = 1;
|
|
|
|
$cgt->company_gateway_id = $cg->id;
|
|
|
|
$cgt->save();
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
|
|
|
|
$i = Invoice::factory()->create([
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_name3' => '',
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'discount' => 0,
|
|
|
|
]);
|
|
|
|
|
2023-10-11 01:25:21 +02:00
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
|
|
|
$item->type_id = '1';
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
$item = new InvoiceItem;
|
|
|
|
$item->type_id = '5';
|
|
|
|
$item->product_key = trans('texts.fee');
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
2023-10-11 01:25:21 +02:00
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 1;
|
|
|
|
$item->type_id = '3';
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
$i->line_items = $line_items;
|
2023-10-11 01:25:21 +02:00
|
|
|
|
|
|
|
$i = $i->calc()->getInvoice();
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($i->line_items));
|
2023-10-11 05:18:56 +02:00
|
|
|
$this->assertEquals(21, $i->amount);
|
2023-10-11 01:25:21 +02:00
|
|
|
|
|
|
|
$invoice_items = (array) $i->line_items;
|
|
|
|
|
|
|
|
$invoice_items = collect($invoice_items)->filter(function ($item) {
|
|
|
|
return $item->type_id != '3';
|
|
|
|
});
|
|
|
|
|
|
|
|
$i->line_items = $invoice_items;
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
$i = $i->calc()->getInvoice();
|
|
|
|
|
|
|
|
$this->assertEquals(20, $i->amount);
|
|
|
|
|
2023-10-11 01:25:21 +02:00
|
|
|
$i->line_items = collect($i->line_items)
|
|
|
|
->reject(function ($item) {
|
|
|
|
return $item->type_id == '3';
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($i->line_items));
|
|
|
|
|
|
|
|
$i->service()->autoBill();
|
|
|
|
|
|
|
|
$i = $i->fresh();
|
|
|
|
|
|
|
|
$this->assertCount(2, $i->line_items);
|
2023-10-11 05:18:56 +02:00
|
|
|
$this->assertEquals(20, $i->amount);
|
2023-10-11 01:25:21 +02:00
|
|
|
|
|
|
|
$line_items = $i->line_items;
|
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
$item = new InvoiceItem;
|
|
|
|
$item->type_id = '5';
|
|
|
|
$item->product_key = trans('texts.fee');
|
|
|
|
$item->quantity = 1;
|
|
|
|
$item->cost = 10;
|
2023-10-11 01:25:21 +02:00
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$i->line_items = $line_items;
|
|
|
|
|
|
|
|
$i = $i->calc()->getInvoice();
|
2023-10-11 05:18:56 +02:00
|
|
|
$this->assertEquals(30, $i->amount);
|
|
|
|
$this->assertCount(3, $i->line_items);
|
|
|
|
|
|
|
|
$i->service()->autoBill();
|
|
|
|
$i = $i->fresh();
|
2023-10-11 01:25:21 +02:00
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
$this->assertEquals(30, $i->amount);
|
2023-10-11 01:25:21 +02:00
|
|
|
$this->assertCount(3, $i->line_items);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
public function testLateFeeAdded()
|
|
|
|
{
|
|
|
|
|
2023-07-27 10:38:34 +02:00
|
|
|
$this->travelTo(now()->subDays(15));
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->client_online_payment_notification = false;
|
|
|
|
$settings->client_manual_payment_notification = false;
|
|
|
|
$settings->late_fee_amount1 = 10;
|
|
|
|
$settings->late_fee_percent1 = 0;
|
|
|
|
$settings->lock_invoices = 'off';
|
|
|
|
$settings->enable_reminder1 = true;
|
|
|
|
$settings->num_days_reminder1 = 10;
|
|
|
|
$settings->schedule_reminder1 = 'after_due_date';
|
2023-07-27 10:38:34 +02:00
|
|
|
$settings->entity_send_time = '0';
|
2023-04-27 02:19:55 +02:00
|
|
|
|
2023-04-27 02:40:57 +02:00
|
|
|
$client = $this->buildData($settings);
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$i = Invoice::factory()->create([
|
2023-04-27 02:40:57 +02:00
|
|
|
'client_id' => $client->id,
|
2023-04-27 02:19:55 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'amount' => 0,
|
|
|
|
'balance' => 0,
|
|
|
|
'status_id' => 2,
|
|
|
|
'total_taxes' => 1,
|
|
|
|
'date' => now()->format('Y-m-d'),
|
2023-07-27 10:38:34 +02:00
|
|
|
'due_date' => now()->addDays(10)->format('Y-m-d'),
|
2023-04-27 02:19:55 +02:00
|
|
|
'terms' => 'nada',
|
|
|
|
'discount' => 0,
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_name3' => '',
|
|
|
|
'uses_inclusive_taxes' => false,
|
|
|
|
'line_items' => $this->buildLineItems(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$i = $i->calc()->getInvoice();
|
2023-07-27 10:38:34 +02:00
|
|
|
$i->service()->markSent()->setReminder()->applyNumber()->createInvitations()->save();
|
|
|
|
|
|
|
|
// $this->travelBack();
|
|
|
|
$this->travelTo(now()->addDays(20)->startOfDay()->format('Y-m-d'));
|
|
|
|
$i = $i->fresh();
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$this->assertEquals(10, $i->amount);
|
2023-04-27 02:40:57 +02:00
|
|
|
$this->assertEquals(10, $i->balance);
|
2023-07-27 09:52:47 +02:00
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
$reflectionMethod = new \ReflectionMethod(ReminderJob::class, 'sendReminderForInvoice');
|
|
|
|
$reflectionMethod->setAccessible(true);
|
|
|
|
$reflectionMethod->invokeArgs(new ReminderJob(), [$i]);
|
|
|
|
|
|
|
|
$i->fresh();
|
|
|
|
|
|
|
|
$this->assertEquals(20, $i->balance);
|
|
|
|
|
2023-07-27 10:38:34 +02:00
|
|
|
$this->travelBack();
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 02:40:57 +02:00
|
|
|
public function testLateFeeAddedToNewInvoiceWithLockedInvoiceConfig()
|
2023-04-27 02:19:55 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->client_online_payment_notification = false;
|
|
|
|
$settings->client_manual_payment_notification = false;
|
|
|
|
$settings->late_fee_amount1 = 10;
|
|
|
|
$settings->late_fee_percent1 = 0;
|
|
|
|
$settings->lock_invoices = 'when_sent';
|
|
|
|
$settings->enable_reminder1 = true;
|
|
|
|
$settings->num_days_reminder1 = 10;
|
|
|
|
$settings->schedule_reminder1 = 'after_due_date';
|
|
|
|
|
2023-04-27 02:56:30 +02:00
|
|
|
$client = $this->buildData($settings);
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$i = Invoice::factory()->create([
|
2023-04-27 02:40:57 +02:00
|
|
|
'client_id' => $client->id,
|
2023-04-27 02:19:55 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'amount' => 0,
|
|
|
|
'balance' => 0,
|
2023-04-27 02:56:30 +02:00
|
|
|
'status_id' => Invoice::STATUS_DRAFT,
|
2023-04-27 02:19:55 +02:00
|
|
|
'total_taxes' => 1,
|
|
|
|
'date' => now()->format('Y-m-d'),
|
|
|
|
'due_date' => now()->subDays(10)->format('Y-m-d'),
|
|
|
|
'terms' => 'nada',
|
|
|
|
'discount' => 0,
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_name3' => '',
|
|
|
|
'uses_inclusive_taxes' => false,
|
|
|
|
'line_items' => $this->buildLineItems(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$i = $i->calc()->getInvoice();
|
2023-04-27 02:56:30 +02:00
|
|
|
$i->service()->applyNumber()->createInvitations()->markSent()->save();
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$this->assertEquals(10, $i->amount);
|
2023-04-27 02:56:30 +02:00
|
|
|
$this->assertEquals(10, $i->balance);
|
|
|
|
$this->assertEquals(10, $client->fresh()->balance);
|
2023-04-27 02:19:55 +02:00
|
|
|
|
|
|
|
$reflectionMethod = new \ReflectionMethod(ReminderJob::class, 'sendReminderForInvoice');
|
|
|
|
$reflectionMethod->setAccessible(true);
|
|
|
|
$reflectionMethod->invokeArgs(new ReminderJob(), [$i]);
|
|
|
|
|
|
|
|
$i->fresh();
|
|
|
|
|
|
|
|
$this->assertEquals(10, $i->balance);
|
2023-04-27 02:56:30 +02:00
|
|
|
$this->assertEquals(20, $client->fresh()->balance);
|
2022-11-17 07:38:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 02:19:55 +02:00
|
|
|
|
2022-11-17 07:38:27 +01:00
|
|
|
public function testLateFeeBalances()
|
|
|
|
{
|
|
|
|
$this->assertEquals(10, $this->client->balance);
|
|
|
|
$this->assertEquals(10, $this->invoice->balance);
|
|
|
|
|
|
|
|
$this->invoice = $this->setLateFee($this->invoice, 5, 0);
|
|
|
|
|
|
|
|
$this->assertEquals(15, $this->client->fresh()->balance);
|
|
|
|
$this->assertEquals(15, $this->invoice->fresh()->balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setLateFee($invoice, $amount, $percent) :Invoice
|
|
|
|
{
|
|
|
|
$temp_invoice_balance = $invoice->balance;
|
|
|
|
|
|
|
|
if ($amount <= 0 && $percent <= 0) {
|
|
|
|
return $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fee = $amount;
|
|
|
|
|
|
|
|
if ($invoice->partial > 0) {
|
|
|
|
$fee += round($invoice->partial * $percent / 100, 2);
|
|
|
|
} else {
|
|
|
|
$fee += round($invoice->balance * $percent / 100, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice_item = new InvoiceItem;
|
|
|
|
$invoice_item->type_id = '5';
|
|
|
|
$invoice_item->product_key = trans('texts.fee');
|
|
|
|
$invoice_item->notes = ctrans('texts.late_fee_added', ['date' => now()]);
|
|
|
|
$invoice_item->quantity = 1;
|
|
|
|
$invoice_item->cost = $fee;
|
|
|
|
|
|
|
|
$invoice_items = $invoice->line_items;
|
|
|
|
$invoice_items[] = $invoice_item;
|
|
|
|
|
|
|
|
$invoice->line_items = $invoice_items;
|
|
|
|
|
|
|
|
/**Refresh Invoice values*/
|
|
|
|
$invoice = $invoice->calc()->getInvoice();
|
|
|
|
|
|
|
|
$invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
|
|
|
|
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
}
|
|
|
|
}
|