TDD Profit and loss

This commit is contained in:
David Bomba 2022-05-12 14:40:44 +10:00
parent d7084785fe
commit 618d2234d1
2 changed files with 122 additions and 42 deletions

View File

@ -165,7 +165,7 @@ class ProfitLoss
] ]
*/ */
private function invoiceIncome() private function invoiceIncome()
{ nlog(['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date] ); {
return \DB::select( \DB::raw(" return \DB::select( \DB::raw("
SELECT SELECT
sum(invoices.amount) as amount, sum(invoices.amount) as amount,

View File

@ -10,6 +10,7 @@
*/ */
namespace Tests\Feature\Export; namespace Tests\Feature\Export;
use App\DataMapper\ClientSettings;
use App\Factory\InvoiceFactory; use App\Factory\InvoiceFactory;
use App\Models\Account; use App\Models\Account;
use App\Models\Client; use App\Models\Client;
@ -46,7 +47,6 @@ class ProfitAndLossReportTest extends TestCase
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
$this->buildData();
} }
public $company; public $company;
@ -55,6 +55,7 @@ class ProfitAndLossReportTest extends TestCase
public $payload; public $payload;
public $account;
/** /**
* *
* start_date - Y-m-d * start_date - Y-m-d
@ -78,23 +79,22 @@ class ProfitAndLossReportTest extends TestCase
private function buildData() private function buildData()
{ {
$this->account = Account::factory()->create([
$account = Account::factory()->create([
'hosted_client_count' => 1000, 'hosted_client_count' => 1000,
'hosted_company_count' => 1000 'hosted_company_count' => 1000
]); ]);
$account->num_users = 3; $this->account->num_users = 3;
$account->save(); $this->account->save();
$this->user = User::factory()->create([ $this->user = User::factory()->create([
'account_id' => $account->id, 'account_id' => $this->account->id,
'confirmation_code' => 'xyz123', 'confirmation_code' => 'xyz123',
'email' => $this->faker->unique()->safeEmail, 'email' => $this->faker->unique()->safeEmail,
]); ]);
$this->company = Company::factory()->create([ $this->company = Company::factory()->create([
'account_id' => $account->id, 'account_id' => $this->account->id,
]); ]);
$this->payload = [ $this->payload = [
@ -109,15 +109,18 @@ class ProfitAndLossReportTest extends TestCase
public function testProfitLossInstance() public function testProfitLossInstance()
{ {
$this->buildData();
$pl = new ProfitLoss($this->company, $this->payload); $pl = new ProfitLoss($this->company, $this->payload);
$this->assertInstanceOf(ProfitLoss::class, $pl); $this->assertInstanceOf(ProfitLoss::class, $pl);
$this->account->delete();
} }
public function testInvoiceIncome() public function testSimpleInvoiceIncome()
{ {
$this->buildData();
$client = Client::factory()->create([ $client = Client::factory()->create([
'user_id' => $this->user->id, 'user_id' => $this->user->id,
@ -125,43 +128,120 @@ class ProfitAndLossReportTest extends TestCase
'is_deleted' => 0, 'is_deleted' => 0,
]); ]);
// Invoice::factory()->create([ Invoice::factory()->count(2)->create([
// 'client_id' => $client->id, 'client_id' => $client->id,
// 'user_id' => $this->user->id, 'user_id' => $this->user->id,
// 'company_id' => $this->company->id, 'company_id' => $this->company->id,
// 'amount' => 10, 'amount' => 11,
// 'balance' => 10, 'balance' => 11,
// 'status_id' => 2, 'status_id' => 2,
// 'total_taxes' => 1, 'total_taxes' => 1,
// 'date' => '2022-01-01', 'date' => '2022-01-01',
// 'terms' => 'nada', 'terms' => 'nada',
// 'discount' => 0, 'discount' => 0,
// 'tax_rate1' => 0, 'tax_rate1' => 0,
// 'tax_rate2' => 0, 'tax_rate2' => 0,
// 'tax_rate3' => 0, 'tax_rate3' => 0,
// 'tax_name1' => '', 'tax_name1' => '',
// 'tax_name2' => '', 'tax_name2' => '',
// 'tax_name3' => '', 'tax_name3' => '',
// ]); 'uses_inclusive_taxes' => false,
]);
$i = InvoiceFactory::create($this->company->id, $this->user->id);
$i->client_id = $client->id;
$i->amount = 10;
$i->balance = 10;
$i->status_id = 2;
$i->terms = "nada";
$i->total_taxes = 1;
$i->save();
nlog(Invoice::where('company_id', $this->company->id)->get()->toArray());
$pl = new ProfitLoss($this->company, $this->payload); $pl = new ProfitLoss($this->company, $this->payload);
$pl->build(); $pl->build();
$this->assertEquals(9.0, $pl->getIncome()); $this->assertEquals(20.0, $pl->getIncome());
$this->assertEquals(1, $pl->getIncomeTaxes()); $this->assertEquals(2, $pl->getIncomeTaxes());
$this->account->delete();
}
public function testSimpleInvoiceIncomeWithInclusivesTaxes()
{
$this->buildData();
$client = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'is_deleted' => 0,
]);
Invoice::factory()->count(2)->create([
'client_id' => $client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'amount' => 10,
'balance' => 10,
'status_id' => 2,
'total_taxes' => 1,
'date' => '2022-01-01',
'terms' => 'nada',
'discount' => 0,
'tax_rate1' => 10,
'tax_rate2' => 0,
'tax_rate3' => 0,
'tax_name1' => "GST",
'tax_name2' => '',
'tax_name3' => '',
'uses_inclusive_taxes' => true,
]);
$pl = new ProfitLoss($this->company, $this->payload);
$pl->build();
$this->assertEquals(18.0, $pl->getIncome());
$this->assertEquals(2, $pl->getIncomeTaxes());
$this->account->delete();
}
public function testSimpleInvoiceIncomeWithForeignExchange()
{
$this->buildData();
$settings = ClientSettings::defaults();
$settings->currency_id = "2";
$client = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'is_deleted' => 0,
'settings' => $settings,
]);
Invoice::factory()->count(2)->create([
'client_id' => $client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'amount' => 10,
'balance' => 10,
'status_id' => 2,
'total_taxes' => 1,
'date' => '2022-01-01',
'terms' => 'nada',
'discount' => 0,
'tax_rate1' => 10,
'tax_rate2' => 0,
'tax_rate3' => 0,
'tax_name1' => "GST",
'tax_name2' => '',
'tax_name3' => '',
'uses_inclusive_taxes' => true,
'exchange_rate' => 0.5
]);
$pl = new ProfitLoss($this->company, $this->payload);
$pl->build();
$this->assertEquals(36.0, $pl->getIncome());
$this->assertEquals(4, $pl->getIncomeTaxes());
$this->account->delete();
}
} }
}