db); $key = $this->resolveEntityString(); $resource = $this->entity::query(); $template = Design::withTrashed()->find($this->decodePrimaryKey($this->template)); $template_service = new \App\Services\Template\TemplateService($template); match($this->entity) { Invoice::class => $resource->with('payments', 'client'), Quote::class => $resource->with('client'), Task::class => $resource->with('client'), Credit::class => $resource->with('client'), RecurringInvoice::class => $resource->with('client'), Project::class => $resource->with('client'), Expense::class => $resource->with('client'), Payment::class => $resource->with('invoices', 'client'), Client::class => $resource, }; $result = $resource->withTrashed() ->whereIn('id', $this->transformKeys($this->ids)) ->where('company_id', $this->company->id) ->get(); /** Set a global currency_code */ $first_entity = $result->first(); /** Lets be clever and sniff out Statements */ if($first_entity instanceof Client && stripos(json_encode($template->design), '##statement##') !== false) { $options = [ 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'all', 'show_credits_table' => false, 'template' => $this->template, ]; $pdfs = []; foreach($result as $client) { $pdfs[] = $client->service()->statement($options); } if(count($pdfs) == 1) { $pdf = $pdfs[0]; } else { $pdf = (new PdfMerge($pdfs))->run(); } if($this->send_email) { $this->sendEmail($pdf, $template); } else { $filename = "templates/{$this->hash}.pdf"; Storage::disk(config('filesystems.default'))->put($filename, $pdf); return $pdf; } } if($first_entity instanceof Client) $currency_code = $first_entity->currency()->code; elseif($first_entity->client) $currency_code = $first_entity->client->currency()->code; else $currency_code = $this->company->currency()->code; if($result->count() <= 1) { $data[$key] = collect($result); } else { $data[$key] = $result; } $ts = $template_service ->setCompany($this->company) ->addGlobal(['currency_code' => $currency_code]) ->build($data); if($this->send_email) { $pdf = $ts->getPdf(); $this->sendEmail($pdf, $template); } else { $pdf = $ts->getPdf(); $filename = "templates/{$this->hash}.pdf"; Storage::disk(config('filesystems.default'))->put($filename, $pdf); return $pdf; } } private function sendEmail(mixed $pdf, Design $template) { $user = $this->user_id ? User::find($this->user_id) : $this->company->owner(); $template_name = " [{$template->name}]"; $email_object = new EmailObject(); $email_object->to = [new Address($user->email, $user->present()->name())]; $email_object->attachments = [['file' => base64_encode($pdf), 'name' => ctrans('texts.template') . ".pdf"]]; $email_object->company_key = $this->company->company_key; $email_object->company = $this->company; $email_object->settings = $this->company->settings; $email_object->logo = $this->company->present()->logo(); $email_object->whitelabel = $this->company->account->isPaid() ? true : false; $email_object->user_id = $user->id; $email_object->text_body = ctrans('texts.download_report_description') . $template_name; $email_object->body = ctrans('texts.download_report_description') . $template_name; $email_object->subject = ctrans('texts.download_report_description') . $template_name; (new AdminEmail($email_object, $this->company))->handle(); } /** * Context * * If I have an array of invoices, what could I possib * * */ private function resolveEntityString() { return match ($this->entity) { Invoice::class => 'invoices', Quote::class => 'quotes', Task::class => 'tasks', Credit::class => 'credits', RecurringInvoice::class => 'recurring_invoices', Project::class => 'projects', Expense::class => 'expenses', Payment::class => 'payments', Product::class => 'products', PurchaseOrder::class => 'purchase_orders', Project::class => 'projects', Client::class => 'clients', Vendor::class => 'vendors', }; } public function middleware() { return [new WithoutOverlapping("template-{$this->company->company_key}")]; } }