Merge pull request #10405 from agh1/crm-19612
[civicrm-core.git] / CRM / Financial / Form / FinancialType.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
006389de 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
35 * This class generates form components for Financial Type
6a488035
TO
36 */
37class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
38
1ac87087
PN
39 /**
40 * Set variables up before form is built.
41 */
42 public function preProcess() {
43 // Check permission for Financial Type when ACL-FT is enabled
44 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
45 && !CRM_Core_Permission::check('administer CiviCRM Financial Types')
46 ) {
47 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
48 }
49 parent::preProcess();
50 }
51
6a488035 52 /**
fe482240 53 * Build the form object.
6a488035 54 */
ddaa8ef1
PN
55 public function buildQuickForm() {
56 parent::buildQuickForm();
e2046b33 57 $this->setPageTitle(ts('Financial Type'));
6a488035 58
045f52a3 59 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
ddaa8ef1
PN
60 if ($this->_id) {
61 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'name');
6a488035 62 }
ddaa8ef1 63 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
64 return;
65 }
66 $this->applyFilter('__ALL__', 'trim');
ddaa8ef1 67 $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'name'), TRUE);
6a488035 68
ddaa8ef1 69 $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'description'));
6a488035 70
ddaa8ef1
PN
71 $this->add('checkbox', 'is_deductible', ts('Tax-Deductible?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_deductible'));
72 $this->add('checkbox', 'is_active', ts('Enabled?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_active'));
73 $this->add('checkbox', 'is_reserved', ts('Reserved?'), CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialType', 'is_reserved'));
74 if ($this->_action == CRM_Core_Action::UPDATE) {
75 $this->assign('aid', $this->_id);
76 }
045f52a3 77 if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved', 'vid')) {
ddaa8ef1 78 $this->freeze(array('is_active'));
6a488035 79 }
d75f2f47 80
045f52a3 81 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists',
77292b55
PN
82 array('CRM_Financial_DAO_FinancialType', $this->_id)
83 );
6a488035
TO
84 }
85
86 /**
fe482240 87 * Process the form submission.
6a488035
TO
88 */
89 public function postProcess() {
90 if ($this->_action & CRM_Core_Action::DELETE) {
91 $errors = CRM_Financial_BAO_FinancialType::del($this->_id);
92 if (!empty($errors)) {
43e00027 93 CRM_Core_Error::statusBounce($errors['error_message'], CRM_Utils_System::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete'));
6a488035 94 }
46526baa 95 CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success');
eea16664 96 }
ddaa8ef1 97 else {
045f52a3 98 $params = $ids = array();
6a488035
TO
99 // store the submitted values in an array
100 $params = $this->exportValues();
101
102 if ($this->_action & CRM_Core_Action::UPDATE) {
103 $ids['financialType'] = $this->_id;
104 }
105
106 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
ddaa8ef1
PN
107 if ($this->_action & CRM_Core_Action::UPDATE) {
108 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse');
481a74f4 109 CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', array(1 => $financialType->name)), ts('Saved'), 'success');
eea16664 110 }
ddaa8ef1
PN
111 else {
112 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType->id);
113 $statusArray = array(
114 1 => $financialType->name,
115 2 => $financialType->name,
77292b55
PN
116 3 => CRM_Utils_Array::value(0, $financialType->titles),
117 4 => CRM_Utils_Array::value(1, $financialType->titles),
118 5 => CRM_Utils_Array::value(2, $financialType->titles),
ddaa8ef1 119 );
77292b55 120 if (empty($financialType->titles)) {
46526baa 121 $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
122 }
123 else {
46526baa 124 $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 125 }
46526baa 126 CRM_Core_Session::setStatus($text, ts('Saved'), 'success', array('expires' => 0));
6a488035
TO
127 }
128
ddaa8ef1 129 $session = CRM_Core_Session::singleton();
6a488035
TO
130 $session->replaceUserContext($url);
131 }
132 }
e2046b33 133
232624b1 134}