Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Financial Account | |
03e04002 | 38 | * |
6a488035 TO |
39 | */ |
40 | class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { | |
41 | ||
7d289724 PN |
42 | /** |
43 | * Flag if its a AR account type | |
44 | * | |
45 | * @var boolean | |
46 | */ | |
47 | protected $_isARFlag = FALSE; | |
8ef12e64 | 48 | |
7d289724 PN |
49 | |
50 | /** | |
51 | * Function to set variables up before form is built | |
52 | * | |
53 | * @return void | |
54 | * @access public | |
55 | */ | |
56 | public function preProcess() { | |
57 | parent::preProcess(); | |
8ef12e64 | 58 | |
7d289724 PN |
59 | if ($this->_id) { |
60 | $params = array( | |
61 | 'id' => $this->_id, | |
62 | ); | |
63 | $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray); | |
64 | $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); | |
65 | if ($financialAccount->financial_account_type_id == array_search('Asset', $financialAccountType) | |
8ef12e64 | 66 | && strtolower($financialAccount->account_type_code) == 'ar' |
7d289724 PN |
67 | && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, array_search('Asset', $financialAccountType))) { |
68 | $this->_isARFlag = TRUE; | |
69 | if ($this->_action & CRM_Core_Action::DELETE) { | |
8ef12e64 | 70 | CRM_Core_Session::setStatus(ts("The selected financial account cannot be deleted because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance)."), |
7d289724 PN |
71 | '', 'error'); |
72 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', | |
73 | "reset=1&action=browse")); | |
74 | } | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
6a488035 TO |
79 | /** |
80 | * Function to build the form | |
81 | * | |
82 | * @return None | |
83 | * @access public | |
84 | */ | |
85 | public function buildQuickForm( ) { | |
86 | parent::buildQuickForm( ); | |
7d289724 PN |
87 | $dataURL = CRM_Utils_System::url('civicrm/ajax/rest', |
88 | 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&org=1', FALSE, NULL, FALSE); | |
6a488035 TO |
89 | $this->assign('dataURL', $dataURL); |
90 | ||
91 | if ($this->_action & CRM_Core_Action::DELETE) { | |
92 | return; | |
93 | } | |
03e04002 | 94 | |
6a488035 TO |
95 | $this->applyFilter('__ALL__', 'trim'); |
96 | $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount'); | |
7d289724 | 97 | $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE); |
6a488035 TO |
98 | $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), |
99 | 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id)); | |
03e04002 | 100 | |
6a488035 TO |
101 | $this->add('text', 'description', ts('Description'), $attributes['description']); |
102 | $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']); | |
7d289724 | 103 | $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']); |
6a488035 TO |
104 | $this->add('text', 'contact_name', ts('Owner'), $attributes['name']); |
105 | $this->add('hidden', 'contact_id', '', array('id' => 'contact_id')); | |
106 | $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']); | |
107 | $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?')); | |
ddaa8ef1 | 108 | $elementActive = $this->add('checkbox', 'is_active', ts('Enabled?')); |
6a488035 | 109 | $this->add('checkbox', 'is_tax', ts('Is Tax?')); |
d0f466d1 | 110 | |
ddaa8ef1 PN |
111 | $element = $this->add('checkbox', 'is_default', ts('Default?')); |
112 | // CRM-12470 freeze is default if is_default is set | |
113 | if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_default')) { | |
114 | $element->freeze(); | |
115 | } | |
03e04002 | 116 | |
7611ae71 | 117 | $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id'); |
6a488035 | 118 | if (!empty($financialAccountType)) { |
7d289724 PN |
119 | $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), |
120 | array('' => '- select -') + $financialAccountType, TRUE); | |
121 | if ($this->_isARFlag) { | |
122 | $element->freeze(); | |
123 | $elementAccounting->freeze(); | |
ddaa8ef1 | 124 | $elementActive->freeze(); |
7d289724 | 125 | } |
6a488035 | 126 | } |
03e04002 | 127 | |
6a488035 TO |
128 | if ($this->_action == CRM_Core_Action::UPDATE && |
129 | CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')) { | |
130 | $this->freeze(array('name', 'description', 'is_active')); | |
131 | } | |
132 | $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this); | |
133 | } | |
03e04002 | 134 | |
6a488035 TO |
135 | /** |
136 | * global validation rules for the form | |
137 | * | |
138 | * @param array $fields posted values of the form | |
139 | * | |
140 | * @return array list of errors to be posted back to the form | |
141 | * @static | |
142 | * @access public | |
143 | */ | |
144 | static function formRule( $values, $files, $self ) { | |
145 | $errorMsg = array( ); | |
146 | if (!empty( $values['tax_rate'])) { | |
147 | if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) { | |
148 | $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100'); | |
149 | } | |
150 | } | |
151 | return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg; | |
152 | } | |
03e04002 | 153 | |
6a488035 TO |
154 | /** |
155 | * This function sets the default values for the form. | |
156 | * the default values are retrieved from the database | |
157 | * | |
158 | * @access public | |
159 | * | |
160 | * @return None | |
161 | */ | |
162 | function setDefaultValues() { | |
163 | $defaults = parent::setDefaultValues(); | |
164 | if ($this->_action & CRM_Core_Action::ADD) { | |
165 | $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'); | |
166 | $defaults['contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $defaults['contact_id'], 'sort_name'); | |
167 | } | |
168 | return $defaults; | |
169 | } | |
03e04002 | 170 | |
6a488035 TO |
171 | /** |
172 | * Function to process the form | |
173 | * | |
174 | * @access public | |
175 | * @return None | |
176 | */ | |
177 | public function postProcess() { | |
178 | if ($this->_action & CRM_Core_Action::DELETE) { | |
179 | CRM_Financial_BAO_FinancialAccount::del($this->_id); | |
180 | CRM_Core_Session::setStatus( ts('Selected Financial Account has been deleted.') ); | |
181 | } | |
182 | else { | |
183 | $ids = array( ); | |
184 | // store the submitted values in an array | |
185 | $params = $this->exportValues(); | |
03e04002 | 186 | |
6a488035 TO |
187 | if ($this->_action & CRM_Core_Action::UPDATE) { |
188 | $ids['contributionType'] = $this->_id; | |
189 | } | |
03e04002 | 190 | |
6a488035 TO |
191 | $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids); |
192 | CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name))); | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 |