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_Adminhtml
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: * Adminhtml billing agreement controller
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: class Mage_Adminhtml_Sales_Billing_AgreementController extends Mage_Adminhtml_Controller_Action
33: {
34: /**
35: * Billing agreements
36: *
37: */
38: public function indexAction()
39: {
40: $this->_title($this->__('Sales'))
41: ->_title($this->__('Billing Agreements'));
42:
43: $this->loadLayout()
44: ->_setActiveMenu('sales/billing_agreement')
45: ->renderLayout();
46: }
47:
48: /**
49: * Ajax action for billing agreements
50: *
51: */
52: public function gridAction()
53: {
54: $this->loadLayout(false)
55: ->renderLayout();
56: }
57:
58: /**
59: * View billing agreement action
60: *
61: */
62: public function viewAction()
63: {
64: $agreementModel = $this->_initBillingAgreement();
65:
66: if ($agreementModel) {
67: $this->_title($this->__('Sales'))
68: ->_title($this->__('Billing Agreements'))
69: ->_title(sprintf("#%s", $agreementModel->getReferenceId()));
70:
71: $this->loadLayout()
72: ->_setActiveMenu('sales/billing_agreement')
73: ->renderLayout();
74: return;
75: }
76:
77: $this->_redirect('*/*/');
78: return;
79: }
80:
81: /**
82: * Related orders ajax action
83: *
84: */
85: public function ordersGridAction()
86: {
87: $this->_initBillingAgreement();
88: $this->loadLayout(false)
89: ->renderLayout();
90: }
91:
92: /**
93: * Cutomer billing agreements ajax action
94: *
95: */
96: public function customerGridAction()
97: {
98: $this->_initCustomer();
99: $this->loadLayout(false)
100: ->renderLayout();
101: }
102:
103: /**
104: * Cancel billing agreement action
105: *
106: */
107: public function cancelAction()
108: {
109: $agreementModel = $this->_initBillingAgreement();
110:
111: if ($agreementModel && $agreementModel->canCancel()) {
112: try {
113: $agreementModel->cancel();
114: $this->_getSession()->addSuccess($this->__('The billing agreement has been canceled.'));
115: $this->_redirect('*/*/view', array('_current' => true));
116: return;
117: } catch (Mage_Core_Exception $e) {
118: $this->_getSession()->addError($e->getMessage());
119: } catch (Exception $e) {
120: $this->_getSession()->addError($this->__('Failed to cancel the billing agreement.'));
121: Mage::logException($e);
122: }
123: $this->_redirect('*/*/view', array('_current' => true));
124: }
125: return $this->_redirect('*/*/');
126: }
127:
128: /**
129: * Delete billing agreement action
130: */
131: public function deleteAction()
132: {
133: $agreementModel = $this->_initBillingAgreement();
134:
135: if ($agreementModel) {
136: try {
137: $agreementModel->delete();
138: $this->_getSession()->addSuccess($this->__('The billing agreement has been deleted.'));
139: $this->_redirect('*/*/');
140: return;
141: } catch (Mage_Core_Exception $e) {
142: $this->_getSession()->addError($e->getMessage());
143: } catch (Exception $e) {
144: $this->_getSession()->addError($this->__('Failed to delete the billing agreement.'));
145: Mage::logException($e);
146: }
147: $this->_redirect('*/*/view', array('_current' => true));
148: }
149: $this->_redirect('*/*/');
150: }
151:
152: /**
153: * Initialize billing agreement by ID specified in request
154: *
155: * @return Mage_Sales_Model_Billing_Agreement | false
156: */
157: protected function _initBillingAgreement()
158: {
159: $agreementId = $this->getRequest()->getParam('agreement');
160: $agreementModel = Mage::getModel('sales/billing_agreement')->load($agreementId);
161:
162: if (!$agreementModel->getId()) {
163: $this->_getSession()->addError($this->__('Wrong billing agreement ID specified.'));
164: return false;
165: }
166:
167: Mage::register('current_billing_agreement', $agreementModel);
168: return $agreementModel;
169: }
170:
171: /**
172: * Initialize customer by ID specified in request
173: *
174: * @return Mage_Adminhtml_Sales_Billing_AgreementController
175: */
176: protected function _initCustomer()
177: {
178: $customerId = (int) $this->getRequest()->getParam('id');
179: $customer = Mage::getModel('customer/customer');
180:
181: if ($customerId) {
182: $customer->load($customerId);
183: }
184:
185: Mage::register('current_customer', $customer);
186: return $this;
187: }
188:
189: /**
190: * Retrieve adminhtml session
191: *
192: * @return Mage_Adminhtml_Model_Session
193: */
194: protected function _getSession()
195: {
196: return Mage::getSingleton('adminhtml/session');
197: }
198:
199: /**
200: * Check currently called action by permissions for current user
201: *
202: * @return bool
203: */
204: protected function _isAllowed()
205: {
206: switch ($this->getRequest()->getActionName()) {
207: case 'index':
208: case 'grid' :
209: case 'view' :
210: return Mage::getSingleton('admin/session')->isAllowed('sales/billing_agreement/actions/view');
211: break;
212: case 'cancel':
213: case 'delete':
214: return Mage::getSingleton('admin/session')->isAllowed('sales/billing_agreement/actions/manage');
215: break;
216: default:
217: return Mage::getSingleton('admin/session')->isAllowed('sales/billing_agreement');
218: break;
219: }
220: }
221: }
222: