Skip to content

Commit

Permalink
Generate Quote PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
tqt97 committed Nov 10, 2023
1 parent c67c35c commit 6a53532
Show file tree
Hide file tree
Showing 15 changed files with 1,111 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@
- Creating the Quote Model
- Creating Quote Resource
- Create Quotes From Customer Table
17. **[Generate Quote PDF](https://github.com/tqt97/laravel-crm-filament)**
- Creating a Simple View Page for Quote
- Installing PDF Package
- Generating PDF
- Displaying PDF in View Page
21 changes: 20 additions & 1 deletion app/Filament/Resources/QuoteResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use App\Filament\Resources\QuoteResource\Pages;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\QuoteResource\RelationManagers;
use Filament\Infolists\Components\ViewEntry;
use Filament\Infolists\Infolist;

class QuoteResource extends Resource
{
Expand Down Expand Up @@ -156,7 +158,10 @@ public static function table(Table $table): Table
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
])
->recordUrl(function ($record) {
return Pages\ViewQuote::getUrl([$record]);
});
}

public static function getRelations(): array
Expand All @@ -171,6 +176,7 @@ public static function getPages(): array
return [
'index' => Pages\ListQuotes::route('/'),
'create' => Pages\CreateQuote::route('/create'),
'view' => Pages\ViewQuote::route('/{record}'),
'edit' => Pages\EditQuote::route('/{record}/edit'),
];
}
Expand All @@ -195,4 +201,17 @@ public static function updateTotals(Get $get, $livewire): void
data_set($livewire, $statePath . '.subtotal', number_format($subtotal, 2, '.', ''));
data_set($livewire, $statePath . '.total', number_format($subtotal + ($subtotal * (data_get($livewire, $statePath . '.taxes') / 100)), 2, '.', ''));
}

public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
ViewEntry::make('invoice')
->columnSpanFull()
->viewData([
'record' => $infolist->record
])
->view('infolists.components.quote-invoice-view')
]);
}
}
26 changes: 26 additions & 0 deletions app/Filament/Resources/QuoteResource/Pages/ViewQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Filament\Resources\QuoteResource\Pages;

use App\Filament\Resources\QuoteResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
use Filament\Actions\Action;
use URL;

class ViewQuote extends ViewRecord
{
protected static string $resource = QuoteResource::class;

protected function getHeaderActions(): array
{
return [
Action::make('Edit Quote')
->icon('heroicon-m-pencil-square')
->url(EditQuote::getUrl([$this->record])),
Action::make('Download Quote')
->icon('heroicon-s-document-check')
->url(URL::signedRoute('quotes.pdf', [$this->record->id]), true),
];
}
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/QuotePdfController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Http\Controllers;


use App\Models\Quote;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
use LaravelDaily\Invoices\Invoice;

class QuotePdfController extends Controller
{
public function __invoke(Request $request, Quote $quote)
{
$quote->load(['quoteProducts.product', 'customer']);

$customer = new Buyer([
'name' => $quote->customer->first_name . ' ' . $quote->customer->last_name,
'custom_fields' => [
'email' => $quote->customer->email,
],
]);

$items = [];

foreach ($quote->quoteProducts as $product) {
$items[] = (new InvoiceItem())
->title($product->product->name)
->pricePerUnit($product->price)
->subTotalPrice($product->price * $product->quantity)
->quantity($product->quantity);
}

$invoice = Invoice::make()
->sequence($quote->id)
->buyer($customer)
->taxRate($quote->taxes)
->totalAmount($quote->total)
->addItems($items);

if ($request->has('preview')) {
return $invoice->stream();
}

return $invoice->download();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
"laraveldaily/laravel-invoices": "^3.1",
"saade/filament-fullcalendar": "^3.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 6a53532

Please sign in to comment.