Merge pull request #17583 from civicrm/5.27
[civicrm-core.git] / CRM / Financial / Page / FinancialTypeAccount.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Page for displaying list of financial type accounts
20 */
21class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page {
22 /**
fe482240 23 * The action links that we need to display for the browse screen.
6a488035
TO
24 *
25 * @var array
6a488035 26 */
7b966967 27 public static $_links = NULL;
6a488035
TO
28
29 /**
fe482240 30 * The account id that we need to display for the browse screen.
6a488035
TO
31 *
32 * @var array
6a488035 33 */
045f52a3 34 protected $_aid = NULL;
6a488035
TO
35
36 /**
fe482240 37 * Get BAO Name.
6a488035 38 *
a6c01b45
CW
39 * @return string
40 * Classname of BAO.
6a488035 41 */
00be9182 42 public function getBAOName() {
6a488035
TO
43 return 'CRM_Financial_BAO_FinancialTypeAccount';
44 }
45
46 /**
fe482240 47 * Get action Links.
6a488035 48 *
a6c01b45
CW
49 * @return array
50 * (reference) of action links
6a488035 51 */
00be9182 52 public function &links() {
6a488035 53 if (!(self::$_links)) {
be2fb01f
CW
54 self::$_links = [
55 CRM_Core_Action::UPDATE => [
7d289724
PN
56 'name' => ts('Edit'),
57 'url' => 'civicrm/admin/financial/financialType/accounts',
58 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1',
6a488035 59 'title' => ts('Edit Financial Type Account'),
be2fb01f
CW
60 ],
61 CRM_Core_Action::DELETE => [
7d289724
PN
62 'name' => ts('Delete'),
63 'url' => 'civicrm/admin/financial/financialType/accounts',
64 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%',
6a488035 65 'title' => ts('Delete Financial Type Account'),
be2fb01f
CW
66 ],
67 ];
6a488035
TO
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.
6a488035 78 */
00be9182 79 public function run() {
6a488035 80 // get the requested action
7b966967
SL
81 // default to 'browse'
82 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
6a488035
TO
83
84 // assign vars to templates
85 $this->assign('action', $action);
045f52a3
TO
86 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
87 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0);
6a488035
TO
88
89 // what action to take ?
90 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
045f52a3 91 $this->edit($action, $id);
6a488035
TO
92 }
93 else {
94 $this->browse($action, $id);
95 }
96
97 // parent run
98 return parent::run();
99 }
100
101 /**
fe482240 102 * Browse all Financial Type Account data.
6a488035 103 */
00be9182 104 public function browse() {
6a488035 105 // get all Financial Type Account data sorted by weight
be2fb01f
CW
106 $financialType = [];
107 $params = [];
6a488035
TO
108 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
109 $params['entity_id'] = $this->_aid;
110 $params['entity_table'] = 'civicrm_financial_type';
111 if ($this->_aid) {
7d289724 112 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
6a488035 113 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name');
86bfa4f6 114 CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Assigned Financial Accounts'));
7611ae71
AS
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');
6a488035
TO
117 $dao->copyValues($params);
118 $dao->find();
119 while ($dao->fetch()) {
be2fb01f 120 $financialType[$dao->id] = [];
7d289724 121 CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]);
6a488035 122
be2fb01f
CW
123 $params = ['id' => $dao->financial_account_id];
124 $defaults = [];
6a488035
TO
125 $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
126 if (!empty($financialAccount)) {
045f52a3 127 $financialType[$dao->id]['financial_account'] = $financialAccount->name;
6a488035
TO
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';
9c1bc317 136 $financialType[$dao->id]['financial_account_type'] = $financialAccountType[$financialAccount->financial_account_type_id] ?? NULL;
03e04002 137
6a488035
TO
138 }
139 if (!empty($dao->account_relationship)) {
140 $optionGroupName = 'account_relationship';
9c1bc317 141 $financialType[$dao->id]['account_relationship'] = $accountRelationship[$dao->account_relationship] ?? NULL;
6a488035
TO
142 }
143 }
6a488035
TO
144 // form all action links
145 $action = array_sum(array_keys($this->links()));
7d289724 146 $links = self::links();
8ef12e64 147
cded2ebf 148 // CRM-12492
7d289724
PN
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,
be2fb01f 153 [
6a488035 154 'id' => $dao->id,
045f52a3 155 'aid' => $dao->entity_id,
be2fb01f 156 ],
87dab4a4
AH
157 ts('more'),
158 FALSE,
159 'financialTypeAccount.manage.action',
160 'FinancialTypeAccount',
161 $dao->id
6a488035
TO
162 );
163 }
164 $this->assign('rows', $financialType);
481a74f4 165 $this->assign('aid', $this->_aid);
6a488035
TO
166 $this->assign('financialTypeTitle', $this->_title);
167 }
168 else {
79e11805 169 CRM_Core_Error::statusBounce('No Financial Accounts found for the Financial Type');
6a488035
TO
170 }
171 }
172
173 /**
100fef9d 174 * Edit CiviCRM Financial Type Account data.
6a488035
TO
175 *
176 * editing would involved modifying existing financial Account Type + adding data
177 * to new financial Account Type.
178 *
ed5dd492
TO
179 * @param string $action
180 * The action to be invoked.
6a488035 181 */
045f52a3 182 public function edit($action) {
6a488035 183 // create a simple controller for editing CiviCRM Profile data
481a74f4 184 $controller = new CRM_Core_Controller_Simple('CRM_Financial_Form_FinancialTypeAccount', ts('Financial Account Types'), $action);
6a488035
TO
185
186 // set the userContext stack
187 $session = CRM_Core_Session::singleton();
481a74f4
TO
188 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
189 'reset=1&action=browse&aid=' . $this->_aid));
190 $controller->set('aid', $this->_aid);
6a488035 191
481a74f4 192 $controller->setEmbedded(TRUE);
6a488035
TO
193 $controller->process();
194 $controller->run();
195 }
96025800 196
6a488035 197}