File "CashFlowProjectCreditResource.php"

Full path: /var/www/html/back/CashFlowProjectCreditResource.php
File size: 1.69 KB B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

namespace App\Http\Resources;

use App\Domain\Payment\Enums\PaymentTypeEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CashFlowProjectCreditResource extends JsonResource
{
    public function toArray(Request $request): array
    {

        $totalExtradition = 0;

        return [
            'id' => $this->id,
            'name' => $this->name,

            'project' => $this->paymentDistributions->map(function ($item) use (&$totalExtradition) {

                if (!$item->project) {
                    return null;
                }

                if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_ISSUEANCE->value) {
                    $amount = (float)$item->amount;
                    $totalExtradition += $amount;
                }

                return [
                    'id' => $item->project->id,
                    'description' => $item->project->description,
                    'short_description' => $item->project->short_description,
                    'offer_number' => $item->project->offer_number,
                    'object_address' => $item->project->object_address,
                    'total_extradition' => $totalExtradition,

                    'payment' => [
                        'id' => $item->payment->id,
                        'name' => $item->payment->name,
                        'amount' => $item->amount,
                        'status' => $item->payment->status,
                        'payment_type' => $item->payment->payment_type,
                        'payment_date' => $item->payment->payment_date,
                    ],
                ];
            })->filter()->values(),
        ];
    }
}