'date', 'due_date' => 'date', 'status' => InvoiceStatus::class, 'subtotal' => MoneyCast::class, 'tax_total' => MoneyCast::class, 'discount_total' => MoneyCast::class, 'total' => MoneyCast::class, 'amount_paid' => MoneyCast::class, 'amount_due' => MoneyCast::class, ]; public function client(): BelongsTo { return $this->belongsTo(Client::class); } public function lineItems(): MorphMany { return $this->morphMany(DocumentLineItem::class, 'documentable'); } public function transactions(): MorphMany { return $this->morphMany(Transaction::class, 'transactionable'); } public function payments(): MorphMany { return $this->transactions()->where('is_payment', true); } public function deposits(): MorphMany { return $this->transactions()->where('type', TransactionType::Deposit)->where('is_payment', true); } public function withdrawals(): MorphMany { return $this->transactions()->where('type', TransactionType::Withdrawal)->where('is_payment', true); } protected function lastSent(): Attribute { return Attribute::get(function () { return $this->getStatusChangedAt(InvoiceStatus::Sent); }); } protected function approvedAt(): Attribute { return Attribute::get(function () { return $this->getStatusChangedAt(InvoiceStatus::Unsent); }); } protected function paidAt(): Attribute { return Attribute::get(function () { return $this->getStatusChangedAt(InvoiceStatus::Paid); }); } protected function getStatusChangedAt(InvoiceStatus $status): ?Carbon { return $this->statusHistories ->where('new_status', $status) ->sortByDesc('changed_at') ->first()?->changed_at; } public function approvalTransaction(): MorphOne { return $this->morphOne(Transaction::class, 'transactionable') ->where('type', TransactionType::Journal); } public function statusHistories(): HasMany { return $this->hasMany(InvoiceStatusHistory::class); } protected function isCurrentlyOverdue(): Attribute { return Attribute::get(function () { return $this->due_date->isBefore(today()) && $this->canBeOverdue(); }); } public function isDraft(): bool { return $this->status === InvoiceStatus::Draft; } public function canRecordPayment(): bool { return ! in_array($this->status, [ InvoiceStatus::Draft, InvoiceStatus::Paid, InvoiceStatus::Void, ]); } public function canBulkRecordPayment(): bool { return ! in_array($this->status, [ InvoiceStatus::Draft, InvoiceStatus::Paid, InvoiceStatus::Void, InvoiceStatus::Overpaid, ]); } public function canBeOverdue(): bool { return in_array($this->status, InvoiceStatus::canBeOverdue()); } public static function getNextDocumentNumber(): string { $company = auth()->user()->currentCompany; if (! $company) { throw new \RuntimeException('No current company is set for the user.'); } $defaultInvoiceSettings = $company->defaultInvoice; $numberPrefix = $defaultInvoiceSettings->number_prefix; $numberDigits = $defaultInvoiceSettings->number_digits; $latestDocument = static::query() ->whereNotNull('invoice_number') ->latest('invoice_number') ->first(); $lastNumberNumericPart = $latestDocument ? (int) substr($latestDocument->invoice_number, strlen($numberPrefix)) : 0; $numberNext = $lastNumberNumericPart + 1; return $defaultInvoiceSettings->getNumberNext( padded: true, format: true, prefix: $numberPrefix, digits: $numberDigits, next: $numberNext ); } public function recordPayment(array $data): void { $isRefund = $this->status === InvoiceStatus::Overpaid; if ($isRefund) { $transactionType = TransactionType::Withdrawal; $transactionDescription = 'Refund for Overpayment on Invoice #' . $this->invoice_number; } else { $transactionType = TransactionType::Deposit; $transactionDescription = 'Payment for Invoice #' . $this->invoice_number; } // Create transaction $this->transactions()->create([ 'company_id' => $this->company_id, 'type' => $transactionType, 'is_payment' => true, 'posted_at' => $data['posted_at'], 'amount' => $data['amount'], 'payment_method' => $data['payment_method'], 'bank_account_id' => $data['bank_account_id'], 'account_id' => Account::getAccountsReceivableAccount()->id, 'description' => $transactionDescription, 'notes' => $data['notes'] ?? null, ]); } public function approveDraft(): void { if (! $this->isDraft()) { throw new \RuntimeException('Invoice is not in draft status.'); } $transaction = $this->transactions()->create([ 'company_id' => $this->company_id, 'type' => TransactionType::Journal, 'posted_at' => now(), 'amount' => $this->total, 'description' => 'Invoice Approval for Invoice #' . $this->invoice_number, ]); $transaction->journalEntries()->create([ 'company_id' => $this->company_id, 'type' => JournalEntryType::Debit, 'account_id' => Account::getAccountsReceivableAccount()->id, 'amount' => $this->total, 'description' => $transaction->description, ]); foreach ($this->lineItems as $lineItem) { $transaction->journalEntries()->create([ 'company_id' => $this->company_id, 'type' => JournalEntryType::Credit, 'account_id' => $lineItem->offering->income_account_id, 'amount' => $lineItem->subtotal, 'description' => $transaction->description, ]); foreach ($lineItem->adjustments as $adjustment) { $transaction->journalEntries()->create([ 'company_id' => $this->company_id, 'type' => $adjustment->category->isDiscount() ? JournalEntryType::Debit : JournalEntryType::Credit, 'account_id' => $adjustment->account_id, 'amount' => $lineItem->calculateAdjustmentTotal($adjustment)->getAmount(), 'description' => $transaction->description, ]); } } $this->update([ 'status' => InvoiceStatus::Unsent, ]); } }