$this->id, 'invoice_number' => $this->invoice_number, 'duplicate' => BillingDuplicateDetector::forInvoice($this->id), 'order_num' => $this->whenLoaded('order', fn() => $this->order?->order_num), 'order_items' => $this->when( $this->relationLoaded('order') && $this->order?->relationLoaded('orderItems'), fn() => OrderItemServiceResource::collection($this->order->orderItems)->resolve(), ), 'customer' => $this->whenLoaded('customer', fn() => [ 'id' => $this->customer->id, 'uuid' => $this->customer->uuid, 'fullname' => $this->customer->fullname, 'email' => $this->customer->email, ]), 'sub_total' => $this->sub_total, 'discount_amount' => $this->discount_amount, 'coupon_code' => $this->coupon_code, 'tax' => $this->tax, 'total' => $this->total, // Công nợ: đã thu bao nhiêu / còn thiếu bao nhiêu. Kế toán cần thấy ngay trên // danh sách thay vì phải suy từ số dư ví của khách. 'amount_paid' => (int) $this->amount_paid, 'outstanding' => \App\Services\InvoicePaymentService::outstanding($this->resource), 'status' => $this->status, 'is_starred' => (bool) $this->is_starred, 'tags' => $this->whenLoaded('tags'), 'due_date' => $this->due_date, 'paid_at' => $this->paid_at, 'reminder_step' => (int) $this->reminder_step, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } }