Files
shipping-portal/app/Http/Resources/TransactionForOrderShowPageResource.php
T
2025-07-05 14:52:50 +08:00

72 lines
2.9 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Group;
use App\Models\PackingList;
use App\Models\Transaction;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
class TransactionForOrderShowPageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$groupTransactions = null;
if ($this->owner instanceof Transaction) {
// if ($this->owner) {
// if ($this->owner->owner) {
// $order = new OrderResource($this->owner->owner->owner);
// }
// }
} else if (!($this->owner instanceof Transaction) && !($this->owner instanceof Wallet)) {
// if ($this->owner) {
// $order = new OrderResource($this->owner->owner);
// }
} else {
$group = Group::where('reference', $this->payment_reference)->first();
if ($group) {
$groupTransactions = GroupTransactionResource::collection($group->groupTransactions);
}
}
$packingListReference = null;
if ($this->owner instanceof PackingList) {
$packingListReference = $this->owner->reference;
}
return [
'id' => $this->id,
'owner_type' => $this->owner_type,
'documents' => $groupTransactions ? DocumentResource::collection($this->documents->where('status', ApprovalStatus::PENDING_VERIFICATION)) : DocumentResource::collection($this->documents),
'type' => (int) $this->type,
'bill_no' => $this->bill_no,
'amount' => (double) $this->amount,
'payment_method' => (int) $this->payment_method,
'payment_reference' => $this->payment_reference,
'service_charge' => (double) $this->service_charge,
'tax' => (double) $this->tax,
'original_amount' => (double) $this->original_amount,
'currency_rate' => (double) $this->currency_rate,
'status' => (int) $this->status,
'remarks' => RemarkResource::collection($this->remarks),
'packing_list_reference' => $packingListReference,
'storages' => $this->storages ? $this->storages : null, //from middleware
'is_waived' => (int) $this->is_waived,
'expires_on' => Carbon::parse($this->expires_on)->format('d-m-Y h:i:s A'),
'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y'),
'created_at' => Carbon::parse($this->created_at)->format('d-m-Y'),
'is_einvoice_applicable' => Carbon::parse($this->created_at)->isAfter(Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00'))) && $this->is_einvoice_applicable,
];
}
}