File "TotalAmountRepository.php"

Full Path: /var/www/html/back/app/Repositories/CashFlowIndex/TotalAmountRepository.php
File size: 7.16 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Repositories\CashFlowIndex;

use App\Domain\Payment\Enums\PaymentStatusEnum;
use App\Domain\Payment\Enums\PaymentTypeEnum;
use App\Models\Payment;
use App\Models\PaymentDistribution;
use App\Repositories\CashFlowIndex\Interfaces\TotalAmountRepositoryInterface;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class TotalAmountRepository implements TotalAmountRepositoryInterface
{
    public function getTotalAmountPayment(int $modelID, $filters, $type): float
    {
        $query = Payment::query();

        $query->where('model_id', $modelID);

        if ($type) {
            $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_RECEPTION_FROM_1C->value);
        } else {
            $query->whereIn('payment_type', [
                PaymentTypeEnum::PAYMENT_TYPE_PAYMENT->value,
                PaymentTypeEnum::PAYMENT_TYPE_ISSUEANCE->value
            ]);
        }

        $filters = array_filter($filters, function ($value) {
            return $value !== null;
        });

        if (!empty($filters)) {
            if (isset($filters['date_from']) && $filters['date_from'] != null) {
                $query->whereBetween('payment_date', [$filters['date_from'], $filters['date_to']]);
            }

            if (isset($filters['projects'])) {
                $query->whereHas('distributions', function ($query) use ($filters) {
                    $query->whereIn('project_id', $filters['projects']);
                });
            }

            if (isset($filters['accounts'])) {
                $query->whereIn('account_id', $filters['accounts']);
            }

            if (!empty($filters['payments_made'])) {
                $query->whereIn('status', $filters['payments_made']);
            }

            if (!empty($filters['cash'])) {
                $query->whereIn('payment_type', $filters['cash']);
            }

        }

        $query->whereHas('distributions', function ($subQuery) {
            $subQuery->whereNull('article_id');
        });

        return $query->sum('amount');
    }

    public function getTotalAmountCashPayment(int $modelID, $filters, $type, $debitType): float
    {
        $query = Payment::query();

        $totalAmount = 0;

        $query->where('model_id', $modelID);

        if ($type) {
            $query->whereHas('distributions.payment', function ($query) {
                $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value);
            })->with(['distributions' => function ($query) use ($filters) {
                $query->whereHas('payment', function ($query) {
                    $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value);
                })->with(['payment' => function ($query) {
                    $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value);
                }]);
            }]);
        } else {
            if ($debitType == 'cash') {
                $query->whereHas('distributions.payment', function ($query) {
                    $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_MOVING->value);
                })->with(['distributions' => function ($query) use ($filters) {
                    $query->whereHas('payment', function ($query) {
                        $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_MOVING->value);
                    })->with(['payment' => function ($query) {
                        $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_MOVING->value);
                    }]);
                }]);
            }
            if ($debitType == 'moving') {
                $query->whereHas('distributions.payment', function ($query) {
                    $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_MOVING->value);
                })->with(['distributions' => function ($query) use ($filters) {
                    $query->whereHas('payment', function ($query) {
                        $query->where('payment_type', PaymentTypeEnum::PAYMENT_TYPE_MOVING->value);
                    });
                }]);
            }
        }

        $filters = array_filter($filters, function ($value) {
            return $value !== null;
        });

        if (!empty($filters)) {
            if (isset($filters['date_from']) && $filters['date_from'] != null) {
                $query->whereBetween('payment_date', [$filters['date_from'], $filters['date_to']]);
            }

            if (!empty($filters['projects'])) {
                $query->whereHas('distributions', function ($query) use ($filters) {
                    $query->whereIn('project_id', $filters['projects']);
                })->with(['distributions' => function ($query) use ($filters) {
                    $query->whereIn('project_id', $filters['projects']);
                }]);
            }

            if (!empty($filters['accounts'])) {
                $query->whereIn('account_id', $filters['accounts']);
            }

            if (!empty($filters['payments_made'])) {
                $query->whereIn('status', $filters['payments_made']);
            }

            if (!empty($filters['cash'])) {
                $query->whereIn('payment_type', $filters['cash']);
            }

        }

        $query->has('distributions');

        $data = $query->get();

        if ($debitType == 'moving') {
            return $data->sum(fn($item) => abs($item->distributions->first()->cashbox));
        }

        if ($debitType == 'cash') {
            return $data->sum(fn($item) => abs($item->distributions->first()->amount));
        }

        if ($debitType == 'commission') {
            return $data->sum(fn($item) => abs($item->distributions->first()->commission));
        }

        $amount = $data->sum(fn($item) => $item->amount);

        return $amount;
    }

    public function getTotalCommissionPaymentDistribution(int $modelID, $filters, $debitType): float
    {
        $query = PaymentDistribution::query();

        $query->whereHas('payment', function ($query) use ($modelID) {
            $query->where('model_id', $modelID);
        });

        if (isset($filters['date_from'])) {
            $dateFrom = $filters['date_from'];
            $dateTo = isset($filters['date_to']) ? $filters['date_to'] : Carbon::now();

            $query->whereHas('payment', function ($query) use ($dateFrom, $dateTo) {
                $query->whereBetween('payment_date', [$dateFrom, $dateTo]);
            });
        }

        if (!empty($filters['projects'])) {
            $query->whereIn('project_id', $filters['projects']);
        }

        if (!empty($filters['accounts'])) {
            $query->whereHas('payment', function ($query) use ($filters) {
                $query->whereIn('account_id', $filters['accounts']);
            });
        }

        if (!empty($filters['payments_made'])) {
            $query->whereHas('payment', function ($query) use ($filters) {
                $query->whereIn('status', $filters['payments_made']);
            });
        }

        if (!empty($filters['cash'])) {
            $query->whereHas('payment', function ($query) use ($filters) {
                $query->whereIn('payment_type', $filters['cash']);
            });
        }

        return $query->sum('comission');
    }
}