<?php
declare(strict_types=1);
namespace App\Services\CashFlow;
use App\Domain\Article\Enums\ArticleTypeEnum;
use App\Domain\Payment\Enums\PaymentTypeEnum;
use App\Http\Resources\CashFlowCreditResource;
use App\Http\Resources\CashFlowDebitResource;
use App\Models\ArticleGroup;
use App\Repositories\CashFlowIndex\Interfaces\PaymentDistributionsRepositoryInterface;
use App\Repositories\CashFlowIndex\Interfaces\TotalAmountRepositoryInterface;
use App\Responses\ResponseDto;
use App\Services\CashFlowService;
use Illuminate\Support\Facades\Log;
class ActiveTabPeriodService
{
private $distributionsRepository;
private $paymentRepository;
public function __construct(PaymentDistributionsRepositoryInterface $paymentDistributionsRepository,
TotalAmountRepositoryInterface $paymentRepository,
protected CashFlowService $cashFlowService,
protected ActiveTabQuarterService $activeTabQuarterService)
{
$this->distributionsRepository = $paymentDistributionsRepository;
$this->paymentRepository = $paymentRepository;
}
public function getIndex(int $modelID, $filters)
{
$paymentDistributionsCredit = $this->distributionsRepository->getPaymentDistributions($modelID, $filters, ArticleTypeEnum::ARTICLE_TYPE_CREDIT->value);
$paymentDistributionsDebit = $this->distributionsRepository->getPaymentDistributions($modelID, $filters, ArticleTypeEnum::ARTICLE_TYPE_DEBIT->value);
$totalAmountCreditPayment = $this->paymentRepository->getTotalAmountPayment($modelID, $filters, $credit = true);
$totalAmountDebitPayment = $this->paymentRepository->getTotalAmountPayment($modelID, $filters, $credit = false);
$totalAmountCashCreditPayment = $this->paymentRepository->getTotalAmountCashPayment($modelID, $filters, $credit = true, $debitType = null);
$totalAmountDebitCashPayment = $this->paymentRepository->getTotalAmountCashPayment($modelID, $filters, $credit = false, $debitType = 'cash');
$totalAmountCashDebitCommissionPayment = $this->paymentRepository->getTotalCommissionPaymentDistribution($modelID, $filters, $debitType);
$totalMoving = $this->paymentRepository->getTotalAmountCashPayment($modelID, $filters, $credit = false, $debitType = 'moving');
$totalCredit = $this->sumTotalAmount($paymentDistributionsCredit);
$totalDebit = $this->sumTotalAmount($paymentDistributionsDebit);
$groupsWithoutArticlesCredit = ArticleGroup::where(['article_type' => 'credit', 'model_id' => $modelID])
->orderBy('sort', 'desc')
->doesntHave('articles')
->get();
$groupsWithoutArticlesDebit = ArticleGroup::where(['article_type' => 'debit', 'model_id' => $modelID])->doesntHave('articles')->get();
$totalCash = $this->cashFlowService->getTotalCash($filters, $modelID);
$totalCashDebit = $this->getTotalCashDebit($paymentDistributionsDebit);
$creditArticles = CashFlowCreditResource::collection($paymentDistributionsCredit);
$debitArticles = CashFlowDebitResource::collection($paymentDistributionsDebit);
$dynamicTotalAmountCredit = $this->sumTotalAmount($paymentDistributionsCredit);
$dynamicTotalCashCredit = $this->sumTotalCash($paymentDistributionsCredit);
$dynamicTotalAmountDebit = $this->sumTotalAmount($paymentDistributionsDebit);
$dynamicTotalCashDebit = $this->sumTotalCash($paymentDistributionsDebit);
return new ResponseDto(
data: [
'header' => [
'start_period' => [
'amount' => 0,
]
],
'credit' => [
'groups_without_articles' => $groupsWithoutArticlesCredit,
'total_amount' => $totalCredit + $totalAmountCreditPayment,
'total_cash' => $totalCash,
'articles' => $creditArticles,
'not_distribution_payments_total_amount' => $totalAmountCreditPayment,
'cash' => $totalAmountCashCreditPayment,
'other_data' => [
'total_cash' => $totalCash,
],
'dynamic' => [
'total_amount' => $dynamicTotalAmountCredit,
'total_cash' => $dynamicTotalCashCredit,
'articles' => $creditArticles,
],
],
'debit' => [
'groups_without_articles' => $groupsWithoutArticlesDebit,
'total_amount' => $totalDebit + $totalAmountDebitPayment,
'total_cash_debit' => $totalCashDebit,
'articles' => $debitArticles,
'not_distribution_payments_total_amount' => $totalAmountDebitPayment,
'other_data' => [
'moving' => $totalMoving, //касса
'commission' => $totalAmountCashDebitCommissionPayment, // комиссия
'cash' => $totalAmountDebitCashPayment, //перемещение
],
'dynamic' => [
'total_amount' => $dynamicTotalAmountDebit,
'total_cash' => $dynamicTotalCashDebit,
'articles' => $debitArticles,
],
],
'footer' => [
'end_period' => [
'amount' => (($totalCredit + $totalCash) - ($totalDebit + $totalAmountDebitCashPayment)),
'cash' => $totalCash - $totalCashDebit,
]
],
],
status: true
);
}
public function getTotalCashDebit($paymentDistributions)
{
$totalExtradition = 0;
foreach ($paymentDistributions as $article) {
foreach ($article->paymentDistributions as $distribution) {
if ($distribution->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_ISSUEANCE->value) {
$amount = (float)$distribution->amount;
$totalExtradition += $amount;
}
}
}
return $totalExtradition;
}
public function sumTotalAmount($paymentDistributions)
{
$total = 0;
foreach ($paymentDistributions as $article) {
foreach ($article->paymentDistributions as $distribution) {
if ($distribution->payment == null) {
continue;
}
if (($distribution->payment->payment_type != PaymentTypeEnum::PAYMENT_TYPE_MOVING->value) &&
($distribution->payment->payment_type != PaymentTypeEnum::PAYMENT_TYPE_ISSUEANCE->value)) {
$amount = (float)$distribution->amount;
$total += $amount;
}
}
}
return $total;
}
public function sumTotalCash($paymentDistributions)
{
$total = 0;
foreach ($paymentDistributions as $article) {
foreach ($article->paymentDistributions as $distribution) {
if ($distribution->payment && isset($distribution->article_id) && $distribution->article_id == 1) {
// Фильтруем только счета-кассы (is_cash = true)
if ($distribution->payment->account && $distribution->payment->account->is_cash) {
$amount = (float)$distribution->amount;
$total += $amount;
}
}
}
}
return $total;
}
}