Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for Financial Type | |
39 | * | |
40 | */ | |
41 | class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form { | |
42 | ||
43 | /** | |
44 | * Function to build the form | |
45 | * | |
355ba699 | 46 | * @return void |
6a488035 TO |
47 | * @access public |
48 | */ | |
ddaa8ef1 PN |
49 | public function buildQuickForm() { |
50 | parent::buildQuickForm(); | |
e2046b33 | 51 | $this->setPageTitle(ts('Financial Type')); |
6a488035 | 52 | |
ddaa8ef1 PN |
53 | $this->_id = CRM_Utils_Request::retrieve('id' , 'Positive', $this); |
54 | if ($this->_id) { | |
55 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'name'); | |
6a488035 | 56 | } |
ddaa8ef1 | 57 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
58 | return; |
59 | } | |
60 | $this->applyFilter('__ALL__', 'trim'); | |
ddaa8ef1 | 61 | $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'name'), TRUE); |
6a488035 | 62 | |
ddaa8ef1 | 63 | $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'description')); |
6a488035 | 64 | |
ddaa8ef1 PN |
65 | $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_deductible')); |
66 | $this->add('checkbox', 'is_active', ts('Enabled?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_active')); | |
67 | $this->add('checkbox', 'is_reserved', ts('Reserved?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_reserved')); | |
68 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
69 | $this->assign('aid', $this->_id); | |
70 | } | |
71 | if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved','vid')) { | |
72 | $this->freeze(array('is_active')); | |
6a488035 | 73 | } |
77292b55 PN |
74 | |
75 | $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),'objectExists', | |
76 | array('CRM_Financial_DAO_FinancialType', $this->_id) | |
77 | ); | |
6a488035 TO |
78 | } |
79 | ||
80 | /** | |
81 | * Function to process the form | |
82 | * | |
83 | * @access public | |
355ba699 | 84 | * @return void |
6a488035 TO |
85 | */ |
86 | public function postProcess() { | |
87 | if ($this->_action & CRM_Core_Action::DELETE) { | |
88 | $errors = CRM_Financial_BAO_FinancialType::del($this->_id); | |
89 | if (!empty($errors)) { | |
43e00027 | 90 | CRM_Core_Error::statusBounce($errors['error_message'], CRM_Utils_System::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete')); |
6a488035 | 91 | } |
46526baa | 92 | CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success'); |
eea16664 | 93 | } |
ddaa8ef1 | 94 | else { |
6a488035 TO |
95 | $params = $ids = array( ); |
96 | // store the submitted values in an array | |
97 | $params = $this->exportValues(); | |
98 | ||
99 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
100 | $ids['financialType'] = $this->_id; | |
101 | } | |
102 | ||
103 | $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); | |
ddaa8ef1 PN |
104 | if ($this->_action & CRM_Core_Action::UPDATE) { |
105 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse'); | |
46526baa | 106 | CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', array( 1 => $financialType->name)), ts('Saved'), 'success'); |
eea16664 | 107 | } |
ddaa8ef1 PN |
108 | else { |
109 | $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType->id); | |
110 | $statusArray = array( | |
111 | 1 => $financialType->name, | |
112 | 2 => $financialType->name, | |
77292b55 PN |
113 | 3 => CRM_Utils_Array::value(0, $financialType->titles), |
114 | 4 => CRM_Utils_Array::value(1, $financialType->titles), | |
115 | 5 => CRM_Utils_Array::value(2, $financialType->titles), | |
ddaa8ef1 | 116 | ); |
77292b55 | 117 | if (empty($financialType->titles)) { |
46526baa | 118 | $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 PN |
119 | } |
120 | else { | |
46526baa | 121 | $text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%2". That income account, along with standard financial accounts "%3", "%4" and "%5" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray); |
77292b55 | 122 | } |
46526baa | 123 | CRM_Core_Session::setStatus($text, ts('Saved'), 'success', array('expires' => 0)); |
6a488035 TO |
124 | } |
125 | ||
ddaa8ef1 | 126 | $session = CRM_Core_Session::singleton(); |
6a488035 TO |
127 | $session->replaceUserContext($url); |
128 | } | |
129 | } | |
e2046b33 | 130 | |
232624b1 | 131 | } |