feat: Enhance invoice creation and editing with improved part selection and error handling
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled

This commit is contained in:
2025-08-16 15:27:29 +00:00
parent e3b2b220d2
commit b11cdc39c2
6 changed files with 257 additions and 192 deletions

View File

@ -52,6 +52,10 @@ class Create extends Component
public $service_items = [];
public $filteredParts = [];
public $partSearchTerms = [];
public function mount()
{
$this->invoice_date = now()->format('Y-m-d');
@ -63,6 +67,9 @@ class Create extends Component
$this->parts = Part::where('status', 'active')->orderBy('name')->get();
$this->service_items = ServiceItem::where('status', 'active')->orderBy('service_name')->get();
// Initialize filtered parts and search terms for each line item
$this->filteredParts = $this->parts->toArray();
// Initialize with one empty line item
$this->addLineItem();
}
@ -181,10 +188,12 @@ class Create extends Component
}
// Create invoice
$branch = Branch::find($this->branch_id);
$invoice = Invoice::create([
'customer_id' => $this->customer_id,
'branch_id' => $this->branch_id,
'created_by' => Auth::id(),
'invoice_number' => Invoice::generateInvoiceNumber($branch->code ?? 'MAIN'),
'invoice_date' => $this->invoice_date,
'due_date' => $this->due_date,
'description' => $this->description,

View File

@ -7,6 +7,7 @@ use App\Models\Estimate;
use App\Models\Invoice;
use App\Models\Part;
use App\Models\ServiceItem;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Validate;
use Livewire\Component;
@ -152,40 +153,59 @@ class CreateFromEstimate extends Component
{
$this->validate();
$invoice = Invoice::create([
'job_card_id' => $this->estimate->job_card_id,
'estimate_id' => $this->estimate->id,
'customer_id' => $this->estimate->customer_id,
'branch_id' => $this->branch_id,
'created_by' => auth()->id(),
'invoice_number' => Invoice::generateInvoiceNumber($this->branch_id),
'subject' => $this->subject,
'description' => $this->description,
'invoice_date' => $this->invoice_date,
'due_date' => $this->due_date,
'subtotal' => $this->subtotal,
'tax_rate' => $this->tax_rate,
'tax_amount' => $this->taxAmount,
'total_amount' => $this->total,
'status' => 'draft',
]);
// Create line items
foreach ($this->lineItems as $item) {
$invoice->invoiceLineItems()->create([
'type' => $item['type'],
'service_item_id' => $item['service_item_id'] ?: null,
'part_id' => $item['part_id'] ?: null,
'description' => $item['description'],
'quantity' => $item['quantity'],
'unit_price' => $item['unit_price'],
'total' => $item['total'],
DB::transaction(function () {
$branch = Branch::find($this->branch_id);
$invoice = Invoice::create([
'job_card_id' => $this->estimate->job_card_id,
'estimate_id' => $this->estimate->id,
'customer_id' => $this->estimate->customer_id,
'branch_id' => $this->branch_id,
'created_by' => auth()->id(),
'invoice_number' => Invoice::generateInvoiceNumber($branch->code ?? 'MAIN'),
'subject' => $this->subject,
'description' => $this->description,
'invoice_date' => $this->invoice_date,
'due_date' => $this->due_date,
'subtotal' => $this->estimate->subtotal_amount,
'tax_rate' => 10.0, // Default tax rate
'tax_amount' => $this->estimate->tax_amount,
'total_amount' => $this->estimate->total_amount,
'status' => 'draft',
]);
}
// Copy line items from estimate to invoice
foreach ($this->estimate->lineItems as $estimateItem) {
// Map estimate line item types to invoice line item types
$invoiceType = match ($estimateItem->type) {
'parts' => 'part',
'labour' => 'labour',
'miscellaneous' => 'other',
default => $estimateItem->type
};
$lineItemData = [
'type' => $invoiceType,
'description' => $estimateItem->description,
'quantity' => $estimateItem->quantity,
'unit_price' => $estimateItem->unit_price,
'total_amount' => $estimateItem->total_amount,
];
// Add part reference if it's a parts item
if ($estimateItem->type === 'parts' && $estimateItem->part_id) {
$lineItemData['part_id'] = $estimateItem->part_id;
$lineItemData['part_number'] = $estimateItem->part->part_number ?? null;
}
$invoice->invoiceLineItems()->create($lineItemData);
}
$this->invoice = $invoice;
});
session()->flash('success', 'Invoice created successfully from estimate.');
return redirect()->route('invoices.show', $invoice);
return redirect()->route('invoices.show', $this->invoice);
}
public function render()

View File

@ -82,7 +82,7 @@
Create Work Order
</flux:menu.item>
@can('create', \App\Models\Invoice::class)
<flux:menu.item icon="document-text" wire:click="convertToInvoice">
<flux:menu.item icon="document-text" href="{{ route('invoices.create-from-estimate', $estimate) }}">
Convert to Invoice
</flux:menu.item>
@endcan

View File

@ -1,171 +1,207 @@
<div class="max-w-7xl mx-auto">
<div class="mb-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-2xl font-semibold text-gray-900">Create Invoice from Estimate</h1>
<p class="mt-1 text-sm text-gray-600">Converting estimate #{{ $estimate->estimate_number }} to invoice</p>
</div>
<div class="flex space-x-3">
<flux:button variant="outline" href="{{ route('estimates.show', $estimate) }}">
Back to Estimate
</flux:button>
</div>
</div>
</div>
<div>
<x-slot name="title">Create Invoice from Estimate</x-slot>
<!-- Estimate Reference -->
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
<div class="flex items-center">
<flux:icon name="document-text" class="h-5 w-5 text-blue-400 mr-2" />
<div>
<h3 class="text-sm font-medium text-blue-800">Source Estimate</h3>
<p class="text-sm text-blue-600">{{ $estimate->subject }} - {{ $estimate->estimate_number }}</p>
</div>
</div>
</div>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-zinc-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-zinc-900 dark:text-zinc-100">
<div class="flex justify-between items-center mb-6">
<div>
<h2 class="text-2xl font-semibold">Create Invoice from Estimate #{{ $estimate->estimate_number }}</h2>
<p class="text-sm text-zinc-600 dark:text-zinc-400 mt-1">Converting estimate to invoice</p>
</div>
<a href="{{ route('invoices.index') }}" class="text-zinc-600 hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-zinc-100">
Back to Invoices
</a>
</div>
<form wire:submit="save" class="space-y-6">
@if ($errors->any())
<div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form wire:submit="save" class="space-y-6">
<!-- Basic Information -->
<div class="bg-white shadow rounded-lg p-6">
<h3 class="text-lg font-medium text-gray-900 mb-4">Invoice Details</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<flux:field>
<flux:label>Subject</flux:label>
<flux:input wire:model="subject" placeholder="Invoice subject" />
<flux:error name="subject" />
</flux:field>
</div>
<!-- Customer Information (Read-only) -->
<div class="bg-zinc-50 dark:bg-zinc-700 p-4 rounded-lg">
<h3 class="text-lg font-medium mb-4">Customer Information</h3>
<div class="space-y-2">
<p><strong>Name:</strong> {{ $estimate->customer->first_name }} {{ $estimate->customer->last_name }}</p>
<p><strong>Email:</strong> {{ $estimate->customer->email }}</p>
<p><strong>Phone:</strong> {{ $estimate->customer->phone }}</p>
</div>
</div>
<div>
<flux:field>
<flux:label>Branch</flux:label>
<flux:select wire:model="branch_id">
<flux:option value="">Select branch</flux:option>
@foreach($branches as $branch)
<flux:option value="{{ $branch->id }}">{{ $branch->name }}</flux:option>
@endforeach
</flux:select>
<flux:error name="branch_id" />
</flux:field>
</div>
<!-- Estimate Information (Read-only) -->
<div class="bg-zinc-50 dark:bg-zinc-700 p-4 rounded-lg">
<h3 class="text-lg font-medium mb-4">Original Estimate</h3>
<div class="space-y-2">
<p><strong>Estimate #:</strong> {{ $estimate->estimate_number }}</p>
<p><strong>Date:</strong> {{ $estimate->estimate_date->format('Y-m-d') }}</p>
<p><strong>Total:</strong> ${{ number_format($estimate->total_amount, 2) }}</p>
<p><strong>Status:</strong> {{ ucfirst($estimate->status) }}</p>
</div>
</div>
</div>
<div>
<flux:field>
<flux:label>Invoice Date</flux:label>
<flux:input type="date" wire:model="invoice_date" />
<flux:error name="invoice_date" />
</flux:field>
</div>
<!-- Invoice Details -->
<div class="bg-white dark:bg-zinc-800 shadow rounded-lg p-6">
<h3 class="text-lg font-medium text-zinc-900 dark:text-zinc-100 mb-4">Invoice Details</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label for="subject" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
Invoice Subject *
</label>
<input type="text"
id="subject"
wire:model="subject"
placeholder="Invoice subject"
class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
@error('subject')
<span class="text-red-500 text-sm">{{ $message }}</span>
@enderror
</div>
<div>
<flux:field>
<flux:label>Due Date</flux:label>
<flux:input type="date" wire:model="due_date" />
<flux:error name="due_date" />
</flux:field>
</div>
<div>
<label for="branch_id" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
Branch *
</label>
<select id="branch_id"
wire:model="branch_id"
class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
<option value="">Select branch</option>
@foreach($branches as $branch)
<option value="{{ $branch->id }}">{{ $branch->name }} ({{ $branch->code }})</option>
@endforeach
</select>
@error('branch_id')
<span class="text-red-500 text-sm">{{ $message }}</span>
@enderror
</div>
<div class="md:col-span-2">
<flux:field>
<flux:label>Description</flux:label>
<flux:textarea wire:model="description" rows="3" placeholder="Invoice description..." />
<flux:error name="description" />
</flux:field>
</div>
<div>
<label for="invoice_date" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
Invoice Date *
</label>
<input type="date"
id="invoice_date"
wire:model="invoice_date"
class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
@error('invoice_date')
<span class="text-red-500 text-sm">{{ $message }}</span>
@enderror
</div>
<div>
<label for="due_date" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
Due Date *
</label>
<input type="date"
id="due_date"
wire:model="due_date"
class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100">
@error('due_date')
<span class="text-red-500 text-sm">{{ $message }}</span>
@enderror
</div>
<div class="md:col-span-2">
<label for="description" class="block text-sm font-medium text-zinc-700 dark:text-zinc-300 mb-2">
Description
</label>
<textarea id="description"
wire:model="description"
rows="3"
placeholder="Additional notes or description for this invoice..."
class="w-full px-3 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm focus:outline-none focus:ring-orange-500 focus:border-orange-500 dark:bg-zinc-700 dark:text-zinc-100"></textarea>
@error('description')
<span class="text-red-500 text-sm">{{ $message }}</span>
@enderror
</div>
</div>
</div>
<!-- Line Items Preview -->
<div class="bg-white dark:bg-zinc-800 shadow rounded-lg p-6">
<h3 class="text-lg font-medium text-zinc-900 dark:text-zinc-100 mb-4">Line Items to be Converted</h3>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-zinc-200 dark:divide-zinc-600">
<thead class="bg-zinc-50 dark:bg-zinc-700">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Type</th>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Item</th>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Description</th>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Qty</th>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Unit Price</th>
<th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-300 uppercase tracking-wider">Total</th>
</tr>
</thead>
<tbody class="bg-white dark:bg-zinc-800 divide-y divide-zinc-200 dark:divide-zinc-600">
@foreach($estimate->lineItems as $item)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
{{ ucfirst($item->type) }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
@if($item->type === 'parts' && $item->part)
{{ $item->part->name }} ({{ $item->part->part_number }})
@else
{{ $item->description }}
@endif
</td>
<td class="px-6 py-4 text-sm text-zinc-900 dark:text-zinc-100">
{{ $item->description }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
{{ $item->quantity }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-900 dark:text-zinc-100">
${{ number_format($item->unit_price, 2) }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-zinc-900 dark:text-zinc-100">
${{ number_format($item->total_amount, 2) }}
</td>
</tr>
@endforeach
</tbody>
<tfoot class="bg-zinc-50 dark:bg-zinc-700">
<tr>
<td colspan="5" class="px-6 py-4 text-right text-sm font-medium text-zinc-900 dark:text-zinc-100">
Total Amount:
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-zinc-900 dark:text-zinc-100">
${{ number_format($estimate->total_amount, 2) }}
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="flex justify-end space-x-4">
<a href="{{ route('invoices.index') }}"
class="px-4 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md shadow-sm text-sm font-medium text-zinc-700 dark:text-zinc-300 bg-white dark:bg-zinc-800 hover:bg-zinc-50 dark:hover:bg-zinc-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500">
Cancel
</a>
<button type="submit"
wire:loading.attr="disabled"
class="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-orange-600 hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500 disabled:opacity-50">
<span wire:loading.remove>Create Invoice</span>
<span wire:loading>Creating...</span>
</button>
</div>
</form>
</div>
</div>
<!-- Line Items -->
<div class="bg-white shadow rounded-lg p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-medium text-gray-900">Line Items</h3>
<flux:button type="button" wire:click="addLineItem" variant="outline">
Add Item
</flux:button>
</div>
<div class="space-y-4">
@foreach($lineItems as $index => $item)
<div class="border border-gray-200 rounded-lg p-4">
<div class="flex items-center justify-between mb-4">
<h4 class="text-sm font-medium text-gray-900">Item {{ $index + 1 }}</h4>
@if(count($lineItems) > 1)
<flux:button type="button" wire:click="removeLineItem({{ $index }})" variant="danger" size="sm">
Remove
</flux:button>
@endif
</div>
<div class="grid grid-cols-1 md:grid-cols-6 gap-4">
<div>
<flux:field>
<flux:label>Type</flux:label>
<flux:select wire:model="lineItems.{{ $index }}.type">
<flux:option value="service">Service</flux:option>
<flux:option value="part">Part</flux:option>
<flux:option value="other">Other</flux:option>
</flux:select>
</flux:field>
</div>
@if($item['type'] === 'service')
<div>
<flux:field>
<flux:label>Service Item</flux:label>
<flux:select wire:model="lineItems.{{ $index }}.service_item_id">
<flux:option value="">Select service</flux:option>
@foreach($serviceItems as $serviceItem)
<flux:option value="{{ $serviceItem->id }}">{{ $serviceItem->name }} - ${{ number_format($serviceItem->price, 2) }}</flux:option>
@endforeach
</flux:select>
</flux:field>
</div>
@elseif($item['type'] === 'part')
<div>
<flux:field>
<flux:label>Part</flux:label>
<select wire:model="lineItems.{{ $index }}.part_id" class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500">
<option value="">Select part</option>
@foreach($parts as $part)
<option value="{{ $part->id }}">{{ $part->part_number }} - {{ $part->name }} - ${{ number_format($part->sell_price, 2) }}</option>
@endforeach
</select>
</flux:field>
</div>
@else
<div></div>
@endif
<div>
<flux:field>
<flux:label>Description</flux:label>
<flux:input wire:model="lineItems.{{ $index }}.description" placeholder="Description" />
</flux:field>
</div>
<div>
<flux:field>
<flux:label>Quantity</flux:label>
<flux:input type="number" wire:model="lineItems.{{ $index }}.quantity" min="1" step="1" />
</flux:field>
</div>
<div>
<flux:field>
<flux:label>Unit Price</flux:label>
<flux:input type="number" wire:model="lineItems.{{ $index }}.unit_price" step="0.01" min="0" />
</flux:field>
</div>
<div>
<flux:field>
<flux:label>Total</flux:label>
<flux:input value="${{ number_format($item['total'], 2) }}" readonly />
</flux:field>
</div>
</div>
</div>
</div>
</div>
@endforeach
</div>

View File

@ -78,7 +78,7 @@
@if($item['type'] === 'parts')
<div class="md:col-span-3">
<flux:label>Part</flux:label>
<select wire:model="line_items.{{ $index }}.part_id" class="w-full rounded-md border-zinc-300 dark:border-zinc-600 bg-white dark:bg-zinc-800 text-zinc-900 dark:text-zinc-100 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<select wire:model.live="line_items.{{ $index }}.part_id" class="w-full rounded-md border-zinc-300 dark:border-zinc-600 bg-white dark:bg-zinc-800 text-zinc-900 dark:text-zinc-100 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="">Select part...</option>
@if(count($parts) === 0)
<option value="" disabled>No parts available</option>

View File

@ -84,7 +84,7 @@
@if($item['type'] === 'parts')
<div class="md:col-span-3">
<flux:label>Part</flux:label>
<select wire:model="line_items.{{ $index }}.part_id" class="w-full rounded-md border-zinc-300 dark:border-zinc-600 bg-white dark:bg-zinc-800 text-zinc-900 dark:text-zinc-100 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<select wire:model.live="line_items.{{ $index }}.part_id" class="w-full rounded-md border-zinc-300 dark:border-zinc-600 bg-white dark:bg-zinc-800 text-zinc-900 dark:text-zinc-100 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="">Select part...</option>
@if(count($parts) === 0)
<option value="" disabled>No parts available</option>