File "OrderResource.php"

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

<?php

namespace App\Http\Resources\Admin;

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

class OrderResource extends JsonResource
{
    public function toArray($request)
    {
        $total_items_qty = $this->items->sum('quantity') ?? 0;
        $total_engraving_fee = $this->items->sum('engraving_fee') ?? 0;

        $existing_order_statuses = $this->statuses;
        foreach (Status::all() as $status) {
            $st = $existing_order_statuses->where('id', $status->id)->first();
            if ($st) {
                $checked = true;
                $notes = $st->pivot->notes;
            } else {
                $checked = false;
                $notes = null;
            }
            $statuses[] = [
                'id' => $status->id,
                'name' => $status->name,
                'color' => $status->color,
                'notes' => $notes,
                'is_selected' => $checked,
            ];
        }

        $sub_total_after_discount = ($this->items_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;
        }

        return [
            'id' => $this->id,
            'order_number' => $this->order_number,
            'book_number' => $this->book_number,
            'rush_order' => $this->rush_order,
            'rush_order_fee' => ($this->rush_order_fee ?? 0) . '%',
            'admin_approved' => $this->admin_approved_status,
            'payment_status' => $this->payment_status,
            'payment_date' => $this->formated_payment_date,
            'payment_type' => $this->payment_type,
            'cheque_number' => $this->cheque_number,
            'purchase_order_number' => $this->purchase_order_number ?? 'N/A',
            'purchase_order_copy' => $this->purchase_order_copy_url ?? 'N/A',
            'user_name' => $this->user?->name,
            'user_email' => $this->user?->email,
            'billing_company_name' => $this->billing_company_name,
            'assigned_to' => $this->assigned_to?->name,
            'current_status' => $this->current_status?->name,
            'is_completed' => $this->current_status_id == 13 || $this->current_status_id == 14,
            'total_products' => $total_items_qty,
            'engraving_fee' => '$' . number_format($total_engraving_fee, 2),
            'sub_total' => '$' . (number_format($this->items_total, 2)),
            'sub_total_after_discount' => '$' . (number_format($sub_total_after_discount, 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),
            ],
            'waive_off_sales_tax' => $this->waive_off_sales_tax == 1 ? 'Yes' : 'No',
            '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' => '$' . (number_format($this->remaining_order_amount, 2)),
            'delivery_date' => $this->delivery_date?->format('Y-m-d'),
            'event_date' => $this->event_date?->format('Y-m-d'),
            'delivery_type' => $this->delivery_type,
            'tracking_number' => $this->tracking_number,
            'pickup_by' => $this->pickup_by,
            'description' => $this->description,
            'stock_location' => $this->stockLocation?->location,
            'statuses' => $statuses,
        ];
    }
}