1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Explicitly call the service() method, rather than obfuscate. (#3281)

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

* Refactor createinvitations away from jobs

* Clean up

* Fixes for service() refactoring

* Fixes for services refactor
This commit is contained in:
David Bomba 2020-02-04 18:51:44 +11:00 committed by GitHub
parent c25de936ed
commit cda534e996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 106 additions and 255 deletions

View File

@ -12,7 +12,6 @@ use App\Factory\PaymentFactory;
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;
@ -464,7 +463,7 @@ class CreateTestData extends Command
$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);

View File

@ -618,7 +618,7 @@ class InvoiceController extends BaseController
return $this->errorResponse(['message' => 'Invoice cannot be marked as paid'], 400);
}
$invoice = $invoice->markPaid();
$invoice = $invoice->service()->markPaid();
if (!$bulk) {
return $this->itemResponse($invoice);

View File

@ -1,81 +0,0 @@
<?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\Jobs\Invoice;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\CreditFactory;
use App\Factory\PaymentFactory;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Credit;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class MarkCreditPaid implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $credit;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Credit $credit, Company $company)
{
$this->credit = $credit;
$this->company = $company;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
if($this->credit->status_id == Credit::STATUS_DRAFT)
$this->credit->markSent();
/* Create Payment */
$payment = PaymentFactory::create($this->credit->company_id, $this->credit->user_id);
$payment->amount = $this->credit->balance;
$payment->status_id = Credit::STATUS_COMPLETED;
$payment->client_id = $this->credit->client_id;
$payment->transaction_reference = ctrans('texts.manual_entry');
/* Create a payment relationship to the invoice entity */
$payment->save();
$payment->credits()->attach($this->credit->id, [
'amount' => $payment->amount
]);
$this->credit->updateBalance($payment->amount*-1);
/* Update Credit balance */
event(new PaymentWasCreated($payment, $payment->company));
// UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1), $this->company);
return $this->credit;
}
}

View File

@ -44,13 +44,13 @@ class StoreCredit implements ShouldQueue
*/
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;
}

View File

@ -113,7 +113,7 @@ class ApplyPaymentToInvoice implements ShouldQueue
$this->invoice->save();
$this->invoice = $this->invoice->applyNumber()->save();
$this->invoice = $this->invoice->service()->applyNumber()->save();
return $this->invoice;
}

View File

@ -1,67 +0,0 @@
<?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\Jobs\Invoice;
use App\Factory\InvoiceInvitationFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CreateInvoiceInvitations implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
public function handle()
{
MultiDB::setDB($this->company->db);
$contacts = $this->invoice->client->contacts;
$contacts->each(function ($contact) {
$invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id)
->whereClientContactId($contact->id)
->whereInvoiceId($this->invoice->id)
->first();
if (!$invitation && $contact->send_invoice) {
$ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id);
$ii->invoice_id = $this->invoice->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && !$contact->send_invoice) {
$invitation->delete();
}
});
}
}

View File

@ -103,7 +103,7 @@ class UpdateInvoicePayment implements ShouldQueue
$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)

View File

@ -168,56 +168,6 @@ class Invoice extends BaseModel
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 */
/* ---------------- */

View File

@ -16,7 +16,6 @@ use App\Events\Invoice\InvoiceWasUpdated;
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;
@ -100,7 +99,7 @@ class InvoiceRepository extends BaseRepository
/* 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();
@ -114,7 +113,7 @@ class InvoiceRepository extends BaseRepository
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);
@ -132,6 +131,6 @@ class InvoiceRepository extends BaseRepository
*/
public function markSent(Invoice $invoice) : ?Invoice
{
return $invoice->markSent()->save();
return $invoice->service()->markSent()->save();
}
}

View File

@ -94,7 +94,7 @@ class PaymentRepository extends BaseRepository
$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 {

View File

@ -44,16 +44,16 @@ class ApplyPayment
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;

View File

@ -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;
}
}

View File

@ -15,6 +15,8 @@ use App\Models\Invoice;
use App\Models\Payment;
use App\Services\Client\ClientService;
use App\Services\Invoice\ApplyNumber;
use App\Services\Invoice\ApplyPayment;
use App\Services\Invoice\CreateInvitations;
use App\Services\Invoice\MarkInvoicePaid;
use App\Services\Invoice\MarkSent;
use App\Services\Invoice\UpdateBalance;
@ -92,6 +94,15 @@ class InvoiceService
return $this;
}
public function createInvitations()
{
$create_invitation = new CreateInvitations();
$this->invoice = $create_invitation($this->invoice);
return $this;
}
public function markSent()
{
$mark_sent = new MarkSent($this->invoice->client);
@ -149,9 +160,6 @@ class InvoiceService
/**
* Saves the invoice
* @return Invoice object

View File

@ -48,7 +48,10 @@ class MarkPaid
'amount' => $payment->amount
]);
$invoice->updateBalance($payment->amount*-1)->save();
$invoice->service()
->updateBalance($payment->amount*-1)
->setStatus(Invoice::STATUS_PAID)
->save();
/* Update Invoice balance */
event(new PaymentWasCreated($payment, $payment->company));

View File

@ -43,7 +43,7 @@ class MarkSent
$this->client->updateBalance($invoice->balance)->save();
$invoice->applyNumber()->save();
$invoice->service()->applyNumber()->save();
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);

View File

@ -225,12 +225,12 @@ trait Refundable
{
$invoice = Invoice::find($refunded_invoice['invoice_id']);
$invoice->updateBalance($refunded_invoice['amount'])->save();
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
if($invoice->amount == $invoice->balance)
$invoice->setStatus(Invoice::STATUS_SENT);
$invoice->service()->setStatus(Invoice::STATUS_SENT);
else
$invoice->setStatus(Invoice::STATUS_PARTIAL);
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
$client = $invoice->client;

View File

@ -7,7 +7,6 @@ use App\DataMapper\DefaultSettings;
use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Factory\InvoiceInvitationFactory;
use App\Jobs\Account\CreateAccount;
use App\Listeners\Invoice\CreateInvoiceInvitations;
use App\Models\Account;
use App\Models\Client;
use App\Models\Invoice;

View File

@ -295,8 +295,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$data = [
@ -373,7 +372,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent()->save();
$this->invoice->is_deleted = false;
$this->invoice->save();
@ -445,8 +444,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$data = [
@ -506,8 +504,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$data = [
@ -563,8 +560,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$data = [
@ -630,8 +626,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$payment = PaymentFactory::create($this->company->id, $this->user->id);
$payment->amount = 10;
@ -690,8 +685,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->save();
$this->invoice->service()->markSent()->save();
$payment = PaymentFactory::create($this->company->id, $this->user->id);
$payment->amount = 10;
@ -757,7 +751,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent()->save();
$data = [
'amount' => 15.0,
@ -810,7 +804,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent()->save();
$data = [
@ -870,7 +864,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent()->save();
$data = [
'amount' => 20.0,
@ -914,7 +908,7 @@ class PaymentTest extends TestCase
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent()->save();
$data = [

View File

@ -45,7 +45,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->invoice->setReminder($settings);
$this->assertEquals(0, Carbon::now()->addDays(7)->diffInDays($this->invoice->next_send_date));
@ -68,8 +68,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->setStatus(Invoice::STATUS_PAID);
$this->invoice->service()->markSent()->setStatus(Invoice::STATUS_PAID);
$this->invoice->setReminder($settings);
$this->assertEquals($this->invoice->next_send_date, null);
@ -92,7 +91,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->invoice->setReminder($settings);
$this->assertEquals(0, Carbon::parse($this->invoice->due_date)->subDays(29)->diffInDays($this->invoice->next_send_date));
@ -115,7 +114,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->invoice->setReminder($settings);
$this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date));
@ -138,7 +137,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->invoice->setReminder($settings);
$this->assertEquals($this->invoice->next_send_date, null);
@ -161,7 +160,7 @@ class CheckRemindersTest extends TestCase
$settings->num_days_reminder3 = 0;
$this->company->settings = $settings;
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->invoice->setReminder($settings);
$this->assertEquals($this->invoice->next_send_date, null);

View File

@ -42,7 +42,7 @@ class MarkInvoicePaidTest extends TestCase
$client = $invoice->client;
$client_balance = $client->balance;
$this->invoice->markPaid();
$this->invoice->service()->markPaid();
$invoice = Invoice::find($this->invoice->id);
$client = $invoice->client;

View File

@ -37,7 +37,7 @@ class UpdateCompanyLedgerTest extends TestCase
public function testPaymentIsPresentInLedger()
{
$invoice = $this->invoice->markPaid()->save();
$invoice = $this->invoice->service()->markPaid()->save();
$ledger = CompanyLedger::whereClientId($invoice->client_id)
->whereCompanyId($invoice->company_id)

View File

@ -23,7 +23,6 @@ use App\Factory\InvoiceItemFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\CreateInvoiceInvitations;
use App\Models\Client;
use App\Models\CompanyGateway;
use App\Models\CompanyToken;
@ -179,7 +178,7 @@ trait MockAccountData
$this->invoice->save();
$this->invoice->markSent();
$this->invoice->service()->markSent();
$this->credit = CreditFactory::create($this->company->id,$this->user->id);
$this->credit->client_id = $this->client->id;