More work on invoices

orders
Nisse Lommerde 3 weeks ago
parent 256cc1f7ed
commit 562e499d12

@ -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,
];
}

@ -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]);
}
}

@ -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');
}
}

@ -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;

@ -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();

@ -313,7 +313,7 @@
<label for="total-quantity" class="text-nowrap col-form-label">Total Quantity:</label>
<input type="number" name="total-quantity" id="" class="form-control-plaintext" readonly
value="{{$order->totalProductQuantity()}}">
value="{{$order->getTotalProductQuantity()}}">
</div>
<!-- Product Services -->

@ -172,7 +172,7 @@
</div>
<div class="row mt-3 text-end" style="font-size: 11px">
<div class="col">
TOTAL QUANTITY: {{$order->totalProductQuantity()}}
TOTAL QUANTITY: {{$order->totalProductQuantity}}
</div>
</div>

Loading…
Cancel
Save