1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #6005 from turbo124/v5-develop

Refactor for entity paths
This commit is contained in:
David Bomba 2021-06-13 08:49:42 +10:00 committed by GitHub
commit 86f97cc443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 88 additions and 65 deletions

View File

@ -74,14 +74,12 @@ class GmailTransport extends Transport
} }
} }
$this->gmail->send(); $this->gmail->send();
$this->sendPerformed($message); $this->sendPerformed($message);
return $this->numberOfRecipients($message); return $this->numberOfRecipients($message);
} }
} }

View File

@ -164,8 +164,9 @@ class InvoiceController extends Controller
//if only 1 pdf, output to buffer for download //if only 1 pdf, output to buffer for download
if ($invoices->count() == 1) { if ($invoices->count() == 1) {
$invoice = $invoices->first();
$file = $invoices->first()->pdf_file_path(); $invitation = $invoice->invitations->first();
$file = $invoice->pdf_file_path($invitation);
return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);; return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);;
} }

View File

@ -102,8 +102,6 @@ class CreateEntityPdf implements ShouldQueue
/* Set the locale*/ /* Set the locale*/
App::setLocale($this->contact->preferredLocale()); App::setLocale($this->contact->preferredLocale());
// nlog($this->entity->client->getMergedSettings());
/* Set customized translations _NOW_ */ /* Set customized translations _NOW_ */
$t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings())); $t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
@ -116,16 +114,16 @@ class CreateEntityPdf implements ShouldQueue
$entity_design_id = ''; $entity_design_id = '';
if ($this->entity instanceof Invoice) { if ($this->entity instanceof Invoice) {
$path = $this->entity->client->invoice_filepath(); $path = $this->entity->client->invoice_filepath($this->invitation);
$entity_design_id = 'invoice_design_id'; $entity_design_id = 'invoice_design_id';
} elseif ($this->entity instanceof Quote) { } elseif ($this->entity instanceof Quote) {
$path = $this->entity->client->quote_filepath(); $path = $this->entity->client->quote_filepath($this->invitation);
$entity_design_id = 'quote_design_id'; $entity_design_id = 'quote_design_id';
} elseif ($this->entity instanceof Credit) { } elseif ($this->entity instanceof Credit) {
$path = $this->entity->client->credit_filepath(); $path = $this->entity->client->credit_filepath($this->invitation);
$entity_design_id = 'credit_design_id'; $entity_design_id = 'credit_design_id';
} elseif ($this->entity instanceof RecurringInvoice) { } elseif ($this->entity instanceof RecurringInvoice) {
$path = $this->entity->client->recurring_invoice_filepath(); $path = $this->entity->client->recurring_invoice_filepath($this->invitation);
$entity_design_id = 'invoice_design_id'; $entity_design_id = 'invoice_design_id';
} }

View File

@ -78,13 +78,16 @@ class ZipInvoices implements ShouldQueue
// create a new zipstream object // create a new zipstream object
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip'; $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
$path = $this->invoices->first()->client->invoice_filepath(); $invoice = $this->invoices->first();
$invitation = $invoice->invitations->first();
$path = $invoice->client->invoice_filepath($invitation);
$zip = new ZipStream($file_name, $options); $zip = new ZipStream($file_name, $options);
foreach ($this->invoices as $invoice) { foreach ($this->invoices as $invoice) {
//$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path())); //$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
$zip->addFileFromPath(basename($invoice->pdf_file_path()), $invoice->pdf_file_path()); $zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path());
} }
$zip->finish(); $zip->finish();

View File

@ -101,9 +101,9 @@ class CreditEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->credit->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { if ($this->client->getSetting('pdf_email_attachment') !== false && $this->credit->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
if(Ninja::isHosted()) if(Ninja::isHosted())
$this->setAttachments([$this->credit->pdf_file_path(null, 'url', true)]); $this->setAttachments([$this->credit->pdf_file_path($this->invitation, 'url', true)]);
else else
$this->setAttachments([$this->credit->pdf_file_path()]); $this->setAttachments([$this->credit->pdf_file_path($this->invitation)]);
} }

View File

@ -112,9 +112,9 @@ class InvoiceEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
if(Ninja::isHosted()) if(Ninja::isHosted())
$this->setAttachments([$this->invoice->pdf_file_path(null, 'url', true)]); $this->setAttachments([$this->invoice->pdf_file_path($this->invitation, 'url', true)]);
else else
$this->setAttachments([$this->invoice->pdf_file_path()]); $this->setAttachments([$this->invoice->pdf_file_path($this->invitation)]);
// $this->setAttachments(['path' => $this->invoice->pdf_file_path(), 'name' => basename($this->invoice->pdf_file_path())]); // $this->setAttachments(['path' => $this->invoice->pdf_file_path(), 'name' => basename($this->invoice->pdf_file_path())]);

View File

@ -77,7 +77,7 @@ class PaymentEmailEngine extends BaseEmailEngine
$this->payment->invoices->each(function ($invoice){ $this->payment->invoices->each(function ($invoice){
$this->setAttachments([$invoice->pdf_file_path()]); $this->setAttachments([$invoice->pdf_file_path($invoice->invitations->first())]);
}); });

View File

@ -103,9 +103,9 @@ class QuoteEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->quote->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { if ($this->client->getSetting('pdf_email_attachment') !== false && $this->quote->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
if(Ninja::isHosted()) if(Ninja::isHosted())
$this->setAttachments([$this->quote->pdf_file_path(null, 'url', true)]); $this->setAttachments([$this->quote->pdf_file_path($this->invitation, 'url', true)]);
else else
$this->setAttachments([$this->quote->pdf_file_path()]); $this->setAttachments([$this->quote->pdf_file_path($this->invitation)]);
} }

View File

@ -41,9 +41,6 @@ class MigrationCompleted extends Mailable
$result = $this->from(config('mail.from.address'), config('mail.from.name')) $result = $this->from(config('mail.from.address'), config('mail.from.name'))
->view('email.import.completed', $data); ->view('email.import.completed', $data);
// if($this->company->invoices->count() >=1)
// $result->attach($this->company->invoices->first()->pdf_file_path());
return $result; return $result;
} }
} }

View File

@ -638,24 +638,28 @@ class Client extends BaseModel implements HasLocalePreference
})->first()->locale; })->first()->locale;
} }
public function invoice_filepath() public function invoice_filepath($invitation)
{ {
return $this->company->company_key.'/'.$this->client_hash.'/invoices/'; $contact_key = $invitation->contact->contact_key;
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/invoices/';
} }
public function quote_filepath() public function quote_filepath($invitation)
{ {
return $this->company->company_key.'/'.$this->client_hash.'/quotes/'; $contact_key = $invitation->contact->contact_key;
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/quotes/';
} }
public function credit_filepath() public function credit_filepath($invitation)
{ {
return $this->company->company_key.'/'.$this->client_hash.'/credits/'; $contact_key = $invitation->contact->contact_key;
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/credits/';
} }
public function recurring_invoice_filepath() public function recurring_invoice_filepath($invitation)
{ {
return $this->company->company_key.'/'.$this->client_hash.'/recurring_invoices/'; $contact_key = $invitation->contact->contact_key;
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/recurring_invoices/';
} }
public function company_filepath() public function company_filepath()

View File

@ -267,7 +267,7 @@ class Credit extends BaseModel
if(!$invitation) if(!$invitation)
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
$file_path = $this->client->credit_filepath().$this->numberFormatter().'.pdf'; $file_path = $this->client->credit_filepath($invitation).$this->numberFormatter().'.pdf';
if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){ if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){
return Storage::disk(config('filesystems.default'))->{$type}($file_path); return Storage::disk(config('filesystems.default'))->{$type}($file_path);

View File

@ -126,9 +126,9 @@ class CreditInvitation extends BaseModel
public function pdf_file_path() public function pdf_file_path()
{ {
$storage_path = Storage::url($this->credit->client->quote_filepath().$this->credit->numberFormatter().'.pdf'); $storage_path = Storage::url($this->credit->client->quote_filepath($this).$this->credit->numberFormatter().'.pdf');
if (! Storage::exists($this->credit->client->credit_filepath().$this->credit->numberFormatter().'.pdf')) { if (! Storage::exists($this->credit->client->credit_filepath($this).$this->credit->numberFormatter().'.pdf')) {
event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
CreateEntityPdf::dispatchNow($this); CreateEntityPdf::dispatchNow($this);
} }

View File

@ -409,7 +409,7 @@ class Invoice extends BaseModel
if(!$invitation) if(!$invitation)
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
$file_path = $this->client->invoice_filepath().$this->numberFormatter().'.pdf'; $file_path = $this->client->invoice_filepath($invitation).$this->numberFormatter().'.pdf';
if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){ if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){
return Storage::disk(config('filesystems.default'))->{$type}($file_path); return Storage::disk(config('filesystems.default'))->{$type}($file_path);

View File

@ -142,7 +142,7 @@ class InvoiceInvitation extends BaseModel
{ {
$storage_path = Storage::url($this->invoice->client->invoice_filepath().$this->invoice->numberFormatter().'.pdf'); $storage_path = Storage::url($this->invoice->client->invoice_filepath().$this->invoice->numberFormatter().'.pdf');
if (! Storage::exists($this->invoice->client->invoice_filepath().$this->invoice->numberFormatter().'.pdf')) { if (! Storage::exists($this->invoice->client->invoice_filepath($this).$this->invoice->numberFormatter().'.pdf')) {
event(new InvoiceWasUpdated($this->invoice, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new InvoiceWasUpdated($this->invoice, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
CreateEntityPdf::dispatchNow($this); CreateEntityPdf::dispatchNow($this);
} }

View File

@ -219,7 +219,7 @@ class Quote extends BaseModel
if(!$invitation) if(!$invitation)
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
$file_path = $this->client->quote_filepath().$this->numberFormatter().'.pdf'; $file_path = $this->client->quote_filepath($invitation).$this->numberFormatter().'.pdf';
if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){ if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){
return Storage::disk(config('filesystems.default'))->{$type}($file_path); return Storage::disk(config('filesystems.default'))->{$type}($file_path);

View File

@ -130,9 +130,9 @@ class QuoteInvitation extends BaseModel
public function pdf_file_path() public function pdf_file_path()
{ {
$storage_path = Storage::url($this->quote->client->quote_filepath().$this->quote->numberFormatter().'.pdf'); $storage_path = Storage::url($this->quote->client->quote_filepath($this).$this->quote->numberFormatter().'.pdf');
if (! Storage::exists($this->quote->client->quote_filepath().$this->quote->numberFormatter().'.pdf')) { if (! Storage::exists($this->quote->client->quote_filepath($this).$this->quote->numberFormatter().'.pdf')) {
event(new QuoteWasUpdated($this->quote, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new QuoteWasUpdated($this->quote, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
CreateEntityPdf::dispatchNow($this); CreateEntityPdf::dispatchNow($this);
} }

View File

@ -52,11 +52,6 @@ class InvoiceObserver
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company);
} }
// if($invoice->isDirty('date') || $invoice->isDirty('due_date'))
// $invoice->service()->setReminder()->save();
// UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->numberFormatter().'.pdf');
} }
/** /**

View File

@ -140,7 +140,11 @@ class CreditService
public function deletePdf() public function deletePdf()
{ {
UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath() . $this->credit->numberFormatter().'.pdf'); $this->credit->invitations->each(function ($invitation){
UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath($invitation) . $this->credit->numberFormatter().'.pdf');
});
return $this; return $this;
} }

View File

@ -37,7 +37,7 @@ class GetCreditPdf extends AbstractService
$this->contact = $this->credit->client->primary_contact()->first(); $this->contact = $this->credit->client->primary_contact()->first();
} }
$path = $this->credit->client->credit_filepath(); $path = $this->credit->client->credit_filepath($this->invitation);
$file_path = $path.$this->credit->numberFormatter().'.pdf'; $file_path = $path.$this->credit->numberFormatter().'.pdf';

View File

@ -60,14 +60,15 @@ class GenerateDeliveryNote
? $this->invoice->design_id ? $this->invoice->design_id
: $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id')); : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
$file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath(), $this->invoice->number); $invitation = $this->invoice->invitations->first();
$file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath($invitation), $this->invoice->number);
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
return (new Phantom)->generate($this->invoice->invitations->first()); return (new Phantom)->generate($this->invoice->invitations->first());
} }
$design = Design::find($design_id); $design = Design::find($design_id);
$html = new HtmlEngine($this->invoice->invitations->first()); $html = new HtmlEngine($invitation);
if ($design->is_custom) { if ($design->is_custom) {
$options = ['custom_partials' => json_decode(json_encode($design->design), true)]; $options = ['custom_partials' => json_decode(json_encode($design->design), true)];

View File

@ -35,7 +35,7 @@ class GetInvoicePdf extends AbstractService
$invitation = $this->invoice->invitations->where('client_contact_id', $this->contact->id)->first(); $invitation = $this->invoice->invitations->where('client_contact_id', $this->contact->id)->first();
$path = $this->invoice->client->invoice_filepath(); $path = $this->invoice->client->invoice_filepath($invitation);
$file_path = $path.$this->invoice->numberFormatter().'.pdf'; $file_path = $path.$this->invoice->numberFormatter().'.pdf';

View File

@ -307,13 +307,16 @@ class InvoiceService
public function deletePdf() public function deletePdf()
{ {
//UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf'); $this->invoice->invitations->each(function ($invitation){
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath($invitation) . $this->invoice->numberFormatter().'.pdf');
if(Ninja::isHosted()) { if(Ninja::isHosted()) {
Storage::disk('public')->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf'); Storage::disk('public')->delete($this->invoice->client->invoice_filepath($invitation) . $this->invoice->numberFormatter().'.pdf');
} }
});
return $this; return $this;
} }
@ -351,8 +354,17 @@ class InvoiceService
* PDF when it is updated etc. * PDF when it is updated etc.
* @return InvoiceService * @return InvoiceService
*/ */
public function touchPdf() public function touchPdf($force = false)
{ {
if($force){
$this->invoice->invitations->each(function ($invitation) {
CreateEntityPdf::dispatchNow($invitation);
});
return $this;
}
$this->invoice->invitations->each(function ($invitation) { $this->invoice->invitations->each(function ($invitation) {
CreateEntityPdf::dispatch($invitation); CreateEntityPdf::dispatch($invitation);
}); });

View File

@ -35,7 +35,7 @@ class GetQuotePdf extends AbstractService
$invitation = $this->quote->invitations->where('client_contact_id', $this->contact->id)->first(); $invitation = $this->quote->invitations->where('client_contact_id', $this->contact->id)->first();
$path = $this->quote->client->quote_filepath(); $path = $this->quote->client->quote_filepath($invitation);
$file_path = $path.$this->quote->numberFormatter().'.pdf'; $file_path = $path.$this->quote->numberFormatter().'.pdf';

View File

@ -178,7 +178,11 @@ class QuoteService
public function deletePdf() public function deletePdf()
{ {
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath() . $this->quote->numberFormatter().'.pdf'); $this->quote->invitations->each(function ($invitation){
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath($invitation) . $this->quote->numberFormatter().'.pdf');
});
return $this; return $this;
} }

View File

@ -37,7 +37,7 @@ class GetInvoicePdf extends AbstractService
$invitation = $this->entity->invitations->where('client_contact_id', $this->contact->id)->first(); $invitation = $this->entity->invitations->where('client_contact_id', $this->contact->id)->first();
$path = $this->entity->client->recurring_invoice_filepath(); $path = $this->entity->client->recurring_invoice_filepath($invitation);
$file_path = $path.$this->entity->hashed_id.'.pdf'; $file_path = $path.$this->entity->hashed_id.'.pdf';

View File

@ -87,7 +87,13 @@ class RecurringService
public function deletePdf() public function deletePdf()
{ {
UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath() . $this->recurring_entity->numberFormatter().'.pdf');
$this->recurring_entity->invitations->each(function ($invitation){
UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath($invitation) . $this->recurring_entity->numberFormatter().'.pdf');
});
return $this; return $this;
} }

View File

@ -62,19 +62,19 @@ class Phantom
$entity_obj = $invitation->{$entity}; $entity_obj = $invitation->{$entity};
if ($entity == 'invoice') { if ($entity == 'invoice') {
$path = $entity_obj->client->invoice_filepath(); $path = $entity_obj->client->invoice_filepath($invitation);
} }
if ($entity == 'quote') { if ($entity == 'quote') {
$path = $entity_obj->client->quote_filepath(); $path = $entity_obj->client->quote_filepath($invitation);
} }
if ($entity == 'credit') { if ($entity == 'credit') {
$path = $entity_obj->client->credit_filepath(); $path = $entity_obj->client->credit_filepath($invitation);
} }
if ($entity == 'recurring_invoice') { if ($entity == 'recurring_invoice') {
$path = $entity_obj->client->recurring_invoice_filepath(); $path = $entity_obj->client->recurring_invoice_filepath($invitation);
} }
$file_path = $path.$entity_obj->numberFormatter().'.pdf'; $file_path = $path.$entity_obj->numberFormatter().'.pdf';