diff --git a/hesabixCore/src/Controller/ExploreAccountsController.php b/hesabixCore/src/Controller/ExploreAccountsController.php index 3be898d..b27683e 100644 --- a/hesabixCore/src/Controller/ExploreAccountsController.php +++ b/hesabixCore/src/Controller/ExploreAccountsController.php @@ -10,7 +10,9 @@ use App\Entity\HesabdariTable; use App\Entity\Person; use App\Entity\Salary; use App\Service\Access; +use App\Service\Explore; use App\Service\Extractor; +use App\Service\Jdate; use App\Service\Provider; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -28,6 +30,70 @@ final class ExploreAccountsController extends AbstractController $this->em = $entityManager; $this->provider = $provider; } + + #[Route('/api/report/acc/get_details', name: 'app_report_acc_get_details')] + public function app_report_acc_get_details(Provider $provider, Jdate $jdate, Access $access, Request $request, EntityManagerInterface $entityManagerInterface): JsonResponse + { + $acc = $access->hasRole('report'); + if (!$acc) { + throw $this->createAccessDeniedException(); + } + + $params = []; + if ($content = $request->getContent()) { + $params = json_decode($content, true); + } + if (!array_key_exists('node', $params)) + throw $this->createNotFoundException(); + + if ($params['isObject'] == false) { + $node = $entityManagerInterface->getRepository(HesabdariTable::class)->findNode($params['node'], $acc['bid']->getId()); + if (!$node) + throw $this->createNotFoundException(); + $rows = $this->tree2flat($node, $acc); + } else { + $node = $entityManagerInterface->getRepository(HesabdariTable::class)->findNode($params['upperID'], $acc['bid']->getId()); + if (!$node) + throw $this->createNotFoundException(); + if ($node->getType() == 'bank') { + $item = $entityManagerInterface->getRepository(BankAccount::class)->findOneBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + 'id' => $params['node'] + ]); + $rows = $this->getAllBanksRows($node, $acc, [$item]); + } elseif ($node->getType() == 'cashdesk') { + $item = $entityManagerInterface->getRepository(Cashdesk::class)->findOneBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + 'id' => $params['node'] + ]); + $rows = $this->getAllCashdeskRows($node, $acc, [$item]); + } elseif ($node->getType() == 'salary') { + $item = $entityManagerInterface->getRepository(Salary::class)->findOneBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + 'id' => $params['node'] + ]); + $rows = $this->getAllSalarysRows($node, $acc, [$item]); + } elseif ($node->getType() == 'person') { + $item = $entityManagerInterface->getRepository(Person::class)->findOneBy([ + 'bid' => $acc['bid'], + 'id' => $params['node'] + ]); + $rows = $this->getAllPersonsRows($node, $acc, [$item]); + + } elseif ($node->getType() == 'commodity') { + $item = $entityManagerInterface->getRepository(Commodity::class)->findOneBy([ + 'bid' => $acc['bid'], + 'id' => $params['node'] + ]); + $rows = $this->getAllCommoditiesRows($node, $acc, [$item]); + } + } + return $this->json(Explore::ExploreHesabdariRows($rows)); + } + #[Route('/api/report/acc/explore_accounts_det', name: 'app_explore_accounts_det')] public function app_explore_accounts_det(Access $access, Request $request, EntityManagerInterface $entityManagerInterface, Extractor $extractor): JsonResponse { @@ -53,23 +119,191 @@ final class ExploreAccountsController extends AbstractController $node = $entityManagerInterface->getRepository(HesabdariTable::class)->findNode($params['node'], $acc['bid']->getId()); if (!$node) throw $this->createNotFoundException(); - $childs = $this->getChilds($node, $acc); - $output = []; - foreach ($childs as $child) { - $childRows = $this->tree2flat($child, $acc); - $temp = [ - 'id' => $child->getId(), - 'code' => $child->getCode(), - 'name' => $child->getName(), - ]; - $temp = array_merge($temp, $this->calculateOutput($childRows, $child)); - $output[] = $temp; + + if ($node->getType() == 'calc') { + $childs = $this->getChilds($node, $acc); + $output = []; + foreach ($childs as $child) { + $childRows = $this->tree2flat($child, $acc); + $output[] = $this->calculateOutput($childRows, $child, $acc); + } + } elseif ($node->getType() == 'bank') { + $items = $entityManagerInterface->getRepository(BankAccount::class)->findBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + ]); + foreach ($items as $item) { + $rows = $this->getAllBanksRows($node, $acc, [$item]); + $output[] = $this->calculateOutputObject($rows, $node, $acc, $item); + } + } elseif ($node->getType() == 'cashdesk') { + $items = $entityManagerInterface->getRepository(Cashdesk::class)->findBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + ]); + foreach ($items as $item) { + $rows = $this->getAllCashdeskRows($node, $acc, [$item]); + $output[] = $this->calculateOutputObject($rows, $node, $acc, $item); + } + } elseif ($node->getType() == 'salary') { + $items = $entityManagerInterface->getRepository(Salary::class)->findBy([ + 'bid' => $acc['bid'], + 'money' => $acc['money'], + ]); + foreach ($items as $item) { + $rows = $this->getAllSalarysRows($node, $acc, [$item]); + $output[] = $this->calculateOutputObject($rows, $node, $acc, $item); + } + } elseif ($node->getType() == 'person') { + $items = $entityManagerInterface->getRepository(Person::class)->findBy([ + 'bid' => $acc['bid'], + ]); + foreach ($items as $item) { + $rows = $this->getAllPersonsRows($node, $acc, [$item]); + $output[] = $this->calculateOutputObject($rows, $node, $acc, $item); + } + } elseif ($node->getType() == 'commodity') { + $items = $entityManagerInterface->getRepository(Commodity::class)->findBy([ + 'bid' => $acc['bid'], + ]); + foreach ($items as $item) { + $rows = $this->getAllCommoditiesRows($node, $acc, [$item]); + $output[] = $this->calculateOutputObject($rows, $node, $acc, $item); + } } + $data = []; $data['itemData'] = $output; $data['tree'] = $this->getTree($node); return $this->json($data); } + 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 $acc): 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(), + 'name' => $item->getName(), + 'isObject' => false, + 'hasChild' => $this->hasChild($item, $acc), + 'upperID' => $item->getUpper()->getId() + ]; + 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 calculateOutputObject(array $items, HesabdariTable $node, array $acc, BankAccount|Person|Commodity|Cashdesk|Salary $obj): array + { + $res = [ + 'his_bd' => 0, + 'his_bs' => 0, + 'bal_bd' => 0, + 'bal_bs' => 0, + 'id' => $obj->getId(), + 'account' => $obj->getName(), + 'type' => $node->getType(), + 'isObject' => true, + 'code' => $obj->getCode(), + 'name' => $obj->getName(), + 'hasChild' => false, + 'upperID' => $node->getId() + ]; + 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; + } + if ($node->getType() == 'person') { + $res['name'] = $obj->getNikename(); + $res['account'] = $obj->getNikename(); + } + 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); + } private function hasChild(HesabdariTable $table, array $acc = []): bool { @@ -156,93 +390,4 @@ final class ExploreAccountsController extends AbstractController ], $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); - } } diff --git a/hesabixCore/src/Controller/ReportController.php b/hesabixCore/src/Controller/ReportController.php index 56416bf..03a40a1 100644 --- a/hesabixCore/src/Controller/ReportController.php +++ b/hesabixCore/src/Controller/ReportController.php @@ -285,451 +285,4 @@ class ReportController extends AbstractController } return $this->json($response); } - - #[Route('/api/report/acc/explore_accounts', name: 'app_report_acc_explore_accounts')] - public function app_report_acc_explore_accounts(Provider $provider, Jdate $jdate, Access $access, Request $request, EntityManagerInterface $entityManagerInterface): JsonResponse - { - $acc = $access->hasRole('report'); - if (!$acc) { - throw $this->createAccessDeniedException(); - } - - $params = []; - if ($content = $request->getContent()) { - $params = json_decode($content, true); - } - if (!array_key_exists('node', $params)) - throw $this->createNotFoundException(); - if ($params['node'] == 'root') { - $rootNode = $entityManagerInterface->getRepository(HesabdariTable::class)->findOneBy(['upper' => null]); - } else { - $rootNode = $entityManagerInterface->getRepository(HesabdariTable::class)->find($params['node']); - } - if (!$rootNode) - throw $this->createNotFoundException(); - - - if ($params['node'] == 'root') { - $tableItems = $entityManagerInterface->getRepository(HesabdariTable::class)->findBy([ - 'upper' => 1 - ]); - } else { - if ($rootNode->getType() == 'calc') { - $tableItems = $entityManagerInterface->getRepository(HesabdariTable::class)->findBy([ - 'upper' => $rootNode - ]); - } elseif ($rootNode->getType() == 'bank') { - $tableItems = $entityManagerInterface->getRepository(BankAccount::class)->findBy([ - 'bid' => $acc['bid'], - 'money' => $acc['money'], - ]); - } elseif ($rootNode->getType() == 'cashdesk') { - $tableItems = $entityManagerInterface->getRepository(Cashdesk::class)->findBy([ - 'bid' => $acc['bid'], - 'money' => $acc['money'], - ]); - } elseif ($rootNode->getType() == 'salary') { - $tableItems = $entityManagerInterface->getRepository(Salary::class)->findBy([ - 'bid' => $acc['bid'], - 'money' => $acc['money'], - ]); - } elseif ($rootNode->getType() == 'person') { - $tableItems = $entityManagerInterface->getRepository(Person::class)->findBy([ - 'bid' => $acc['bid'], - ]); - } - } - $response = []; - if ($rootNode->getType() == 'calc') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getName(), - 'type' => $item->getType(), - 'code' => $item->getCode(), - ]; - $childs = $entityManagerInterface->getRepository(HesabdariTable::class)->findBy([ - 'upper' => $item - ]); - $temp['hasChild'] = false; - if (count($childs) > 0 || $item->getType() != 'calc') { - $temp['hasChild'] = true; - } - $temp = array_merge($temp, $this->getBalaceTree($acc, $item)); - $response[] = $temp; - } - } elseif ($rootNode->getType() == 'bank') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getName(), - 'type' => 'bank', - 'code' => $item->getCode(), - ]; - $temp = array_merge($temp, $this->getBalance($acc, $item->getCode(), $rootNode->getType())); - $temp['hasChild'] = false; - $response[] = $temp; - } - } elseif ($rootNode->getType() == 'cashdesk') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getName(), - 'type' => 'cashdesk', - 'code' => $item->getCode(), - ]; - $temp = array_merge($temp, $this->getBalance($acc, $item->getCode(), $rootNode->getType())); - $temp['hasChild'] = false; - $response[] = $temp; - } - } elseif ($rootNode->getType() == 'salary') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getName(), - 'type' => 'salary', - 'code' => $item->getCode(), - ]; - $temp = array_merge($temp, $this->getBalance($acc, $item->getCode(), $rootNode->getType())); - $temp['hasChild'] = false; - $response[] = $temp; - } - } elseif ($rootNode->getType() == 'person') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getNikename(), - 'type' => 'person', - 'code' => $item->getCode(), - ]; - $temp = array_merge($temp, $this->getBalance($acc, $item->getCode(), $rootNode->getType())); - $temp['hasChild'] = false; - $response[] = $temp; - } - } elseif ($rootNode->getType() == 'commodity') { - foreach ($tableItems as $item) { - $temp = [ - 'id' => $item->getId(), - 'account' => $item->getName(), - 'type' => 'commodity', - 'code' => $item->getCode(), - ]; - $temp = array_merge($temp, $this->getBalance($acc, $item->getCode(), $rootNode->getType())); - $temp['hasChild'] = false; - $response[] = $temp; - } - } - $data = []; - $data['itemData'] = $response; - $data['tree'] = $this->getTree($rootNode); - - return $this->json($data); - } - - 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); - } - private function getBalance($acc, $code, $type = 'calc'): array - { - $res = [ - 'bal_bd' => 0, - 'bal_bs' => 0, - 'his_bd' => 0, - 'his_bs' => 0, - ]; - if ($type == 'calc') { - $calc = $this->em->getRepository(HesabdariTable::class)->findOneBy([ - 'code' => $code, - ]); - $faltItemsArray = $this->provider->tree2flat($calc); - - //var_dump($faltItemsArray); - $bs = 0; - $bd = 0; - - foreach ($faltItemsArray as $item) { - if ($item['type'] == 'commodity') { - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'commodity' => $item['id'] - ]); - foreach ($items as $objItem) { - $bs += $objItem->getBs(); - $bd += $objItem->getBd(); - } - } elseif ($item['type'] == 'person') { - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'person' => $item['id'] - ]); - foreach ($items as $objItem) { - $bs += $objItem->getBs(); - $bd += $objItem->getBd(); - } - } elseif ($item['type'] == 'cashdesk') { - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'cashdesk' => $item['id'] - ]); - foreach ($items as $objItem) { - $bs += $objItem->getBs(); - $bd += $objItem->getBd(); - } - } elseif ($item['type'] == 'salary') { - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'salary' => $item['id'] - ]); - foreach ($items as $objItem) { - $bs += $objItem->getBs(); - $bd += $objItem->getBd(); - } - } - } - - - } elseif ($type == 'bank') { - $bank = $this->em->getRepository(BankAccount::class)->findOneBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'code' => $code, - ]); - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'bank' => $bank - ]); - } elseif ($type == 'cashdesk') { - $cashdesk = $this->em->getRepository(Cashdesk::class)->findOneBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'code' => $code, - ]); - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'cashdesk' => $cashdesk - ]); - } elseif ($type == 'salary') { - $salary = $this->em->getRepository(Salary::class)->findOneBy([ - 'money' => $acc['money'], - 'bid' => $acc['bid'], - 'code' => $code, - ]); - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'salary' => $salary - ]); - } elseif ($type == 'person') { - $person = $this->em->getRepository(Person::class)->findOneBy([ - 'bid' => $acc['bid'], - 'code' => $code, - ]); - $items = $this->em->getRepository(HesabdariRow::class)->findBy([ - 'person' => $person - ]); - } - - foreach ($items as $item) { - $res['his_bd'] += $item->getBd(); - $res['his_bs'] += $item->getBs(); - } - 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 getBalaceTree(array $acc, HesabdariTable $table): array - { - $res = [ - 'bal_bd' => 0, - 'bal_bs' => 0, - 'his_bd' => 0, - 'his_bs' => 0, - ]; - if ($this->hasChild($table)) { - foreach ($this->getChilds($table) as $child) { - if ($this->hasChild($child)) { - $temp = $this->getBalaceTree($acc, $child); - $res['his_bd'] += $temp['his_bd']; - $res['his_bs'] += $temp['his_bs']; - } else { - $temp = $this->calcBalance($acc, $child); - $res['his_bd'] += $temp['his_bd']; - $res['his_bs'] += $temp['his_bs']; - } - } - } else { - $temp = $this->calcBalance($acc, $table); - $res['his_bd'] += $temp['his_bd']; - $res['his_bs'] += $temp['his_bs']; - } - - 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 calcBalance(array $acc, HesabdariTable $table): array - { - $res = [ - 'his_bd' => 0, - 'his_bs' => 0, - ]; - - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'ref' => $table - ], $acc['money']); - - foreach ($items as $objItem) { - $res['his_bs'] += $objItem->getBs(); - $res['his_bd'] += $objItem->getBd(); - } - return $res; - } - private function hasChild(HesabdariTable $table): bool - { - if ($this->em->getRepository(HesabdariTable::class)->findOneBy(['upper' => $table])) - return true; - return false; - } - - private function getChilds(HesabdariTable $table): array - { - return $this->em->getRepository(HesabdariTable::class)->findBy(['upper' => $table]); - } - - #[Route('/api/report/acc/get_details', name: 'app_report_acc_get_details')] - public function app_report_acc_get_details(Provider $provider, Jdate $jdate, Access $access, Request $request, EntityManagerInterface $entityManagerInterface): JsonResponse - { - $acc = $access->hasRole('report'); - if (!$acc) { - throw $this->createAccessDeniedException(); - } - - $params = []; - if ($content = $request->getContent()) { - $params = json_decode($content, true); - } - if (!array_key_exists('node', $params)) - throw $this->createNotFoundException(); - if ($params['type'] == 'calc') { - $rootNode = $entityManagerInterface->getRepository(HesabdariTable::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } elseif ($params['type'] == 'bank') { - $rootNode = $entityManagerInterface->getRepository(BankAccount::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } elseif ($params['type'] == 'cashdesk') { - $rootNode = $entityManagerInterface->getRepository(Cashdesk::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } elseif ($params['type'] == 'salary') { - $rootNode = $entityManagerInterface->getRepository(Salary::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } elseif ($params['type'] == 'person') { - $rootNode = $entityManagerInterface->getRepository(Person::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } elseif ($params['type'] == 'commodity') { - $rootNode = $entityManagerInterface->getRepository(Commodity::class)->find($params['node']); - if (!$rootNode) - throw $this->createNotFoundException(); - } - $items = $this->tree2flat($rootNode, $acc); - return $this->json(Explore::ExploreHesabdariRows($items)); - } - - private function tree2flat(Person|HesabdariTable|BankAccount|Cashdesk|Salary $item, array $acc): array - { - $res = []; - if ($this->getEntityName($item) == 'App\Entity\HesabdariTable') { - if ($this->hasChild($item)) { - $temp = []; - foreach ($this->getChilds($item) as $child) { - $temp = array_merge($temp, $this->getTree($child)); - } - $res = array_merge($res, $temp); - } 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') { - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'bank' => $item - ], $acc['money']); - $res = array_merge($res, $items); - } elseif ($this->getEntityName($item) == 'App\Entity\Cashdesk') { - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'bank' => $item - ], $acc['money']); - $res = array_merge($res, $items); - } elseif ($this->getEntityName($item) == 'App\Entity\Salary') { - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'salary' => $item - ], $acc['money']); - $res = array_merge($res, $items); - } elseif ($this->getEntityName($item) == 'App\Entity\Person') { - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'person' => $item - ], $acc['money']); - $res = array_merge($res, $items); - } elseif ($this->getEntityName($item) == 'App\Entity\Commodity') { - $items = $this->em->getRepository(HesabdariRow::class)->findByJoinMoney([ - 'bid' => $acc['bid'], - 'commodity' => $item, - ], $acc['money']); - $res = array_merge($res, $items); - } - - 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; - } }