From 758111de7661a7e7c6c252079cdab45bea541389 Mon Sep 17 00:00:00 2001 From: Gloomy Date: Tue, 19 Aug 2025 16:37:49 +0000 Subject: [PATCH] update for Moadian plugin --- .../Plugins/TaxSettingsController.php | 115 +++++++++--------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/hesabixCore/src/Controller/Plugins/TaxSettingsController.php b/hesabixCore/src/Controller/Plugins/TaxSettingsController.php index e533de7..62a094e 100644 --- a/hesabixCore/src/Controller/Plugins/TaxSettingsController.php +++ b/hesabixCore/src/Controller/Plugins/TaxSettingsController.php @@ -23,6 +23,7 @@ use App\Entity\HesabdariDoc; use App\Entity\PluginTaxInvoice; use App\Dto\TaxSettingsDto; use Symfony\Component\Validator\Validator\ValidatorInterface; +use App\Entity\User; use DateTime; class TaxSettingsController extends AbstractController @@ -34,6 +35,45 @@ class TaxSettingsController extends AbstractController return $sandboxMode ? 'https://sandboxrc.tax.gov.ir/' : 'https://tp.tax.gov.ir/'; } + private function getTaxSettings(EntityManagerInterface $em, int $businessId, User $user): array + { + $perm = $em->getRepository(Permission::class)->findOneBy([ + 'bid' => $businessId, + 'user' => $user + ]); + + $business = $em->getRepository(Business::class)->find($businessId); + + if ($business->getOwner() == $user) { + $repo = $em->getRepository(PluginTaxsettingsKey::class); + $entity = $repo->findOneBy(['business_id' => $businessId]); + + $settings = [ + 'taxMemoryId' => $entity ? $entity->getTaxMemoryId() : '', + 'economicCode' => $entity ? $entity->getEconomicCode() : '', + 'privateKey' => $entity ? $entity->getPrivateKey() : '', + ]; + } else { + if (!$perm || !$perm->isPlugTaxSettings()) { + return [ + 'success' => false, + 'message' => 'شما دسترسی لازم را ندارید.' + ]; + } + + $repo = $em->getRepository(PluginTaxsettingsKey::class); + $entity = $repo->findOneBy(['business_id' => $businessId]); + + $settings = [ + 'taxMemoryId' => $entity ? $entity->getTaxMemoryId() : '', + 'economicCode' => $entity ? $entity->getEconomicCode() : '', + 'privateKey' => $entity ? $entity->getPrivateKey() : '', + ]; + } + + return $settings; + } + #[Route('/api/plugins/tax/settings/get', name: 'plugin_tax_settings_get', methods: ['GET'])] public function plugin_tax_settings_get(EntityManagerInterface $em, Access $access): JsonResponse { @@ -45,40 +85,7 @@ class TaxSettingsController extends AbstractController $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); - $perm = $em->getRepository(Permission::class)->findOneBy([ - 'bid' => $businessId, - 'user' => $user - ]); - - $business = $em->getRepository(Business::class)->find($businessId); - - if ($business->getOwner() == $user) { - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $entity = $repo->findOneBy(['business_id' => $businessId]); - - $settings = [ - 'taxMemoryId' => $entity ? $entity->getTaxMemoryId() : '', - 'economicCode' => $entity ? $entity->getEconomicCode() : '', - 'privateKey' => $entity ? $entity->getPrivateKey() : '', - ]; - } else { - if (!$perm || !$perm->isPlugTaxSettings()) { - return $this->json([ - 'success' => false, - 'message' => 'شما دسترسی لازم را ندارید.' - ]); - } - - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $entity = $repo->findOneBy(['business_id' => $businessId]); - - $settings = [ - 'taxMemoryId' => $entity ? $entity->getTaxMemoryId() : '', - 'economicCode' => $entity ? $entity->getEconomicCode() : '', - 'privateKey' => $entity ? $entity->getPrivateKey() : '', - ]; - } - + $settings = $this->getTaxSettings($em, $businessId, $user); return $this->json($settings); } @@ -777,7 +784,6 @@ class TaxSettingsController extends AbstractController $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); - $userId = $user instanceof \App\Entity\User ? $user->getId() : null; $taxInvoiceRepo = $em->getRepository(PluginTaxInvoice::class); $taxInvoice = $taxInvoiceRepo->findOneBy([ @@ -801,10 +807,9 @@ class TaxSettingsController extends AbstractController ]); } - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $taxSettings = $repo->findOneBy(['business_id' => $businessId, 'user_id' => $userId]); + $taxSettings = $this->getTaxSettings($em, $businessId, $user); - if (!$taxSettings || !$taxSettings->getPrivateKey() || !$taxSettings->getTaxMemoryId()) { + if (!$taxSettings['privateKey'] || !$taxSettings['taxMemoryId']) { return $this->json([ 'success' => false, 'message' => 'تنظیمات مالیاتی تکمیل نشده است. لطفاً ابتدا تنظیمات را تکمیل کنید.' @@ -812,8 +817,8 @@ class TaxSettingsController extends AbstractController } try { - $username = $taxSettings->getTaxMemoryId(); - $privateKey = $taxSettings->getPrivateKey(); + $username = $taxSettings['taxMemoryId']; + $privateKey = $taxSettings['privateKey']; if (!$username || !$privateKey) { return $this->json([ @@ -872,7 +877,7 @@ class TaxSettingsController extends AbstractController throw new \Exception($validationResult['message']); } - $invoiceDto = $this->buildInvoiceDto($invoice, $moadian, $taxSettings->getEconomicCode()); + $invoiceDto = $this->buildInvoiceDto($invoice, $moadian, $taxSettings['economicCode']); if (!$invoiceDto) { throw new \Exception('خطا در آماده‌سازی فاکتور: خطا در ساخت DTO فاکتور'); } @@ -1004,12 +1009,10 @@ class TaxSettingsController extends AbstractController $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); - $userId = $user instanceof \App\Entity\User ? $user->getId() : null; - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $taxSettings = $repo->findOneBy(['business_id' => $businessId, 'user_id' => $userId]); + $taxSettings = $this->getTaxSettings($em, $businessId, $user); - if (!$taxSettings || !$taxSettings->getPrivateKey() || !$taxSettings->getTaxMemoryId()) { + if (!$taxSettings['privateKey'] || !$taxSettings['taxMemoryId']) { return $this->json([ 'success' => false, 'message' => 'تنظیمات مالیاتی تکمیل نشده است. لطفاً ابتدا تنظیمات را تکمیل کنید.' @@ -1017,8 +1020,8 @@ class TaxSettingsController extends AbstractController } try { - $username = $taxSettings->getTaxMemoryId(); - $privateKey = $taxSettings->getPrivateKey(); + $username = $taxSettings['taxMemoryId']; + $privateKey = $taxSettings['privateKey']; $taxOrgPublicKey = ''; $taxOrgKeyId = ''; @@ -1359,12 +1362,10 @@ class TaxSettingsController extends AbstractController $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); - $userId = $user instanceof \App\Entity\User ? $user->getId() : null; - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $taxSettings = $repo->findOneBy(['business_id' => $businessId, 'user_id' => $userId]); + $taxSettings = $this->getTaxSettings($em, $businessId, $user); - if (!$taxSettings || !$taxSettings->getPrivateKey() || !$taxSettings->getTaxMemoryId()) { + if (!$taxSettings['privateKey'] || !$taxSettings['taxMemoryId']) { return $this->json([ 'success' => false, 'message' => 'تنظیمات مالیاتی تکمیل نشده است. لطفاً ابتدا تنظیمات را تکمیل کنید.' @@ -1372,8 +1373,8 @@ class TaxSettingsController extends AbstractController } try { - $username = $taxSettings->getTaxMemoryId(); - $privateKey = $taxSettings->getPrivateKey(); + $username = $taxSettings['taxMemoryId']; + $privateKey = $taxSettings['privateKey']; if (!$username || !$privateKey) { return $this->json([ @@ -1482,7 +1483,7 @@ class TaxSettingsController extends AbstractController continue; } - $invoiceDto = $this->buildInvoiceDto($invoice, $moadian, $taxSettings->getEconomicCode()); + $invoiceDto = $this->buildInvoiceDto($invoice, $moadian, $taxSettings['economicCode']); if (!$invoiceDto) { $results[] = [ 'id' => $id, @@ -1581,12 +1582,10 @@ class TaxSettingsController extends AbstractController $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); - $userId = $user instanceof \App\Entity\User ? $user->getId() : null; - $repo = $em->getRepository(PluginTaxsettingsKey::class); - $taxSettings = $repo->findOneBy(['business_id' => $businessId, 'user_id' => $userId]); + $taxSettings = $this->getTaxSettings($em, $businessId, $user); - if (!$taxSettings || !$taxSettings->getPrivateKey() || !$taxSettings->getTaxMemoryId()) { + if (!$taxSettings['privateKey'] || !$taxSettings['taxMemoryId']) { return $this->json([ 'success' => false, 'message' => 'تنظیمات مالیاتی تکمیل نشده است. لطفاً ابتدا تنظیمات را تکمیل کنید.'