notice fix
[civicrm-core.git] / CRM / Financial / Form / FinancialAccount.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37 /**
38 * This class generates form components for Financial Account
39 *
40 */
41 class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form {
42
43 /**
44 * Function to build the form
45 *
46 * @return None
47 * @access public
48 */
49 public function buildQuickForm( ) {
50 parent::buildQuickForm( );
51 $dataURL = CRM_Utils_System::url( 'civicrm/ajax/rest',
52 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&org=1', false, null, false );
53 $this->assign('dataURL', $dataURL);
54
55 if ($this->_action & CRM_Core_Action::DELETE) {
56 return;
57 }
58
59 $this->applyFilter('__ALL__', 'trim');
60 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount');
61 $this->add('text', 'name', ts('Name'), $attributes['name'],true);
62 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),
63 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id));
64
65 $this->add('text', 'description', ts('Description'), $attributes['description']);
66 $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']);
67 $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']);
68 $this->add('text', 'contact_name', ts('Owner'), $attributes['name']);
69 $this->add('hidden', 'contact_id', '', array('id' => 'contact_id'));
70 $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']);
71 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'));
72 $this->add('checkbox', 'is_active', ts('Enabled?'));
73 $this->add('checkbox', 'is_tax', ts('Is Tax?'));
74 $this->add('checkbox', 'is_default', ts('Default?'));
75
76 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
77 if (!empty($financialAccountType)) {
78 $this->add('select', 'financial_account_type_id', ts('Financial Account Type'),
79 array('' => '- select -') + $financialAccountType, true);
80 }
81
82 if ($this->_action == CRM_Core_Action::UPDATE &&
83 CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $this->_id, 'is_reserved')) {
84 $this->freeze(array('name', 'description', 'is_active'));
85 }
86 $this->addFormRule(array('CRM_Financial_Form_FinancialAccount', 'formRule'), $this);
87 }
88
89 /**
90 * global validation rules for the form
91 *
92 * @param array $fields posted values of the form
93 *
94 * @return array list of errors to be posted back to the form
95 * @static
96 * @access public
97 */
98 static function formRule( $values, $files, $self ) {
99 $errorMsg = array( );
100 if (!empty( $values['tax_rate'])) {
101 if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) {
102 $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100');
103 }
104 }
105 return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg;
106 }
107
108 /**
109 * This function sets the default values for the form.
110 * the default values are retrieved from the database
111 *
112 * @access public
113 *
114 * @return None
115 */
116 function setDefaultValues() {
117 $defaults = parent::setDefaultValues();
118 if ($this->_action & CRM_Core_Action::ADD) {
119 $defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
120 $defaults['contact_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $defaults['contact_id'], 'sort_name');
121 }
122 return $defaults;
123 }
124
125 /**
126 * Function to process the form
127 *
128 * @access public
129 * @return None
130 */
131 public function postProcess() {
132 if ($this->_action & CRM_Core_Action::DELETE) {
133 CRM_Financial_BAO_FinancialAccount::del($this->_id);
134 CRM_Core_Session::setStatus( ts('Selected Financial Account has been deleted.') );
135 }
136 else {
137 $ids = array( );
138 // store the submitted values in an array
139 $params = $this->exportValues();
140
141 if ($this->_action & CRM_Core_Action::UPDATE) {
142 $ids['contributionType'] = $this->_id;
143 }
144
145 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
146 CRM_Core_Session::setStatus(ts('The Financial Account \'%1\' has been saved.', array(1 => $contributionType->name)));
147 }
148 }
149 }
150
151