Documentation

Snippets

Don't forget to connect the module.

\Bitrix\Main\Loader::includeModule('documentgenerator');


  • Get list of providers, attached to the specific module
  • $providers = \Bitrix\DocumentGenerator\DataProviderManager::getInstance()->getList(['filter' => ['MODULE' => 'crm']]);

    Returns list of providers, filtered by module.


  • Provider initialization and getting list of its values (for debugging)
  • $manager = \Bitrix\DocumentGenerator\DataProviderManager::getInstance();
    $provider = $manager->getDataProvider(\Bitrix\DocumentGenerator\DataProvider\User::class, 1);
    if($provider)
    {
        print_r($manager->getArray($provider));
    }
    

  • Full document generation cycle
  • // $templateId - template ID
    // $invoiceId - invoice ID
    $template = \Bitrix\DocumentGenerator\Template::loadById($templateId);
    
    $template->setSourceType(\Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Invoice::class);
    $document = \Bitrix\DocumentGenerator\Document::createByTemplate($template, $invoiceId);
    
    // add / redefine field description
    $document->setFields([
        'myField' => [
            'VALUE' => 'defaultValue', // default value 
        ],
    ]);
    // specify field value
    $document->setValues([
        'myField' => 'newValue',
    ]);
    
    $result = $document->getFile();
    if($result->isSuccess())
    {
        print_r($result->getData())
    }
    else
    {
        print_r($result->getErrorMessages());
    }
    

  • Redefine preset templates

    Attention! This operation rewrites user created template.

  • $controller = new \Bitrix\DocumentGenerator\Controller\Template();
    
    // compile the filter 
    $filter = [
        'MODULE_ID' => 'crm', // module ID
        'CODE' => 'BILL_RU', // preset template ID
        'REGION' => ['US'], // country. Array may be passed
        'NAME' => 'Invoice (United States)', // preset template name
    ];
    
    // get list of templates
    $result = $controller::getDefaultTemplateList($filter);
    if($result->isSuccess())
    {
        $templates = $result->getData();
        foreach($templates as $template)
        {
            // this is how we can skip already predefined templates
            if(isset($template['ID']) && $template['ID'] > 0)
            {
                continue;
            }
            $controller->installDefaultTemplate($template);
        }
    }
    


    © «Bitrix24», 2001-2022
    Up