1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Ledger Tests

This commit is contained in:
David Bomba 2023-11-07 21:16:33 +11:00
parent 2c567a0d95
commit 2c5b94c474
3 changed files with 79 additions and 23 deletions

View File

@ -46,19 +46,22 @@ class UpdateLedger implements ShouldQueue
$cl = CompanyLedger::find($this->company_ledger_id);
$ledger_item = CompanyLedger::query()
->where('company_id', $cl->company_id)
->where('client_id', $cl->client_id)
->where('company_ledgerable_id', $cl->company_ledgerable_id)
->where('company_ledgerable_type', $cl->company_ledgerable_type)
->exists();
// $ledger_item = CompanyLedger::query()
// ->where('company_id', $cl->company_id)
// ->where('client_id', $cl->client_id)
// ->where('company_ledgerable_id', $cl->company_ledgerable_id)
// ->where('company_ledgerable_type', $cl->company_ledgerable_type)
// ->exists();
$ledger_item = $cl->company_ledgerable->company_ledger()->count() == 1;
nlog($cl->company_ledgerable->company_ledger()->count());
if(!$cl)
return;
$entity = $cl->company_ledgerable;
$balance = $entity->calc()->getBalance();
$cl->adjustment = $ledger_item ? $balance - $this->start_amount : $balance;
$cl->adjustment = $ledger_item ? $balance : ($balance - $this->start_amount);
$parent_ledger = CompanyLedger::query()
->where('id', '<', $cl->id)

View File

@ -39,18 +39,13 @@ class HandleCancellation extends AbstractService
$this->backupCancellation($adjustment);
//set invoice balance to 0
// $this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->balance = 0;
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
// $this->invoice->client->service()->updateBalance($adjustment)->save();
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for cancellation of Invoice {$this->invoice->number}");
$this->invoice->client->service()->calculateBalance();
$this->invoice->service()->workFlow()->save();

View File

@ -11,21 +11,23 @@
namespace Tests\Integration;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Models\Account;
use Tests\TestCase;
use App\Models\User;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Account;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\User;
use App\Models\CompanyToken;
use App\Models\ClientContact;
use App\DataMapper\InvoiceItem;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Jobs\Ledger\UpdateLedger;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/** @test*/
class CompanyLedgerTest extends TestCase
@ -86,7 +88,7 @@ class CompanyLedgerTest extends TestCase
$user = User::whereEmail($fake_email)->first();
if (! $user) {
$user = User::factory()->create([
'email' => $fake_email,
@ -95,6 +97,7 @@ class CompanyLedgerTest extends TestCase
'confirmation_code' => $this->createDbHash(config('database.default')),
]);
}
$this->user = $user;
$cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id);
$cu->is_owner = true;
@ -126,6 +129,61 @@ class CompanyLedgerTest extends TestCase
]);
}
public function testLedgerAdjustments()
{
$c = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'balance' => 0,
'paid_to_date' => 0
]);
$i = Invoice::factory()->create([
'client_id' => $c->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'status_id' => Invoice::STATUS_DRAFT,
'tax_name1' => '',
'tax_name2' => '',
'tax_name3' => '',
'tax_rate1' => 0,
'tax_rate2' => 0,
'tax_rate3' => 0,
'discount' => 0,
]);
$item = new InvoiceItem();
$item->cost = 10;
$item->quantity = 10;
$i->line_items = [$item];
$i->calc()->getInvoice();
$this->assertEquals(0, $c->balance);
$this->assertEquals(100, $i->amount);
$this->assertEquals(0, $i->balance);
$this->assertEquals(0, $i->company_ledger()->count());
$i->service()->markSent()->save();
$i = $i->fresh();
// \Illuminate\Support\Facades\Bus::fake();
// \Illuminate\Support\Facades\Bus::assertDispatched(UpdateLedger::class);
$this->assertEquals(1, $i->company_ledger()->count());
$cl = $i->company_ledger()->first();
(new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
$cl = $cl->fresh();
$this->assertEquals(100, $cl->adjustment);
$this->assertEquals(100, $cl->balance);
}
public function testBaseLine()
{
$this->assertEquals($this->company->invoices->count(), 0);