2caac4ae8e
Loads subcontractJobs.subcontractor relation and renders a new table in the PDF after parts: description, subcontractor, category, client price. Adds ru/en translations for the new section labels. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
63 lines
2.2 KiB
PHP
63 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Tenant\WorkOrder;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
|
class WorkOrderPdfService
|
|
{
|
|
public function generate(WorkOrder $wo): \Barryvdh\DomPDF\PDF
|
|
{
|
|
$wo->load(['client', 'vehicle', 'master', 'works.master', 'parts', 'payments', 'subcontractJobs.subcontractor']);
|
|
$company = $wo->company ?: \App\Models\Central\Company::find($wo->company_id);
|
|
|
|
$themeColor = $company?->settings['theme_color'] ?? '#3B82F6';
|
|
$currency = $company?->settings['currency'] ?? 'MDL';
|
|
$worksTotal = (float) $wo->works->sum('total');
|
|
$partsTotal = (float) $wo->parts->sum('total');
|
|
$paid = (float) $wo->payments->sum('amount');
|
|
$balance = max(0, (float) $wo->total - $paid);
|
|
|
|
// Embed logo as data URI (DomPDF can't fetch external URLs reliably).
|
|
$logoData = null;
|
|
if ($company && ($logoMedia = $company->getFirstMedia('logo'))) {
|
|
try {
|
|
$path = $logoMedia->getPath();
|
|
if (file_exists($path)) {
|
|
$mime = mime_content_type($path) ?: 'image/png';
|
|
$logoData = 'data:' . $mime . ';base64,' . base64_encode(file_get_contents($path));
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$logoData = null;
|
|
}
|
|
}
|
|
|
|
$html = view('pdf.work-order', [
|
|
'wo' => $wo,
|
|
'company' => $company,
|
|
'themeColor' => $themeColor,
|
|
'currency' => $currency,
|
|
'worksTotal' => $worksTotal,
|
|
'partsTotal' => $partsTotal,
|
|
'paid' => $paid,
|
|
'balance' => $balance,
|
|
'logoData' => $logoData,
|
|
])->render();
|
|
|
|
// DejaVu Sans lacks U+0219 (ș) and U+021B (ț) — normalize to cedilla
|
|
// variants (U+015F ş, U+0163 ţ) which are visually identical and supported.
|
|
$html = strtr($html, [
|
|
"\u{0219}" => "\u{015F}", "\u{0218}" => "\u{015E}",
|
|
"\u{021B}" => "\u{0163}", "\u{021A}" => "\u{0162}",
|
|
]);
|
|
|
|
return Pdf::loadHTML($html)->setPaper('A4');
|
|
}
|
|
|
|
public function filename(WorkOrder $wo): string
|
|
{
|
|
return $wo->number . '-' . optional($wo->opened_at)->format('Ymd') . '.pdf';
|
|
}
|
|
}
|