diff --git a/app/Filament/Resources/InvoiceResource.php b/app/Filament/Resources/InvoiceResource.php index 4004b29..91e949d 100644 --- a/app/Filament/Resources/InvoiceResource.php +++ b/app/Filament/Resources/InvoiceResource.php @@ -3,10 +3,10 @@ namespace App\Filament\Resources; use App\Filament\Resources\InvoiceResource\Pages; +use App\Filament\Resources\InvoiceResource\RelationManagers\OrdersRelationManager; use App\Filament\Resources\InvoiceResource\RelationManagers\ProductServicesRelationManager; use App\Models\Customer; use App\Models\Invoice; -use App\Models\Order; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Grid; use Filament\Forms\Components\Section; @@ -43,15 +43,6 @@ class InvoiceResource extends Resource ->searchable() ->columnSpan(2), - Select::make('orders') - ->options(fn ($get): array => Order::where('customer_id', $get('customer_id') ?? null)->whereNull('invoice_id') - ->get() - ->pluck('customer_po', 'id') - ->toArray()) - ->multiple() - ->searchable() - ->columnSpan(2), - Split::make([ DatePicker::make('date') ->required() @@ -114,9 +105,6 @@ class InvoiceResource extends Resource ->defaultSort('created_at', 'desc') ->actions([ Tables\Actions\EditAction::make(), - // ->after(function (Invoice $record) { - // return redirect(EditInvoice::getUrl(['record' => $record->id])); - // }), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ @@ -128,6 +116,7 @@ class InvoiceResource extends Resource public static function getRelations(): array { return [ + OrdersRelationManager::class, ProductServicesRelationManager::class, ]; } diff --git a/app/Filament/Resources/InvoiceResource/Pages/EditInvoice.php b/app/Filament/Resources/InvoiceResource/Pages/EditInvoice.php index 8c379e6..3ed30f3 100644 --- a/app/Filament/Resources/InvoiceResource/Pages/EditInvoice.php +++ b/app/Filament/Resources/InvoiceResource/Pages/EditInvoice.php @@ -3,50 +3,17 @@ namespace App\Filament\Resources\InvoiceResource\Pages; use App\Filament\Resources\InvoiceResource; -use App\Models\Invoice; -use App\Models\Order; use Filament\Actions; use Filament\Resources\Pages\EditRecord; -use Illuminate\Database\Eloquent\Model; class EditInvoice extends EditRecord { protected static string $resource = InvoiceResource::class; - protected function mutateFormDataBeforeFill(array $data): array - { - $invoice = Invoice::findOrFail($data['id']); - - foreach ($invoice->orders as $order) { - $data['orders'][] = $order->customer_po; - } - - return $data; - } - - protected function handleRecordUpdate(Model $record, array $data): Model - { - foreach ($record->orders as $order) { - $order->invoice()->disassociate(); - $order->save(); - } - - $record->orders()->saveMany(Order::findMany($data['orders'])); - - $record->calculateTotals(); - - return $record; - } - protected function getHeaderActions(): array { return [ Actions\DeleteAction::make(), ]; } - - protected function getRedirectUrl(): string - { - return $this->getResource()::getUrl('edit', ['record' => $this->record->id]); - } } diff --git a/app/Filament/Resources/InvoiceResource/RelationManagers/OrdersRelationManager.php b/app/Filament/Resources/InvoiceResource/RelationManagers/OrdersRelationManager.php index 92614c4..e9946d4 100644 --- a/app/Filament/Resources/InvoiceResource/RelationManagers/OrdersRelationManager.php +++ b/app/Filament/Resources/InvoiceResource/RelationManagers/OrdersRelationManager.php @@ -7,6 +7,7 @@ use Filament\Forms\Form; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; class OrdersRelationManager extends RelationManager { @@ -18,7 +19,7 @@ class OrdersRelationManager extends RelationManager ->schema([ Forms\Components\TextInput::make('customer_po') ->required() - ->maxLength(255), + ->maxLength(100), ]); } @@ -27,22 +28,36 @@ class OrdersRelationManager extends RelationManager return $table ->recordTitleAttribute('customer_po') ->columns([ - Tables\Columns\TextColumn::make('customer_po'), + Tables\Columns\TextColumn::make('customer_po') + ->color('code') + ->weight('bold'), + Tables\Columns\TextColumn::make('total_product_quantity'), + Tables\Columns\TextColumn::make('total_service_price') + ->prefix('$'), ]) ->filters([ // ]) ->headerActions([ Tables\Actions\AssociateAction::make() - ->multiple(), + ->multiple() + ->preloadRecordSelect() + ->recordSelectOptionsQuery(fn (Builder $query) => $query->where('customer_id', $this->ownerRecord->customer->id)) + ->after(function () { + $this->ownerRecord->calculateTotals(); + }), ]) ->actions([ - Tables\Actions\DeleteAction::make(), + Tables\Actions\DissociateAction::make() + ->after(function () { + $this->ownerRecord->calculateTotals(); + }), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ - Tables\Actions\DeleteBulkAction::make(), + Tables\Actions\DissociateBulkAction::make(), ]), - ]); + ]) + ->inverseRelationship('invoice'); } } diff --git a/app/Models/Order.php b/app/Models/Order.php index e5bff02..25c17a7 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -45,6 +45,7 @@ class Order extends Model protected $appends = [ 'total_service_price', + 'total_product_quantity', ]; protected $casts = [ @@ -79,7 +80,7 @@ class Order extends Model return 'TN'.$year.'-'.$po; } - public function totalProductQuantity(): int + public function getTotalProductQuantityAttribute(): int { $total = 0; diff --git a/database/migrations/2024_09_08_163053_create_invoices_table.php b/database/migrations/2024_09_08_163053_create_invoices_table.php index 99c5463..0a1da22 100644 --- a/database/migrations/2024_09_08_163053_create_invoices_table.php +++ b/database/migrations/2024_09_08_163053_create_invoices_table.php @@ -18,6 +18,7 @@ return new class extends Migration $table->boolean('gst')->default(0); $table->boolean('pst')->default(0); + $table->boolean('paid')->default(0); $table->date('date')->default(today()); $table->date('due_date')->nullable(); diff --git a/resources/views/orders/show.blade.php b/resources/views/orders/show.blade.php index 8ff4989..006367c 100644 --- a/resources/views/orders/show.blade.php +++ b/resources/views/orders/show.blade.php @@ -313,7 +313,7 @@ + value="{{$order->getTotalProductQuantity()}}"> diff --git a/resources/views/pdf/order.blade.php b/resources/views/pdf/order.blade.php index 5a8db5d..76ba189 100644 --- a/resources/views/pdf/order.blade.php +++ b/resources/views/pdf/order.blade.php @@ -172,7 +172,7 @@