Merge pull request #9888 from eileenmcnaughton/cust_another
[civicrm-core.git] / CRM / Financial / Page / FinancialTypeAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * Page for displaying list of financial type accounts
36 */
37 class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page {
38 /**
39 * The action links that we need to display for the browse screen.
40 *
41 * @var array
42 */
43 static $_links = NULL;
44
45 /**
46 * The account id that we need to display for the browse screen.
47 *
48 * @var array
49 */
50 protected $_aid = NULL;
51
52 /**
53 * Get BAO Name.
54 *
55 * @return string
56 * Classname of BAO.
57 */
58 public function getBAOName() {
59 return 'CRM_Financial_BAO_FinancialTypeAccount';
60 }
61
62 /**
63 * Get action Links.
64 *
65 * @return array
66 * (reference) of action links
67 */
68 public function &links() {
69 if (!(self::$_links)) {
70 self::$_links = array(
71 CRM_Core_Action::UPDATE => array(
72 'name' => ts('Edit'),
73 'url' => 'civicrm/admin/financial/financialType/accounts',
74 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1',
75 'title' => ts('Edit Financial Type Account'),
76 ),
77 CRM_Core_Action::DELETE => array(
78 'name' => ts('Delete'),
79 'url' => 'civicrm/admin/financial/financialType/accounts',
80 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%',
81 'title' => ts('Delete Financial Type Account'),
82 ),
83 );
84 }
85 return self::$_links;
86 }
87
88 /**
89 * Run the page.
90 *
91 * This method is called after the page is created. It checks for the
92 * type of action and executes that action.
93 * Finally it calls the parent's run method.
94 */
95 public function run() {
96 // get the requested action
97 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); // default to 'browse'
98
99 // assign vars to templates
100 $this->assign('action', $action);
101 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
102 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0);
103
104 // what action to take ?
105 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
106 $this->edit($action, $id);
107 }
108 else {
109 $this->browse($action, $id);
110 }
111
112 // parent run
113 return parent::run();
114 }
115
116 /**
117 * Browse all Financial Type Account data.
118 */
119 public function browse() {
120 // get all Financial Type Account data sorted by weight
121 $financialType = array();
122 $params = array();
123 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
124 $params['entity_id'] = $this->_aid;
125 $params['entity_table'] = 'civicrm_financial_type';
126 if ($this->_aid) {
127 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
128 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name');
129 CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Assigned Financial Accounts'));
130 $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
131 $accountRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
132 $dao->copyValues($params);
133 $dao->find();
134 while ($dao->fetch()) {
135 $financialType[$dao->id] = array();
136 CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]);
137
138 $params = array('id' => $dao->financial_account_id);
139 $defaults = array();
140 $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
141 if (!empty($financialAccount)) {
142 $financialType[$dao->id]['financial_account'] = $financialAccount->name;
143 $financialType[$dao->id]['accounting_code'] = $financialAccount->accounting_code;
144 $financialType[$dao->id]['account_type_code'] = $financialAccount->account_type_code;
145 $financialType[$dao->id]['is_active'] = $financialAccount->is_active;
146 if (!empty($financialAccount->contact_id)) {
147 $financialType[$dao->id]['owned_by'] = CRM_Contact_BAO_Contact::displayName($financialAccount->contact_id);
148 }
149 if (!empty($financialAccount->financial_account_type_id)) {
150 $optionGroupName = 'financial_account_type';
151 $financialType[$dao->id]['financial_account_type'] = CRM_Utils_Array::value($financialAccount->financial_account_type_id, $financialAccountType);
152
153 }
154 if (!empty($dao->account_relationship)) {
155 $optionGroupName = 'account_relationship';
156 $financialType[$dao->id]['account_relationship'] = CRM_Utils_Array::value($dao->account_relationship, $accountRelationship);
157 }
158 }
159 // form all action links
160 $action = array_sum(array_keys($this->links()));
161 $links = self::links();
162
163 // CRM-12492
164 if ($dao->account_relationship == $relationTypeId) {
165 unset($links[CRM_Core_Action::DELETE]);
166 }
167 $financialType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
168 array(
169 'id' => $dao->id,
170 'aid' => $dao->entity_id,
171 ),
172 ts('more'),
173 FALSE,
174 'financialTypeAccount.manage.action',
175 'FinancialTypeAccount',
176 $dao->id
177 );
178 }
179 $this->assign('rows', $financialType);
180 $this->assign('aid', $this->_aid);
181 $this->assign('financialTypeTitle', $this->_title);
182 }
183 else {
184 CRM_Core_Error::fatal();
185 return NULL;
186 }
187 }
188
189 /**
190 * Edit CiviCRM Financial Type Account data.
191 *
192 * editing would involved modifying existing financial Account Type + adding data
193 * to new financial Account Type.
194 *
195 * @param string $action
196 * The action to be invoked.
197 */
198 public function edit($action) {
199 // create a simple controller for editing CiviCRM Profile data
200 $controller = new CRM_Core_Controller_Simple('CRM_Financial_Form_FinancialTypeAccount', ts('Financial Account Types'), $action);
201
202 // set the userContext stack
203 $session = CRM_Core_Session::singleton();
204 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
205 'reset=1&action=browse&aid=' . $this->_aid));
206 $controller->set('aid', $this->_aid);
207
208 $controller->setEmbedded(TRUE);
209 $controller->process();
210 $controller->run();
211 }
212
213 }