diff --git a/app/Jobs/Ledger/UpdateLedger.php b/app/Jobs/Ledger/UpdateLedger.php index a86ad994da62..5c464746fd39 100644 --- a/app/Jobs/Ledger/UpdateLedger.php +++ b/app/Jobs/Ledger/UpdateLedger.php @@ -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) diff --git a/app/Services/Invoice/HandleCancellation.php b/app/Services/Invoice/HandleCancellation.php index 62b2ca60ac2e..13ea7de5d03a 100644 --- a/app/Services/Invoice/HandleCancellation.php +++ b/app/Services/Invoice/HandleCancellation.php @@ -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(); diff --git a/tests/Integration/CompanyLedgerTest.php b/tests/Integration/CompanyLedgerTest.php index 789d13ae8a62..2a63c5cb21d7 100644 --- a/tests/Integration/CompanyLedgerTest.php +++ b/tests/Integration/CompanyLedgerTest.php @@ -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);