(NFC) Update CRM/Cxn CRM/Dashlet CRM/Export CRM/Extension and CRM/Financial files...
[civicrm-core.git] / CRM / Financial / Page / FinancialTypeAccount.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * Page for displaying list of financial type accounts
36 */
37class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page {
38 /**
fe482240 39 * The action links that we need to display for the browse screen.
6a488035
TO
40 *
41 * @var array
6a488035 42 */
7b966967 43 public static $_links = NULL;
6a488035
TO
44
45 /**
fe482240 46 * The account id that we need to display for the browse screen.
6a488035
TO
47 *
48 * @var array
6a488035 49 */
045f52a3 50 protected $_aid = NULL;
6a488035
TO
51
52 /**
fe482240 53 * Get BAO Name.
6a488035 54 *
a6c01b45
CW
55 * @return string
56 * Classname of BAO.
6a488035 57 */
00be9182 58 public function getBAOName() {
6a488035
TO
59 return 'CRM_Financial_BAO_FinancialTypeAccount';
60 }
61
62 /**
fe482240 63 * Get action Links.
6a488035 64 *
a6c01b45
CW
65 * @return array
66 * (reference) of action links
6a488035 67 */
00be9182 68 public function &links() {
6a488035 69 if (!(self::$_links)) {
be2fb01f
CW
70 self::$_links = [
71 CRM_Core_Action::UPDATE => [
7d289724
PN
72 'name' => ts('Edit'),
73 'url' => 'civicrm/admin/financial/financialType/accounts',
74 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1',
6a488035 75 'title' => ts('Edit Financial Type Account'),
be2fb01f
CW
76 ],
77 CRM_Core_Action::DELETE => [
7d289724
PN
78 'name' => ts('Delete'),
79 'url' => 'civicrm/admin/financial/financialType/accounts',
80 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%',
6a488035 81 'title' => ts('Delete Financial Type Account'),
be2fb01f
CW
82 ],
83 ];
6a488035
TO
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.
6a488035 94 */
00be9182 95 public function run() {
6a488035 96 // get the requested action
7b966967
SL
97 // default to 'browse'
98 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
6a488035
TO
99
100 // assign vars to templates
101 $this->assign('action', $action);
045f52a3
TO
102 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
103 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0);
6a488035
TO
104
105 // what action to take ?
106 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
045f52a3 107 $this->edit($action, $id);
6a488035
TO
108 }
109 else {
110 $this->browse($action, $id);
111 }
112
113 // parent run
114 return parent::run();
115 }
116
117 /**
fe482240 118 * Browse all Financial Type Account data.
6a488035 119 */
00be9182 120 public function browse() {
6a488035 121 // get all Financial Type Account data sorted by weight
be2fb01f
CW
122 $financialType = [];
123 $params = [];
6a488035
TO
124 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
125 $params['entity_id'] = $this->_aid;
126 $params['entity_table'] = 'civicrm_financial_type';
127 if ($this->_aid) {
7d289724 128 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
6a488035 129 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name');
86bfa4f6 130 CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Assigned Financial Accounts'));
7611ae71
AS
131 $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id');
132 $accountRelationship = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
6a488035
TO
133 $dao->copyValues($params);
134 $dao->find();
135 while ($dao->fetch()) {
be2fb01f 136 $financialType[$dao->id] = [];
7d289724 137 CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]);
6a488035 138
be2fb01f
CW
139 $params = ['id' => $dao->financial_account_id];
140 $defaults = [];
6a488035
TO
141 $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
142 if (!empty($financialAccount)) {
045f52a3 143 $financialType[$dao->id]['financial_account'] = $financialAccount->name;
6a488035
TO
144 $financialType[$dao->id]['accounting_code'] = $financialAccount->accounting_code;
145 $financialType[$dao->id]['account_type_code'] = $financialAccount->account_type_code;
146 $financialType[$dao->id]['is_active'] = $financialAccount->is_active;
147 if (!empty($financialAccount->contact_id)) {
148 $financialType[$dao->id]['owned_by'] = CRM_Contact_BAO_Contact::displayName($financialAccount->contact_id);
149 }
150 if (!empty($financialAccount->financial_account_type_id)) {
151 $optionGroupName = 'financial_account_type';
152 $financialType[$dao->id]['financial_account_type'] = CRM_Utils_Array::value($financialAccount->financial_account_type_id, $financialAccountType);
03e04002 153
6a488035
TO
154 }
155 if (!empty($dao->account_relationship)) {
156 $optionGroupName = 'account_relationship';
157 $financialType[$dao->id]['account_relationship'] = CRM_Utils_Array::value($dao->account_relationship, $accountRelationship);
158 }
159 }
6a488035
TO
160 // form all action links
161 $action = array_sum(array_keys($this->links()));
7d289724 162 $links = self::links();
8ef12e64 163
cded2ebf 164 // CRM-12492
7d289724
PN
165 if ($dao->account_relationship == $relationTypeId) {
166 unset($links[CRM_Core_Action::DELETE]);
167 }
168 $financialType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
be2fb01f 169 [
6a488035 170 'id' => $dao->id,
045f52a3 171 'aid' => $dao->entity_id,
be2fb01f 172 ],
87dab4a4
AH
173 ts('more'),
174 FALSE,
175 'financialTypeAccount.manage.action',
176 'FinancialTypeAccount',
177 $dao->id
6a488035
TO
178 );
179 }
180 $this->assign('rows', $financialType);
481a74f4 181 $this->assign('aid', $this->_aid);
6a488035
TO
182 $this->assign('financialTypeTitle', $this->_title);
183 }
184 else {
481a74f4 185 CRM_Core_Error::fatal();
045f52a3 186 return NULL;
6a488035
TO
187 }
188 }
189
190 /**
100fef9d 191 * Edit CiviCRM Financial Type Account data.
6a488035
TO
192 *
193 * editing would involved modifying existing financial Account Type + adding data
194 * to new financial Account Type.
195 *
ed5dd492
TO
196 * @param string $action
197 * The action to be invoked.
6a488035 198 */
045f52a3 199 public function edit($action) {
6a488035 200 // create a simple controller for editing CiviCRM Profile data
481a74f4 201 $controller = new CRM_Core_Controller_Simple('CRM_Financial_Form_FinancialTypeAccount', ts('Financial Account Types'), $action);
6a488035
TO
202
203 // set the userContext stack
204 $session = CRM_Core_Session::singleton();
481a74f4
TO
205 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts',
206 'reset=1&action=browse&aid=' . $this->_aid));
207 $controller->set('aid', $this->_aid);
6a488035 208
481a74f4 209 $controller->setEmbedded(TRUE);
6a488035
TO
210 $controller->process();
211 $controller->run();
212 }
96025800 213
6a488035 214}