Initial commit: Import existing Laravel project
This commit is contained in:
53
app/Http/Resources/OrderResource.php
Normal file
53
app/Http/Resources/OrderResource.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$now = now()->startOfDay();
|
||||
$deadline = $this->delivery_deadline ? \Carbon\Carbon::parse($this->delivery_deadline)->startOfDay() : null;
|
||||
$isOverdue = $deadline && $deadline->lt($now) && !in_array($this->status, ['delivered', 'closed']);
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'number' => $this->number,
|
||||
'label' => $this->label,
|
||||
'type' => $this->type,
|
||||
'supplier' => $this->supplier,
|
||||
'quote_number' => $this->quote_number,
|
||||
'amount_ht' => (float) $this->amount_ht,
|
||||
'amount_ttc' => (float) $this->amount_ttc,
|
||||
'exclude_vat' => (bool) $this->exclude_vat,
|
||||
'requested_by' => $this->requested_by,
|
||||
'prescriber' => $this->prescriber,
|
||||
'delivery_deadline' => $this->delivery_deadline?->format('Y-m-d'),
|
||||
'delivery_deadline_formatted' => $this->delivery_deadline?->format('d/m/Y'),
|
||||
'status' => $this->status,
|
||||
'notes' => $this->notes,
|
||||
'is_overdue' => $isOverdue,
|
||||
'created_at' => $this->created_at?->format('d/m/Y H:i'),
|
||||
'attachments' => AttachmentResource::collection($this->whenLoaded('attachments')),
|
||||
'status_logs' => OrderStatusLogResource::collection($this->whenLoaded('statusLogs')),
|
||||
'can' => [
|
||||
'update' => $request->user()?->can('update', $this->resource),
|
||||
'delete' => $request->user()?->can('delete', $this->resource),
|
||||
],
|
||||
'can_transition_to' => [
|
||||
'validated' => $this->status === 'draft' && ($request->user()?->can('transition', [$this->resource, 'validated']) ?? false),
|
||||
'ordered' => $this->status === 'validated' && ($request->user()?->can('transition', [$this->resource, 'ordered']) ?? false),
|
||||
'delivered' => $this->status === 'ordered' && ($request->user()?->can('transition', [$this->resource, 'delivered']) ?? false),
|
||||
'closed' => $this->status === 'delivered' && ($request->user()?->can('transition', [$this->resource, 'closed']) ?? false),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user