File "PurchaseOrderDetailsResource.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Resources/Admin/PurchaseOrderDetailsResource.php
File size: 3.79 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Resources\Admin;

use Illuminate\Http\Resources\Json\JsonResource;

class PurchaseOrderDetailsResource extends JsonResource
{
    public function toArray($request)
    {
        $po_items = [];
        foreach ($this->orderDetails as $item){
            $orders = [];
            foreach ($item->ordersPODetails as $ordersPODetail) {
                if ($ordersPODetail->order_id) {
                    $orders[] = [
                        'order_id' => $ordersPODetail->order_id,
                        'order_number' => $ordersPODetail?->order?->order_number,
                        'quantity' => $ordersPODetail->quantity,
                    ];
                } else {
                    $orders[] = [
                        'extra_quantity' => $ordersPODetail->quantity,
                    ];
                }
            }

            $variations = [];
            if ($item->product_type == 'office_supplies') {
                $po_items[] = [
                    'id' => $item->id,
                    'product_id' => $item->officeSupply?->name,
                    'product_number' => $item->product_number,
                    'product_type' => $item->product_type,
                    'product_variations' => $variations,
                    'quantity' => $item->quantity,
                    'price' => $item->price,
                    'total_price' => $item->total_price,
                    'featured_image' => $item->officeSupply?->supply_featured_image ? [
                        'url'       => $item->officeSupply?->supply_featured_image->url,
                        'thumbnail' => $item->officeSupply?->supply_featured_image->thumbnail,
                        'preview'   => $item->officeSupply?->supply_featured_image->preview
                    ] :[],
                    'orders' => [],
                ];
            } else {
                if ($item->product_type == 'standard') {
                    foreach ($item->productPrice->combinations ?? [] as $combination) {
                        $variations[] = $combination->variation->type.': '.$combination->variation->value;
                    }
                }
                $po_items[] = [
                    'id' => $item->id,
                    'product_id' => $item->product_type == 'standard' ? $item->product?->name : $item->variation?->value,
                    'product_number' => $item->product_number,
                    'product_type' => $item->product_type,
                    'product_variations' => $variations,
                    'quantity' => $item->quantity,
                    'price' => $item->price,
                    'total_price' => $item->total_price,
                    'featured_image' => $item->product?->featured_image ? [
                        'url'       => $item->product?->featured_image->url,
                        'thumbnail' => $item->product?->featured_image->thumbnail,
                        'preview'   => $item->product?->featured_image->preview
                    ] :[],
                    'orders' => $orders,
                ];
            }
        }
        return [
            'id' => $this->id,
            'order_number' => $this->order_number,
            'ordered_by' => $this->user?->name,
            'vendor' => $this->vendor?->name,
            'payment_terms' => $this->payment_terms,
            'reference' => $this->reference,
            'admin_notes' => $this->admin_notes,
            'shipping_address' => $this->shipping_address,
            'billing_address' => $this->billing_address,
            'total_quantity' => $this->total_quantity,
            'total_price' => '$'.number_format($this->total_price, 2),
            'status' => $this->status,
            'created_at' => $this->created_at->format('m/d/Y H:i:s'),
            'products' => $po_items,
        ];
    }
}