1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31:
32: class Mage_Sales_Block_Billing_Agreement_View extends Mage_Core_Block_Template
33: {
34: 35: 36: 37: 38:
39: protected $_paymentMethods = array();
40:
41: 42: 43: 44: 45:
46: protected $_billingAgreementInstance = null;
47:
48: 49: 50: 51: 52:
53: protected $_relatedOrders = null;
54:
55: 56: 57: 58: 59:
60: public function getRelatedOrders()
61: {
62: if (is_null($this->_relatedOrders)) {
63: $this->_relatedOrders = Mage::getResourceModel('sales/order_collection')
64: ->addFieldToSelect('*')
65: ->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
66: ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
67: ->addBillingAgreementsFilter($this->_billingAgreementInstance->getAgreementId())
68: ->setOrder('created_at', 'desc');
69: }
70: return $this->_relatedOrders;
71: }
72:
73: 74: 75: 76: 77: 78: 79:
80: public function getOrderItemValue(Mage_Sales_Model_Order $order, $key)
81: {
82: $escape = true;
83: switch ($key) {
84: case 'order_increment_id':
85: $value = $order->getIncrementId();
86: break;
87: case 'created_at':
88: $value = $this->helper('core')->formatDate($order->getCreatedAt(), 'short', true);
89: break;
90: case 'shipping_address':
91: $value = $order->getShippingAddress()
92: ? $this->htmlEscape($order->getShippingAddress()->getName()) : $this->__('N/A');
93: break;
94: case 'order_total':
95: $value = $order->formatPrice($order->getGrandTotal());
96: $escape = false;
97: break;
98: case 'status_label':
99: $value = $order->getStatusLabel();
100: break;
101: case 'view_url':
102: $value = $this->getUrl('*/order/view', array('order_id' => $order->getId()));
103: break;
104: default:
105: $value = ($order->getData($key)) ? $order->getData($key) : $this->__('N/A');
106: }
107: return ($escape) ? $this->escapeHtml($value) : $value;
108: }
109:
110: 111: 112: 113: 114:
115: protected function _prepareLayout()
116: {
117: if (is_null($this->_billingAgreementInstance)) {
118: $this->_billingAgreementInstance = Mage::registry('current_billing_agreement');
119: }
120: parent::_prepareLayout();
121:
122: $pager = $this->getLayout()->createBlock('page/html_pager')
123: ->setCollection($this->getRelatedOrders())->setIsOutputRequired(false);
124: $this->setChild('pager', $pager);
125: $this->getRelatedOrders()->load();
126:
127: return $this;
128: }
129:
130: 131: 132: 133: 134:
135: protected function _loadPaymentMethods()
136: {
137: if (!$this->_paymentMethods) {
138: foreach ($this->helper('payment')->getBillingAgreementMethods() as $paymentMethod) {
139: $this->_paymentMethods[$paymentMethod->getCode()] = $paymentMethod->getTitle();
140: }
141: }
142: return $this->_paymentMethods;
143: }
144:
145: 146: 147: 148: 149:
150: protected function _toHtml()
151: {
152: $this->_loadPaymentMethods();
153: $this->setBackUrl($this->getUrl('*/billing_agreement/'));
154: if ($this->_billingAgreementInstance) {
155: $this->setReferenceId($this->_billingAgreementInstance->getReferenceId());
156:
157: $this->setCanCancel($this->_billingAgreementInstance->canCancel());
158: $this->setCancelUrl(
159: $this->getUrl('*/billing_agreement/cancel', array(
160: '_current' => true,
161: 'payment_method' => $this->_billingAgreementInstance->getMethodCode()))
162: );
163:
164: $paymentMethodTitle = $this->_billingAgreementInstance->getAgreementLabel();
165: $this->setPaymentMethodTitle($paymentMethodTitle);
166:
167: $createdAt = $this->_billingAgreementInstance->getCreatedAt();
168: $updatedAt = $this->_billingAgreementInstance->getUpdatedAt();
169: $this->setAgreementCreatedAt(
170: ($createdAt) ? $this->helper('core')->formatDate($createdAt, 'short', true) : $this->__('N/A')
171: );
172: if ($updatedAt) {
173: $this->setAgreementUpdatedAt(
174: $this->helper('core')->formatDate($updatedAt, 'short', true)
175: );
176: }
177: $this->setAgreementStatus($this->_billingAgreementInstance->getStatusLabel());
178: }
179:
180: return parent::_toHtml();
181: }
182: }
183: