Laravel Vapor - Code refactoring

This commit is contained in:
Dillon Ngo
2024-09-21 13:15:05 +08:00
parent 3a97b5e26d
commit 5d8d57e399
6 changed files with 123 additions and 113 deletions
+14 -3
View File
@@ -25,12 +25,15 @@ class PackingListBaseResource extends JsonResource
$receive_packing_list = null;
if($this->include_packages){
$packages = !$this->packingLists()->exists() || $exceptionUsers ? $this->packages : $this->packingLists->first()->packages;
foreach ($packages as $item) {
$item['include_order'] = $this->include_order_in_packages;
}
}
if($this->include_receive_packing_list){
$receive_packing_list = $this->when($this->type === PackingListType::SHIPPING_PACKING_LIST, new PackingListResource(PackingList::where('reference', $this->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->first()));
}
return [
$data = [
'id' => $this->id,
'claimant_id' => $this->claimant_id,
'reference' => $this->reference,
@@ -43,9 +46,17 @@ class PackingListBaseResource extends JsonResource
$this->mergeWhen($this->owner instanceof Order, [
'order' => New OrderResource($this->owner)
]),
'shipping_transaction' => new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first()),
'suspended_invoice' => new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first()),
// 'suspended_invoices' => TransactionResource::collection($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->get()),
];
if($this->include_shipping_transaction){
$data['shipping_transaction'] = new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first());
}
if($this->include_suspended_invoice){
$data['suspended_invoice'] = new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first());
}
return $data;
}
}