Fix Credit Service Methods (#3350)

This commit is contained in:
David Bomba 2020-02-19 07:56:21 +11:00 committed by GitHub
parent 96a250edac
commit 4a41685e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 32 deletions

View File

@ -20,23 +20,27 @@ class ApplyNumber extends AbstractService
{
use GeneratesCounter;
private $customer;
private $client;
public function __construct(Client $client)
private $credit;
public function __construct(Client $client, Credit $credit)
{
$this->client = $client;
$this->credit = $credit;
}
public function run($credit)
public function run()
{
if ($credit->number != '') {
return $credit;
if ($this->credit->number != '') {
return $this->credit;
}
$credit->number = $this->getNextCreditNumber($this->client);
$this->credit->number = $this->getNextCreditNumber($this->client);
return $credit;
return $this->credit;
}
}

View File

@ -1,31 +1,35 @@
<?php
namespace App\Services\Credit;
use App\Factory\CreditInvitationFactory;
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Services\AbstractService;
class CreateInvitations extends AbstractService
{
private $credit;
public function __construct()
public function __construct(Credit $credit)
{
$this->credit = $credit;
}
public function run($credit)
public function run()
{
$contacts = $credit->client->contacts;
$contacts = $this->credit->client->contacts;
$contacts->each(function ($contact) use($credit){
$invitation = CreditInvitation::whereCompanyId($credit->account_id)
$contacts->each(function ($contact){
$invitation = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereClientContactId($contact->id)
->whereCreditId($credit->id)
->whereCreditId($this->credit->id)
->first();
if (!$invitation) {
$ii = CreditInvitationFactory::create($credit->company_id, $credit->user_id);
$ii->credit_id = $credit->id;
$ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id);
$ii->credit_id = $this->credit->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && !$contact->send_email) {

View File

@ -16,9 +16,7 @@ class CreditService
public function getCreditPdf($contact)
{
$get_invoice_pdf = new GetCreditPdf();
return $get_invoice_pdf($this->credit, $contact);
return (new GetCreditPdf($this->credit, $contact))->run();
}
/**
@ -27,18 +25,14 @@ class CreditService
*/
public function applyNumber()
{
$apply_number = new ApplyNumber($this->credit->customer);
$this->credit = $apply_number($this->credit);
$this->credit = (new ApplyNumber($this->credit->client, $this->credit))->run();
return $this;
}
public function createInvitations()
{
$create_invitation = new CreateInvitations();
$this->invoice = $create_invitation($this->invoice);
$this->credit = (new CreateInvitations($this->credit))->run();
return $this;
}

View File

@ -9,23 +9,29 @@ use Illuminate\Support\Facades\Storage;
class GetCreditPdf extends AbstractService
{
public function __construct()
private $credit;
private $contact;
public function __construct(Credit $credit, ClientContact $contact = null)
{
$this->credit = $credit;
$this->contact = $contact;
}
public function __invoke($credit, $contact = null)
public function run()
{
if (!$contact) {
$contact = $credit->client->primary_contact()->first();
if (!$this->contact) {
$this->contact = $this->credit->client->primary_contact()->first();
}
$path = 'public/' . $credit->client->id . '/credits/';
$file_path = $path . $credit->number . '.pdf';
$path = 'public/' . $this->credit->client->id . '/credits/';
$file_path = $path . $this->credit->number . '.pdf';
$disk = config('filesystems.default');
$file = Storage::disk($disk)->exists($file_path);
if (!$file) {
$file_path = CreateInvoicePdf::dispatchNow($this, $credit->company, $contact);
$file_path = CreateInvoicePdf::dispatchNow($this->credit, $this->credit->company, $this->contact);
}
return Storage::disk($disk)->url($file_path);

View File

@ -97,7 +97,6 @@ trait MockAccountData
$settings = CompanySettings::defaults();
$settings->name = 'The Best Company Name';
$settings->company_logo = 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png';
$settings->website = 'www.invoiceninja.com';
$settings->address1 = 'Address 1';