Installation:
composer require codersfree/report
Ensure your project uses PHP 8.0+ and Laravel 8+ (or compatible).
First Use Case: Generate a basic HTML invoice representation for SUNAT-compliant electronic billing in Peru.
use CodersFree\Report\Report;
$report = new Report();
$report->setType('01') // Factura Electrónica (01)
->setSerie('F001')
->setNumber('001-2023')
->addItem('Product A', 10, 100.00)
->addItem('Product B', 5, 50.00)
->setTotal(1000.00)
->setClient('Client Name', '12345678901', 'client@example.com');
$html = $report->render();
Where to Look First:
src/Report.php for core methods and available modifiers.config/report.php (if auto-generated) for default settings like logos, colors, or tax configurations.Dynamic Report Generation:
// Generate reports from a Laravel controller
public function generateInvoice(Request $request) {
$report = new Report();
$report->setData($request->all()); // Hydrate from API/form
return response($report->render(), 200, ['Content-Type' => 'text/html']);
}
Reusable Report Templates:
Report class to create domain-specific templates (e.g., InvoiceReport, CreditNoteReport).render() to customize HTML structure while reusing core logic:
class InvoiceReport extends Report {
protected function render(): string {
$html = parent::render();
return $this->addFooter($html); // Custom footer logic
}
}
Integration with SUNAT API:
sunat.pe).$sunatResponse = $this->sunatService->sendInvoice($invoiceData);
if ($sunatResponse->isValid()) {
$report = new Report($sunatResponse->getData());
return $report->render();
}
PDF Conversion:
barryvdh/laravel-dompdf):
use Barryvdh\DomPDF\Facade\Pdf;
$pdf = Pdf::load($report->render());
return $pdf->download('invoice.pdf');
Localization:
$report->setLanguage('es'); // Spanish for Peru
$report->setLabels([
'total' => 'Total a Pagar',
'tax' => 'IGV (18%)'
]);
SUNAT Compliance:
$report->setTaxRate(18.0); // Explicitly set for clarity.
Data Sanitization:
e() helper or HTML entities:
$report->setClient(htmlspecialchars($request->client_name));
Configuration Overrides:
config/report.php. Manually create it to override defaults:
return [
'logo_path' => public_path('images/logo.png'),
'default_tax_rate' => 18.0,
'qr_size' => 100,
];
Performance:
$report->setItems(array_chunk($items, 100)); // Process in batches
Validate HTML Output:
<div class="sunat-required">• Número de Comprobante</div>
Logging:
$report->debug(true);
// Logs to storage/logs/report.log
Testing:
$report = new Report();
$report->setSunatData(['cdr' => 'mock_cdr']); // Simulate SUNAT response
Custom Fields:
setOperationType(), setGuiaRemision()) by extending the class:
class PeruInvoiceReport extends Report {
public function setOperationType(string $type): self {
$this->data['operation_type'] = $type;
return $this;
}
}
Hooks:
beforeRender() to modify data before HTML generation:
protected function beforeRender(): void {
$this->data['footer_text'] = 'Pago contra entrega';
}
Theming:
getStyles() method:
protected function getStyles(): string {
return file_get_contents(public_path('css/custom-report.css'));
}
Multi-Currency:
$report->setCurrency('PEN'); // Default
$report->setCurrency('USD', 3.8); // USD with exchange rate
```markdown
### Laravel-Specific Tips
1. **Service Provider Binding**:
Bind the report class for dependency injection:
```php
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->bind(Report::class, function ($app) {
return new Report(config('report.defaults'));
});
}
Artisan Commands: Create a command to generate reports from CLI:
// app/Console/Commands/GenerateReport.php
public function handle() {
$report = app(Report::class);
$report->setData($this->getInvoiceData());
file_put_contents('invoice.html', $report->render());
}
Storage Integration: Save reports to Laravel’s storage:
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->put('reports/invoice-' . $id . '.html', $report->render());
Queue Jobs: Offload report generation to queues for long-running tasks:
// app/Jobs/GenerateReportJob.php
public function handle() {
$report = new Report($this->data);
$this->saveReport($report->render());
}
API Responses: Return reports as API responses with proper headers:
return response($report->render())
->header('Content-Type', 'text/html')
->header('X-Report-Type', 'sunat-invoice');
How can I help you explore Laravel packages today?