File "ExtendedShowAccountResource.php"

Full Path: /var/www/html/back/app/Http/Resources/ExtendedShowAccountResource.php
File size: 1.61 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Resources;

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

class ExtendedShowAccountResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $credit = 0;
        $debit = 0;
        $balanceOfTheDate = 0;
        $currentBalance = 0;

        $addedDate = Carbon::parse($this->added);

        foreach ($this->payments as $payment) {

            $paymentDate = Carbon::parse($payment->payment_date);

            if ($payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value) {
                $credit += $payment->amount;
            } else {
                $debit += $payment->amount;
            }

            if ($addedDate->gt($paymentDate)) {
                if ($payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value) {
                    $balanceOfTheDate += $payment->amount;
                }
            }
        }

        $currentBalance = $balanceOfTheDate + $credit - $debit;

        return [
            'id' => $this->id,
            'name' => $this->name,
            'number' => $this->number,
            'organization' => $this->organization_name,
            'date_added' => $this->added,
            'balance_of_the_date' => $this->balance_of_the_date,
            'cor_number' => $this->cor_number,
            'bank_name' => $this->bank_name,
            'bic' => $this->bank_bic,
            'city' => $this->bank_city,
            'archived' => $this->archived,
            'is_cash' => $this->is_cash,
        ];
    }
}