progress in explore balance

This commit is contained in:
Hesabix 2025-01-31 12:05:01 +00:00
parent 61dabcf451
commit 46b198ea1a

View file

@ -4,8 +4,11 @@ namespace App\Controller;
use App\Entity\BankAccount; use App\Entity\BankAccount;
use App\Entity\Cashdesk; use App\Entity\Cashdesk;
use App\Entity\Commodity;
use App\Entity\HesabdariRow; use App\Entity\HesabdariRow;
use App\Entity\HesabdariTable; use App\Entity\HesabdariTable;
use App\Entity\Person;
use App\Entity\Salary;
use App\Service\Access; use App\Service\Access;
use App\Service\Extractor; use App\Service\Extractor;
use App\Service\Provider; use App\Service\Provider;
@ -40,72 +43,206 @@ final class ExploreAccountsController extends AbstractController
if (!array_key_exists('node', $params)) if (!array_key_exists('node', $params))
throw $this->createNotFoundException(); throw $this->createNotFoundException();
// if($params['node'] == 'root'){
// $params['node'] = $entityManagerInterface
// ->getRepository(HesabdariTable::class)
// ->findOneBy(['upper'=>null])
// ->getId();
// }
if ($params['node'] == 'root') { if ($params['node'] == 'root') {
$params['node'] = 9; $params['node'] = $entityManagerInterface
->getRepository(HesabdariTable::class)
->findOneBy(['upper' => null])
->getId();
} }
$node = $entityManagerInterface->getRepository(HesabdariTable::class)->findNode($params['node'], $acc['bid']->getId()); $node = $entityManagerInterface->getRepository(HesabdariTable::class)->findNode($params['node'], $acc['bid']->getId());
if (!$node) if (!$node)
throw $this->createNotFoundException(); throw $this->createNotFoundException();
$childs = $this->getChilds($node, $acc);
if ($node->getType() == 'bank') { $output = [];
$rows = $this->getAllBanksRows($node, $acc); foreach ($childs as $child) {
} elseif ($node->getType() == 'cashdesk') { $childRows = $this->tree2flat($child, $acc);
$temp = [
} elseif ($node->getType() == 'salary') { 'id' => $child->getId(),
'code' => $child->getCode(),
} elseif ($node->getType() == 'calc') { 'name' => $child->getName(),
echo 33; ];
} elseif ($node->getType() == 'person') { $temp = array_merge($temp, $this->calculateOutput($childRows, $child));
$output[] = $temp;
} elseif ($node->getType() == 'commodity') {
} elseif ($node->getType() == 'cheque') {
} }
return $this->json([]); $data = [];
$data['itemData'] = $output;
$data['tree'] = $this->getTree($node);
return $this->json($data);
} }
private function hasChild(HesabdariTable $table): bool private function hasChild(HesabdariTable $table, array $acc = []): bool
{ {
if ($this->em->getRepository(HesabdariTable::class)->findOneBy(['upper' => $table])) if ($this->em->getRepository(HesabdariTable::class)->findOneBy(['upper' => $table]))
return true; return true;
return false; return false;
} }
private function getChilds(HesabdariTable $table): array private function getChilds(HesabdariTable $table, array $acc = []): array
{ {
return $this->em->getRepository(HesabdariTable::class)->findBy(['upper' => $table]); return $this->em->getRepository(HesabdariTable::class)->findBy(['upper' => $table]);
} }
private function getAllBanksRows(HesabdariTable $table, array $acc): array private function getAllBanksRows(HesabdariTable $table, array $acc, array $items = []): array
{ {
$items = $this->em->getRepository(BankAccount::class)->findBy([ if (count($items) == 0) {
'bid' => $acc['bid'], $items = $this->em->getRepository(BankAccount::class)->findBy([
'money' => $acc['money'], 'bid' => $acc['bid'],
]); 'money' => $acc['money'],
]);
}
return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'bank' => $items, 'bank' => $items,
'year' => $acc['year'], 'year' => $acc['year'],
'ref' => $table 'ref' => $table
], $acc['money']); ], $acc['money']);
} }
private function getAllCashdeskRows(HesabdariTable $table, array $acc): array private function getAllCashdeskRows(HesabdariTable $table, array $acc, array $items = []): array
{ {
$items = $this->em->getRepository(Cashdesk::class)->findBy([ if (count($items) == 0) {
'bid' => $acc['bid'], $items = $this->em->getRepository(Cashdesk::class)->findBy([
'money' => $acc['money'], 'bid' => $acc['bid'],
]); 'money' => $acc['money'],
]);
}
return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'cashdesk' => $items, 'cashdesk' => $items,
'year' => $acc['year'], 'year' => $acc['year'],
'ref' => $table 'ref' => $table
], $acc['money']); ], $acc['money']);
} }
private function getAllSalarysRows(HesabdariTable $table, array $acc, array $items = []): array
{
if (count($items) == 0) {
$items = $this->em->getRepository(Salary::class)->findBy([
'bid' => $acc['bid'],
'money' => $acc['money'],
]);
}
return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'salary' => $items,
'year' => $acc['year'],
'ref' => $table
], $acc['money']);
}
private function getAllPersonsRows(HesabdariTable $table, array $acc, array $items = []): array
{
if (count($items) == 0) {
$items = $this->em->getRepository(Person::class)->findBy([
'bid' => $acc['bid'],
'money' => $acc['money'],
]);
}
return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'person' => $items,
'year' => $acc['year'],
'ref' => $table
], $acc['money']);
}
private function getAllCommoditiesRows(HesabdariTable $table, array $acc, array $items = []): array
{
if (count($items) == 0) {
$items = $this->em->getRepository(Commodity::class)->findBy([
'bid' => $acc['bid'],
'money' => $acc['money'],
]);
}
return $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'commodity' => $items,
'year' => $acc['year'],
'ref' => $table
], $acc['money']);
}
private function tree2flat(Person|HesabdariTable|BankAccount|Cashdesk|Salary $item, array $acc): array
{
$res = [];
if ($this->getEntityName($item) == 'App\Entity\HesabdariTable') {
if ($this->hasChild($item)) {
foreach ($this->getChilds($item) as $child) {
$res = array_merge($res, $this->tree2flat($child, $acc));
}
return array_merge($res, $res);
} else {
$items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([
'bid' => $acc['bid'],
'ref' => $item
], $acc['money']);
$res = array_merge($res, $items);
}
} elseif ($this->getEntityName($item) == 'App\Entity\BankAccount') {
$res = $this->getAllBanksRows($item, $acc);
} elseif ($this->getEntityName($item) == 'App\Entity\Cashdesk') {
$res = $this->getAllCashdeskRows($item, $acc);
} elseif ($this->getEntityName($item) == 'App\Entity\Salary') {
$res = $this->getAllSalarysRows($item, $acc);
} elseif ($this->getEntityName($item) == 'App\Entity\Person') {
$res = $this->getAllPersonsRows($item, $acc);
} elseif ($this->getEntityName($item) == 'App\Entity\Commodity') {
$res = $this->getAllCommoditiesRows($item, $acc);
}
return $res;
}
/**
* Returns Doctrine entity name
*
* @param mixed $entity
*
* @return string
* @throws \Exception
*/
private function getEntityName($entity): string
{
$entityName = $this->em->getMetadataFactory()->getMetadataFor(get_class($entity))->getName();
return $entityName;
}
private function calculateOutput(array $items, HesabdariTable $item): array
{
$res = [
'his_bd' => 0,
'his_bs' => 0,
'bal_bd' => 0,
'bal_bs' => 0,
'id' => $item->getId(),
'account' => $item->getName(),
'type' => $item->getType(),
'code' => $item->getCode(),
];
foreach ($items as $item) {
$res['his_bs'] += $item->getBs();
$res['his_bd'] += $item->getBd();
}
if ($res['his_bd'] > $res['his_bs']) {
$res['bal_bd'] = $res['his_bd'] - $res['his_bs'];
$res['bal_bs'] = 0;
} else {
$res['bal_bs'] = $res['his_bs'] - $res['his_bd'];
$res['bal_bd'] = 0;
}
return $res;
}
private function getTree(HesabdariTable $table): array
{
$tree = [];
while ($table->getUpper() != null) {
$tree[] = [
'id' => $table->getId(),
'code' => $table->getCode(),
'name' => $table->getName(),
];
$table = $table->getUpper();
}
$tree[] = [
'id' => 1,
'code' => 'root',
'name' => 'جدول حساب‌ها',
];
return array_reverse($tree);
}
} }