2020-04-08 12:48:31 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-04-08 12:48:31 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
|
|
|
use App\Listeners\Credit\CreateCreditInvitation;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Paymentable;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Services\Invoice\HandleReversal
|
|
|
|
*/
|
|
|
|
class ReverseInvoiceTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReverseInvoice()
|
|
|
|
{
|
2020-04-15 02:30:52 +02:00
|
|
|
$amount = $this->invoice->amount;
|
|
|
|
$balance = $this->invoice->balance;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->invoice->service()->markPaid()->save();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$first_payment = $this->invoice->payments->first();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertEquals((float) $first_payment->amount, (float) $this->invoice->amount);
|
|
|
|
$this->assertEquals((float) $first_payment->applied, (float) $this->invoice->amount);
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->assertTrue($this->invoice->invoiceReversable($this->invoice));
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
$balance_remaining = $this->invoice->balance;
|
|
|
|
$total_paid = $this->invoice->amount - $this->invoice->balance;
|
|
|
|
|
|
|
|
/*Adjust payment applied and the paymentables to the correct amount */
|
|
|
|
|
|
|
|
$paymentables = Paymentable::wherePaymentableType(Invoice::class)
|
2020-04-15 02:30:52 +02:00
|
|
|
->wherePaymentableId($this->invoice->id)
|
|
|
|
->get();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$paymentables->each(function ($paymentable) use ($total_paid) {
|
|
|
|
$reversable_amount = $paymentable->amount - $paymentable->refunded;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$total_paid -= $reversable_amount;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$paymentable->amount = $paymentable->refunded;
|
|
|
|
$paymentable->save();
|
|
|
|
});
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
/* Generate a credit for the $total_paid amount */
|
|
|
|
$credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
|
|
|
$credit->client_id = $this->invoice->client_id;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = (float) $total_paid;
|
|
|
|
$item->notes = 'Credit for reversal of '.$this->invoice->number;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$line_items[] = $item;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
$credit->line_items = $line_items;
|
|
|
|
|
|
|
|
$credit->save();
|
|
|
|
|
|
|
|
$credit_calc = new InvoiceSum($credit);
|
|
|
|
$credit_calc->build();
|
|
|
|
|
|
|
|
$credit = $credit_calc->getCredit();
|
|
|
|
|
|
|
|
$credit->service()
|
2020-04-15 02:30:52 +02:00
|
|
|
->setStatus(Credit::STATUS_SENT)
|
|
|
|
->markSent()->save();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
/* Set invoice balance to 0 */
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice->ledger()->updateInvoiceBalance($balance_remaining * -1, $item->notes)->save();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
/* Set invoice status to reversed... somehow*/
|
|
|
|
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
/* Reduce client.paid_to_date by $total_paid amount */
|
|
|
|
$this->client->paid_to_date -= $total_paid;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
/* Reduce the client balance by $balance_remaining */
|
|
|
|
$this->client->balance -= $balance_remaining;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->client->save();
|
2020-04-08 12:48:31 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
//create a ledger row for this with the resulting Credit ( also include an explanation in the notes section )
|
2020-04-08 12:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReversalViaAPI()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->client->balance, $this->invoice->balance);
|
|
|
|
|
|
|
|
$client_paid_to_date = $this->client->paid_to_date;
|
|
|
|
$client_balance = $this->client->balance;
|
|
|
|
$invoice_balance = $this->invoice->balance;
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_SENT, $this->invoice->status_id);
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->service()->markPaid()->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertEquals($this->client->balance, ($this->invoice->balance * -1));
|
|
|
|
$this->assertEquals($this->client->paid_to_date, ($client_paid_to_date + $invoice_balance));
|
2020-04-08 12:48:31 +02:00
|
|
|
$this->assertEquals(0, $this->invoice->balance);
|
|
|
|
$this->assertEquals(Invoice::STATUS_PAID, $this->invoice->status_id);
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->service()->handleReversal()->save();
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_REVERSED, $this->invoice->status_id);
|
|
|
|
$this->assertEquals(0, $this->invoice->balance);
|
|
|
|
$this->assertEquals($this->client->paid_to_date, ($client_paid_to_date));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReversalNoPayment()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->client->balance, $this->invoice->balance);
|
|
|
|
|
|
|
|
$client_paid_to_date = $this->client->paid_to_date;
|
|
|
|
$client_balance = $this->client->balance;
|
|
|
|
$invoice_balance = $this->invoice->balance;
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_SENT, $this->invoice->status_id);
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->service()->handleReversal()->save();
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_REVERSED, $this->invoice->status_id);
|
|
|
|
$this->assertEquals(0, $this->invoice->balance);
|
|
|
|
$this->assertEquals($this->client->paid_to_date, ($client_paid_to_date));
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertEquals($this->client->balance, ($client_balance - $invoice_balance));
|
2020-04-08 12:48:31 +02:00
|
|
|
}
|
|
|
|
}
|