96 lines
3.3 KiB
PHP
96 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
use App\Models\ShoppingOrder;
|
|
use App\Models\UserInvoice;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Mail\Mailable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Storage;
|
|
|
|
class MailInvoice extends Mailable
|
|
{
|
|
use Queueable, SerializesModels;
|
|
|
|
protected $shopping_order;
|
|
|
|
protected $user_invoice;
|
|
|
|
public $subject;
|
|
|
|
public function __construct(ShoppingOrder $shopping_order, UserInvoice $user_invoice)
|
|
{
|
|
$this->shopping_order = $shopping_order;
|
|
$this->user_invoice = $user_invoice;
|
|
|
|
// Unterscheide zwischen Stornorechnung und normaler Rechnung
|
|
$isCancellation = $user_invoice->cancellation && ! $user_invoice->cancellation_id;
|
|
|
|
if ($isCancellation) {
|
|
$this->subject = __('email.cancellation_invoice_subject').': '.$shopping_order->getLastShoppingPayment('reference');
|
|
} else {
|
|
$this->subject = __('email.invoice_subject').': '.$shopping_order->getLastShoppingPayment('reference');
|
|
}
|
|
}
|
|
|
|
public function build()
|
|
{
|
|
// Unterscheide zwischen Stornorechnung und normaler Rechnung
|
|
$isCancellation = $this->user_invoice->cancellation && ! $this->user_invoice->cancellation_id;
|
|
|
|
if ($isCancellation) {
|
|
$title = __('email.cancellation_invoice_title');
|
|
$copy1line = __('email.cancellation_invoice_copy1line').$this->shopping_order->getLastShoppingPayment('reference');
|
|
} else {
|
|
$title = __('email.invoice_title');
|
|
$copy1line = __('email.invoice_copy1line').$this->shopping_order->getLastShoppingPayment('reference');
|
|
}
|
|
|
|
$disk = $this->user_invoice->disk;
|
|
|
|
// 1. Deutsches Original immer anhängen
|
|
$pathDe = $this->user_invoice->getDownloadPath();
|
|
$filenameDe = $this->user_invoice->filename;
|
|
|
|
if (! Storage::disk($disk)->exists($pathDe)) {
|
|
return;
|
|
}
|
|
|
|
$fileDe = Storage::disk($disk)->path($pathDe);
|
|
$fileDe = str_replace('//', '/', $fileDe);
|
|
$mimeDe = Storage::disk($disk)->mimeType($pathDe);
|
|
|
|
$mail = $this->view('emails.blank')->with([
|
|
'title' => $title,
|
|
'copy1line' => $copy1line,
|
|
])->attach($fileDe, [
|
|
'as' => $filenameDe,
|
|
'mime' => $mimeDe,
|
|
]);
|
|
|
|
// 2. Lokalisierte Version zusätzlich anhängen (wenn vorhanden und != DE)
|
|
$locale = $this->shopping_order->shopping_user
|
|
? $this->shopping_order->shopping_user->getLocale()
|
|
: 'de';
|
|
|
|
if ($locale && $locale !== 'de') {
|
|
$pathLocale = $this->user_invoice->getDownloadPathLocale($locale);
|
|
$filenameLocale = $this->user_invoice->getFilenameLocale($locale);
|
|
|
|
// Nur anhängen wenn lokalisierte Version existiert und nicht gleich dem Original ist
|
|
if (Storage::disk($disk)->exists($pathLocale) && $pathLocale !== $pathDe) {
|
|
$fileLocale = Storage::disk($disk)->path($pathLocale);
|
|
$fileLocale = str_replace('//', '/', $fileLocale);
|
|
$mimeLocale = Storage::disk($disk)->mimeType($pathLocale);
|
|
|
|
$mail->attach($fileLocale, [
|
|
'as' => $filenameLocale,
|
|
'mime' => $mimeLocale,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $mail;
|
|
}
|
|
}
|