1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Tests for using Unapplied Payments

This commit is contained in:
David Bomba 2024-02-18 15:06:20 +11:00
parent 6974841921
commit 730e0a17ac
2 changed files with 184 additions and 12 deletions

View File

@ -68,7 +68,9 @@ class AutoBillInvoice extends AbstractService
$this->applyCreditPayment();
}
nlog($this->client->getSetting('use_unapplied_payment'));
if($this->client->getSetting('use_unapplied_payment') != 'off') {
nlog("meeeeeeerp");
$this->applyUnappliedPayment();
}
@ -182,9 +184,6 @@ class AutoBillInvoice extends AbstractService
$payment->amount = 0;
$payment->applied = 0;
// $payment->amount = $amount;
// $payment->applied = $amount;
$payment->client_id = $this->invoice->client_id;
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
$payment->date = now()->addSeconds($this->invoice->company->utc_offset())->format('Y-m-d');
@ -259,7 +258,8 @@ class AutoBillInvoice extends AbstractService
*/
private function applyUnappliedPayment(): self
{
$unapplied_payments = Payment::query()->where('client_id', $this->client->id)
$unapplied_payments = Payment::query()
->where('client_id', $this->client->id)
->where('status_id', Payment::STATUS_COMPLETED)
->where('is_deleted', false)
->where('amount', '>', 'applied')
@ -408,14 +408,6 @@ class AutoBillInvoice extends AbstractService
})->orderBy('is_default', 'DESC')
->get();
// $gateway_tokens = $this->client
// ->gateway_tokens()
// ->whereHas('gateway', function ($query) {
// $query->where('is_deleted', 0)
// ->where('deleted_at', null);
// })->orderBy('is_default', 'DESC')
// ->get();
$filtered_gateways = $gateway_tokens->filter(function ($gateway_token) use ($amount) {
$company_gateway = $gateway_token->gateway;

View File

@ -0,0 +1,180 @@
<?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\Feature\Payments;
use App\DataMapper\ClientSettings;
use App\Factory\InvoiceFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockUnitData;
use Tests\TestCase;
/**
* @test
*/
class AutoUnappliedPaymentTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockUnitData;
protected function setUp() :void
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
// $this->withoutExceptionHandling();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testUnappliedPaymentsAreEnabled()
{
$settings = ClientSettings::defaults();
$settings->use_unapplied_payment = 'always';
$client = Client::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'settings' => $settings,
]);
$this->assertEquals('always', $client->settings->use_unapplied_payment);
$invoice = Invoice::factory()->for($client)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'auto_bill_enabled' => true,
'client_id' => $client->id,
]);
$invoice = $invoice->calc()->getInvoice();
$payment = Payment::factory()->for($client)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $client->id,
'amount' => 100,
'applied' => 0,
'refunded' => 0,
'status_id' => Payment::STATUS_COMPLETED,
'is_deleted' => 0,
]);
$invoice->service()->markSent()->save();
$this->assertGreaterThan(0, $invoice->balance);
nlog($invoice->balance);
try{
$invoice->service()->autoBill()->save();
}
catch(\Exception $e){
}
$invoice = $invoice->fresh();
$payment = $payment->fresh();
nlog($invoice->toArray());
nlog($payment->toArray());
$this->assertEquals($payment->applied, $invoice->paid_to_date);
$this->assertGreaterThan(2, $invoice->status_id);
$this->assertGreaterThan(0, $payment->applied);
// $this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id);
// $this->assertEquals(0, $invoice->balance);
}
public function testUnappliedPaymentsAreDisabled()
{
$settings = ClientSettings::defaults();
$settings->use_unapplied_payment = 'off';
$client = Client::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'settings' => $settings,
]);
$this->assertEquals('off', $client->settings->use_unapplied_payment);
$invoice = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $client->id,
'auto_bill_enabled' => true,
'status_id' => 2
]);
$invoice = $invoice->calc()->getInvoice();
$invoice_balance = $invoice->balance;
$payment = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $client->id,
'amount' => 100,
'applied' => 0,
'refunded' => 0,
'status_id' => Payment::STATUS_COMPLETED
]);
$invoice->service()->markSent()->save();
$this->assertGreaterThan(0, $invoice->balance);
try {
$invoice->service()->autoBill()->save();
}
catch(\Exception $e) {
}
$invoice = $invoice->fresh();
$payment = $payment->fresh();
$this->assertEquals($invoice_balance, $invoice->balance);
$this->assertEquals(0, $payment->applied);
$this->assertEquals(2, $invoice->status_id);
$this->assertEquals(0, $invoice->paid_to_date);
$this->assertEquals($invoice->amount, $invoice->balance);
// $this->assertEquals($payment->applied, $invoice->paid_to_date);
// $this->assertEquals(2, $invoice->status_id);
}
}