diff --git a/app/Models/Company.php b/app/Models/Company.php index f7ebd4e4ea..49531af286 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -410,6 +410,7 @@ class Company extends BaseModel self::ENTITY_RECURRING_INVOICE => 1, self::ENTITY_CREDIT => 2, self::ENTITY_QUOTE => 4, + // @phpstan-ignore-next-line self::ENTITY_TASK => 8, self::ENTITY_EXPENSE => 16, self::ENTITY_PROJECT => 32, diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 84cb253604..d7a50c34ec 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -1,3 +1,4 @@ + email->email_object->email_template_body); + { + if ($this->email->email_object->body) { // A Custom Message has been set in the email screen. return $this; @@ -209,7 +215,7 @@ class EmailDefaults if ($this->template != 'custom') { $this->email->email_object->body = $this->parseMarkdownToHtml($this->email->email_object->body); } - nlog($this->email->email_object->subject); + return $this; } @@ -261,14 +267,59 @@ class EmailDefaults { $documents = []; - if ($this->email->email_object->settings->document_email_attachment && $this->email->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) { + if (!$this->email->email_object->settings->document_email_attachment || $this->email->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) + return $this; - $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->company->documents->pluck('id')->toArray()); + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->company->documents->pluck('id')->toArray()); + + if ($this->email->email_object->entity?->documents) { + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->email_object->entity->documents->pluck('id')->toArray()); } - + if ($this->email->email_object->entity instanceof Invoice && $this->email->email_object->entity->recurring_id != null){ + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $this->email->email_object->entity->recurring_invoice->documents->pluck('id')->toArray()); + } + + if ($this->email->email_object->entity instanceof Invoice) { + + $expense_ids = []; + $task_ids = []; + + foreach ($this->email->email_object->entity->line_items as $item) + { + + if (property_exists($item, 'expense_id')) { + $expense_ids[] = $item->expense_id; + } + + if (property_exists($item, 'task_id')) { + $task_ids[] = $item->task_id; + } + + } + + if (count($expense_ids) > 0) { + + Expense::whereIn('id', $this->transformKeys($expense_ids)) + ->where('invoice_documents', 1) + ->cursor() + ->each(function ($expense) { + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $expense->documents->pluck('id')->toArray()); + }); + } + + if (count($task_ids) > 0 && $this->email->company->invoice_task_documents) { + Task::whereIn('id', $this->transformKeys($task_ids)) + ->cursor() + ->each(function ($task) { + $this->email->email_object->documents = array_merge($this->email->email_object->documents, $task->documents->pluck('id')->toArray()); + }); + } + + } return $this; + } /** diff --git a/app/Services/Email/EmailObject.php b/app/Services/Email/EmailObject.php index c056cc5f8a..6f3c10ece3 100644 --- a/app/Services/Email/EmailObject.php +++ b/app/Services/Email/EmailObject.php @@ -107,7 +107,7 @@ class EmailObject public ?string $invitation_key = null; - // @phpstan-ignore-next-line + // @phpstan-ignore-next-line public array $documents = []; public ?string $template = null; //invoice //quote //reminder1