File "CashFlowController.php"

Full Path: /var/www/html/back/app/Http/Controllers/Api/V1/CashFlowController.php
File size: 43.44 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Controllers\Api\V1;

use App\Domain\Payment\Enums\PaymentTypeEnum;
use App\Http\Controllers\Api\ApiController;
use app\Http\Resources\CashFlow\OtherData\PaymentByCounterpartyResource;
use app\Http\Resources\CashFlow\OtherData\PaymentByDateResource;
use app\Http\Resources\CashFlow\Project\ShowProjectResource;
use App\Http\Resources\CashFlowShowArticlesGroup;
use App\Http\Resources\CashFlowShowResource;
use App\Http\Resources\CashTypesResource;
use App\Responses\ResourceResponse;
use App\Responses\ResponseDto;
use App\Services\CashFlow\ActiveTabDayService;
use App\Services\CashFlow\ActiveTabMonthService;
use App\Services\CashFlow\ActiveTabPeriodService;
use App\Services\CashFlow\ActiveTabProjectService;
use App\Services\CashFlow\ActiveTabQuarterService;
use App\Services\CashFlowService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CashFlowController extends ApiController
{
    public function __construct(protected CashFlowService         $cashFlowService,
                                protected ActiveTabPeriodService  $activeTabPeriodService,
                                protected ActiveTabQuarterService $activeTabQuarterService,
                                protected ActiveTabMonthService   $activeTabMonthService,
                                protected ActiveTabDayService     $activeTabDayService,
                                protected ActiveTabProjectService $activeTabProjectService)
    {
    }

    public function index($modelID, Request $request)
    {
        $activeTab = $request->input('activeTab');

        $activePage = $request->input('activePage');

        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMade($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from'),
            'date_to' => $request->input('date_to'),
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeInput ?? null,
            'cash' => $paymentTypes ?? null,
        ];

        if ($activePage == 'period') {
            if ($activeTab == 'period') {
                return $this->activeTabPeriodService->getIndex($modelID, $filters);
            }

            if ($activeTab == 'quarter') {
                return $this->activeTabQuarterService->getIndex($modelID, $filters);
            }

            if ($activeTab == 'month') {
                return $this->activeTabMonthService->getIndex($modelID, $filters);
            }
            if ($activeTab == 'days') {
                return $this->activeTabDayService->getIndex($modelID, $filters);
            }
        }

        if ($activePage == 'project') {
            return $this->activeTabProjectService->getActivePageProjectToIndex($modelID, $filters);
        }

        return 1;
    }


    public function show($modelId, $articleId, Request $request)
    {
        $year = $request->input('year');

        $month = $request->input('month');

        $day = $request->input('day');

        $quarter = $request->input('quarter');


        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMade($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from') ?? null,
            'date_to' => $request->input('date_to') ?? null,
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeInput ?? null,
            'cash' => $paymentTypes ?? null,
        ];

        if (isset($year) && isset($month) && isset($day)) {
            $article = $this->activeTabDayService->getArticleShow($modelId, $articleId, $year, $month, $day, $filters);

            return new ResponseDto(
                data: new CashFlowShowResource($article),
                status: true
            );
        }

        if (isset($year) && isset($month)) {
            $article = $this->activeTabMonthService->getArticleShow($modelId, $articleId, $year, $month, $filters);

            return new ResponseDto(
                data: new CashFlowShowResource($article),
                status: true
            );
        }


        if (isset($year) && isset($quarter)) {
            $article = $this->activeTabQuarterService->getArticleShow($modelId, $articleId, $year, $quarter, $filters);

            return new ResponseDto(
                data: new CashFlowShowResource($article),
                status: true
            );
        }

        $article = $this->cashFlowService->getArticleShow($modelId, $articleId, $filters);

        return new ResponseDto(
            data: new CashFlowShowResource($article),
            status: true
        );
    }

    public function showProjects($modelId, $projectID, Request $request)
    {
        $articleId = $request->input('articleID');
        $articleGroupId = $request->input('articleGroupId');

        $dateToInput = $request->input('date_to');
        $dateFromInput = $request->input('date_from');

        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMadeForApi($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_to' => $dateToInput ?? null,
            'date_from' => $dateFromInput ?? null,
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeArray ?? null,
            'cash' => $paymentTypes ?? null,
        ];

        if ($articleId) {

            $project = $this->activeTabProjectService->getProjectShow($modelId, $projectID, $articleId, $filters);

            return new ResponseDto(
                data: new ShowProjectResource($project),
                status: true
            );
        }

        if ($articleGroupId) {
            $project = $this->activeTabProjectService->getArticleGroupProjectShow($modelId, $projectID, $articleGroupId, $filters);

            return new ResponseDto(
                data: new ShowProjectResource($project),
                status: true
            );
        }
    }

    public function showArticlesGroup($modelId, $articleGroupId, Request $request)
    {
        $year = $request->input('year');

        $month = $request->input('month');

        $day = $request->input('day');

        $quarter = $request->input('quarter');

        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMade($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from') ?? null,
            'date_to' => $request->input('date_to') ?? null,
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeInput ?? null,
            'cash' => $paymentTypes ?? null,
        ];

        if (isset($year) && isset($month) && isset($day)) {
            $articleGroups = $this->activeTabDayService->getArticlesGroupShow($modelId, $articleGroupId, $year, $month, $day, $filters);

            return new ResponseDto(
                data: new CashFlowShowArticlesGroup($articleGroups),
                status: true
            );
        }


        if (isset($year) && isset($month)) {
            $articleGroups = $this->activeTabMonthService->getArticlesGroupShow($modelId, $articleGroupId, $year, $month, $filters);

            return new ResponseDto(
                data: new CashFlowShowArticlesGroup($articleGroups),
                status: true
            );
        }

        if (isset($year) && isset($quarter)) {
            $articleGroups = $this->activeTabQuarterService->getArticlesGroupShow($modelId, $articleGroupId, $year, $quarter, $filters);

            return new ResponseDto(
                data: new CashFlowShowArticlesGroup($articleGroups),
                status: true
            );
        }

        $articleGroups = $this->cashFlowService->getShowArticlesGroup($modelId, $articleGroupId, $filters);

        return new ResponseDto(
            data: new CashFlowShowArticlesGroup($articleGroups),
            status: true
        );
    }


    public function showOtherData($modelId, $type, Request $request)
    {
        $year = $request->input('year');

        $month = $request->input('month');

        $day = $request->input('day');

        $quarter = $request->input('quarter');


        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        $scoreAndCashInput = $request->input('cash');

        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from'),
            'date_to' => $request->input('date_to'),
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeInput ?? null,
            'cash' => $paymentTypes ?? null,
        ];

        if ($type == 'moving') {

            if (isset($year) && isset($month) && isset($day)) {

                $otherData = $this->activeTabDayService->getOtherDataShow($modelId, $year, $month, $day, $type,$filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount, $type) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } elseif ($type == 'commission') {
                            $totalAmount += $item->commission;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (isset($year) && isset($month)) {
                $otherData = $this->activeTabMonthService->getOtherDataShow($modelId, $year, $month, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id) && isset($item->payment);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount, $type) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } elseif ($type == 'commission') {
                            $totalAmount += $item->commission;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );

            }

            if (isset($year) && isset($quarter)) {
                $otherData = $this->activeTabQuarterService->getOtherDataShow($modelId, $year, $quarter, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount, $type) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->amount)) {
                            $totalAmount += $item->amount;
                        } elseif ($type == 'commission') {
                            $totalAmount += $item->commission;
                        } else {
                            $totalAmount += $item->cashbox;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (!isset($year) && !isset($month) && !isset($day)) {
                $otherData = $this->cashFlowService->getOtherDataShow($modelId, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->amount)) {
                        $totalAmount += abs($item->amount);
                    } else {
                        $totalAmount += $item->cashbox;
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

        }

        if ($type == 'commission') {

            if (isset($year) && isset($month) && isset($day)) {

                $otherData = $this->activeTabDayService->getOtherDataShow($modelId, $year, $month, $day, $type,$filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->comission;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (isset($year) && isset($month)) {
                $otherData = $this->activeTabMonthService->getOtherDataShow($modelId, $year, $month, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->comission;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );

            }

            if (isset($year) && isset($quarter)) {
                $otherData = $this->activeTabQuarterService->getOtherDataShow($modelId, $year, $quarter, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->comission;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (!isset($year) && !isset($month) && !isset($day)) {
                $otherData = $this->cashFlowService->getOtherDataShow($modelId, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $counterparties->type = $type;

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->comission;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }
        }

        if ($type == 'cash') {

            if (isset($year) && isset($month) && isset($day)) {

                $otherData = $this->activeTabDayService->getOtherDataShow($modelId, $year, $month, $day, $type,$filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->cashbox;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (isset($year) && isset($month)) {
                $otherData = $this->activeTabMonthService->getOtherDataShow($modelId, $year, $month, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->cashbox;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );

            }

            if (isset($year) && isset($quarter)) {
                $otherData = $this->activeTabQuarterService->getOtherDataShow($modelId, $year, $quarter, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->cashbox;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (!isset($year) && !isset($month) && !isset($day)) {
                $otherData = $this->cashFlowService->getOtherDataShow($modelId, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    $totalAmount += $item->cashbox;
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }
        }

        if ($type == 'cash-credit') {
            if (isset($year) && isset($month) && isset($day)) {

                $otherData = $this->activeTabDayService->getOtherDataShow($modelId, $year, $month, $day, $type,$filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount, $type) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (isset($year) && isset($month)) {
                $otherData = $this->activeTabMonthService->getOtherDataShow($modelId, $year, $month, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );

            }

            if (isset($year) && isset($quarter)) {
                $otherData = $this->activeTabQuarterService->getOtherDataShow($modelId, $year, $quarter, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }

            if (!isset($year) && !isset($month) && !isset($day)) {
                $otherData = $this->cashFlowService->getOtherDataShow($modelId, $type, $filters);

                $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

                $totalAmount = 0;

                $totalCount = $otherData->filter(function ($item) {
                    return isset($item->payment_id);
                })->count();

                $otherData->each(function ($item) use (&$totalAmount) {
                    if (isset($item->payment) && $item->payment) {
                        if ($item->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_MOVING->value && isset($item->cashbox)) {
                            $totalAmount += $item->cashbox;
                        } else {
                            $totalAmount += $item->amount;
                        }
                    }
                });

                return new ResponseDto(
                    data: [
                        'total' => [
                            'count' => $totalCount,
                            'amount' => $totalAmount,
                        ],
                        'payments_by_date' => PaymentByDateResource::collectionWithType($otherData, $type),
                        'payments_by_counterparty' => PaymentByCounterpartyResource::collectionWithType($counterparties, $type),
                    ],
                    status: true
                );
            }
        }


        return 1;
    }

    public function showIncomeData($modelId, Request $request)
    {
        $year = $request->input('year');

        $month = $request->input('month');

        $day = $request->input('day');

        $quarter = $request->input('quarter');

        $projectId = $request->input('project_id');


        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMadeForApi($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from') ?? null,
            'date_to' => $request->input('date_to') ?? null,
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeArray ?? null,
            'cash' => $paymentTypes ?? null,
        ];


        if (isset($projectId)) {
            $otherData = $this->activeTabProjectService->getIncomeAndExpensesToProjectShow($modelId, 'credit', $projectId);

            return $this->uniqueFormat($otherData);
        }

        if (isset($year) && isset($month) && isset($day)) {
            $baseIncome = $this->activeTabDayService->getIncomeAndExpensesToDayShow($modelId, 'credit', $year, $month, $day, $filters);
            $cashIncome = $this->activeTabDayService->getCashIncomeToDayShow($modelId, $year, $month, $day);
            $receptionIncome = $this->activeTabDayService->getReceptionIncomeToDayShow($modelId, $year, $month, $day, $filters['payments_made']);
            $undistributedIncome = $this->activeTabDayService->getUndistributedIncomeShowByDays($modelId, $year, $month, $day, $filters['payments_made']);

            $otherData = $baseIncome->merge($cashIncome)
                                    ->merge($receptionIncome)
                                    ->merge($undistributedIncome)
                                    ->unique('id')
                                    ->values();

            return $this->uniqueFormat($otherData);
        }

        if (isset($year) && isset($month)) {
            $baseIncome = $this->activeTabMonthService->getIncomeAndExpensesToMonthShow($modelId, 'credit', $year, $month, $filters);
            $cashIncome = $this->activeTabMonthService->getCashIncomeToMonthShow($modelId, $year, $month);
            $receptionIncome = $this->activeTabMonthService->getReceptionIncomeToMonthShow($modelId, $year, $month, $filters['payments_made']);
            $undistributedIncome = $this->activeTabMonthService->getUndistributedIncomeShowByMonths($modelId, $year, $month, $filters['payments_made']);


            $otherData = $baseIncome->merge($cashIncome)
                                    ->merge($receptionIncome)
                                    ->merge($undistributedIncome)
                                    ->unique('id')
                                    ->values();

            return $this->uniqueFormat($otherData);
        }

        if (isset($year) && isset($quarter)) {
            $baseIncome = $this->activeTabQuarterService->getIncomeAndExpensesToQuarterShow($modelId, 'credit', $year, $quarter, $filters);
            $cashIncome = $this->activeTabQuarterService->getCashIncomeToQuarterShow($modelId, $year, $quarter);
            $receptionIncome = $this->activeTabQuarterService->getReceptionIncomeToQuarterShow($modelId, $year, $quarter, $filters['payments_made']);
            $undistributedIncome = $this->activeTabQuarterService->getUndistributedIncomeShowByQuarter($modelId, $year, $quarter, $filters['payments_made']);

            $otherData = $baseIncome->merge($cashIncome)
                                    ->merge($receptionIncome)
                                    ->merge($undistributedIncome)
                                    ->unique('id')
                                    ->values();

            return $this->uniqueFormat($otherData);
        }

        $baseIncome = $this->cashFlowService->getIncomeAndExpensesShow($modelId, 'credit', $filters);
        $cashIncome = $this->cashFlowService->getCashIncomeShow($modelId);
        $receptionIncome = $this->cashFlowService->getReceptionIncomeShow($modelId, $filters['payments_made']);
        $undistributedIncome = $this->cashFlowService->getUndistributedIncomeShow($modelId, $filters['payments_made']);

        $otherData = $baseIncome
            ->merge($cashIncome)
            ->merge($receptionIncome)
            ->merge($undistributedIncome)
            ->unique('id')
            ->values();

        return $this->uniqueFormat($otherData);

    }

    public function showExpensesData($modelId, Request $request)
    {
        $year = $request->input('year');

        $month = $request->input('month');

        $day = $request->input('day');

        $quarter = $request->input('quarter');

        $projectId = $request->input('project_id');

        $projectsInput = $request->input('projects');
        if (isset($projectsInput)) {
            $projectsArray = $this->cashFlowService->getFilter($projectsInput);
        }

        $accountsInput = $request->input('accounts');
        if (isset($accountsInput)) {
            $accountsArray = $this->cashFlowService->getFilter($accountsInput);
        }

        $paymentsMadeInput = $request->input('payments_made');

        if (isset($paymentsMadeInput)) {
            $paymentsMadeArray = $this->cashFlowService->getPaymentsMade($paymentsMadeInput);
        }

        $scoreAndCashInput = $request->input('cash');
        if (isset($scoreAndCashInput)) {
            $paymentTypes = $this->cashFlowService->getScoreAndCashFilter($scoreAndCashInput);
        }

        $filters = [
            'date_from' => $request->input('date_from') ?? null,
            'date_to' => $request->input('date_to') ?? null,
            'projects' => $projectsArray ?? null,
            'accounts' => $accountsArray ?? null,
            'payments_made' => $paymentsMadeInput ?? null,
            'cash' => $paymentTypes ?? null,
        ];


        if (isset($projectId)) {
            $otherData = $this->activeTabProjectService->getIncomeAndExpensesToProjectShow($modelId, 'debit', $projectId);

            return $this->uniqueFormat($otherData);
        }


        if (isset($year) && isset($month) && isset($day)) {
            $otherData = $this->activeTabDayService->getIncomeAndExpensesToDayShow($modelId, 'debit', $year, $month, $day);

            return $this->uniqueFormat($otherData);
        }

        if (isset($year) && isset($month)) {
            $otherData = $this->activeTabMonthService->getIncomeAndExpensesToMonthShow($modelId, 'debit', $year, $month);

            return $this->uniqueFormat($otherData);
        }

        if (isset($year) && isset($quarter)) {
            $otherData = $this->activeTabQuarterService->getIncomeAndExpensesToQuarterShow($modelId, 'debit', $year, $quarter);

            return $this->uniqueFormat($otherData);
        }

        $baseIncome = $this->cashFlowService->getIncomeAndExpensesShow($modelId, 'debit', $filters);
        $issueanceData = $this->cashFlowService->getIssueanceShow($modelId);

        $otherData = $baseIncome
            ->merge($issueanceData)
            ->unique('id')
            ->values();

        return $this->uniqueFormat($otherData);
    }

    public function showUndistributedPayments($modelId, $type, Request $request)
    {
        $filters = array_filter([
            'payments_made' => $request->input('payments_made'),
            'year'          => $request->input('year'),
            'quarter'       => $request->input('quarter'),
            'month'         => $request->input('month'),
            'day'           => $request->input('day'),
        ], fn ($value) => !is_null($value) && $value !== '');

        return $this->cashFlowService->getUndistributedPayments($modelId, $type, $filters);
    }


    public function uniqueFormat($otherData)
    {
        $counterparties = $this->cashFlowService->getUniqueCounterpariesToPayment($otherData);

        $totalAmount = 0;

        $totalCount = $otherData->filter(function ($item) {
            return isset($item->payment_id);
        })->count();

        $otherData->each(function ($item) use (&$totalAmount) {
            if (isset($item->payment) && $item->payment) {
                $totalAmount += $item->amount;
            }
        });

        return new ResponseDto(
            data: [
                'total' => [
                    'count' => $totalCount,
                    'amount' => $totalAmount,
                ],
                'payments_by_date' => PaymentByDateResource::collection($otherData),
                'payments_by_counterparty' => PaymentByCounterpartyResource::collection($counterparties),
            ],
            status: true
        );
    }


    public function getCashTypes(): ResourceResponse
    {
        return new ResourceResponse(
            data: new CashTypesResource([]),
            status: 200
        );
    }

    public function articleMove(Request $request)
    {
        $articleId = $request->input('article_id');
        $newLine = $request->input('new_line');
        $articleGroupId = $request->input('article_group_id');

        $this->cashFlowService->moveArticleOrArticleGroupInCashFlow($articleId, $newLine, $articleGroupId);
    }

    public function excelExport(int $modelID, Request $request): ResponseDto
    {
        $ids = $request->input('articleIds');

        $filename = $this->cashFlowService->excelExport($ids);

        return new ResponseDto(
            data: ['url' => asset("storage/{$filename}")],
            status: true
        );
    }

}