Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
7 
15 {
21  protected $_paymentMethods = [];
22 
28  protected $_billingAgreementInstance = null;
29 
35  protected $_relatedOrders = null;
36 
42  protected $_coreRegistry = null;
43 
48 
52  protected $_customerSession;
53 
57  protected $_orderConfig;
58 
62  protected $_helper;
63 
68 
79  public function __construct(
80  \Magento\Framework\View\Element\Template\Context $context,
81  \Magento\Framework\Registry $registry,
82  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
83  \Magento\Customer\Model\Session $customerSession,
84  \Magento\Sales\Model\Order\Config $orderConfig,
85  \Magento\Paypal\Helper\Data $helper,
86  \Magento\Paypal\Model\ResourceModel\Billing\Agreement $agreementResource,
87  array $data = []
88  ) {
89  $this->_helper = $helper;
90  $this->_orderCollectionFactory = $orderCollectionFactory;
91  $this->_customerSession = $customerSession;
92  $this->_orderConfig = $orderConfig;
93  $this->_coreRegistry = $registry;
94  $this->_agreementResource = $agreementResource;
95  parent::__construct($context, $data);
96  $this->_isScopePrivate = true;
97  }
98 
104  public function getRelatedOrders()
105  {
106  if ($this->_relatedOrders === null) {
108  $billingAgreementId = $billingAgreement ? $billingAgreement->getAgreementId() : 0;
109  $this->_relatedOrders = $this->_orderCollectionFactory->create()->addFieldToSelect(
110  '*'
111  )->addFieldToFilter(
112  'customer_id',
113  (int)$this->_customerSession->getCustomerId()
114  )->addFieldToFilter(
115  'status',
116  ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
117  )->setOrder(
118  'created_at',
119  'desc'
120  );
121  $this->_agreementResource->addOrdersFilter($this->_relatedOrders, $billingAgreementId);
122  }
123  return $this->_relatedOrders;
124  }
125 
134  public function getOrderItemValue(\Magento\Sales\Model\Order $order, $key)
135  {
136  $escape = true;
137  switch ($key) {
138  case 'order_increment_id':
139  $value = $order->getIncrementId();
140  break;
141  case 'created_at':
142  $value = $this->formatDate($order->getCreatedAt(), \IntlDateFormatter::SHORT, true);
143  break;
144  case 'shipping_address':
145  $value = $order->getShippingAddress() ? $this->escapeHtml(
146  $order->getShippingAddress()->getName()
147  ) : __(
148  'N/A'
149  );
150  break;
151  case 'order_total':
152  $value = $order->formatPrice($order->getGrandTotal());
153  $escape = false;
154  break;
155  case 'status_label':
156  $value = $order->getStatusLabel();
157  break;
158  case 'view_url':
159  $value = $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
160  break;
161  default:
162  $value = $order->getData($key) ? $order->getData($key) : __('N/A');
163  break;
164  }
165  return $escape ? $this->escapeHtml($value) : $value;
166  }
167 
173  protected function _prepareLayout()
174  {
175  parent::_prepareLayout();
176 
177  $pager = $this->getLayout()->createBlock(
178  \Magento\Theme\Block\Html\Pager::class
179  )->setCollection(
180  $this->getRelatedOrders()
181  )->setIsOutputRequired(
182  false
183  );
184  $this->setChild('pager', $pager);
185  $this->getRelatedOrders()->load();
186 
187  return $this;
188  }
189 
195  protected function _getBillingAgreementInstance()
196  {
197  if ($this->_billingAgreementInstance === null) {
198  $this->_billingAgreementInstance = $this->_coreRegistry->registry('current_billing_agreement');
199  }
201  }
202 
208  protected function _loadPaymentMethods()
209  {
210  if (!$this->_paymentMethods) {
211  foreach ($this->_helper->getBillingAgreementMethods() as $paymentMethod) {
212  $this->_paymentMethods[$paymentMethod->getCode()] = $paymentMethod->getTitle();
213  }
214  }
215  return $this->_paymentMethods;
216  }
217 
223  protected function _toHtml()
224  {
225  $this->_loadPaymentMethods();
226  $this->setBackUrl($this->getUrl('*/billing_agreement/'));
228  if ($billingAgreement) {
229  $this->setReferenceId($billingAgreement->getReferenceId());
230 
231  $this->setCanCancel($billingAgreement->canCancel());
232  $this->setCancelUrl(
233  $this->getUrl(
234  '*/billing_agreement/cancel',
235  ['_current' => true, 'payment_method' => $billingAgreement->getMethodCode()]
236  )
237  );
238 
239  $paymentMethodTitle = $billingAgreement->getAgreementLabel();
240  $this->setPaymentMethodTitle($paymentMethodTitle);
241 
242  $createdAt = $billingAgreement->getCreatedAt();
243  $updatedAt = $billingAgreement->getUpdatedAt();
244  $this->setAgreementCreatedAt(
245  $createdAt ? $this->formatDate($createdAt, \IntlDateFormatter::SHORT, true) : __('N/A')
246  );
247  if ($updatedAt) {
248  $this->setAgreementUpdatedAt($this->formatDate($updatedAt, \IntlDateFormatter::SHORT, true));
249  }
250  $this->setAgreementStatus($billingAgreement->getStatusLabel());
251  }
252 
253  return parent::_toHtml();
254  }
255 }
$helper
Definition: iframe.phtml:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Sales\Model\Order\Config $orderConfig, \Magento\Paypal\Helper\Data $helper, \Magento\Paypal\Model\ResourceModel\Billing\Agreement $agreementResource, array $data=[])
Definition: View.php:79
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
$billingAgreement
$order
Definition: order.php:55
__()
Definition: __.php:13
getOrderItemValue(\Magento\Sales\Model\Order $order, $key)
Definition: View.php:134
$value
Definition: gender.phtml:16