File "PaymentByDateResource.php"

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

<?php

namespace app\Http\Resources\CashFlow\OtherData;

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

class PaymentByDateResource extends JsonResource
{
    use HasPaymentType;

    protected string $paymentType;

    public function __construct($resource, string $paymentType)
    {
        parent::__construct($resource);
        $this->paymentType = $paymentType;
    }

    public function toArray(Request $request): array
    {
        $paymentType = $this->paymentType;


        if ($paymentType == 'cash') {
            $displayAmount = $this->cashbox ?? $this->amount;
        } elseif ($paymentType == 'commission') {
            $displayAmount = $this->comission ?? $this->amount;
        } elseif ($paymentType == 'moving') {
            $displayAmount = $this->amount ?? $this->cashbox;
        } elseif ($paymentType == 'cash-credit') {
            if ($this->payment && $this->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value) {
                $displayAmount = $this->cashbox ?? 0;
            } else {
                $displayAmount = $this->amount ?? 0;
            }

            $displayAmount = abs($displayAmount);
        } else {
            $displayAmount = $this->display_amount ?? $this->amount;
        }

        $dateField = 'payment_date';
        if ($paymentType == 'cash-credit' && $this->payment) {
            if ($this->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($this->payment->actual_date)) {
                $dateField = 'actual_date';
            } else {
                $dateField = 'payment_date';
            }
        } else {
            $dateField = 'payment_date';
        }

        return [
            'date' => $this->payment ? ($dateField === 'actual_date' ? $this->payment->actual_date : $this->payment->payment_date) : null,
            'payment' => $this->payment ? [
                'id' => $this->payment->id,
                'name' => $this->payment->name,
                'amount' => abs($displayAmount),
                'status' => $this->payment->status,
                'payment_type' => $this->payment->payment_type,
                'payment_date' => $this->payment->payment_date,
                'actual_date' => $this->payment->actual_date,
                'counterparty' => $this->payment->contragent ? [
                    'id' => $this->payment->contragent->id,
                    'name' => $this->payment->contragent->name,
                    'inn' => $this->payment->contragent->inn,
                    'kpp' => $this->payment->contragent->kpp,
                    'comment' => $this->payment->contragent->comment,
                    'c_type' => $this->payment->contragent->c_type,
                ] : null,
            ] : null,
        ];
    }
}