Send invoices by email.
This commit is contained in:
52
app/Http/Controllers/Api/MailController.php
Normal file
52
app/Http/Controllers/Api/MailController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user