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; use GeneratesCounter;
private $customer; private $client;
public function __construct(Client $client) private $credit;
public function __construct(Client $client, Credit $credit)
{ {
$this->client = $client; $this->client = $client;
$this->credit = $credit;
} }
public function run($credit) public function run()
{ {
if ($credit->number != '') { if ($this->credit->number != '') {
return $credit; 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 <?php
namespace App\Services\Credit; namespace App\Services\Credit;
use App\Factory\CreditInvitationFactory; use App\Factory\CreditInvitationFactory;
use App\Models\Credit;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
use App\Services\AbstractService; use App\Services\AbstractService;
class CreateInvitations extends 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){ $contacts->each(function ($contact){
$invitation = CreditInvitation::whereCompanyId($credit->account_id) $invitation = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereClientContactId($contact->id) ->whereClientContactId($contact->id)
->whereCreditId($credit->id) ->whereCreditId($this->credit->id)
->first(); ->first();
if (!$invitation) { if (!$invitation) {
$ii = CreditInvitationFactory::create($credit->company_id, $credit->user_id); $ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id);
$ii->credit_id = $credit->id; $ii->credit_id = $this->credit->id;
$ii->client_contact_id = $contact->id; $ii->client_contact_id = $contact->id;
$ii->save(); $ii->save();
} elseif ($invitation && !$contact->send_email) { } elseif ($invitation && !$contact->send_email) {

View File

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

View File

@ -9,23 +9,29 @@ use Illuminate\Support\Facades\Storage;
class GetCreditPdf extends AbstractService 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) { if (!$this->contact) {
$contact = $credit->client->primary_contact()->first(); $this->contact = $this->credit->client->primary_contact()->first();
} }
$path = 'public/' . $credit->client->id . '/credits/'; $path = 'public/' . $this->credit->client->id . '/credits/';
$file_path = $path . $credit->number . '.pdf'; $file_path = $path . $this->credit->number . '.pdf';
$disk = config('filesystems.default'); $disk = config('filesystems.default');
$file = Storage::disk($disk)->exists($file_path); $file = Storage::disk($disk)->exists($file_path);
if (!$file) { 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); return Storage::disk($disk)->url($file_path);

View File

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