Send invoices by email.

This commit is contained in:
2025-01-09 11:18:12 +01:00
parent 8da6da471d
commit 6235112f74
19 changed files with 468 additions and 23 deletions

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Mail\InvoiceMail;
use App\Models\Invoice;
use App\TenantMail;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;
class MailController extends Controller
{
/**
* The Mailer used for sending Tenant Mails defined by options.
* @var Mailer
*/
protected Mailer $mailer;
/**
* Set the TenantMail::class as mailer for further usage
*/
public function __construct()
{
$this->mailer = TenantMail::get();
}
/**
* Send the invoice mail for the given request.
*/
public function sendInvoice(Request $request): JsonResponse
{
$invoice = Invoice::find($request->id);
$invoiceMail = new InvoiceMail($invoice);
$invoiceMail->subject($request->Subject);
$invoiceMail->body = $request->Body;
try {
$this->mailer->to($request->To)
->cc($request->Cc)
->Bcc($request->Bcc)
->send($invoiceMail);
} catch (\Exception $exception) {
return response()->json(['status' => 'error', 'message' => $exception->getMessage()], 500);
}
$invoice->update(['status' => 'sent']);
return response()->json($invoice);
}
}

View File

@@ -8,22 +8,45 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class EController extends Controller
{
/**
* Return an invoice as download for the specified resource.
*/
public function downloadInvoice(int $id): StreamedResponse
{
$invoice = Invoice::find($id);
$taxes = [];
foreach ($invoice->items as $item) {
if (!isset($taxes[$item->tax])) {
$taxes[$item->tax] = ['tax' => 0, 'taxable' => 0];
}
$taxes[$item->tax]['tax'] += round($item->price * $item->amount * $item->tax / 100, 2, PHP_ROUND_HALF_UP);
$taxes[$item->tax]['taxable'] += $item->price * $item->amount;
}
$taxes = self::buildTaxes($invoice->items);
return response()->streamDownload(function () use ($invoice, $taxes) {
echo view('xml.invoice', ['invoice' => $invoice, 'options' => Option::optionsAsObject(), 'taxes' => $taxes]);
}, 'test.xml', ['Content-Type' => 'application/xml']);
}
/**
* Return a rendered xml invoice as string to build an email attachment from it for the specified resource.
*/
public function attachInvoice(int $id): string
{
$invoice = Invoice::find($id);
$taxes = self::buildTaxes($invoice->items);
return view('xml.invoice', ['invoice' => $invoice, 'options' => Option::optionsAsObject(), 'taxes' => $taxes])->render();
}
/**
* Build taxes for the given invoice items grouped by tax.
*/
private static function buildTaxes($items): array
{
$taxes = [];
foreach ($items as $item) {
if (!isset($taxes[$item->tax])) {
$taxes[$item->tax] = ['tax' => 0, 'taxable' => 0];
}
$taxes[$item->tax]['tax'] += round($item->price * $item->amount * $item->tax / 100, 2, PHP_ROUND_HALF_UP);
$taxes[$item->tax]['taxable'] += $item->price * $item->amount;
}
return $taxes;
}
}

View File

@@ -3,14 +3,14 @@
namespace App\Http\Controllers;
use App\Models\Invoice;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
class InvoiceController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
public function index(): View
{
return view('invoice.index');
}
@@ -18,7 +18,7 @@ class InvoiceController extends Controller
/**
* Show the form for creating a new resource.
*/
public function create()
public function create(): View
{
return view('invoice.create');
}
@@ -26,7 +26,7 @@ class InvoiceController extends Controller
/**
* Display the specified resource.
*/
public function show(Invoice $invoice)
public function show(Invoice $invoice): View
{
return view('invoice.show', ['invoice' => $invoice]);
}
@@ -38,4 +38,14 @@ class InvoiceController extends Controller
{
//
}
/**
* Show the form for sending the specified invoice.
*/
public function mail(int $id): View
{
$invoice = Invoice::find($id);
return view('invoice.mail', ['invoice' => $invoice]);
}
}

View File

@@ -9,10 +9,29 @@ use Illuminate\Http\Response;
class PdfController extends Controller
{
/**
* Return an invoice as download for the specified resource.
*/
public function downloadInvoice(int $invoice_id): Response
{
return $this->buildInvoicePdf($invoice_id)->stream();
}
/**
* Return a rendered pdf invoice as string to build an email attachment from it for the specified resource.
*/
public function attachInvoice(int $invoice_id): string
{
return $this->buildInvoicePdf($invoice_id)->output();
}
/**
* Render the pdf view for the given invoice.
*/
protected function buildInvoicePdf(int $invoice_id): \Barryvdh\DomPDF\PDF
{
$invoice = Invoice::find($invoice_id);
return Pdf::loadView('pdfs.invoice', ['invoice' => $invoice->load(['address', 'delivery']), 'options' => Option::optionsAsObject()])->stream();
return Pdf::loadView('pdfs.invoice', ['invoice' => $invoice->load(['address', 'delivery']), 'options' => Option::optionsAsObject()]);
}
}

63
app/Mail/InvoiceMail.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
namespace App\Mail;
use App\Http\Controllers\EController;
use App\Http\Controllers\PdfController;
use App\Models\Invoice;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
class InvoiceMail extends Mailable
{
use Queueable, SerializesModels;
public string $body {
set {
$this->body = $value;
}
}
public string $pdf;
public string $xml;
/**
* Create a new message instance.
*/
public function __construct(protected Invoice $invoice)
{
$pdf = new PdfController();
$this->pdf = $pdf->attachInvoice($invoice->id);
$xml = new EController();
$this->xml = $xml->attachInvoice($invoice->id);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
html: 'mail.invoice.sent',
with: ['html' => $this->body],
);
}
/**
* Get the attachments for the message.
*/
public function attachments(): array
{
return [
Attachment::fromData(fn() => $this->pdf, __('invoice.Invoice') . '_' . $this->invoice->number . '.pdf')
->withMime('application/pdf'),
Attachment::fromData(fn() => $this->xml, __('invoice.Invoice') . '_' . $this->invoice->number . '.xml')
->withMime('application/xml'),
];
}
}

View File

@@ -32,9 +32,20 @@ class Invoice extends Model
*/
protected $appends = [
'created',
'number'
'number',
'localized_state'
];
/**
* Get the invoice state as translated string.
*
* @return string
*/
public function getLocalizedStateAttribute(): string
{
return __('invoice.state_' . $this->status);
}
/**
* Get the invoice number formatted
*/

32
app/TenantMail.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App;
use App\Http\Option;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\Mail;
class TenantMail
{
/**
* Build a tenant-mailer from options
*
* @return Mailer
*/
public static function get(): Mailer
{
$options = Option::optionsAsObject();
$mailer = Mail::build([
'transport' => $options->mail_transport,
'host' => $options->mail_host,
'port' => $options->mail_port,
'encryption' => (property_exists($options, 'mail_encryption')) ? $options->mail_encryption : null,
'username' => (property_exists($options, 'mail_encryption')) ? $options->mail_username : null,
'password' => (property_exists($options, 'mail_encryption')) ? $options->mail_password : null,
]);
$mailer->alwaysFrom($options->email, $options->representative);
return $mailer;
}
}