Merge pull request #3772 from turbo124/v2

Invoice History
This commit is contained in:
David Bomba 2020-06-02 07:49:08 +10:00 committed by GitHub
commit d4a5dae169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 16 deletions

View File

@ -19,6 +19,7 @@ use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\Backup;
use App\Models\CompanyLedger;
use App\Models\Currency;
use App\Models\Filterable;
@ -208,7 +209,7 @@ class Invoice extends BaseModel
public function history()
{
$this->activities->with('backup');
return $this->hasManyThrough(Backup::class, Activity::class);
}
// public function credits()

View File

@ -46,6 +46,7 @@ class RefundPayment
->buildCreditLineItems() //generate the credit note items
->updateCreditables() //return the credits first
->updatePaymentables() //update the paymentable items
->adjustInvoices()
->createActivity() // create the refund activity
->processGatewayRefund() //process the gateway refund if needed
->save();
@ -242,6 +243,46 @@ class RefundPayment
return $this;
}
private function adjustInvoices()
{
$adjustment_amount = 0;
if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0)
{
foreach ($this->refund_data['invoices'] as $refunded_invoice) {
$invoice = Invoice::find($refunded_invoice['invoice_id']);
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
if ($invoice->amount == $invoice->balance) {
$invoice->service()->setStatus(Invoice::STATUS_SENT);
} else {
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
}
$client = $invoice->client;
$adjustment_amount += $refunded_invoice['amount'];
$client->balance += $refunded_invoice['amount'];
$client->save();
//todo adjust ledger balance here? or after and reference the credit and its total
}
$ledger_string = ''; //todo
$this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string);
$this->payment->client->paid_to_date -= $this->refund_data['amount'];
$this->payment->client->save();
}
return $this;
}
private function save()
{
$this->payment->save();

View File

@ -0,0 +1,38 @@
<?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 App\Transformers;
use App\Models\Backup;
use App\Utils\Traits\MakesHash;
class InvoiceHistoryTransformer extends EntityTransformer
{
use MakesHash;
protected $defaultIncludes = [
];
protected $availableIncludes = [
];
public function transform(Backup $backup)
{
return [
'id' => $this->encodePrimaryKey($backup->id),
'activity_id' => $this->encodePrimaryKey($backup->activity_id),
'json_backup' => (string) $backup->json_backup ?: '',
'html_backup' => (string) $backup->html_backup ?: '',
'created_at' => (int)$backup->created_at,
'updated_at' => (int)$backup->updated_at,
];
}
}

View File

@ -11,10 +11,12 @@
namespace App\Transformers;
use App\Models\Backup;
use App\Models\Document;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Transformers\DocumentTransformer;
use App\Transformers\InvoiceHistoryTransformer;
use App\Transformers\InvoiceInvitationTransformer;
use App\Utils\Traits\MakesHash;
@ -29,6 +31,7 @@ class InvoiceTransformer extends EntityTransformer
protected $availableIncludes = [
'invitations',
'history'
// 'payments',
// 'client',
// 'documents',
@ -40,16 +43,15 @@ class InvoiceTransformer extends EntityTransformer
return $this->includeCollection($invoice->invitations, $transformer, InvoiceInvitation::class);
}
public function includeHistory(Invoice $invoice)
{
$transformer = new InvoiceHistoryTransformer($this->serializer);
return $this->includeCollection($invoice->history, $transformer, Backup::class);
}
/*
public function includeInvoiceItems(Invoice $invoice)
{
$transformer = new InvoiceItemTransformer($this->serializer);
return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEM);
}
public function includePayments(Invoice $invoice)
{
$transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);

View File

@ -21,6 +21,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\WithoutEvents;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
@ -43,10 +44,6 @@ class PaymentTest extends TestCase
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
Session::start();
$this->faker = \Faker\Factory::create();
@ -55,6 +52,10 @@ class PaymentTest extends TestCase
$this->makeTestData();
$this->withoutExceptionHandling();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
public function testPaymentList()

View File

@ -125,8 +125,6 @@ class RefundTest extends TestCase
$response->assertStatus(200);
info($arr);
$this->assertEquals(50, $arr['data']['refunded']);
$this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']);
}