Create invoices in XML Format.

This commit is contained in:
2025-01-07 12:52:25 +01:00
parent 8e0696d438
commit c20a3bcdcf
9 changed files with 217 additions and 14 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Controllers;
use App\Http\Option;
use App\Models\Invoice;
use Symfony\Component\HttpFoundation\StreamedResponse;
class EController extends Controller
{
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;
}
return response()->streamDownload(function () use ($invoice, $taxes) {
echo view('xml.invoice', ['invoice' => $invoice, 'options' => Option::optionsAsObject(), 'taxes' => $taxes]);
}, 'test.xml', ['Content-Type' => 'application/xml']);
}
}

View File

@@ -2,8 +2,8 @@
namespace App\Http\Controllers;
use App\Http\Option;
use App\Models\Invoice;
use App\Models\Option;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Response;
@@ -12,13 +12,7 @@ class PdfController extends Controller
public function downloadInvoice(int $invoice_id): Response
{
$invoice = Invoice::find($invoice_id);
$all_options = Option::all(['name', 'value']);
$options = new \stdClass();
foreach ($all_options as $option) {
$key = $option->name;
$options->$key = $option->value;
}
return Pdf::loadView('pdfs.invoice', ['invoice' => $invoice->load(['address', 'delivery']), 'options' => $options])->stream();
return Pdf::loadView('pdfs.invoice', ['invoice' => $invoice->load(['address', 'delivery']), 'options' => Option::optionsAsObject()])->stream();
}
}

17
app/Http/Option.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http;
class Option
{
public static function optionsAsObject()
{
$all_options = \App\Models\Option::all(['name', 'value']);
$options = new \stdClass();
foreach ($all_options as $option) {
$key = $option->name;
$options->$key = $option->value;
}
return $options;
}
}