View.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Billing\Agreement;
  7. /**
  8. * Customer account billing agreement view block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class View extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Payment methods array
  17. *
  18. * @var array
  19. */
  20. protected $_paymentMethods = [];
  21. /**
  22. * Billing Agreement instance
  23. *
  24. * @var \Magento\Paypal\Model\Billing\Agreement
  25. */
  26. protected $_billingAgreementInstance = null;
  27. /**
  28. * Related orders collection
  29. *
  30. * @var \Magento\Sales\Model\ResourceModel\Order\Collection
  31. */
  32. protected $_relatedOrders = null;
  33. /**
  34. * Core registry
  35. *
  36. * @var \Magento\Framework\Registry
  37. */
  38. protected $_coreRegistry = null;
  39. /**
  40. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
  41. */
  42. protected $_orderCollectionFactory;
  43. /**
  44. * @var \Magento\Customer\Model\Session
  45. */
  46. protected $_customerSession;
  47. /**
  48. * @var \Magento\Sales\Model\Order\Config
  49. */
  50. protected $_orderConfig;
  51. /**
  52. * @var \Magento\Paypal\Helper\Data
  53. */
  54. protected $_helper;
  55. /**
  56. * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement
  57. */
  58. protected $_agreementResource;
  59. /**
  60. * @param \Magento\Framework\View\Element\Template\Context $context
  61. * @param \Magento\Framework\Registry $registry
  62. * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
  63. * @param \Magento\Customer\Model\Session $customerSession
  64. * @param \Magento\Sales\Model\Order\Config $orderConfig
  65. * @param \Magento\Paypal\Helper\Data $helper
  66. * @param \Magento\Paypal\Model\ResourceModel\Billing\Agreement $agreementResource
  67. * @param array $data
  68. */
  69. public function __construct(
  70. \Magento\Framework\View\Element\Template\Context $context,
  71. \Magento\Framework\Registry $registry,
  72. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
  73. \Magento\Customer\Model\Session $customerSession,
  74. \Magento\Sales\Model\Order\Config $orderConfig,
  75. \Magento\Paypal\Helper\Data $helper,
  76. \Magento\Paypal\Model\ResourceModel\Billing\Agreement $agreementResource,
  77. array $data = []
  78. ) {
  79. $this->_helper = $helper;
  80. $this->_orderCollectionFactory = $orderCollectionFactory;
  81. $this->_customerSession = $customerSession;
  82. $this->_orderConfig = $orderConfig;
  83. $this->_coreRegistry = $registry;
  84. $this->_agreementResource = $agreementResource;
  85. parent::__construct($context, $data);
  86. $this->_isScopePrivate = true;
  87. }
  88. /**
  89. * Retrieve related orders collection
  90. *
  91. * @return \Magento\Sales\Model\ResourceModel\Order\Collection
  92. */
  93. public function getRelatedOrders()
  94. {
  95. if ($this->_relatedOrders === null) {
  96. $billingAgreement = $this->_getBillingAgreementInstance();
  97. $billingAgreementId = $billingAgreement ? $billingAgreement->getAgreementId() : 0;
  98. $this->_relatedOrders = $this->_orderCollectionFactory->create()->addFieldToSelect(
  99. '*'
  100. )->addFieldToFilter(
  101. 'customer_id',
  102. (int)$this->_customerSession->getCustomerId()
  103. )->addFieldToFilter(
  104. 'status',
  105. ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
  106. )->setOrder(
  107. 'created_at',
  108. 'desc'
  109. );
  110. $this->_agreementResource->addOrdersFilter($this->_relatedOrders, $billingAgreementId);
  111. }
  112. return $this->_relatedOrders;
  113. }
  114. /**
  115. * Retrieve order item value by key
  116. *
  117. * @param \Magento\Sales\Model\Order $order
  118. * @param string $key
  119. * @return string
  120. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  121. */
  122. public function getOrderItemValue(\Magento\Sales\Model\Order $order, $key)
  123. {
  124. $escape = true;
  125. switch ($key) {
  126. case 'order_increment_id':
  127. $value = $order->getIncrementId();
  128. break;
  129. case 'created_at':
  130. $value = $this->formatDate($order->getCreatedAt(), \IntlDateFormatter::SHORT, true);
  131. break;
  132. case 'shipping_address':
  133. $value = $order->getShippingAddress() ? $this->escapeHtml(
  134. $order->getShippingAddress()->getName()
  135. ) : __(
  136. 'N/A'
  137. );
  138. break;
  139. case 'order_total':
  140. $value = $order->formatPrice($order->getGrandTotal());
  141. $escape = false;
  142. break;
  143. case 'status_label':
  144. $value = $order->getStatusLabel();
  145. break;
  146. case 'view_url':
  147. $value = $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
  148. break;
  149. default:
  150. $value = $order->getData($key) ? $order->getData($key) : __('N/A');
  151. break;
  152. }
  153. return $escape ? $this->escapeHtml($value) : $value;
  154. }
  155. /**
  156. * Set pager
  157. *
  158. * @return $this
  159. */
  160. protected function _prepareLayout()
  161. {
  162. parent::_prepareLayout();
  163. $pager = $this->getLayout()->createBlock(
  164. \Magento\Theme\Block\Html\Pager::class
  165. )->setCollection(
  166. $this->getRelatedOrders()
  167. )->setIsOutputRequired(
  168. false
  169. );
  170. $this->setChild('pager', $pager);
  171. $this->getRelatedOrders()->load();
  172. return $this;
  173. }
  174. /**
  175. * Return current billing agreement.
  176. *
  177. * @return \Magento\Paypal\Model\Billing\Agreement|null
  178. */
  179. protected function _getBillingAgreementInstance()
  180. {
  181. if ($this->_billingAgreementInstance === null) {
  182. $this->_billingAgreementInstance = $this->_coreRegistry->registry('current_billing_agreement');
  183. }
  184. return $this->_billingAgreementInstance;
  185. }
  186. /**
  187. * Load available billing agreement methods
  188. *
  189. * @return array
  190. */
  191. protected function _loadPaymentMethods()
  192. {
  193. if (!$this->_paymentMethods) {
  194. foreach ($this->_helper->getBillingAgreementMethods() as $paymentMethod) {
  195. $this->_paymentMethods[$paymentMethod->getCode()] = $paymentMethod->getTitle();
  196. }
  197. }
  198. return $this->_paymentMethods;
  199. }
  200. /**
  201. * Set data to block
  202. *
  203. * @return string
  204. */
  205. protected function _toHtml()
  206. {
  207. $this->_loadPaymentMethods();
  208. $this->setBackUrl($this->getUrl('*/billing_agreement/'));
  209. $billingAgreement = $this->_getBillingAgreementInstance();
  210. if ($billingAgreement) {
  211. $this->setReferenceId($billingAgreement->getReferenceId());
  212. $this->setCanCancel($billingAgreement->canCancel());
  213. $this->setCancelUrl(
  214. $this->getUrl(
  215. '*/billing_agreement/cancel',
  216. ['_current' => true, 'payment_method' => $billingAgreement->getMethodCode()]
  217. )
  218. );
  219. $paymentMethodTitle = $billingAgreement->getAgreementLabel();
  220. $this->setPaymentMethodTitle($paymentMethodTitle);
  221. $createdAt = $billingAgreement->getCreatedAt();
  222. $updatedAt = $billingAgreement->getUpdatedAt();
  223. $this->setAgreementCreatedAt(
  224. $createdAt ? $this->formatDate($createdAt, \IntlDateFormatter::SHORT, true) : __('N/A')
  225. );
  226. if ($updatedAt) {
  227. $this->setAgreementUpdatedAt($this->formatDate($updatedAt, \IntlDateFormatter::SHORT, true));
  228. }
  229. $this->setAgreementStatus($billingAgreement->getStatusLabel());
  230. }
  231. return parent::_toHtml();
  232. }
  233. }