3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Financial_BAO_FinancialType
extends CRM_Financial_DAO_FinancialType
{
38 * Static holder for the default LT
40 static $_defaultContributionType = NULL;
45 public function __construct() {
46 parent
::__construct();
50 * Fetch object based on array of properties
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
57 * @return CRM_Contribute_BAO_ContributionType
60 public static function retrieve(&$params, &$defaults) {
61 $financialType = new CRM_Financial_DAO_FinancialType();
62 $financialType->copyValues($params);
63 if ($financialType->find(TRUE)) {
64 CRM_Core_DAO
::storeValues($financialType, $defaults);
65 return $financialType;
71 * Update the is_active flag in the db
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
79 * DAO object on sucess, null otherwise
82 public static function setIsActive($id, $is_active) {
83 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active);
87 * Add the financial types
89 * @param array $params
90 * Reference array contains the values submitted by the form.
92 * Reference array contains the id.
97 public static function add(&$params, &$ids = array()) {
98 if (empty($params['id'])) {
99 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
100 $params['is_deductible'] = CRM_Utils_Array
::value('is_deductible', $params, FALSE);
101 $params['is_reserved'] = CRM_Utils_Array
::value('is_reserved', $params, FALSE);
104 // action is taken depending upon the mode
105 $financialType = new CRM_Financial_DAO_FinancialType();
106 $financialType->copyValues($params);
107 if (!empty($ids['financialType'])) {
108 $financialType->id
= CRM_Utils_Array
::value('financialType', $ids);
110 $financialType->save();
112 if (empty($ids['financialType']) && empty($params['id'])) {
113 $titles = CRM_Financial_BAO_FinancialTypeAccount
::createDefaultFinancialAccounts($financialType);
114 $financialType->titles
= $titles;
116 return $financialType;
120 * Delete financial Types
122 * @param int $financialTypeId
127 public static function del($financialTypeId) {
128 $financialType = new CRM_Financial_DAO_FinancialType();
129 $financialType->id
= $financialTypeId;
130 $financialType->find(TRUE);
131 // tables to ingore checks for financial_type_id
132 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
134 //TODO: if (!$financialType->find(true)) {
136 // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null
137 $occurrences = $financialType->findReferences();
140 foreach ($occurrences as $occurence) {
141 $className = get_class($occurence);
142 if (!in_array($className, $ignoreTables)) {
143 $tables[] = $className;
146 if (!empty($tables)) {
147 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
150 $errors['is_error'] = 1;
151 $errors['error_message'] = $message;
156 //delete from financial Type table
157 $financialType->delete();
159 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
160 $entityFinancialType->entity_id
= $financialTypeId;
161 $entityFinancialType->entity_table
= 'civicrm_financial_type';
162 $entityFinancialType->delete();
167 * fetch financial type having relationship as Income Account is
171 * all financial type with income account is relationship
174 public static function getIncomeFinancialType() {
176 $financialType = CRM_Contribute_PseudoConstant
::financialType();
177 $revenueFinancialType = array();
178 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
179 CRM_Core_PseudoConstant
::populate(
180 $revenueFinancialType,
181 'CRM_Financial_DAO_EntityFinancialAccount',
183 $retrieve = 'entity_id',
185 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
188 foreach ($financialType as $key => $financialTypeName) {
189 if (!in_array($key, $revenueFinancialType)) {
190 unset($financialType[$key]);
193 return $financialType;