PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sale/handlers/paysystem/orderdocument/handler.php

https://gitlab.com/alexprowars/bitrix
PHP | 328 lines | 249 code | 33 blank | 46 comment | 13 complexity | 2bcd8dcff8cb4d734714f209e4df7082 MD5 | raw file
  1. <?php
  2. namespace Sale\Handlers\PaySystem;
  3. use Bitrix\Main\Localization\Loc;
  4. use Bitrix\Main\Request;
  5. use Bitrix\Sale;
  6. use Bitrix\Main;
  7. use Bitrix\Sale\Payment;
  8. use Bitrix\Sale\PaySystem;
  9. use Bitrix\DocumentGenerator;
  10. use Bitrix\Crm\Integration;
  11. Loc::loadMessages(__FILE__);
  12. /**
  13. * Class OrderDocumentHandler
  14. * @package Sale\Handlers\PaySystem
  15. */
  16. class OrderDocumentHandler
  17. extends PaySystem\BaseServiceHandler
  18. implements PaySystem\IDocumentGeneratePdf
  19. {
  20. /**
  21. * @return string
  22. */
  23. protected static function getDataProviderClass()
  24. {
  25. return Integration\DocumentGenerator\DataProvider\Order::class;
  26. }
  27. /**
  28. * @param Sale\Payment $payment
  29. * @param Request|null $request
  30. * @return PaySystem\ServiceResult
  31. * @throws Main\ArgumentTypeException
  32. * @throws Main\LoaderException
  33. */
  34. public function initiatePay(Sale\Payment $payment, Request $request = null)
  35. {
  36. $result = new PaySystem\ServiceResult();
  37. if (!Main\Loader::includeModule('documentgenerator')
  38. ||
  39. !Main\Loader::includeModule('crm')
  40. )
  41. {
  42. return $result;
  43. }
  44. if ($request === null)
  45. {
  46. $request = Main\Context::getCurrent()->getRequest();
  47. }
  48. $document = $this->getDocument($payment);
  49. if ($document === null)
  50. {
  51. return new PaySystem\ServiceResult();
  52. }
  53. $documentInfo = $document->getFile()->getData();
  54. $result->setData($documentInfo);
  55. $params = array_merge($documentInfo, DocumentGenerator\Model\ExternalLinkTable::getPublicUrlsByDocumentId($document->ID));
  56. if(!empty($params['hash']))
  57. {
  58. $params['isPublicMode'] = true;
  59. }
  60. if ($this->service->getField('NEW_WINDOW') === 'Y')
  61. {
  62. $params['IFRAME'] = 'Y';
  63. $params['PRINT'] = 'Y';
  64. }
  65. $params['PAYMENT_ID'] = $payment->getId();
  66. $params['PAYSYSTEM_ID'] = $this->service->getField('ID');
  67. $this->setExtraParams($params);
  68. $showTemplateResult = $this->showTemplate($payment, $this->getTemplate($request));
  69. if ($showTemplateResult->isSuccess())
  70. {
  71. $result->setTemplate($showTemplateResult->getTemplate());
  72. }
  73. else
  74. {
  75. $result->addErrors($showTemplateResult->getErrors());
  76. }
  77. return $result;
  78. }
  79. private function getTemplate(Request $request)
  80. {
  81. return $request->get('template') ?? 'template';
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function getCurrencyList()
  87. {
  88. return [];
  89. }
  90. /**
  91. * @return array
  92. * @throws Main\ArgumentException
  93. * @throws Main\LoaderException
  94. * @throws Main\ObjectPropertyException
  95. * @throws Main\SystemException
  96. */
  97. public static function getHandlerModeList()
  98. {
  99. $result = [];
  100. if (!Main\Loader::includeModule('documentgenerator')
  101. ||
  102. !Main\Loader::includeModule('crm')
  103. )
  104. {
  105. return $result;
  106. }
  107. $provider = static::getDataProviderClass();
  108. $templateList = DocumentGenerator\Model\TemplateTable::getListByClassName($provider);
  109. foreach ($templateList as $item)
  110. {
  111. $result[$item['ID']] = htmlspecialcharsbx($item['NAME']);
  112. }
  113. return $result;
  114. }
  115. /**
  116. * @param Payment $payment
  117. * @param $params
  118. * @return PaySystem\ServiceResult|mixed
  119. * @throws Main\ArgumentException
  120. * @throws Main\ObjectException
  121. * @throws Main\ObjectPropertyException
  122. * @throws Main\SystemException
  123. */
  124. public function registerCallbackOnGenerate(Payment $payment, $params)
  125. {
  126. $document = $this->getDocument($payment);
  127. if ($document === null)
  128. {
  129. return new PaySystem\ServiceResult();
  130. }
  131. $callback = [
  132. 'DOCUMENT_ID' => $document->ID,
  133. 'MODULE_ID' => $params['MODULE_ID'],
  134. 'CALLBACK_CLASS' => $params['CALLBACK_CLASS'],
  135. 'CALLBACK_METHOD' => $params['CALLBACK_METHOD'],
  136. ];
  137. Sale\DocumentGenerator\CallbackRegistry::add($callback);
  138. return new PaySystem\ServiceResult();
  139. }
  140. /**
  141. * @param Payment $payment
  142. * @return DocumentGenerator\Document|false|null
  143. * @throws Main\ArgumentException
  144. * @throws Main\ObjectPropertyException
  145. * @throws Main\SystemException
  146. */
  147. protected function getDocument(Sale\Payment $payment)
  148. {
  149. $dbRes = DocumentGenerator\Model\DocumentTable::getList([
  150. 'select' => ['ID', 'UPDATE_TIME'],
  151. 'filter' => [
  152. '=PROVIDER' => static::getDataProviderClass(),
  153. '=VALUE' => $payment->getOrderId(),
  154. '=TEMPLATE_ID' => $this->service->getField('PS_MODE'),
  155. ],
  156. 'order' => ['ID' => 'DESC'],
  157. 'limit' => 1,
  158. ]);
  159. $data = $dbRes->fetch();
  160. if ($data)
  161. {
  162. $document = DocumentGenerator\Document::loadById($data['ID']);
  163. /** @var Sale\PaymentCollection $collection */
  164. $collection = $payment->getCollection();
  165. $order = $collection->getOrder();
  166. if ($data['UPDATE_TIME'] < $order->getField('DATE_UPDATE'))
  167. {
  168. $document->update([]);
  169. }
  170. }
  171. else
  172. {
  173. if (!$this->service->getField('PS_MODE'))
  174. {
  175. return null;
  176. }
  177. $template = DocumentGenerator\Template::loadById($this->service->getField('PS_MODE'));
  178. if (!$template || $template->isDeleted())
  179. {
  180. return null;
  181. }
  182. $template->setSourceType(static::getDataProviderClass());
  183. $document = DocumentGenerator\Document::createByTemplate($template, $payment->getOrderId());
  184. $document->setValues(['DocumentTitle' => $this->getFileName($payment)]);
  185. $document->getFile();
  186. }
  187. $document->enablePublicUrl();
  188. return $document;
  189. }
  190. /**
  191. * @param Payment $payment
  192. * @return string
  193. * @throws Main\ObjectException
  194. */
  195. protected function getFileName(Payment $payment)
  196. {
  197. $today = new Main\Type\Date();
  198. return 'invoice_'.$this->getInvoiceNumber($payment).'_'.str_replace(['.', '\\', '/'], '-' ,$today->toString());
  199. }
  200. /**
  201. * @param $payment
  202. * @return mixed
  203. */
  204. protected function getInvoiceNumber(Payment $payment)
  205. {
  206. return $payment->getField('ACCOUNT_NUMBER');
  207. }
  208. /**
  209. * @param Payment $payment
  210. * @return bool|false|mixed|string|null
  211. * @throws Main\ArgumentException
  212. * @throws Main\LoaderException
  213. * @throws Main\NotImplementedException
  214. * @throws Main\ObjectPropertyException
  215. * @throws Main\SystemException
  216. */
  217. public function getContent(Payment $payment)
  218. {
  219. global $APPLICATION;
  220. $file = $this->getFile($payment);
  221. if ($file)
  222. {
  223. return $APPLICATION->GetFileContent($file['src']);
  224. }
  225. return null;
  226. }
  227. /**
  228. * @param Payment $payment
  229. * @return array|bool|false|mixed|null
  230. * @throws Main\ArgumentException
  231. * @throws Main\LoaderException
  232. * @throws Main\NotImplementedException
  233. * @throws Main\ObjectPropertyException
  234. * @throws Main\SystemException
  235. */
  236. public function getFile(Payment $payment)
  237. {
  238. if (!Main\Loader::includeModule('documentgenerator'))
  239. {
  240. return null;
  241. }
  242. $document = $this->getDocument($payment);
  243. if ($document === null)
  244. {
  245. return null;
  246. }
  247. $documentInfo = $document->getFile()->getData();
  248. if (isset($documentInfo['pdfUrl']))
  249. {
  250. $fileId = DocumentGenerator\Model\FileTable::getBFileId($document->PDF_ID);
  251. if ($fileId !== false)
  252. {
  253. $fileArray = \CFile::GetFileArray($fileId);
  254. if ($fileArray)
  255. {
  256. return $fileArray;
  257. }
  258. }
  259. }
  260. return null;
  261. }
  262. /**
  263. * @param Payment $payment
  264. * @return bool|mixed
  265. * @throws Main\ArgumentException
  266. * @throws Main\LoaderException
  267. * @throws Main\ObjectPropertyException
  268. * @throws Main\SystemException
  269. */
  270. public function isGenerated(Payment $payment)
  271. {
  272. if (!Main\Loader::includeModule('documentgenerator'))
  273. {
  274. return false;
  275. }
  276. $document = $this->getDocument($payment);
  277. if ($document === null)
  278. {
  279. return false;
  280. }
  281. $documentInfo = $document->getFile()->getData();
  282. return isset($documentInfo['pdfUrl']);
  283. }
  284. }