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