DocumentGenerateOperation example

$this->salesChannelContext->setRuleIds($ruleIds);
    }

    public function testCustomerAbleToViewUploadDocumentWithDeepLinkCode(): void
    {
        $context = Context::createDefaultContext();

        $cart = $this->generateDemoCart(2);
        $orderId = $this->persistCart($cart);
        $fileName = 'invoice';

        $operation = new DocumentGenerateOperation($orderId, FileTypes::PDF, [], null, true);

        $document = $this->getContainer()->get(DocumentGenerator::class)->generate(
            InvoiceRenderer::TYPE,
            [$operation->getOrderId() => $operation],
            $context,
        )->getSuccess()->first();

        static::assertNotNull($document);

        $expectedFileContent = 'simple invoice';
        $expectedContentType = 'text/plain; charset=UTF-8';

        
string $deepLinkCode,
        string $documentTypeName,
        Context $context
    ): Response {
        $config = $request->query->get('config');
        $config = \is_string($config) ? json_decode($config, true, 512, \JSON_THROW_ON_ERROR) : [];

        $fileType = $request->query->getAlnum('fileType', FileTypes::PDF);
        $download = $request->query->getBoolean('download');
        $referencedDocumentId = $request->query->getAlnum('referencedDocumentId');

        $operation = new DocumentGenerateOperation($orderId$fileType$config$referencedDocumentId, false, true);

        $generatedDocument = $this->documentGenerator->preview($documentTypeName$operation$deepLinkCode$context);

        return $this->createResponse(
            $generatedDocument->getName(),
            $generatedDocument->getContent(),
            $download,
            $generatedDocument->getContentType()
        );
    }

    
$ruleIds[] = $paymentRuleId;
        }
        $this->salesChannelContext->setRuleIds($ruleIds);
    }

    public function testMigrationWorks(): void
    {
        $cart = $this->generateDemoCart(2);
        $orderId = $this->persistCart($cart);

        $documentGenerator = $this->getContainer()->get(DocumentGenerator::class);
        $operation = new DocumentGenerateOperation($orderId);
        $result = $documentGenerator->generate(DeliveryNoteRenderer::TYPE, [$orderId => $operation]$this->context)->getSuccess();

        $documentStruct = $result->first();

        static::assertNotNull($documentStruct);
        static::assertTrue(Uuid::isValid($documentStruct->getId()));

        // Set Document to Live Version         $documentRepository = $this->getContainer()->get('document.repository');

        $documentRepository
            
private function ensureDocumentMediaFileGenerated(DocumentEntity $document, Context $context): DocumentEntity
    {
        $documentMediaId = $document->getDocumentMediaFileId();

        if ($documentMediaId !== null || $document->isStatic()) {
            return $document;
        }

        $documentId = $document->getId();

        $operation = new DocumentGenerateOperation(
            $document->getOrderId(),
            FileTypes::PDF,
            $document->getConfig(),
            $document->getReferencedDocumentId()
        );

        $operation->setDocumentId($documentId);

        /** @var DocumentTypeEntity $documentType */
        $documentType = $document->getDocumentType();

        
$orderRepository = $this->getContainer()->get('order.repository');

        $orderRepository->upsert([$order]$context);

        return $orderId;
    }

    private function createDocumentWithFile(string $orderId, Context $context, string $documentType = InvoiceRenderer::TYPE): string
    {
        $documentGenerator = $this->getContainer()->get(DocumentGenerator::class);

        $operation = new DocumentGenerateOperation($orderId, FileTypes::PDF, []);
        $document = $documentGenerator->generate($documentType[$orderId => $operation]$context)->getSuccess()->first();

        static::assertNotNull($document);

        return $document->getId();
    }
}
$this->flow->expects(static::exactly(2))->method('getData')->willReturn(Uuid::randomHex());
        $this->flow->expects(static::exactly(1))->method('getContext')->willReturn(Context::createDefaultContext());

        $this->flow->expects(static::once())->method('getConfig')->willReturn($config);

        $documentType = $config['documentTypes'][0]['documentType'] ?? $config['documentType'] ?? null;
        $orderId = $this->flow->getData(OrderAware::ORDER_ID);
        $fileType = $config['documentTypes'][0]['fileType'] ?? $config['fileType'] ?? FileTypes::PDF;
        $conf = $config['documentTypes'][0]['config'] ?? $config['config'] ?? [];
        $static = $config['documentTypes'][0]['static'] ?? $config['static'] ?? false;

        $operation = new DocumentGenerateOperation($orderId$fileType$conf, null, $static);

        $this->documentGenerator->expects(static::exactly($expected))
            ->method('generate')
            ->with($documentType[$orderId => $operation], Context::createDefaultContext());

        $this->action->handleFlow($this->flow);
    }

    public static function actionExecutedProvider(): \Generator
    {
        yield 'Generate invoice multi' => [
            [
static::assertInstanceOf(DocumentBaseConfigEntity::class$config);

        return $config;
    }

    /** * @param array<string, array<string, string>|string> $config */
    private function createDocument(string $documentType, string $orderId, array $config, Context $context): DocumentIdCollection
    {
        $operations = [];
        $operation = new DocumentGenerateOperation($orderId, FileTypes::PDF, $config);
        $operations[$orderId] = $operation;

        return $this->getContainer()->get(DocumentGenerator::class)->generate($documentType$operations$context)->getSuccess();
    }

    /** * @param array<string|bool, string|bool|int> $config */
    private function upsertBaseConfig(array $config, string $documentType, ?string $salesChannelId = null): void
    {
        $baseConfig = $this->getBaseConfig($documentType$salesChannelId);

        
/** * @dataProvider invoiceDataProvider * * @param array<int|string, int> $possibleTaxes */
    public function testRender(array $possibleTaxes, ?\Closure $beforeRenderHook, \Closure $assertionCallback): void
    {
        $cart = $this->generateDemoCart($possibleTaxes);
        $orderId = $this->persistCart($cart);

        $operationInvoice = new DocumentGenerateOperation($orderId);

        $caughtEvent = null;

        $this->getContainer()->get('event_dispatcher')
            ->addListener(InvoiceOrdersEvent::classfunction DInvoiceOrdersEvent $event) use (&$caughtEvent): void {
                $caughtEvent = $event;
            });

        if ($beforeRenderHook instanceof \Closure) {
            $beforeRenderHook($operationInvoice$this->getContainer());
        }

        
static::assertNull($mergeResult);
    }

    /** * @dataProvider documentMergeDataProvider */
    public function testMerge(int $numDocs, bool $static, bool $withMedia, \Closure $assertionCallback): void
    {
        $docIds = [];

        for ($i = 0; $i < $numDocs; ++$i) {
            $deliveryOperation = new DocumentGenerateOperation($this->orderId, FileTypes::PDF, [], null, $static);
            $result = $this->documentGenerator->generate(DeliveryNoteRenderer::TYPE, [$this->orderId => $deliveryOperation]$this->context)->getSuccess()->first();
            static::assertNotNull($result);
            $docIds[] = $result->getId();

            if ($static && $withMedia) {
                $staticFileContent = 'this is some content';

                $uploadFileRequest = new Request([
                    'extension' => FileTypes::PDF,
                    'fileName' => Uuid::randomHex(),
                ][][][][][
                    

    public function testRender(array $additionalConfig, \Closure $assertionCallback): void
    {
        $cart = $this->generateDemoCart([7, 31]);
        $orderId = $this->cartService->order($cart$this->salesChannelContext, new RequestDataBag());

        $invoiceConfig = new DocumentConfiguration();
        $invoiceConfig->setDocumentNumber('1001');

        $operationInvoice = new DocumentGenerateOperation($orderId, FileTypes::PDF, $invoiceConfig->jsonSerialize());

        $result = $this->documentGenerator->generate(InvoiceRenderer::TYPE, [$orderId => $operationInvoice]$this->context)->getSuccess()->first();
        static::assertNotNull($result);
        $invoiceId = $result->getId();

        $config = [
            'displayLineItems' => true,
            'itemsPerPage' => 10,
            'displayFooter' => true,
            'displayHeader' => true,
        ];

        
\Closure $errorCallback = null,
        array $additionalConfig = []
    ): void {
        $cart = $this->generateDemoCart($possibleTaxes);
        $cart = $this->generateCreditItems($cart$creditPrices);

        $orderId = $this->cartService->order($cart$this->salesChannelContext, new RequestDataBag());

        $invoiceConfig = new DocumentConfiguration();
        $invoiceConfig->setDocumentNumber('1001');

        $operationInvoice = new DocumentGenerateOperation($orderId, FileTypes::PDF, $invoiceConfig->jsonSerialize());
        $result = $this->documentGenerator->generate(InvoiceRenderer::TYPE, [$orderId => $operationInvoice]$this->context)->getSuccess()->first();
        static::assertNotNull($result);
        $invoiceId = $result->getId();

        $config = [
            'displayLineItems' => true,
            'itemsPerPage' => 10,
            'displayFooter' => true,
            'displayHeader' => true,
        ];

        
Context::createDefaultContext()
        )->firstId() ?? '';

        $cart = $this->generateDemoCart(2);
        $this->orderId = $this->persistCart($cart);

        $this->documentRepository = $this->getContainer()->get('document.repository');
    }

    public function testCreateDeliveryNotePdf(): void
    {
        $operation = new DocumentGenerateOperation($this->orderId);

        $documentStruct = $this->documentGenerator->generate(DeliveryNoteRenderer::TYPE, [$this->orderId => $operation]$this->context)->getSuccess()->first();
        static::assertNotNull($documentStruct);
        static::assertTrue(Uuid::isValid($documentStruct->getId()));

        $criteria = new Criteria([$documentStruct->getId()]);
        $criteria->addAssociation('documentType');

        /** @var DocumentEntity $document */
        $document = $this->documentRepository
            ->search($criteria$this->context)
            
new DataValidationDefinition())
                ->add('orderId', new NotBlank())
                ->add('fileType', new Choice([FileTypes::PDF]))
                ->add('config', new Type('array'))
                ->add('static', new Type('bool'))
                ->add('referencedDocumentId', new Uuid())
        );

        $this->dataValidator->validate($documents$definition);

        foreach ($documents as $operation) {
            $operations[$operation['orderId']] = new DocumentGenerateOperation(
                $operation['orderId'],
                $operation['fileType'] ?? FileTypes::PDF,
                $operation['config'] ?? [],
                $operation['referencedDocumentId'] ?? null,
                $operation['static'] ?? false
            );
        }

        return new JsonResponse($this->documentGenerator->generate($documentTypeName$operations$context));
    }

    


    /** * @dataProvider documentDownloadRouteDataProvider */
    public function testDownload(bool $isGuest, ?bool $withValidDeepLinkCode, \Closure $assertionCallback): void
    {
        $token = $this->getLoggedInContextToken($isGuest ? $this->guestId : $this->customerId, $this->ids->get('sales-channel'));

        $this->browser->setServerParameter('HTTP_SW_CONTEXT_TOKEN', $token);

        $operation = new DocumentGenerateOperation($this->ids->get('order'));
        $document = $this->documentGenerator->generate(InvoiceRenderer::TYPE, [$operation->getOrderId() => $operation], Context::createDefaultContext())->getSuccess()->first();
        static::assertInstanceOf(DocumentIdStruct::class$document);
        $deepLinkCode = '';

        if ($withValidDeepLinkCode !== null) {
            $deepLinkCode = $withValidDeepLinkCode ? $document->getDeepLinkCode() : Uuid::randomHex();
        }

        $endpoint = \sprintf('/store-api/document/download/%s', $document->getId());

        if ($deepLinkCode !== '') {
            
return $renderedDocument;
    }

    private function ensureDocumentMediaFileGenerated(DocumentEntity $document, Context $context): ?string
    {
        $documentMediaId = $document->getDocumentMediaFileId();

        if ($documentMediaId !== null || $document->isStatic()) {
            return $documentMediaId;
        }

        $operation = new DocumentGenerateOperation(
            $document->getOrderId(),
            FileTypes::PDF,
            $document->getConfig(),
            $document->getReferencedDocumentId()
        );

        $operation->setDocumentId($document->getId());

        /** @var DocumentTypeEntity $documentType */
        $documentType = $document->getDocumentType();

        
Home | Imprint | This part of the site doesn't use cookies.