File "OrderInfoResource.php"

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

<?php

namespace App\Http\Resources\Admin;

use App\Models\Status;
use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class OrderInfoResource extends JsonResource
{
    public function toArray($request)
    {
        $statuses = $order_items = [];
        $sub_total = $total_engraving_fee = 0;
        $user = $this->user;
        $updatable_statuses = [];

        $all_statuses = Status::where('id', '!=', 12)->get();

        foreach ($all_statuses as $status) {
            $updatable_statuses[] = [
                'id' => $status->id,
                'name' => $status->name,
            ];
        }
        foreach ($this->statuses as $sts) {
            $status_user = User::find($sts->pivot->user_id);
            $statusId = $sts->pivot->status_id;
            $notes = $sts->pivot->notes;
            $updated_by = $status_user->name ?? 'N/A';
            $updated_at = $sts->pivot->updated_at;

            $db_status = $all_statuses->where('id', $statusId)->first();

            if($db_status) {
                $statuses[] = [
                    'id' => $statusId,
                    'name' => $db_status->name,
                    'color' => $db_status->color,
                    'notes' => $notes ?? 'N/A',
                    'updated_by' => $updated_by,
                    'updated_at' => $updated_at->format('m-d-Y h:i:s'),
                    'is_selected' => true,
                ];
            }
        }

        foreach ($this->items as $item) {
            $variations = $svgs = [];
            foreach ($item->orderItemVariations as $variation){
                $variations[] = [
                    'id' => $variation->variation?->id,
                    'type' => $variation->variation?->type,
                    'value' => $variation->variation?->value,
                ];
            }

            foreach ($item->customizationSvg as $svg){
                $svgs[] = [
                    'order_id' => $svg->order_id,
                    'order_item_id' => $svg->order_item_id,
                    'customization_index' => $svg->customization_index,
                    'url' => $svg->customization_svg_url,
                ];
            }

            $order_items[] = [
                'product_id' => $item->product?->id,
                'product_sku' => $item->product?->sku,
                'product_price_sku' => $item->productPrice?->supplier_prod_number,
                'product_slug' => $item->product?->slug,
                'product_image' => $item->product?->featured_image?->preview,
                'product' => $item->product?->name,
                'product_vendor' => $item->product?->vendor?->name,
                'quantity' => $item->quantity,
                'price' => '$'.number_format($item->price, 2),
                'engraving_fee' => $item->engraving_fee,
                'product_total' => '$'.number_format($item->total_price, 2),
                // 'customization' => $customization_array,
                'product_customization' => json_decode($item->customization ?? '[]'),
                'sketch_file' => $item->productPrice?->product_sketch_media,
                'template' => $item->template_media ?: 'N/A',
                'image' => $item->imageUrl ?: 'N/A',
                'variations' => $variations,
                'notes' => $item->notes,
                'approved_consent' => $item->approved_consent ? 'Yes': 'No',
                'customization_svgs' => $svgs,
            ];
            $sub_total += $item->total_price_after_engraving;
            $total_engraving_fee += $item->engraving_fee;
        }

        //Rush order Calculation ()

        $sub_total_after_discount = ($sub_total + $this->rush_order_amount) - $this->discount_total;
        $grand_total = $sub_total_after_discount + $this->shipping_charges;

        if ($this->waive_off_sales_tax != 1){
            $grand_total += $this->sales_tax_amount;
        }

        foreach ($this->partialPayments as $partialPayment){
            $payments[] = [
                'id' => $partialPayment->id,
                'charged_by' => $partialPayment->user->name,
                'payment_type' => $partialPayment->payment_type,
                'amount' => "$".number_format($partialPayment->amount, 2),
                'cheque_number' => $partialPayment->cheque_number,
                'purchase_order_number' => $partialPayment->purchase_order_number,
                'purchase_order_copy' => $partialPayment->purchase_order_copy_url,
                'cc_details' => $partialPayment->payment ? [
                    'last_four' => $partialPayment->payment->last_four,
                    'card_type' => $partialPayment->payment->account_type,
                    'payment_date' => $partialPayment->payment->created_at->format('m-d-Y h:i:s'),
                    'transaction_id' => $partialPayment->payment->transaction_id
                ] : [],
                'created_at' => $partialPayment->created_at->format('m-d-Y h:i:s'),
            ];
        }

        return [
            'id' => $this->id,
            'assigned_to' => $this->assigned_to?->name,
            'order_type' => $this->order_type,
            'rush_order' => $this->rush_order,
            'rush_order_fee' => ($this->rush_order_fee ?? 0).'%',
            'order_number' => $this->order_number,
            'book_number' => $this->book_number,
            'payment_status' => $this->payment_status,
            'payment_date' => $this->formated_payment_date,
            'payment_type' => $this->payment_type,
            'payment_mode' => $this->payment_mode,
            'cc_details' => $this->payment ? [
                'last_four' => $this->payment->last_four,
                'card_type' => $this->payment->account_type,
                'payment_date' => $this->payment->created_at->format('Y-m-d'),
                'transaction_id' => $this->payment->transaction_id
            ] : [],
            'cheque_number' => $this->cheque_number,
            'notes_count' => $this->notes->count(),
            'purchase_order_number' => $this->purchase_order_number ?? 'N/A',
            'purchase_order_copy' => $this->purchase_order_copy_url ?? 'N/A',
            'delivery_date' => $this->delivery_date?->format('Y-m-d'),
            'event_date' => $this->event_date?->format('Y-m-d'),
            'date_scheduled' => $this->date_scheduled?->format('Y-m-d'),
            'date_pick_or_ship_by' => $this->date_pick_or_ship_by?->format('Y-m-d'),
            'delivery_type' => $this->delivery_type,
            'tracking_number' => $this->tracking_number,
            'pickup_by' => $this->pickup_by,
            'pickup_location_title' => $this->pickLocation?->title,
            'pickup_location_address' => $this->pickLocation?->address,
            'billing_company_name' => $this->billing_company_name,
            'billing_primary_contact_name' => $this->billing_primary_contact_name,
            'billing_primary_contact_email' => $this->billing_primary_contact_email,
            'billing_secondary_contact_name' => $this->billing_secondary_contact_name,
            'billing_secondary_contact_email' => $this->billing_secondary_contact_email,
            'billing_address_line_1' => $this->billing_address_line_1,
            'billing_address_line_2' => $this->billing_address_line_2,
            'billing_city' => $this->billing_city,
            'billing_state' => $this->billing_state,
            'billing_zipcode' => $this->billing_zipcode,
            'billing_phone_number' => $this->billing_phone_number,
            'shipping_company_name' => $this->shipping_company_name,
            'shipping_primary_contact_name' => $this->shipping_primary_contact_name,
            'shipping_primary_contact_email' => $this->shipping_primary_contact_email,
            'shipping_secondary_contact_name' => $this->shipping_secondary_contact_name,
            'shipping_secondary_contact_email' => $this->shipping_secondary_contact_email,
            'shipping_address_line_1' => $this->shipping_address_line_1,
            'shipping_address_line_2' => $this->shipping_address_line_2,
            'shipping_city' => $this->shipping_city,
            'shipping_state' => $this->shipping_state,
            'shipping_zipcode' => $this->shipping_zipcode,
            'is_residential' => $this->is_residential ? 'Yes' : 'No',
            'description' => $this->description,
            'current_status' => $this->current_status?->name,
            'cancellation_charges' => $this->cancellation_charges,
            'waive_off_sales_tax' => $this->waive_off_sales_tax,
            'waive_off_sales_tax_reason' => $this->waive_off_sales_tax_reason,
            'stock_location' => $this->stockLocation?->location,
            'resale_number' => $this->resale_number,
            'products' => $order_items,
            'sub_total' => '$'.number_format($sub_total, 2),
            'sub_total_after_discount' => '$'.number_format($sub_total_after_discount, 2),
            'engraving_fee' => '$'.number_format($total_engraving_fee, 2),
            'discount' => $this->discount_type != null ? [
                'discount_type' => $this->discount_type,
                'discount_value' => $this->discount_value,
                'discount_total' => $this->discount_total,
            ] : null,
            'state_sales_tax' => [
                'percentage' => $this->state_sales_tax,
                'amount' => '$'.number_format($this->sales_tax_amount, 2),
            ],
            'shipping_charges' => '$'.number_format($this->shipping_charges, 2),
            'grand_total' => '$'.(number_format($grand_total, 2)),
            'paid_order_amount' => '$'.(number_format($this->paid_order_amount, 2)),
            'remaining_order_amount' => $this->remaining_order_amount,
            'partial_payments' => $payments ?? [],
            'user' => $user ? [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'phone_number' => $user->phone_number,
            ] : [],
            'available_statuses' => $statuses,
            'updatable_statuses' => $updatable_statuses,
            'relational_shipping_address' => $this->shipping_address ? [
                'company_name' => $this->shipping_address->company_name,
                'primary_contact_name' => $this->shipping_address->primary_contact_name,
                'primary_contact_email' => $this->shipping_address->primary_contact_email,
                'secondary_contact_name' => $this->shipping_address->secondary_contact_name,
                'secondary_contact_email' => $this->shipping_address->secondary_contact_email,
                'address_line_1' => $this->shipping_address->address_line_1,
                'address_line_2' => $this->shipping_address->address_line_2,
                'city' => $this->shipping_address->city,
                'state' => $this->shipping_address->state->name,
                'zipcode' => $this->shipping_address->zipcode,
                'phone_number' => $this->shipping_address->phone_number,
            ] : [],
            'relational_billing_address' => $this->billing_address ? [
                'company_name' => $this->billing_address->company_name,
                'primary_contact_name' => $this->billing_address->primary_contact_name,
                'primary_contact_email' => $this->billing_address->primary_contact_email,
                'secondary_contact_name' => $this->billing_address->secondary_contact_name,
                'secondary_contact_email' => $this->billing_address->secondary_contact_email,
                'address_line_1' => $this->billing_address->address_line_1,
                'address_line_2' => $this->billing_address->address_line_2,
                'city' => $this->billing_address->city,
                'state' => $this->billing_address->state->name,
                'zipcode' => $this->billing_address->zipcode,
                'phone_number' => $this->billing_address->phone_number,
            ] : [],
        ];
    }
}