TreasuryLaunchResource.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use App\Models\TreasuryLaunch;
  8. class TreasuryLaunchResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function toArray(Request $request): array
  16. {
  17. return [
  18. 'id' => $this->id,
  19. 'unit_id' => $this->unit_id,
  20. 'transaction_type' => $this->transaction_type,
  21. 'from_account_id' => $this->from_account_id,
  22. 'to_account_id' => $this->to_account_id,
  23. // Banco da movimentação (destino na entrada, origem na saída).
  24. 'account_id' => $this->transaction_type === 'entrada' ? $this->to_account_id : $this->from_account_id,
  25. 'description' => $this->description,
  26. 'financial_plan_account_id' => $this->financial_plan_account_id,
  27. 'financial_plan_account' => $this->whenLoaded('financialPlanAccount', function () {
  28. return $this->financialPlanAccount ? [
  29. 'id' => $this->financialPlanAccount->id,
  30. 'code' => $this->financialPlanAccount->code,
  31. 'description' => $this->financialPlanAccount->description,
  32. 'label' => "{$this->financialPlanAccount->code} - {$this->financialPlanAccount->description}",
  33. ] : null;
  34. }),
  35. 'amount' => (float) $this->amount,
  36. 'launch_date' => $this->launch_date,
  37. 'origin' => $this->origin,
  38. 'is_reconciled' => (bool) $this->is_reconciled,
  39. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  40. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  41. ];
  42. }
  43. /**
  44. * @param \Illuminate\Database\Eloquent\Collection<TreasuryLaunch> $resource
  45. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<TreasuryLaunchResource>
  46. */
  47. public static function collection($resource): AnonymousResourceCollection
  48. {
  49. return parent::collection($resource);
  50. }
  51. }