2020-02-24 11:15:30 +01:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
namespace Tests\Integration;
|
|
|
|
|
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
use App\Factory\CompanyUserFactory;
|
|
|
|
use App\Models\Account;
|
2020-10-01 12:49:47 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
2020-02-24 11:15:30 +01:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
2020-10-22 08:46:02 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2021-01-26 22:42:00 +01:00
|
|
|
use Tests\MockAccountData;
|
2020-11-25 15:19:52 +01:00
|
|
|
use Tests\TestCase;
|
2020-02-24 11:15:30 +01:00
|
|
|
|
|
|
|
/** @test*/
|
|
|
|
class CompanyLedgerTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
public $company;
|
|
|
|
|
|
|
|
public $client;
|
|
|
|
|
|
|
|
public $user;
|
|
|
|
|
|
|
|
public $token;
|
|
|
|
|
|
|
|
public $account;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2020-02-24 11:15:30 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
|
2021-02-02 09:51:12 +01:00
|
|
|
$this->artisan('db:seed --force');
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
/* Warm up the cache !*/
|
|
|
|
$cached_tables = config('ninja.cached_tables');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
foreach ($cached_tables as $name => $class) {
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
// check that the table exists in case the migration is pending
|
|
|
|
if (! Schema::hasTable((new $class())->getTable())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($name == 'payment_terms') {
|
|
|
|
$orderBy = 'num_days';
|
|
|
|
} elseif ($name == 'fonts') {
|
|
|
|
$orderBy = 'sort_order';
|
|
|
|
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) {
|
|
|
|
$orderBy = 'name';
|
|
|
|
} else {
|
|
|
|
$orderBy = 'id';
|
|
|
|
}
|
|
|
|
$tableData = $class::orderBy($orderBy)->get();
|
|
|
|
if ($tableData->count()) {
|
|
|
|
Cache::forever($name, $tableData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$this->account = Account::factory()->create();
|
|
|
|
$this->company = Company::factory()->create([
|
2020-02-24 11:15:30 +01:00
|
|
|
'account_id' => $this->account->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
|
2022-01-11 05:08:39 +01:00
|
|
|
$settings->company_logo = 'https://pdf.invoicing.co/favicon-v2.png';
|
2020-09-06 11:38:10 +02:00
|
|
|
$settings->website = 'www.invoiceninja.com';
|
|
|
|
$settings->address1 = 'Address 1';
|
|
|
|
$settings->address2 = 'Address 2';
|
|
|
|
$settings->city = 'City';
|
|
|
|
$settings->state = 'State';
|
|
|
|
$settings->postal_code = 'Postal Code';
|
|
|
|
$settings->phone = '555-343-2323';
|
|
|
|
$settings->email = 'user@example.com';
|
|
|
|
$settings->country_id = '840';
|
2020-02-24 11:15:30 +01:00
|
|
|
$settings->vat_number = 'vat number';
|
2020-09-06 11:38:10 +02:00
|
|
|
$settings->id_number = 'id number';
|
2021-01-26 22:42:00 +01:00
|
|
|
$settings->timezone_id = '1';
|
2021-06-29 12:13:10 +02:00
|
|
|
$settings->language_id = '1';
|
2020-02-24 11:15:30 +01:00
|
|
|
|
|
|
|
$this->company->settings = $settings;
|
|
|
|
$this->company->save();
|
|
|
|
|
|
|
|
$this->account->default_company_id = $this->company->id;
|
|
|
|
$this->account->save();
|
|
|
|
|
2021-01-26 22:42:00 +01:00
|
|
|
$user = User::whereEmail('user@example.com')->first();
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2021-01-26 22:42:00 +01:00
|
|
|
if (! $user) {
|
|
|
|
$user = User::factory()->create([
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $this->account->id,
|
2020-02-24 11:15:30 +01:00
|
|
|
'password' => Hash::make('ALongAndBriliantPassword'),
|
2020-09-06 11:38:10 +02:00
|
|
|
'confirmation_code' => $this->createDbHash(config('database.default')),
|
2020-02-24 11:15:30 +01:00
|
|
|
]);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-01-26 22:42:00 +01:00
|
|
|
$cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id);
|
2020-02-24 11:15:30 +01:00
|
|
|
$cu->is_owner = true;
|
|
|
|
$cu->is_admin = true;
|
|
|
|
$cu->save();
|
|
|
|
|
|
|
|
$this->token = \Illuminate\Support\Str::random(64);
|
|
|
|
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token = new CompanyToken;
|
2021-01-26 22:42:00 +01:00
|
|
|
$company_token->user_id = $user->id;
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token->company_id = $this->company->id;
|
|
|
|
$company_token->account_id = $this->account->id;
|
|
|
|
$company_token->name = 'test token';
|
|
|
|
$company_token->token = $this->token;
|
2021-11-09 23:11:56 +01:00
|
|
|
$company_token->is_system = true;
|
2020-04-09 12:48:04 +02:00
|
|
|
$company_token->save();
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$this->client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
]);
|
2020-02-24 11:15:30 +01:00
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
ClientContact::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
2020-02-24 11:15:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBaseLine()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->company->invoices->count(), 0);
|
|
|
|
$this->assertEquals($this->company->clients->count(), 1);
|
|
|
|
$this->assertEquals($this->client->balance, 0);
|
2020-02-26 04:26:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLedger()
|
|
|
|
{
|
2022-05-06 00:40:34 +02:00
|
|
|
$this->markTestSkipped();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-02-26 04:26:07 +01:00
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = [];
|
|
|
|
$item['quantity'] = 1;
|
|
|
|
$item['cost'] = 10;
|
2022-06-21 11:57:17 +02:00
|
|
|
$item['type_id'] = '1';
|
|
|
|
|
2020-02-26 04:26:07 +01:00
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'client_id' => $this->encodePrimaryKey($this->client->id),
|
2020-09-06 11:38:10 +02:00
|
|
|
'line_items' => $line_items,
|
2020-02-26 04:26:07 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Test adding one invoice */
|
2020-02-26 04:26:07 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $data)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$acc = $response->json();
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
//client->balance should = 10
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice->service()->markSent()->save();
|
|
|
|
|
2022-03-29 10:57:14 +02:00
|
|
|
$this->client = Client::find($this->client->id);
|
|
|
|
$this->assertEquals($this->client->balance, 10);
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$invoice_ledger = $invoice->company_ledger->sortByDesc('id')->first();
|
|
|
|
|
2022-04-22 14:01:32 +02:00
|
|
|
$this->assertEquals($invoice_ledger->balance, $this->client->balance);
|
2020-02-26 04:26:07 +01:00
|
|
|
$this->assertEquals($invoice->client->paid_to_date, 0);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Test adding another invoice */
|
2020-02-26 04:26:07 +01:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $data)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$acc = $response->json();
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
|
|
|
$invoice->service()->markSent()->save();
|
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
//client balance should = 20
|
2022-04-22 14:01:32 +02:00
|
|
|
$this->assertEquals($this->client->fresh()->balance, 20);
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice_ledger = $invoice->company_ledger->sortByDesc('id')->first();
|
|
|
|
|
2022-04-22 14:01:32 +02:00
|
|
|
$this->assertEquals($invoice_ledger->balance, $this->client->fresh()->balance);
|
2020-02-26 04:26:07 +01:00
|
|
|
$this->assertEquals($invoice->client->paid_to_date, 0);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Test making a payment */
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'client_id' => $this->encodePrimaryKey($invoice->client_id),
|
|
|
|
'amount' => $invoice->balance,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2022-06-21 11:57:17 +02:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($invoice->id),
|
|
|
|
'amount' => $invoice->balance,
|
2020-02-26 04:26:07 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/11',
|
|
|
|
];
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
try {
|
2020-11-25 15:19:52 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/', $data);
|
2020-10-22 08:46:02 +02:00
|
|
|
} catch (ValidationException $e) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(print_r($e->validator->getMessageBag(), 1));
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
2020-02-26 04:26:07 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$acc = $response->json();
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$payment = Payment::find($this->decodePrimaryKey($acc['data']['id']));
|
|
|
|
|
|
|
|
$payment_ledger = $payment->company_ledger->sortByDesc('id')->first();
|
2020-06-06 03:07:31 +02:00
|
|
|
|
2020-12-29 22:10:03 +01:00
|
|
|
//nlog($payment->client->balance);
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
$this->assertEquals($payment->client->balance, $payment_ledger->balance);
|
|
|
|
$this->assertEquals($payment->client->paid_to_date, 10);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice = Invoice::find($invoice->id);
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Test making a refund of a payment */
|
2020-02-26 04:26:07 +01:00
|
|
|
$refund = $invoice->amount;
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id' => $this->encodePrimaryKey($payment->id),
|
|
|
|
'client_id' => $this->encodePrimaryKey($invoice->client_id),
|
|
|
|
'amount' => $refund,
|
|
|
|
'invoices' => [
|
|
|
|
[
|
2022-06-21 11:57:17 +02:00
|
|
|
'invoice_id' => $this->encodePrimaryKey($invoice->id),
|
|
|
|
'amount' => $refund,
|
2020-02-26 04:26:07 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'date' => '2020/12/11',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/payments/refund', $data);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$acc = $response->json();
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice = Invoice::find($invoice->id);
|
|
|
|
|
|
|
|
$this->assertEquals($refund, $invoice->balance);
|
2020-02-24 11:15:30 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|