mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 16:24:35 -04:00
Testing Autobill
This commit is contained in:
parent
8255b1d7e2
commit
d2d6262941
@ -322,6 +322,8 @@ class BaseRepository
|
|||||||
if ($class->name == Credit::class) {
|
if ($class->name == Credit::class) {
|
||||||
$model = $model->calc()->getCredit();
|
$model = $model->calc()->getCredit();
|
||||||
|
|
||||||
|
$model->ledger()->updateCreditBalance(($state['finished_amount'] - $state['starting_amount']));
|
||||||
|
|
||||||
if (! $model->design_id) {
|
if (! $model->design_id) {
|
||||||
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
|
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ class MarkSent
|
|||||||
->applyNumber()
|
->applyNumber()
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
|
|
||||||
return $this->credit;
|
return $this->credit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ use App\Models\PaymentHash;
|
|||||||
use App\Services\AbstractService;
|
use App\Services\AbstractService;
|
||||||
use App\Services\Client\ClientService;
|
use App\Services\Client\ClientService;
|
||||||
use App\Services\Payment\PaymentService;
|
use App\Services\Payment\PaymentService;
|
||||||
|
use App\Utils\Ninja;
|
||||||
use App\Utils\Traits\GeneratesCounter;
|
use App\Utils\Traits\GeneratesCounter;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
@ -94,12 +95,18 @@ class AutoBillInvoice extends AbstractService
|
|||||||
*/
|
*/
|
||||||
private function finalizePaymentUsingCredits()
|
private function finalizePaymentUsingCredits()
|
||||||
{
|
{
|
||||||
|
info("finalizing");
|
||||||
|
info(print_r($this->used_credit,1));
|
||||||
$amount = array_sum(array_column($this->used_credit, 'amount'));
|
$amount = array_sum(array_column($this->used_credit, 'amount'));
|
||||||
|
info("amount {$amount}");
|
||||||
|
|
||||||
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
||||||
$payment->amount = $amount;
|
$payment->amount = $amount;
|
||||||
|
$payment->client_id = $this->invoice->client_id;
|
||||||
|
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
|
||||||
$payment->date = now();
|
$payment->date = now();
|
||||||
$payment->save();
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
|
$payment->service()->applyNumber()->save();
|
||||||
|
|
||||||
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
|
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
|
||||||
|
|
||||||
@ -107,7 +114,7 @@ class AutoBillInvoice extends AbstractService
|
|||||||
|
|
||||||
foreach($this->used_credit as $credit)
|
foreach($this->used_credit as $credit)
|
||||||
{
|
{
|
||||||
$payment->credits()->attach($credit['credit_id'], ['amount' => $credit['amount']);
|
$payment->credits()->attach($credit['credit_id'], ['amount' => $credit['amount']]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$payment->ledger()
|
$payment->ledger()
|
||||||
@ -122,9 +129,11 @@ class AutoBillInvoice extends AbstractService
|
|||||||
|
|
||||||
$this->invoice->ledger()
|
$this->invoice->ledger()
|
||||||
->updateInvoiceBalance($amount * -1, 'Invoice payment using Credit')
|
->updateInvoiceBalance($amount * -1, 'Invoice payment using Credit')
|
||||||
->updateCreditBalance($amount * -1, 'Credits used to pay down Invoice ' . $invoice->number)
|
->updateCreditBalance($amount * -1, 'Credits used to pay down Invoice ' . $this->invoice->number)
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
|
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
||||||
|
|
||||||
return $this->invoice->service()->setStatus(Invoice::STATUS_PAID)->save();
|
return $this->invoice->service()->setStatus(Invoice::STATUS_PAID)->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +163,7 @@ class AutoBillInvoice extends AbstractService
|
|||||||
$is_partial_amount = true;
|
$is_partial_amount = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->used_credit = []
|
$this->used_credit = [];
|
||||||
|
|
||||||
foreach($available_credits as $key => $credit) {
|
foreach($available_credits as $key => $credit) {
|
||||||
|
|
||||||
@ -179,14 +188,14 @@ class AutoBillInvoice extends AbstractService
|
|||||||
|
|
||||||
//more credit than needed
|
//more credit than needed
|
||||||
if($credit->balance >= $this->invoice->balance) {
|
if($credit->balance >= $this->invoice->balance) {
|
||||||
$used_credit[$key]['credit_id'] = $credit->id;
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
||||||
$used_credit[$key]['amount'] = $this->invoice->balance;
|
$this->used_credit[$key]['amount'] = $this->invoice->balance;
|
||||||
$this->invoice->balance = 0;
|
$this->invoice->balance = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$used_credit[$key]['credit_id'] = $credit->id;
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
||||||
$used_credit[$key]['amount'] = $credit->balance;
|
$this->used_credit[$key]['amount'] = $credit->balance;
|
||||||
$this->invoice->balance -= $credit->balance;
|
$this->invoice->balance -= $credit->balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,8 @@ class LedgerService
|
|||||||
$company_ledger->save();
|
$company_ledger->save();
|
||||||
|
|
||||||
$this->entity->company_ledger()->save($company_ledger);
|
$this->entity->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateCreditBalance($adjustment, $notes = '')
|
public function updateCreditBalance($adjustment, $notes = '')
|
||||||
|
@ -227,12 +227,6 @@ trait MockAccountData
|
|||||||
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
$this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id
|
||||||
$this->invoice->client_id = $this->client->id;
|
$this->invoice->client_id = $this->client->id;
|
||||||
|
|
||||||
// $this->invoice = Invoice::factory()->create([
|
|
||||||
// 'user_id' => $this->user->id,
|
|
||||||
// 'client_id' => $this->client->id,
|
|
||||||
// 'company_id' => $this->company->id,
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
$this->invoice->line_items = $this->buildLineItems();
|
$this->invoice->line_items = $this->buildLineItems();
|
||||||
$this->invoice->uses_inclusive_taxes = false;
|
$this->invoice->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
@ -316,7 +310,6 @@ trait MockAccountData
|
|||||||
$this->credit->uses_inclusive_taxes = false;
|
$this->credit->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
$this->credit->save();
|
$this->credit->save();
|
||||||
|
|
||||||
$this->credit->service()->createInvitations()->markSent();
|
$this->credit->service()->createInvitations()->markSent();
|
||||||
|
|
||||||
$this->credit_calc = new InvoiceSum($this->credit);
|
$this->credit_calc = new InvoiceSum($this->credit);
|
||||||
@ -325,6 +318,9 @@ trait MockAccountData
|
|||||||
$this->credit = $this->credit_calc->getCredit();
|
$this->credit = $this->credit_calc->getCredit();
|
||||||
$this->credit->service()->markSent();
|
$this->credit->service()->markSent();
|
||||||
|
|
||||||
|
$this->client->service()->adjustCreditBalance($this->credit->balance)->save();
|
||||||
|
$this->credit->ledger()->updateCreditBalance($this->credit->balance)->save();
|
||||||
|
|
||||||
$contacts = $this->invoice->client->contacts;
|
$contacts = $this->invoice->client->contacts;
|
||||||
|
|
||||||
$contacts->each(function ($contact) {
|
$contacts->each(function ($contact) {
|
||||||
|
57
tests/Unit/AutoBillInvoiceTest.php
Normal file
57
tests/Unit/AutoBillInvoiceTest.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers App\Services\Invoice\AutoBillInvoice
|
||||||
|
*/
|
||||||
|
class AutoBillInvoiceTest extends TestCase
|
||||||
|
{
|
||||||
|
use MockAccountData;
|
||||||
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
public function setUp() :void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->makeTestData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAutoBillFunctionality()
|
||||||
|
{
|
||||||
|
|
||||||
|
// info("client balance = {$this->client->balance}");
|
||||||
|
// info("invoice balance = {$this->invoice->balance}");
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals($this->client->balance, 10);
|
||||||
|
$this->assertEquals($this->client->paid_to_date, 0);
|
||||||
|
$this->assertEquals($this->client->credit_balance, 10);
|
||||||
|
|
||||||
|
$this->invoice->service()->markSent()->autoBill()->save();
|
||||||
|
|
||||||
|
// info(print_r($this->invoice->payments()->first()->toArray(),1));
|
||||||
|
$this->assertNotNull($this->invoice->payments());
|
||||||
|
$this->assertEquals(10, $this->invoice->payments()->sum('payments.amount'));
|
||||||
|
|
||||||
|
//info(print_r($this->invoice->payments()->get(),1));
|
||||||
|
$this->assertEquals($this->client->balance, 0);
|
||||||
|
$this->assertEquals($this->client->paid_to_date, 10);
|
||||||
|
$this->assertEquals($this->client->credit_balance, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user