1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Sales
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Customer account billing agreements block
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: class Mage_Sales_Block_Billing_Agreements extends Mage_Core_Block_Template
33: {
34: /**
35: * Payment methods array
36: *
37: * @var array
38: */
39: protected $_paymentMethods = array();
40:
41: /**
42: * Billing agreements collection
43: *
44: * @var Mage_Sales_Model_Mysql4_Billing_Agreement_Collection
45: */
46: protected $_billingAgreements = null;
47:
48: /**
49: * Set Billing Agreement instance
50: *
51: * @return Mage_Core_Block_Abstract
52: */
53: protected function _prepareLayout()
54: {
55: parent::_prepareLayout();
56: $pager = $this->getLayout()->createBlock('page/html_pager')
57: ->setCollection($this->getBillingAgreements())->setIsOutputRequired(false);
58: $this->setChild('pager', $pager)
59: ->setBackUrl($this->getUrl('customer/account/'));
60: $this->getBillingAgreements()->load();
61: return $this;
62: }
63:
64: /**
65: * Retrieve billing agreements collection
66: *
67: * @return Mage_Sales_Model_Mysql4_Billing_Agreement_Collection
68: */
69: public function getBillingAgreements()
70: {
71: if (is_null($this->_billingAgreements)) {
72: $this->_billingAgreements = Mage::getResourceModel('sales/billing_agreement_collection')
73: ->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomerId())
74: ->setOrder('agreement_id', 'desc');
75: }
76: return $this->_billingAgreements;
77: }
78:
79: /**
80: * Retrieve item value by key
81: *
82: * @param Varien_Object $item
83: * @param string $key
84: * @return mixed
85: */
86: public function getItemValue(Mage_Sales_Model_Billing_Agreement $item, $key)
87: {
88: switch ($key) {
89: case 'created_at':
90: case 'updated_at':
91: $value = ($item->getData($key))
92: ? $this->helper('core')->formatDate($item->getData($key), 'short', true) : $this->__('N/A');
93: break;
94: case 'edit_url':
95: $value = $this->getUrl('*/billing_agreement/view', array('agreement' => $item->getAgreementId()));
96: break;
97: case 'payment_method_label':
98: $label = $item->getAgreementLabel();
99: $value = ($label) ? $label : $this->__('N/A');
100: break;
101: case 'status':
102: $value = $item->getStatusLabel();
103: break;
104: default:
105: $value = ($item->getData($key)) ? $item->getData($key) : $this->__('N/A');
106: }
107: return $this->escapeHtml($value);
108: }
109:
110: /**
111: * Load available billing agreement methods
112: *
113: * @return array
114: */
115: protected function _loadPaymentMethods()
116: {
117: if (!$this->_paymentMethods) {
118: foreach ($this->helper('payment')->getBillingAgreementMethods() as $paymentMethod) {
119: $this->_paymentMethods[$paymentMethod->getCode()] = $paymentMethod->getTitle();
120: }
121: }
122: return $this->_paymentMethods;
123: }
124:
125: /**
126: * Retrieve wizard payment options array
127: *
128: * @return array
129: */
130: public function getWizardPaymentMethodOptions()
131: {
132: $paymentMethodOptions = array();
133: foreach ($this->helper('payment')->getBillingAgreementMethods() as $paymentMethod) {
134: if ($paymentMethod->getConfigData('allow_billing_agreement_wizard') == 1) {
135: $paymentMethodOptions[$paymentMethod->getCode()] = $paymentMethod->getTitle();
136: }
137: }
138: return $paymentMethodOptions;
139: }
140:
141: /**
142: * Set data to block
143: *
144: * @return string
145: */
146: protected function _toHtml()
147: {
148: $this->setCreateUrl($this->getUrl('*/billing_agreement/startWizard'));
149: return parent::_toHtml();
150: }
151: }
152: