2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Jobs\Credit\CreateCreditPdf;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Jobs\Invoice\CreateInvoicePdf;
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Credit;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-15 10:01:15 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class GetCreditPdf extends AbstractService
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
private $credit;
|
|
|
|
|
|
|
|
private $contact;
|
|
|
|
|
|
|
|
public function __construct(Credit $credit, ClientContact $contact = null)
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
$this->credit = $credit;
|
|
|
|
$this->contact = $contact;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
public function run()
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
if (!$this->contact) {
|
|
|
|
$this->contact = $this->credit->client->primary_contact()->first();
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 14:41:15 +01:00
|
|
|
$path = $this->credit->client->credit_filepath();
|
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
$file_path = $path . $this->credit->number . '.pdf';
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$disk = config('filesystems.default');
|
2020-03-06 14:41:15 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$file = Storage::disk($disk)->exists($file_path);
|
|
|
|
|
|
|
|
if (!$file) {
|
2020-03-06 14:41:15 +01:00
|
|
|
$file_path = CreateCreditPdf::dispatchNow($this->credit, $this->credit->company, $this->contact);
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 14:41:15 +01:00
|
|
|
return Storage::disk($disk)->path($file_path);
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
}
|