feat(services): partial/unpaid payment on admin create, payment history per invoice, zip attachments
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
02daefe4c2
commit
fe68171aa7
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Billing;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class InvoicePaymentHistory
|
||||
{
|
||||
private const SERVICE_CREATION_PREFIX = 'SVC-';
|
||||
|
||||
/**
|
||||
* @param Collection<int, Invoice> $invoices
|
||||
* @return array<int, list<array<string, mixed>>> keyed by invoice id, oldest payment first
|
||||
*/
|
||||
public static function forInvoices(Collection $invoices): array
|
||||
{
|
||||
$history = $invoices->mapWithKeys(fn (Invoice $invoice) => [$invoice->id => []])->all();
|
||||
if ($invoices->isEmpty()) {
|
||||
return $history;
|
||||
}
|
||||
|
||||
$invoiceByOrderNum = $invoices
|
||||
->filter(fn (Invoice $invoice) => (int) $invoice->invoice_type === 1 && $invoice->order?->order_num)
|
||||
->keyBy(fn (Invoice $invoice) => $invoice->order->order_num);
|
||||
|
||||
$transactions = Transaction::with('user')
|
||||
->whereIn('customer_id', $invoices->pluck('customer_id')->unique())
|
||||
->where(function ($query) use ($invoices, $invoiceByOrderNum) {
|
||||
foreach ($invoices as $invoice) {
|
||||
$query->orWhere('trans', $invoice->invoice_number)
|
||||
->orWhere('trans', 'like', $invoice->invoice_number . '-%');
|
||||
}
|
||||
if ($invoiceByOrderNum->isNotEmpty()) {
|
||||
$query->orWhere(fn ($creation) => $creation
|
||||
->whereIn('trans', $invoiceByOrderNum->keys())
|
||||
->where('public_code', 'like', self::SERVICE_CREATION_PREFIX . '%'));
|
||||
}
|
||||
})
|
||||
->oldest('id')
|
||||
->get();
|
||||
|
||||
$invoiceByNumber = $invoices->keyBy('invoice_number');
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
$invoice = self::matchInvoice($transaction, $invoiceByNumber, $invoiceByOrderNum);
|
||||
if ($invoice) {
|
||||
$history[$invoice->id][] = self::present($transaction);
|
||||
}
|
||||
}
|
||||
|
||||
return $history;
|
||||
}
|
||||
|
||||
private static function matchInvoice(Transaction $transaction, Collection $invoiceByNumber, Collection $invoiceByOrderNum): ?Invoice
|
||||
{
|
||||
if (Str::startsWith((string) $transaction->public_code, self::SERVICE_CREATION_PREFIX)) {
|
||||
return $invoiceByOrderNum->get($transaction->trans);
|
||||
}
|
||||
|
||||
$number = preg_replace('/-(P\d+|REFUND)$/', '', (string) $transaction->trans);
|
||||
|
||||
return $invoiceByNumber->get($number);
|
||||
}
|
||||
|
||||
private static function present(Transaction $transaction): array
|
||||
{
|
||||
$isRefund = Str::endsWith((string) $transaction->trans, '-REFUND');
|
||||
|
||||
return [
|
||||
'id' => $transaction->id,
|
||||
'public_code' => $transaction->public_code,
|
||||
'kind' => $isRefund ? 'refund' : 'payment',
|
||||
'amount' => abs((int) $transaction->amount),
|
||||
'type' => $transaction->type,
|
||||
'note' => $transaction->note,
|
||||
'recorded_by' => $transaction->user?->fullname ?: $transaction->user?->username,
|
||||
'created_at' => $transaction->created_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user