Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-19532: Custom document types cannot be used in the flow builder #2260

Open
daniel-memo-ict opened this issue Jan 10, 2022 · 5 comments
Open
Labels

Comments

@daniel-memo-ict
Copy link

PHP Version

7.4

Shopware Version

6.4.6.1

Expected behaviour

A document should be generated using our custom document type

Actual behaviour

No document is being generated because the flow builder action only supports the default Shopware document types.

How to reproduce

  1. Create a custom document type
  2. Use the custom document type in the generate document action of a supported flow builder trigger.
  3. Trigger the chosen, well, trigger.
  4. Check if a document using the custom type has been generated
@dominikmank
Copy link
Contributor

dominikmank commented Jan 11, 2022

well, that's because shopware only cares about it's own document types.
you can extend the behaviour. You can use a FlowAction and listen to 'action.generate.document'.
We use this to generate the custom document for a flow and its working.

I can't say if it's a bug that the document is not generating or that the custom type is visible in the flow.
I think that the generating needs to be done by you since shopware (perhaps) does not know all your variables needed for the document.

<?php
declare(strict_types=1);

namespace SomeNamespace\Flow\Dispatching\Action;

use SomeNamespace\ProformaInvoiceGenerator;
use Shopware\Core\Checkout\Document\DocumentConfigurationFactory;
use Shopware\Core\Checkout\Document\DocumentService;
use Shopware\Core\Checkout\Document\FileGenerator\FileTypes;
use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Event\SalesChannelAware;
use Shopware\Core\System\NumberRange\ValueGenerator\NumberRangeValueGeneratorInterface;

class GenerateDocument extends FlowAction
{
    private NumberRangeValueGeneratorInterface $valueGenerator;
    private DocumentService $documentService;

    public function __construct(
        DocumentService $documentService,
        NumberRangeValueGeneratorInterface $valueGenerator
    ) {
        $this->valueGenerator = $valueGenerator;
        $this->documentService = $documentService;
    }

    public static function getName(): string
    {
        return 'action.generate.document';
    }

    public static function getSubscribedEvents(): array
    {
        return [
            self::getName() => 'handle',
        ];
    }

    public function requirements(): array
    {
        return [OrderAware::class];
    }

    public function handle(FlowEvent $event): void
    {
        $baseEvent = $event->getEvent();
        if (!$baseEvent instanceof OrderAware || !$baseEvent instanceof SalesChannelAware) {
            return;
        }

        $eventConfig = $event->getConfig();
        $documentType = $eventConfig['documentType'];
        $documentRangerType = $eventConfig['documentRangerType'];

        if ($documentType !== ProformaInvoiceGenerator::PROFORMA || !$documentRangerType) {
            return;
        }

        $documentNumber = $this->valueGenerator->getValue(
            $eventConfig['documentRangerType'],
            $baseEvent->getContext(),
            $baseEvent->getSalesChannelId()
        );

        $now = (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT);
        $eventConfig['documentNumber'] = $documentNumber;
        $eventConfig['documentDate'] = $now;

        $eventConfig['custom'] = ['invoiceNumber' => $documentNumber];

        $documentConfig = DocumentConfigurationFactory::createConfiguration($eventConfig);

        $this->documentService->create(
            $baseEvent->getOrderId(),
            $documentType,
            $eventConfig['fileType'] ?? FileTypes::PDF,
            $documentConfig,
            $baseEvent->getContext(),
            null,
            $eventConfig['static'] ?? false
        );
    }
}

@daniel-memo-ict
Copy link
Author

Hmm, I guess this is the way to go then. Sadly, the documentation does not mention anything about having to do this, even though every custom document type you create automatically appears in the 'Generate Document' action of the flow builder.

Perhaps we then need an addition to these flow actions that determines which document types it can handle, similar to the requirements method. If there's no flow action for a certain document type, it cannot be selected in the flow builder.

@dominikmank
Copy link
Contributor

dominikmank commented Jan 20, 2022

yes, i think so too.
currently there is also a bug that the number generator get called for your custom type twice (when you use the code from me before). That's because the code by shopware always runs it before it checks if the document type can be processed. (https://github.com/shopware/platform/blob/trunk/src/Core/Content/Flow/Dispatching/Action/GenerateDocumentAction.php#L109-L127) So i would prefer a solution as you say. (no i'm not a shopware employee :D)

@daniel-memo-ict
Copy link
Author

Hey @dominikmank, it's been a year and Shopware has made some changes in the latest versions.

It's now possible to specify multiple document types in a single flow action, but this means Shopware will also generate a document for our custom type, after we've already generated a document ourselves. Have you found a solution to this yet?

@dominikmank
Copy link
Contributor

hey @daniel-memo-ict , thanks for notifying me, but tbh, i don't know. I didn't searched for a solution and currently i don't have an explicit use case, but if you find something i would be pleased to know the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants