user() ? Auth()->user()->type : RoleTypes::USER; $exceptionUsers = in_array($userType, [RoleTypes::SHADOW_ADMIN, RoleTypes::SUPER_ADMIN]); //The Base $data = [ 'id' => $this->id, 'claimant_id' => $this->claimant_id, 'reference' => $this->reference, 'status' => $this->status, 'transport' => new TransportResource($this->transports()->first()), 'type' => $this->type, 'container' => new ContainerResource($this->containers->first()), $this->mergeWhen($this->owner instanceof Order, [ 'order' => New OrderResource($this->owner) ]), ]; //option 1: include_packages $packages = !$this->packingLists()->exists() || $exceptionUsers ? $this->packages : $this->packingLists->first()->packages; $data['packages'] = PackageBaseResource::collection($packages); //option 2a: include_receive_packing_list $temp = PackingList::where('reference', $this->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->first(); // $temp->include_shipping_transaction = true; // $temp->include_suspended_invoice = true; // $temp->include_packages = true; // $temp->include_receive_packing_list = true; // $temp->include_base_only_in_receive_packing_list = false; // $temp->include_order_in_packages = true; // $temp->include_transport_in_packages = true; // $temp->include_container_in_packages = true; // $temp->include_packing_list_reference_in_packages = true; $receive_packing_list = $this->when($this->type === PackingListType::SHIPPING_PACKING_LIST, new PackingListBaseResource($temp)); //cief todo: 100 - PackingListBaseResource was PackingListDeprecatedResource $data['receive_packing_list'] = $receive_packing_list; //option 3: include_shipping_transaction WITHOUT warehouse storage $shippingTransaction = $this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first(); $data['shipping_transaction'] = new TransactionResource($shippingTransaction); //option 4: include_suspended_invoice $data['suspended_invoice'] = new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first()); return $data; } }