mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 03:29:03 -05:00 
			
		
		
		
	Ledger Tests
This commit is contained in:
		
							parent
							
								
									2c567a0d95
								
							
						
					
					
						commit
						2c5b94c474
					
				@ -46,19 +46,22 @@ class UpdateLedger implements ShouldQueue
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $cl = CompanyLedger::find($this->company_ledger_id);
 | 
					        $cl = CompanyLedger::find($this->company_ledger_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $ledger_item = CompanyLedger::query()
 | 
					        // $ledger_item = CompanyLedger::query()
 | 
				
			||||||
                                        ->where('company_id', $cl->company_id)
 | 
					        //                                 ->where('company_id', $cl->company_id)
 | 
				
			||||||
                                        ->where('client_id', $cl->client_id)
 | 
					        //                                 ->where('client_id', $cl->client_id)
 | 
				
			||||||
                                        ->where('company_ledgerable_id', $cl->company_ledgerable_id)
 | 
					        //                                 ->where('company_ledgerable_id', $cl->company_ledgerable_id)
 | 
				
			||||||
                                        ->where('company_ledgerable_type', $cl->company_ledgerable_type)
 | 
					        //                                 ->where('company_ledgerable_type', $cl->company_ledgerable_type)
 | 
				
			||||||
                                        ->exists();
 | 
					        //                                 ->exists();
 | 
				
			||||||
 | 
					        $ledger_item = $cl->company_ledgerable->company_ledger()->count() == 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        nlog($cl->company_ledgerable->company_ledger()->count());
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        if(!$cl)
 | 
					        if(!$cl)
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        $entity = $cl->company_ledgerable;
 | 
					        $entity = $cl->company_ledgerable;
 | 
				
			||||||
        $balance = $entity->calc()->getBalance();
 | 
					        $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()
 | 
					            $parent_ledger = CompanyLedger::query()
 | 
				
			||||||
                ->where('id', '<', $cl->id)
 | 
					                ->where('id', '<', $cl->id)
 | 
				
			||||||
 | 
				
			|||||||
@ -39,18 +39,13 @@ class HandleCancellation extends AbstractService
 | 
				
			|||||||
        $this->backupCancellation($adjustment);
 | 
					        $this->backupCancellation($adjustment);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //set invoice balance to 0
 | 
					        //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->balance = 0;
 | 
				
			||||||
        $this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
 | 
					        $this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // $this->invoice->client->service()->updateBalance($adjustment)->save();
 | 
					        // $this->invoice->client->service()->updateBalance($adjustment)->save();
 | 
				
			||||||
 | 
					        $this->invoice->client->service()->calculateBalance();
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$this->invoice->client->service()->calculateBalance();
 | 
					 | 
				
			||||||
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for cancellation of Invoice {$this->invoice->number}");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->invoice->service()->workFlow()->save();
 | 
					        $this->invoice->service()->workFlow()->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -11,21 +11,23 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Tests\Integration;
 | 
					namespace Tests\Integration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use App\DataMapper\CompanySettings;
 | 
					use Tests\TestCase;
 | 
				
			||||||
use App\Factory\CompanyUserFactory;
 | 
					use App\Models\User;
 | 
				
			||||||
use App\Models\Account;
 | 
					 | 
				
			||||||
use App\Models\Client;
 | 
					use App\Models\Client;
 | 
				
			||||||
use App\Models\ClientContact;
 | 
					use App\Models\Account;
 | 
				
			||||||
use App\Models\Company;
 | 
					use App\Models\Company;
 | 
				
			||||||
use App\Models\CompanyToken;
 | 
					 | 
				
			||||||
use App\Models\Invoice;
 | 
					use App\Models\Invoice;
 | 
				
			||||||
use App\Models\Payment;
 | 
					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 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\Support\Facades\Hash;
 | 
				
			||||||
use Illuminate\Validation\ValidationException;
 | 
					use Illuminate\Validation\ValidationException;
 | 
				
			||||||
use Tests\TestCase;
 | 
					use Illuminate\Foundation\Testing\DatabaseTransactions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** @test*/
 | 
					/** @test*/
 | 
				
			||||||
class CompanyLedgerTest extends TestCase
 | 
					class CompanyLedgerTest extends TestCase
 | 
				
			||||||
@ -95,6 +97,7 @@ class CompanyLedgerTest extends TestCase
 | 
				
			|||||||
                'confirmation_code' => $this->createDbHash(config('database.default')),
 | 
					                'confirmation_code' => $this->createDbHash(config('database.default')),
 | 
				
			||||||
            ]);
 | 
					            ]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        $this->user = $user;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id);
 | 
					        $cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id);
 | 
				
			||||||
        $cu->is_owner = true;
 | 
					        $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()
 | 
					    public function testBaseLine()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->assertEquals($this->company->invoices->count(), 0);
 | 
					        $this->assertEquals($this->company->invoices->count(), 0);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user