Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 | 2 | /* |
bc77d7c0 TO |
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 | +--------------------------------------------------------------------+ | |
006389de | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class generates form components for Financial Type | |
6a488035 | 20 | */ |
448ef7f3 | 21 | class CRM_Financial_Form_FinancialType extends CRM_Core_Form { |
6a488035 | 22 | |
251808cb PN |
23 | use CRM_Core_Form_EntityFormTrait; |
24 | ||
448ef7f3 | 25 | protected $_BAOName = 'CRM_Financial_BAO_FinancialType'; |
26 | ||
251808cb PN |
27 | /** |
28 | * Fields for the entity to be assigned to the template. | |
29 | * | |
30 | * @var array | |
31 | */ | |
32 | protected $entityFields = []; | |
33 | ||
34 | /** | |
35 | * Deletion message to be assigned to the form. | |
36 | * | |
37 | * @var string | |
38 | */ | |
39 | protected $deleteMessage; | |
40 | ||
1ac87087 PN |
41 | /** |
42 | * Set variables up before form is built. | |
beb414cc | 43 | * |
44 | * @throws \CRM_Core_Exception | |
1ac87087 PN |
45 | */ |
46 | public function preProcess() { | |
47 | // Check permission for Financial Type when ACL-FT is enabled | |
48 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() | |
49 | && !CRM_Core_Permission::check('administer CiviCRM Financial Types') | |
50 | ) { | |
beb414cc | 51 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
1ac87087 | 52 | } |
251808cb | 53 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
1ac87087 | 54 | parent::preProcess(); |
251808cb PN |
55 | $this->setPageTitle(ts('Financial Type')); |
56 | if ($this->_id) { | |
57 | $this->_title = CRM_Core_PseudoConstant::getLabel( | |
58 | 'CRM_Financial_BAO_FinancialType', | |
59 | 'financial_type', | |
60 | $this->_id | |
61 | ); | |
62 | $this->assign('aid', $this->_id); | |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
67 | * Set entity fields to be assigned to the form. | |
68 | */ | |
69 | protected function setEntityFields() { | |
70 | $this->entityFields = [ | |
71 | 'name' => [ | |
72 | 'name' => 'name', | |
73 | 'required' => TRUE, | |
74 | ], | |
75 | 'description' => ['name' => 'description'], | |
76 | 'is_deductible' => [ | |
77 | 'name' => 'is_deductible', | |
78 | 'description' => ts('Are contributions of this type tax-deductible?'), | |
79 | ], | |
80 | 'is_reserved' => ['name' => 'is_reserved'], | |
81 | 'is_active' => ['name' => 'is_active'], | |
82 | ]; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Explicitly declare the entity api name. | |
87 | */ | |
88 | public function getDefaultEntity() { | |
89 | return 'FinancialType'; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Set the delete message. | |
94 | * | |
95 | * We do this from the constructor in order to do a translation. | |
96 | */ | |
97 | public function setDeleteMessage() { | |
98 | $this->deleteMessage = ts('WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.') . ts('Deleting a financial type cannot be undone.') . ts('Do you want to continue?'); | |
1ac87087 PN |
99 | } |
100 | ||
6a488035 | 101 | /** |
fe482240 | 102 | * Build the form object. |
448ef7f3 | 103 | * |
104 | * @throws \CRM_Core_Exception | |
6a488035 | 105 | */ |
ddaa8ef1 | 106 | public function buildQuickForm() { |
448ef7f3 | 107 | $this->buildQuickEntityForm(); |
ddaa8ef1 | 108 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
109 | return; |
110 | } | |
251808cb PN |
111 | if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved')) { |
112 | $this->freeze(['is_active']); | |
6a488035 | 113 | } |
045f52a3 | 114 | $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists', |
251808cb | 115 | ['CRM_Financial_DAO_FinancialType', $this->_id] |
77292b55 | 116 | ); |
6a488035 TO |
117 | } |
118 | ||
119 | /** | |
fe482240 | 120 | * Process the form submission. |
448ef7f3 | 121 | * |
122 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
123 | */ |
124 | public function postProcess() { | |
125 | if ($this->_action & CRM_Core_Action::DELETE) { | |
126 | $errors = CRM_Financial_BAO_FinancialType::del($this->_id); | |
06a2050e | 127 | if (is_array($errors) && !empty($errors)) { |
43e00027 | 128 | CRM_Core_Error::statusBounce($errors['error_message'], CRM_Utils_System::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete')); |
6a488035 | 129 | } |
46526baa | 130 | CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success'); |
eea16664 | 131 | } |
ddaa8ef1 | 132 | else { |
6a488035 TO |
133 | // store the submitted values in an array |
134 | $params = $this->exportValues(); | |
251808cb PN |
135 | if ($this->_id) { |
136 | $params['id'] = $this->_id; | |
6a488035 | 137 | } |
251808cb PN |
138 | foreach ([ |
139 | 'is_active', | |
140 | 'is_reserved', | |
141 | 'is_deductible', | |
142 | ] as $field) { | |
143 | $params[$field] = CRM_Utils_Array::value($field, $params, FALSE); | |
144 | } | |
145 | $financialType = civicrm_api3('FinancialType', 'create', $params); | |
ddaa8ef1 PN |
146 | if ($this->_action & CRM_Core_Action::UPDATE) { |
147 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse'); | |
251808cb | 148 | CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', [1 => $params['name']]), ts('Saved'), 'success'); |
eea16664 | 149 | } |
ddaa8ef1 | 150 | else { |
251808cb PN |
151 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType['id']); |
152 | ||
153 | $statusArray = [ | |
154 | 1 => $params['name'], | |
155 | ]; | |
156 | $financialAccounts = civicrm_api3('EntityFinancialAccount', 'get', [ | |
448ef7f3 | 157 | 'return' => ['financial_account_id.name'], |
158 | 'entity_table' => 'civicrm_financial_type', | |
251808cb PN |
159 | 'entity_id' => $financialType['id'], |
160 | 'options' => ['sort' => "id"], | |
448ef7f3 | 161 | 'account_relationship' => ['!=' => 'Income Account is'], |
251808cb PN |
162 | ]); |
163 | if (!empty($financialAccounts['values'])) { | |
164 | foreach ($financialAccounts['values'] as $financialAccount) { | |
165 | $statusArray[] = $financialAccount['financial_account_id.name']; | |
166 | } | |
167 | $text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%1". That income account, along with standard financial accounts "%2", "%3" and "%4" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray); | |
77292b55 PN |
168 | } |
169 | else { | |
251808cb | 170 | $text = ts('Your Financial "%1" Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.', $statusArray); |
77292b55 | 171 | } |
251808cb | 172 | CRM_Core_Session::setStatus($text, ts('Saved'), 'success', ['expires' => 0]); |
6a488035 TO |
173 | } |
174 | ||
ddaa8ef1 | 175 | $session = CRM_Core_Session::singleton(); |
6a488035 TO |
176 | $session->replaceUserContext($url); |
177 | } | |
178 | } | |
e2046b33 | 179 | |
448ef7f3 | 180 | /** |
181 | * Set default values for the form. MobileProvider that in edit/view mode | |
182 | * the default values are retrieved from the database | |
183 | * | |
184 | * @return array | |
185 | */ | |
186 | public function setDefaultValues() { | |
187 | $defaults = $this->getEntityDefaults(); | |
188 | ||
189 | if ($this->_action & CRM_Core_Action::ADD) { | |
190 | $defaults['is_active'] = 1; | |
191 | } | |
192 | return $defaults; | |
193 | } | |
194 | ||
232624b1 | 195 | } |