invoice = $invoice; $this->school_year = $this->defaultSchoolYear(); } private function defaultSchoolYear(): int { $now = now(); return $now->month <= 6 ? $now->year : $now->year + 1; } #[On('invoice-status-changed')] public function refreshInvoice(): void { $this->invoice->refresh(); } #[Computed] public function lines() { return $this->invoice->lines()->with('product')->orderBy('id')->get(); } #[Computed] public function products() { return Product::where('active', true)->orderBy('name')->get(); } public function updatedProductId($value): void { if (!$value) { return; } $product = Product::find($value); if ($product) { $this->sku = $product->sku; $this->name = $product->name; $this->description = $product->description; $this->unit_price = $product->price; } } public function addLine(): void { if ($this->invoice->isLocked()) { return; } $this->validate(); $this->invoice->lines()->create([ 'product_id' => $this->product_id, 'sku' => $this->sku, 'name' => $this->name, 'description' => $this->description, 'school_year' => $this->school_year, 'quantity' => $this->quantity, 'unit_price' => $this->unit_price, ]); $this->invoice->refresh(); $this->resetForm(); $this->dispatch('lines-updated'); } public function editLine(int $lineId): void { if ($this->invoice->isLocked()) { return; } $line = $this->invoice->lines()->find($lineId); if (!$line) return; $this->editingLineId = $lineId; $this->product_id = $line->product_id; $this->sku = $line->sku; $this->name = $line->name; $this->description = $line->description; $this->school_year = $line->school_year; $this->quantity = $line->quantity; $this->unit_price = $line->unit_price; } public function updateLine(): void { if ($this->invoice->isLocked() || !$this->editingLineId) { return; } $this->validate(); $line = $this->invoice->lines()->find($this->editingLineId); if (!$line) return; $line->update([ 'product_id' => $this->product_id, 'sku' => $this->sku, 'name' => $this->name, 'description' => $this->description, 'school_year' => $this->school_year, 'quantity' => $this->quantity, 'unit_price' => $this->unit_price, ]); $this->invoice->refresh(); $this->cancelEdit(); $this->dispatch('lines-updated'); } public function cancelEdit(): void { $this->editingLineId = null; $this->resetForm(); } public function deleteLine(int $lineId): void { if ($this->invoice->isLocked()) { return; } $line = $this->invoice->lines()->find($lineId); $line?->delete(); $this->invoice->refresh(); $this->dispatch('lines-updated'); } private function resetForm(): void { $this->product_id = null; $this->sku = null; $this->name = ''; $this->description = null; $this->school_year = $this->defaultSchoolYear(); $this->quantity = 1; $this->unit_price = 0; } }; ?>