Refactor code structure for improved readability and maintainability

This commit is contained in:
baodev0
2026-08-10 08:37:52 +07:00
parent d6d63e5152
commit e3d3dea8aa
31 changed files with 290 additions and 319 deletions
@@ -31,8 +31,8 @@ class ClientResource extends JsonResource
'date' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : null,
'company' => $this->company ?? '',
'avatar' => $this->avatar,
'cmnd_after' => $this->front_identity_number,
'cmnd_before' => $this->back_identity_number,
'cmnd_before' => $this->front_identity_number,
'cmnd_after' => $this->back_identity_number,
'cmnd' => $this->identity_number,
'fb_link' => null,
'active_2fa' => 0,
@@ -8,6 +8,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
class InvoiceDetailResource extends JsonResource
{
use ResolvesPaymentMethod;
public function toArray(Request $request): array
{
return [
@@ -20,7 +22,7 @@ class InvoiceDetailResource extends JsonResource
'discount_amount' => (float) ($this->discount_amount ?? 0),
'tax' => (float) $this->tax,
'total' => (float) $this->total,
'payment_method' => $this->payment_method,
'payment_method' => $this->resolvePaymentMethod($this->resource),
'auto_renew' => (bool) $this->auto_renew,
'created_at' => $this->created_at?->toISOString(),
'due_date' => $this->due_date?->toISOString(),
@@ -9,6 +9,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
class OrderDetailResource extends JsonResource
{
use ResolvesPaymentMethod;
public function toArray(Request $request): array
{
return [
@@ -33,6 +35,7 @@ class OrderDetailResource extends JsonResource
'total' => (float) $this->invoice->total,
'due_date' => $this->invoice->due_date?->toISOString(),
'paid_at' => $this->invoice->paid_at?->toISOString(),
'payment_method' => $this->resolvePaymentMethod($this->invoice),
]),
'items' => $this->whenLoaded('orderItems') ? OrderItemResource::collection($this->orderItems) : [],
];
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Resources\Api\Service\Order;
use App\Models\Invoice;
trait ResolvesPaymentMethod
{
/** invoice.payment_method holds a code (balance/credit/transfer/...), not always a Payment row id. */
private const METHOD_LABELS = [
'balance' => 'Số dư tài khoản',
'credit' => 'Số dư tài khoản',
'transfer' => 'Chuyển khoản ngân hàng',
'vnpay' => 'VNPay',
'momo' => 'Momo',
'bank_transfer' => 'Chuyển khoản ngân hàng',
'admin' => 'Xác nhận thủ công',
];
private function resolvePaymentMethod(Invoice $invoice): ?array
{
$method = $invoice->payment_method;
if (! $method) {
return null;
}
if (ctype_digit((string) $method) && $invoice->payment) {
return [
'code' => null,
'name' => $invoice->payment->name,
'bank_name' => $invoice->payment->bankName,
'logo' => $invoice->payment->logo,
];
}
return [
'code' => $method,
'name' => self::METHOD_LABELS[$method] ?? $method,
'bank_name' => null,
'logo' => null,
];
}
}