- Created a new Blade view for managing brand contracts with forms for adding and editing contracts, including client and category selection. - Implemented success message notifications for contract actions. - Added a new Blade view for invoice settings, allowing customization of invoice number format, header branding, and default values for new invoices. - Introduced a new PDF view for generating rate cards with customizable styles based on user profile settings. - Developed a profile management page for rate cards, enabling users to update their identity, appearance, and visibility settings.
63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Invoice;
|
|
use App\Models\InvoiceItem;
|
|
use App\Models\InvoiceSettings;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Http\Response;
|
|
|
|
final class InvoiceService
|
|
{
|
|
public function create(array $data, array $items): Invoice
|
|
{
|
|
$invoice = Invoice::create([
|
|
...$data,
|
|
'invoice_number' => Invoice::generateNumber(),
|
|
'subtotal' => 0,
|
|
'tax_amount' => 0,
|
|
'total' => 0,
|
|
]);
|
|
|
|
foreach ($items as $item) {
|
|
$item['total'] = $item['quantity'] * $item['unit_price'];
|
|
$item['is_taxable'] = isset($item['is_taxable']) && $item['is_taxable'] ? true : false;
|
|
$invoice->items()->create($item);
|
|
}
|
|
|
|
$invoice->recalculate();
|
|
|
|
return $invoice;
|
|
}
|
|
|
|
public function update(Invoice $invoice, array $data, array $items): Invoice
|
|
{
|
|
$invoice->update($data);
|
|
$invoice->items()->delete();
|
|
|
|
foreach ($items as $item) {
|
|
$item['total'] = $item['quantity'] * $item['unit_price'];
|
|
$item['is_taxable'] = isset($item['is_taxable']) && $item['is_taxable'] ? true : false;
|
|
$invoice->items()->create($item);
|
|
}
|
|
|
|
$invoice->recalculate();
|
|
|
|
return $invoice->fresh();
|
|
}
|
|
|
|
public function generatePdf(Invoice $invoice): Response
|
|
{
|
|
$invoice->load('client', 'items');
|
|
$invoiceSettings = InvoiceSettings::instance();
|
|
|
|
$pdf = Pdf::loadView('invoices.pdf', compact('invoice', 'invoiceSettings'));
|
|
$pdf->setPaper('A4', 'portrait');
|
|
|
|
return $pdf->download("invoice-{$invoice->invoice_number}.pdf");
|
|
}
|
|
}
|