<?php
namespace App\Http\Resources\User;
use App\Models\Status;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
class OrderUserInfoResource extends JsonResource
{
public function toArray($request)
{
$statuses = $order_items = [];
$sub_total = 0;
$all_statuses = Status::all();
foreach ($this->statuses as $sts) {
$statusId = $sts->pivot->status_id;
$notes = $sts?->pivot?->notes;
$updated_at = $sts->pivot->updated_at;
$db_status = $all_statuses->where('id', $statusId)->first();
$statuses[] = [
'id' => $statusId,
'name' => $db_status->name,
'color' => $db_status->color,
'notes' => $notes ?? 'N/A',
'updated_at' => $updated_at->diffForHumans(),
'is_selected' => true,
];
}
foreach ($this->items as $item) {
$variations = $customization_array = [];
foreach ($item->orderItemVariations as $variation){
$variations[] = [
'id' => $variation->variation?->id,
'type' => $variation->variation?->type,
'value' => $variation->variation?->value,
];
}
foreach ($item->orderItemCustomizations as $customization){
$customization_array['url'][] = $customization->mediaUrl;
}
$order_items[] = [
'product_id' => $item->product?->id,
'product_slug' => $item->product?->slug,
'product_image' => $item->product?->featured_image?->preview,
'product' => $item->product?->name,
'quantity' => $item->quantity,
'engraving_fee' => $item->engraving_fee,
'price' => $item->price,
'product_total' => $item->total_price,
'customization' => $customization_array,
'product_customization' => $item->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',
];
$sub_total += $item->total_price_after_engraving;
}
//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;
}
return [
'id' => $this->id,
'order_number' => $this->order_number,
'payment_status' => $this->payment_status,
'payment_date' => $this->formated_payment_date,
'payment_type' => $this->payment_type,
'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'),
] : [],
'cheque_number' => $this->cheque_number,
'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'),
'description' => $this->description,
'current_status' => $this->current_status?->name,
'products' => $order_items,
'sub_total' => '$'.number_format($sub_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),
],
'rush_order' => $this->rush_order,
'rush_order_fee' => ($this->rush_order_fee ?? 0).'%',
'rush_order_amount' => $this->rush_order_amount,
'shipping_charges' => '$'.number_format($this->shipping_charges, 2),
'grand_total' => '$'.(number_format($grand_total, 2)),
'delivery_type' => $this->delivery_type,
'tracking_number' => $this->tracking_number,
'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,
'available_statuses' => $statuses,
];
}
}