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