From 3abd0e0b17be629ecfd8b2d29a0b9528c563891d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Jun 2020 14:18:33 +1000 Subject: [PATCH 1/3] Fixes for tests --- app/Services/Payment/RefundPayment.php | 41 ++++++++++++++++++++++++++ tests/Feature/RefundTest.php | 2 -- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index ba17898919c5..822eaadbfb5c 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -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(); diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index 1df44379e248..898ac90a82ab 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -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']); } From 06a8ee12152e8ec95c5e301ca375c8eafb97b01a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Jun 2020 17:04:07 +1000 Subject: [PATCH 2/3] Fix throttling with tests --- tests/Feature/PaymentTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 70b8a4c4da3d..8963494ce2cd 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -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() From d3666b41f50d7a5003f973ef2adea49d648c2745 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Jun 2020 21:49:11 +1000 Subject: [PATCH 3/3] Add invoice history as an optional include --- app/Models/Invoice.php | 3 +- .../InvoiceHistoryTransformer.php | 38 +++++++++++++++++++ app/Transformers/InvoiceTransformer.php | 20 +++++----- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 app/Transformers/InvoiceHistoryTransformer.php diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 032fff4f4c04..ab3e1babfbf2 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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() diff --git a/app/Transformers/InvoiceHistoryTransformer.php b/app/Transformers/InvoiceHistoryTransformer.php new file mode 100644 index 000000000000..891fab4de0eb --- /dev/null +++ b/app/Transformers/InvoiceHistoryTransformer.php @@ -0,0 +1,38 @@ + $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, + ]; + } +} diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index eed103abfe2a..e3c49eb70530 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -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);