model = $model; // Benutzersprache aus Account ermitteln (mit Fallback auf de) $this->locale = $model->account && $model->account->language ? $model->account->language : 'de'; } public function _set($name, $value) { $this->{$name} = $value; } private function convert($str) { $search = ['Ő', 'ő', 'Ű', 'ű']; $replace = ['Ö', 'ö', 'Ü', 'ü']; $str = str_replace($search, $replace, $str); return iconv('UTF-8', 'windows-1252//IGNORE', $str); } public function createContractPDF() { if (! Storage::disk($this->disk)->exists($this->dir)) { Storage::disk($this->disk)->makeDirectory($this->dir); // creates directory } // Lösche alle alten Vertrags-Einträge für diesen User File::where('user_id', $this->model->id) ->where('identifier', $this->identifier) ->delete(); // 1. IMMER deutsches Original erstellen $this->createContractPDFForLocale('de'); // 2. Wenn Benutzersprache != DE, lokalisierte Version erstellen if ($this->locale && $this->locale !== 'de') { $this->createContractPDFForLocale($this->locale); } return true; } /** * Erstellt einen Beratervertrag in der angegebenen Sprache */ private function createContractPDFForLocale(string $locale) { $pdf = new ContractPDF; $pdf->setLocale($locale); $pdf->AddPage('P', [210, 297]); $pdf->SetFont('Helvetica', '', 11); $pdf->SetDrawColor(160, 160, 160); $x1 = 16.5; $x2 = 109; $y = 70; $nl = 17.5; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->m_account)); $pdf->SetXY($x2, $y); $contractDate = $this->contract_date ?? now(); $pdf->Write(0, $contractDate->format('d.m.Y')); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->company)); $pdf->SetXY($x2, $y); $pre = $this->model->account->pre_phone_id != '' ? $this->convert($this->model->account->pre_phone->phone).' ' : ''; $pdf->Write(0, $pre.$this->convert($this->model->account->phone)); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->m_first_name)); $pdf->SetXY($x2, $y); $pre = $this->model->account->pre_mobil_id != '' ? $this->convert($this->model->account->pre_mobil->phone).' ' : ''; $pdf->Write(0, $pre.$this->convert($this->model->account->mobil)); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->m_last_name)); $pdf->SetXY($x2, $y); $pdf->Write(0, $this->convert($this->model->email)); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->address)); $pdf->SetXY($x2, $y); $pdf->Write(0, $this->convert($this->model->account->birthday)); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->account->zipcode).' '.$this->convert($this->model->account->city)); $y += $nl; $pdf->SetXY($x1, $y); $pre = $this->model->account->country_id ? $this->convert($this->model->account->country->de).' ' : ''; $pdf->Write(0, $pre); if ($this->model->m_sponsor && $this->model->user_sponsor->account) { $y += 48; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->user_sponsor->account->company)); $pdf->SetXY($x2, $y); $pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_account)); $y += $nl; $pdf->SetXY($x1, $y); $pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_first_name).' '.$this->convert($this->model->user_sponsor->account->m_last_name)); $website = $this->model->user_sponsor->shop()->count() ? $this->model->user_sponsor->shop->getSubdomain(false) : 'www.mivita.care'; } else { $website = 'www.mivita.care'; } $pdf->AddPage('P', [210, 297]); $pdf->SetFont('Helvetica', '', 10); $pdf->SetDrawColor(160, 160, 160); $pdf->SetXY($x1, 65); $pdf->Write(0, $website.' (ID: '.$this->convert($this->model->account->m_account).')'); $pdf->SetXY($x1, 80); $pdf->Write(0, $this->convert($this->model->account->m_first_name)); $pdf->SetXY($x2, 80); $pdf->Write(0, $this->convert($this->model->account->m_last_name)); $pdf->AddPage('P', [210, 297]); // Dateiname mit übersetztem Vertragsnamen $contractName = __('pdf.contract_filename', [], $locale); $filename = "MIVITA_{$contractName}.pdf"; Storage::disk($this->disk)->put($this->dir.$filename, $pdf->Output('S')); $size = Storage::disk($this->disk)->size($this->dir.$filename); $mine = Storage::disk($this->disk)->mimeType($this->dir.$filename); // Erstelle Datenbank-Eintrag für diesen Vertrag File::create([ 'user_id' => $this->model->id, 'identifier' => $this->identifier, 'filename' => $filename, 'dir' => $this->dir, 'original_name' => $filename, 'ext' => 'pdf', 'mine' => $mine, 'size' => $size, ]); } }