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