Guest User

Генератор документов - изменить список товаров (версия 2)

a guest
Jan 29th, 2020
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $templateId = 2;
  2. $value = 1;
  3.  
  4. \Bitrix\Main\Loader::includeModule('crm');
  5. \Bitrix\Main\Loader::includeModule('documentgenerator');
  6.  
  7. /**
  8.  * Returns true if this product should not be added to the document
  9.  *
  10.  * @param \Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Product $product
  11.  * @return bool
  12.  */
  13. function isSkipProduct(\Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Product $product)
  14. {
  15.     return true;
  16.     $typePropertyId = 500;
  17.     $restrictedType = 100;
  18.     return $product->getValue('PROPERTY_'.$typePropertyId) === $restrictedType;
  19. }
  20.  
  21. /**
  22.  * @param \Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Product $product
  23.  * @return \Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Product
  24.  */
  25. function modifyProduct(\Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Product $product)
  26. {
  27.     $reflection = new ReflectionObject($product);
  28.     $data = $reflection->getProperty('data');
  29.     $data->setAccessible(true);
  30.     $dataValue = $data->getValue($product);
  31.     $dataValue['NAME'] = 'Test Name';
  32.     $data->setValue($product, $dataValue);
  33.  
  34.     return $product;
  35. }
  36.  
  37. function modifyProducts(\Bitrix\DocumentGenerator\DataProvider\ArrayDataProvider $productsProvider, array $products)
  38. {
  39.     $reflection = new ReflectionObject($productsProvider);
  40.     $data = $reflection->getProperty('data');
  41.     $data->setAccessible(true);
  42.     $data->setValue($productsProvider, $products);
  43. }
  44.  
  45. $template = \Bitrix\DocumentGenerator\Template::loadById($templateId);
  46. if(!$template)
  47. {
  48.     die('template not found');
  49. }
  50.  
  51. $template->setSourceType(\Bitrix\Crm\Integration\DocumentGenerator\DataProvider\Deal::class);
  52.  
  53. $document = \Bitrix\DocumentGenerator\Document::createByTemplate($template, $value);
  54. if(!$document)
  55. {
  56.     die('could not create document');
  57. }
  58.  
  59. \Bitrix\Main\EventManager::getInstance()->addEventHandler(
  60.     \Bitrix\DocumentGenerator\Driver::MODULE_ID,
  61.     'onBeforeProcessDocument',
  62.     function(\Bitrix\Main\Event $event)
  63.     {
  64.         $productsPlaceholder = 'PRODUCTS';
  65.  
  66.         /** @var \Bitrix\DocumentGenerator\Document $document */
  67.         $document = $event->getParameter('document');
  68.         $provider = $document->getProvider();
  69.  
  70.         $productsProvider = \Bitrix\DocumentGenerator\DataProviderManager::getInstance()->getDataProviderValue($provider, $productsPlaceholder);
  71.         if($productsProvider instanceof \Bitrix\DocumentGenerator\DataProvider\ArrayDataProvider)
  72.         {
  73.             $products = [];
  74.             foreach($productsProvider as $product)
  75.             {
  76.                 if(!isSkipProduct($product))
  77.                 {
  78.                     $products[] = modifyProduct($product);
  79.                 }
  80.             }
  81.  
  82.             modifyProducts($productsProvider, $products);
  83.  
  84.             $productsFieldDescription = $provider->getFields()[$productsPlaceholder];
  85.  
  86.             $document->setValues([$productsPlaceholder => $products]);
  87.             $document->setFields([$productsPlaceholder => $productsFieldDescription]);
  88.         }
  89.     }
  90. );
  91.  
  92. $result = $document->getFile();
  93.  
  94. if($result->isSuccess())
  95. {
  96.     print_r($result->getData());
  97. }
  98. else
  99. {
  100.     print_r($result->getErrorMessages());
  101. }
RAW Paste Data