From b7ecafb3a739316cb37eb352e61c224a9077cee0 Mon Sep 17 00:00:00 2001 From: Babak Alizadeh Date: Sun, 24 Aug 2025 07:00:24 +0000 Subject: [PATCH] bug fix in sell invoice pdf export for via shortlinks --- .../Controller/Front/ShortlinksController.php | 227 ++++++++++++++++-- .../templates/pdf/printers/sell.html.twig | 2 + 2 files changed, 203 insertions(+), 26 deletions(-) diff --git a/hesabixCore/src/Controller/Front/ShortlinksController.php b/hesabixCore/src/Controller/Front/ShortlinksController.php index 02dd289..e106981 100644 --- a/hesabixCore/src/Controller/Front/ShortlinksController.php +++ b/hesabixCore/src/Controller/Front/ShortlinksController.php @@ -102,7 +102,7 @@ class ShortlinksController extends AbstractController } #[Route('/slpdf/sell/{bid}/{link}', name: 'shortlinks_pdf')] - public function shortlinks_pdf(string $bid, string $link, EntityManagerInterface $entityManager, Provider $provider): Response + public function shortlinks_pdf(string $bid, string $link, EntityManagerInterface $entityManager, Provider $provider, \App\Service\PluginService $pluginService = null, \App\Service\TemplateRenderer $renderer = null): Response { $bus = $entityManager->getRepository(Business::class)->find($bid); if (!$bus) @@ -122,6 +122,32 @@ class ShortlinksController extends AbstractController ]); if (!$doc) throw $this->createNotFoundException(); + + // تنظیم پارامترهای پیش‌فرض برای چاپ + $params = [ + 'printers' => false, + 'pdf' => true, + 'posPrint' => false + ]; + + // دریافت تنظیمات پیش‌فرض از PrintOptions + $printSettings = $entityManager->getRepository(PrintOptions::class)->findOneBy(['bid' => $bid]); + + // تنظیم مقادیر پیش‌فرض از تنظیمات ذخیره شده + $defaultOptions = [ + 'note' => $printSettings ? $printSettings->isSellNote() : true, + 'bidInfo' => $printSettings ? $printSettings->isSellBidInfo() : true, + 'taxInfo' => $printSettings ? $printSettings->isSellTaxInfo() : true, + 'discountInfo' => $printSettings ? $printSettings->isSellDiscountInfo() : true, + 'pays' => $printSettings ? $printSettings->isSellPays() : true, + 'paper' => $printSettings ? $printSettings->getSellPaper() : 'A4-L', + 'invoiceIndex' => $printSettings ? $printSettings->isSellInvoiceIndex() : true, + 'businessStamp' => $printSettings ? $printSettings->isSellBusinessStamp() : true + ]; + + // اولویت با پارامترهای ارسالی است + $printOptions = array_merge($defaultOptions, $params['printOptions'] ?? []); + $person = null; $discount = 0; $transfer = 0; @@ -134,35 +160,184 @@ class ShortlinksController extends AbstractController $transfer = $item->getBs(); } } - $printOptions = [ - 'bidInfo' => true, - 'pays' => true, - 'taxInfo' => true, - 'discountInfo' => true, - 'note' => true, - 'paper' => 'A4-L' - ]; - $note = ''; - $printSettings = $entityManager->getRepository(PrintOptions::class)->findOneBy(['bid' => $bid]); - if ($printSettings) { - $note = $printSettings->getSellNoteString(); + $pdfPid = 0; + + // فیلد جدید وضعیت حساب مشتری + $personItems = $entityManager->getRepository(HesabdariRow::class)->findBy(['bid' => $bid, 'person' => $person]); + $accountStatus = []; + $bs = 0; + $bd = 0; + foreach ($personItems as $item) { + $bs += $item->getBs(); + $bd += $item->getBd(); } - $pdfPid = $provider->createPrint( - $bid, - $bid->getOwner(), - $this->renderView('pdf/printers/sell.html.twig', [ - 'bid' => $bid, - 'doc' => $doc, - 'rows' => $doc->getHesabdariRows(), - 'person' => $person, + if ($bs > $bd) { + $accountStatus['label'] = 'بستانکار'; + $accountStatus['value'] = $bs - $bd; + } else { + $accountStatus['label'] = 'بدهکار'; + $accountStatus['value'] = $bd - $bs; + } + + $business = $entityManager->getRepository(Business::class)->find($bid); + $twoApproval = $business && method_exists($business, 'isRequireTwoStepApproval') ? (bool)$business->isRequireTwoStepApproval() : false; + if ($twoApproval && $doc->isApproved() !== true && $doc->isPreview() == true) { + return $this->render('bundles/TwigBundle/Exception/error.html.twig', [ + 'message' => 'فاکتور هنوز تایید نشده است' + ]); + } + + // پیدا کردن مالک کسب و کار + $businessOwner = $bus->getOwner(); + + if ($params['pdf'] == true || $params['printers'] == true) { + $note = ''; + if ($printSettings) { + $note = $printSettings->getSellNoteString(); + } + + // Build safe context data for rendering + $rowsArr = array_map(function ($row) { + return [ + 'commodity' => $row->getCommodity() ? [ + 'name' => method_exists($row->getCommodity(), 'getName') ? $row->getCommodity()->getName() : null, + 'code' => method_exists($row->getCommodity(), 'getCode') ? $row->getCommodity()->getCode() : null, + ] : null, + 'commodityCount' => $row->getCommdityCount(), + 'des' => $row->getDes(), + 'bs' => $row->getBs(), + 'tax' => $row->getTax(), + 'discount' => $row->getDiscount(), + 'showPercentDiscount' => $row->getDiscountType() === 'percent', + 'discountPercent' => $row->getDiscountPercent() + ]; + }, $doc->getHesabdariRows()->toArray()); + + $personArr = $person ? [ + 'name' => $person->getName(), + 'mobile' => $person->getMobile(), + 'tel' => $person->getTel(), + 'address' => $person->getAddress(), + ] : null; + + $businessArr = $bus ? [ + 'name' => method_exists($bus, 'getName') ? $bus->getName() : null, + 'nikename' => method_exists($bus, 'getNikename') ? $bus->getNikename() : null, + 'tel' => method_exists($bus, 'getTel') ? $bus->getTel() : null, + 'mobile' => method_exists($bus, 'getMobile') ? $bus->getMobile() : null, + 'address' => method_exists($bus, 'getAddress') ? $bus->getAddress() : null, + 'shenasemeli' => method_exists($bus, 'getShenasemeli') ? $bus->getShenasemeli() : null, + 'codeeghtesadi' => method_exists($bus, 'getCodeeghtesadi') ? $bus->getCodeeghtesadi() : null, + 'id' => method_exists($bus, 'getId') ? $bus->getId() : null, + ] : null; + + $context = [ + 'accountStatus' => $accountStatus, + 'business' => $businessArr, + 'bid' => $businessArr, + 'doc' => [ + 'code' => $doc->getCode(), + 'date' => method_exists($doc, 'getDate') ? $doc->getDate() : null, + 'taxPercent' => method_exists($doc, 'getTaxPercent') ? $doc->getTaxPercent() : null, + 'discountPercent' => $doc->getDiscountPercent(), + 'discountType' => $doc->getDiscountType(), + 'amount' => $doc->getAmount(), + 'money' => [ + 'shortName' => method_exists($doc, 'getMoney') && $doc->getMoney() && method_exists($doc->getMoney(), 'getShortName') ? $doc->getMoney()->getShortName() : null, + ], + ], + 'rows' => $rowsArr, + 'person' => $personArr, 'discount' => $discount, 'transfer' => $transfer, 'printOptions' => $printOptions, - 'note' => $note - ]), - false, - $printOptions['paper'] - ); + 'note' => $note, + ]; + + // Decide template: custom or default + $html = null; + + // Check if custom invoice plugin is available and active + $isCustomInvoiceActive = false; + $selectedTemplate = null; + + // Use injected services if available + if ($pluginService) { + $isCustomInvoiceActive = $pluginService->isActive('custominvoice', $bid); + $selectedTemplate = $printSettings ? $printSettings->getSellTemplate() : null; + } + + if ($isCustomInvoiceActive && $selectedTemplate && class_exists('App\Entity\CustomInvoiceTemplate') && $selectedTemplate instanceof \App\Entity\CustomInvoiceTemplate) { + if ($renderer) { + $html = $renderer->render($selectedTemplate->getCode() ?? '', $context); + } + } + + if ($html === null) { + // fallback to default Twig template + $html = $this->renderView('pdf/printers/sell.html.twig', [ + 'accountStatus' => $accountStatus, + 'bid' => $bus, + 'doc' => $doc, + 'rows' => array_map(function ($row) { + return [ + 'commodity' => $row->getCommodity(), + 'commodityCount' => $row->getCommdityCount(), + 'des' => $row->getDes(), + 'bs' => $row->getBs(), + 'tax' => $row->getTax(), + 'discount' => $row->getDiscount(), + 'showPercentDiscount' => $row->getDiscountType() === 'percent', + 'discountPercent' => $row->getDiscountPercent() + ]; + }, $doc->getHesabdariRows()->toArray()), + 'person' => $person, + 'printInvoice' => $params['printers'], + 'discount' => $discount, + 'transfer' => $transfer, + 'printOptions' => $printOptions, + 'note' => $note, + 'showPercentDiscount' => $doc->getDiscountType() === 'percent', + 'discountPercent' => $doc->getDiscountPercent() + ]); + } + + $pdfPid = $provider->createPrint( + $bus, + $businessOwner, // مالک کسب و کار + $html, + false, + $printOptions['paper'] + ); + } + + if ($params['posPrint'] == true) { + $pid = $provider->createPrint( + $bus, + $businessOwner, // مالک کسب و کار + $this->renderView('pdf/posPrinters/justSell.html.twig', [ + 'bid' => $bus, + 'doc' => $doc, + 'rows' => array_map(function ($row) { + return [ + 'commodity' => $row->getCommodity(), + 'commodityCount' => $row->getCommdityCount(), + 'des' => $row->getDes(), + 'bs' => $row->getBs(), + 'tax' => $row->getTax(), + 'discount' => $row->getDiscount(), + 'showPercentDiscount' => $row->getDiscountType() === 'percent', + 'discountPercent' => $row->getDiscountPercent() + ]; + }, $doc->getHesabdariRows()->toArray()), + 'discount' => $discount, + 'showPercentDiscount' => $doc->getDiscountType() === 'percent', + 'discountPercent' => $doc->getDiscountPercent() + ]), + false + ); + } + return $this->redirectToRoute('app_front_print', ['id' => $pdfPid]); } } diff --git a/hesabixCore/templates/pdf/printers/sell.html.twig b/hesabixCore/templates/pdf/printers/sell.html.twig index 577db88..3f1790b 100644 --- a/hesabixCore/templates/pdf/printers/sell.html.twig +++ b/hesabixCore/templates/pdf/printers/sell.html.twig @@ -163,6 +163,8 @@

تلفن / نمابر: {% if person.tel is not empty %}{{ person.tel }}{% endif %} + موبایل: + {% if person.mobile is not empty %}{{ person.mobile }}{% endif %}