1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/Feature/MultiPaymentDeleteTest.php

366 lines
12 KiB
PHP
Raw Normal View History

2021-09-06 01:37:35 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2021-09-06 01:37:35 +02:00
*/
2021-09-06 01:37:35 +02:00
namespace Tests\Feature;
use App\Factory\CompanyTokenFactory;
use App\Factory\CompanyUserFactory;
use App\Factory\InvoiceFactory;
use App\Factory\InvoiceItemFactory;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
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\Validation\ValidationException;
use Tests\TestCase;
/**
* @test
*/
class MultiPaymentDeleteTest extends TestCase
{
use DatabaseTransactions, MakesHash;
private $faker;
protected function setUp() :void
2021-09-06 01:37:35 +02:00
{
parent::setUp();
$this->faker = \Faker\Factory::create();
}
public function testComplexRefundDeleteScenario()
{
$account = Account::factory()->create();
$company = Company::factory()->create([
'account_id' => $account->id,
]);
2021-09-06 01:37:35 +02:00
$account->default_company_id = $company->id;
$account->save();
$user = User::factory()->create([
'account_id' => $account->id,
'confirmation_code' => '11',
'email' => $this->faker->unique()->safeEmail(),
2021-09-06 01:37:35 +02:00
]);
$cu = CompanyUserFactory::create($user->id, $company->id, $account->id);
$cu->is_owner = true;
$cu->is_admin = true;
$cu->save();
$token = new CompanyToken;
$token->user_id = $user->id;
$token->company_id = $company->id;
$token->account_id = $account->id;
$token->name = 'test token';
$token->token = 'okeytokey';
$token->is_system = true;
$token->save();
$client = Client::factory()->create([
'user_id' => $user->id,
2021-09-06 01:37:35 +02:00
'company_id' => $company->id,
]);
2021-09-06 01:37:35 +02:00
ClientContact::factory()->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
'is_primary' => 1,
]);
ClientContact::factory()->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
]);
$invoice = Invoice::factory()->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
'number' => (string) $this->faker->randomNumber(6),
]);
2021-09-06 01:37:35 +02:00
$invoice = InvoiceFactory::create($company->id, $user->id);
2021-09-06 01:37:35 +02:00
$invoice->client_id = $client->id;
$invoice->status_id = Invoice::STATUS_DRAFT;
$line_items = [];
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 325;
$item->type_id = 1;
$line_items[] = $item;
$invoice->line_items = $line_items;
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(0, $client->balance);
$this->assertEquals(0, $client->paid_to_date);
2021-09-06 01:37:35 +02:00
$this->assertEquals(0, $invoice->balance);
//mark sent
2021-09-06 01:37:35 +02:00
2022-02-26 13:12:14 +01:00
$invoice = $invoice->service()->markSent()->createInvitations()->save();
2021-09-06 01:37:35 +02:00
$invoice->fresh();
$invoice->client->fresh();
$this->assertEquals(325, $invoice->balance);
$this->assertEquals(0, $invoice->client->fresh()->paid_to_date);
2021-09-06 01:37:35 +02:00
$this->assertEquals(325, $invoice->client->balance);
//payment 163
2021-09-06 02:22:33 +02:00
//
2021-09-06 01:37:35 +02:00
$data = [
'amount' => 163.0,
'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [
[
'invoice_id' => $this->encodePrimaryKey($invoice->id),
'amount' => 163,
2021-09-06 01:37:35 +02:00
],
],
2021-09-06 01:37:35 +02:00
'date' => '2019/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/', $data);
$arr = $response->json();
$payment_id = $arr['data']['id'];
$payment_1 = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
//payment 162
2021-09-06 01:37:35 +02:00
$this->assertEquals(162, $invoice->fresh()->balance);
$this->assertEquals(162, $invoice->client->fresh()->balance);
$this->assertEquals(163, $invoice->client->fresh()->paid_to_date);
2021-09-06 01:37:35 +02:00
$data = [
'amount' => 162.0,
'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [
[
'invoice_id' => $this->encodePrimaryKey($invoice->id),
'amount' => 162,
2021-09-06 01:37:35 +02:00
],
],
2021-09-06 01:37:35 +02:00
'date' => '2019/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/', $data);
$arr = $response->json();
$payment_id = $arr['data']['id'];
$payment_2 = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
2021-09-06 01:42:19 +02:00
//refund payment 2 by 63 dollars
2021-09-06 01:42:19 +02:00
$data = [
'id' => $this->encodePrimaryKey($payment_2->id),
'amount' => 63,
'date' => '2021/12/12',
'invoices' => [
[
'invoice_id' => $invoice->hashed_id,
'amount' => 63,
2021-09-06 01:42:19 +02:00
],
],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/refund', $data);
$this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//delete payment 2
2021-09-06 01:42:19 +02:00
//
$data = [
'ids' => [$this->encodePrimaryKey($payment_2->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/bulk?action=delete', $data);
$this->assertEquals(162, $invoice->fresh()->balance);
$this->assertEquals(162, $invoice->client->fresh()->balance);
$this->assertEquals(163, $invoice->client->fresh()->paid_to_date);
2021-09-06 02:22:33 +02:00
// Pay 162 again and create payment #3
2021-09-06 02:22:33 +02:00
$data = [
'amount' => 162.0,
'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [
[
'invoice_id' => $this->encodePrimaryKey($invoice->id),
'amount' => 162,
2021-09-06 02:22:33 +02:00
],
],
2021-09-06 02:22:33 +02:00
'date' => '2019/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/', $data);
2021-09-06 02:22:33 +02:00
$arr = $response->json();
$payment_id = $arr['data']['id'];
$payment_3 = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$invoice->fresh();
$invoice->client->fresh();
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
2021-09-06 02:22:33 +02:00
//refund payment 3 by 63
2021-09-06 02:22:33 +02:00
$data = [
'id' => $this->encodePrimaryKey($payment_3->id),
'amount' => 63,
'date' => '2021/12/12',
'invoices' => [
[
'invoice_id' => $invoice->hashed_id,
'amount' => 63,
2021-09-06 02:22:33 +02:00
],
],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/refund', $data);
$this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
2021-09-06 02:22:33 +02:00
//payment 4 for 63
2021-09-06 02:22:33 +02:00
$data = [
'amount' => 63.0,
'client_id' => $this->encodePrimaryKey($client->id),
'invoices' => [
[
'invoice_id' => $this->encodePrimaryKey($invoice->id),
'amount' => 63,
2021-09-06 02:22:33 +02:00
],
],
2021-09-06 02:22:33 +02:00
'date' => '2019/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/', $data);
2021-09-06 02:22:33 +02:00
$arr = $response->json();
$payment_id = $arr['data']['id'];
$payment_4 = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(325, $invoice->client->fresh()->paid_to_date);
2021-09-06 02:22:33 +02:00
// delete payment 3
//
2021-09-06 02:22:33 +02:00
//
$data = [
'ids' => [$this->encodePrimaryKey($payment_4->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/bulk?action=delete', $data);
$this->assertEquals(63, $invoice->fresh()->balance);
$this->assertEquals(63, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
2021-09-06 02:22:33 +02:00
//set discount of 63 to invoice
2021-09-06 02:22:33 +02:00
$invoice = $invoice->fresh();
$invoice->discount = 63;
$invoice->is_amount_discount = true;
$invoice->save();
$invoice->calc()->getInvoice()->save();
$invoice->service()->updateStatus()->save();
$invoice->ledger()->updateInvoiceBalance(-63, "Update adjustment for invoice {$invoice->number}");
$invoice->client->service()->updateBalance(-63)->save();
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(262, $invoice->client->fresh()->paid_to_date);
//now delete the invoice
$data = [
'ids' => [$invoice->hashed_id],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/invoices/bulk?action=delete', $data);
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->paid_to_date);
//Delete payment 4 which is for $162
$data = [
'ids' => [$this->encodePrimaryKey($payment_1->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token->token,
])->post('/api/v1/payments/bulk?action=delete', $data);
$this->assertEquals(0, $invoice->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->balance);
$this->assertEquals(0, $invoice->client->fresh()->paid_to_date);
2021-09-06 01:37:35 +02:00
}
}