Skip to content

Commit

Permalink
Explicitly call the service() method, rather than obfuscate. (invoice…
Browse files Browse the repository at this point in the history
…ninja#3281)

* Include fix as describe by @michael-hampton here invoiceninja#3280

* Refactor createinvitations away from jobs

* Clean up

* Fixes for service() refactoring

* Fixes for services refactor
  • Loading branch information
turbo124 authored Feb 4, 2020
1 parent c25de93 commit cda534e
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 255 deletions.
3 changes: 1 addition & 2 deletions app/Console/Commands/CreateTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use App\Factory\QuoteFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\CreateInvoiceInvitations;
use App\Jobs\Invoice\UpdateInvoicePayment;
use App\Jobs\Quote\CreateQuoteInvitations;
use App\Listeners\Credit\CreateCreditInvitation;
Expand Down Expand Up @@ -464,7 +463,7 @@ private function createInvoice($client)

$this->invoice_repo->markSent($invoice);

CreateInvoiceInvitations::dispatch($invoice, $invoice->company);
$invoice->service()->createInvitations();

if (rand(0, 1)) {
$payment = PaymentFactory::create($client->company->id, $client->user->id);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ private function performAction(Invoice $invoice, $action, $bulk = false)
return $this->errorResponse(['message' => 'Invoice cannot be marked as paid'], 400);
}

$invoice = $invoice->markPaid();
$invoice = $invoice->service()->markPaid();

if (!$bulk) {
return $this->itemResponse($invoice);
Expand Down
81 changes: 0 additions & 81 deletions app/Jobs/Credit/MarkCreditPaid.php

This file was deleted.

10 changes: 5 additions & 5 deletions app/Jobs/Credit/StoreCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function __construct(Credit $credit, array $data, Company $company)
*/
public function handle(CreditRepository $credit_repository): ?Credit
{
MultiDB::setDB($this->company->db);
// MultiDB::setDB($this->company->db);

$payment = false;
// $payment = false;

if ($payment) {
PaymentNotification::dispatch($payment, $payment->company);
}
// if ($payment) {
// PaymentNotification::dispatch($payment, $payment->company);
// }

return $this->credit;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/Invoice/ApplyPaymentToInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function handle()

$this->invoice->save();

$this->invoice = $this->invoice->applyNumber()->save();
$this->invoice = $this->invoice->service()->applyNumber()->save();

return $this->invoice;
}
Expand Down
67 changes: 0 additions & 67 deletions app/Jobs/Invoice/CreateInvoiceInvitations.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Jobs/Invoice/UpdateInvoicePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function handle()
$invoice->pivot->amount = $invoice->partial;
$invoice->pivot->save();

$invoice->updateBalance($invoice->partial*-1)
$invoice->service()->updateBalance($invoice->partial*-1)
->clearPartial()
->setDueDate()
->setStatus(Invoice::STATUS_PARTIAL)
Expand Down
50 changes: 0 additions & 50 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,56 +168,6 @@ public function service() :InvoiceService
return new InvoiceService($this);
}

public function markPaid() :InvoiceService
{
return $this->service()->markPaid();
}

public function applyNumber() :InvoiceService
{
return $this->service()->applyNumber();
}

public function applyPayment($payment, $payment_amount) :InvoiceService
{
return $this->service()->applyPayment($payment, $payment_amount);
}

public function updateBalance($balance_adjustment) :InvoiceService
{
return $this->service()->updateBalance($balance_adjustment);
}

public function setDueDate() :InvoiceService
{
return $this->service->setDueDate();
}

public function setStatus($status) :InvoiceService
{
return $this->service()->setStatus($status);
}

public function clearPartial() :InvoiceService
{
return $this->service()->clearPartial();
}

public function updatePartial($amount) :InvoiceService
{
return $this->service()->updatePartial($amount);
}

public function markSent() :InvoiceService
{
return $this->service()->markSent();
}

public function markViewed() :InvoiceService
{
return $this->service()->markViewed();
}

/* ---------------- */
/* Settings getters */
/* ---------------- */
Expand Down
7 changes: 3 additions & 4 deletions app/Repositories/InvoiceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use App\Factory\InvoiceInvitationFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\CreateInvoiceInvitations;
use App\Jobs\Product\UpdateOrCreateProduct;
use App\Listeners\Invoice\CreateInvoiceInvitation;
use App\Models\ClientContact;
Expand Down Expand Up @@ -100,7 +99,7 @@ public function save($data, Invoice $invoice) : ?Invoice

/* If no invitations have been created, this is our fail safe to maintain state*/
if ($invoice->invitations->count() == 0) {
CreateInvoiceInvitations::dispatchNow($invoice, $invoice->company);
$invoice->service()->createInvitations();
}

$invoice = $invoice->calc()->getInvoice();
Expand All @@ -114,7 +113,7 @@ public function save($data, Invoice $invoice) : ?Invoice
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount), $invoice->company);
}

$invoice = $invoice->applyNumber()->save();
$invoice = $invoice->service()->applyNumber()->save();

if ($invoice->company->update_products !== false) {
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice, $invoice->company);
Expand All @@ -132,6 +131,6 @@ public function save($data, Invoice $invoice) : ?Invoice
*/
public function markSent(Invoice $invoice) : ?Invoice
{
return $invoice->markSent()->save();
return $invoice->service()->markSent()->save();
}
}
2 changes: 1 addition & 1 deletion app/Repositories/PaymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function applyPayment(array $data, Payment $payment): ?Payment
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->first();

if ($invoice) {
$invoice->applyPayment($payment, $paid_invoice['amount'])->save();
$invoice->service()->applyPayment($payment, $paid_invoice['amount'])->save();
}
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions app/Services/Invoice/ApplyPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public function __invoke($payment, $payment_amount)
if ($this->invoice->hasPartial()) {
//is partial and amount is exactly the partial amount
if ($this->invoice->partial == $payment_amount) {
$this->invoice->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $payment_amount) { //partial amount exists, but the amount is less than the partial amount
$this->invoice->updatePartial($payment_amount*-1)->updateBalance($payment_amount*-1);
$this->invoice->service()->updatePartial($payment_amount*-1)->updateBalance($payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $payment_amount) { //partial exists and the amount paid is GREATER than the partial amount
$this->invoice->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
}
} elseif ($payment_amount == $this->invoice->balance) { //total invoice paid.
$this->invoice->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($payment_amount*-1);
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($payment_amount*-1);
} elseif($payment_amount < $this->invoice->balance) { //partial invoice payment made
$this->invoice->clearPartial()->updateBalance($payment_amount*-1);
$this->invoice->service()->clearPartial()->updateBalance($payment_amount*-1);
}

return $this->invoice;
Expand Down
49 changes: 49 additions & 0 deletions app/Services/Invoice/CreateInvitations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Services\Invoice;


use App\Factory\InvoiceInvitationFactory;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;

class CreateInvitations
{

public function __construct()
{
}

public function __invoke($invoice)
{

$contacts = $invoice->client->contacts;

$contacts->each(function ($contact) use($invoice){
$invitation = InvoiceInvitation::whereCompanyId($invoice->company_id)
->whereClientContactId($contact->id)
->whereInvoiceId($invoice->id)
->first();

if (!$invitation && $contact->send_invoice) {
$ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
$ii->invoice_id = $invoice->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && !$contact->send_invoice) {
$invitation->delete();
}
});

return $invoice;
}
}
Loading

0 comments on commit cda534e

Please sign in to comment.