Merge pull request #4822 from kurund/indentation-fixes
[civicrm-core.git] / CRM / Financial / BAO / FinancialType.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
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
37class CRM_Financial_BAO_FinancialType extends CRM_Financial_DAO_FinancialType {
38
39 /**
100fef9d 40 * Static holder for the default LT
6a488035
TO
41 */
42 static $_defaultContributionType = null;
43
44 /**
100fef9d 45 * Class constructor
6a488035 46 */
00be9182 47 public function __construct( ) {
6a488035
TO
48 parent::__construct( );
49 }
50
51 /**
c490a46a 52 * Fetch object based on array of properties
6a488035
TO
53 *
54 * @param array $params (reference ) an assoc array of name/value pairs
55 * @param array $defaults (reference ) an assoc array to hold the flattened values
56 *
c490a46a 57 * @return CRM_Contribute_BAO_ContributionType object
6a488035
TO
58 * @static
59 */
00be9182 60 public static function retrieve( &$params, &$defaults ) {
6a488035
TO
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;
66 }
67 return null;
68 }
69
70 /**
100fef9d 71 * Update the is_active flag in the db
6a488035
TO
72 *
73 * @param int $id id of the database record
74 * @param boolean $is_active value we want to set the is_active field
75 *
76 * @return Object DAO object on sucess, null otherwise
77 * @static
78 */
00be9182 79 public static function setIsActive( $id, $is_active ) {
6a488035
TO
80 return CRM_Core_DAO::setFieldValue( 'CRM_Financial_DAO_FinancialType', $id, 'is_active', $is_active );
81 }
82
83 /**
100fef9d 84 * Add the financial types
6a488035
TO
85 *
86 * @param array $params reference array contains the values submitted by the form
87 * @param array $ids reference array contains the id
88 *
6a488035
TO
89 * @static
90 * @return object
91 */
00be9182 92 public static function add(&$params, &$ids = array()) {
bc2bc079 93 if(empty($params['id'])) {
94 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, false);
95 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, false);
96 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, false);
97 }
6a488035
TO
98
99 // action is taken depending upon the mode
ddaa8ef1
PN
100 $financialType = new CRM_Financial_DAO_FinancialType();
101 $financialType->copyValues($params);
a7488080 102 if (!empty($ids['financialType'])) {
6a488035
TO
103 $financialType->id = CRM_Utils_Array::value('financialType', $ids);
104 }
ddaa8ef1
PN
105 $financialType->save();
106 // CRM-12470
bc2bc079 107 if (empty($ids['financialType']) && empty($params['id'])) {
ddaa8ef1
PN
108 $titles = CRM_Financial_BAO_FinancialTypeAccount::createDefaultFinancialAccounts($financialType);
109 $financialType->titles = $titles;
110 }
6a488035
TO
111 return $financialType;
112 }
113
114 /**
100fef9d 115 * Delete financial Types
6a488035 116 *
c490a46a 117 * @param int $financialTypeId
77b97be7
EM
118 *
119 * @return array|bool
6a488035
TO
120 * @static
121 */
00be9182 122 public static function del($financialTypeId) {
71e5aa5c
ARW
123 $financialType = new CRM_Financial_DAO_FinancialType( );
124 $financialType->id = $financialTypeId;
125 $financialType->find(true);
cf1d2db7
PN
126 // tables to ingore checks for financial_type_id
127 $ignoreTables = array('CRM_Financial_DAO_EntityFinancialAccount');
8ef12e64 128
71e5aa5c 129 //TODO: if (!$financialType->find(true)) {
6a488035 130
71e5aa5c
ARW
131 // ensure that we have no objects that have an FK to this financial type id TODO: that cannot be null
132 $occurrences = $financialType->findReferences();
133 if ($occurrences) {
134 $tables = array();
135 foreach ($occurrences as $occurence) {
cf1d2db7
PN
136 $className = get_class($occurence);
137 if (!in_array($className, $ignoreTables)) {
138 $tables[] = $className;
139 }
71e5aa5c 140 }
cf1d2db7
PN
141 if (!empty($tables)) {
142 $message = ts('The following tables have an entry for this financial type: %1', array('%1' => implode(', ', $tables)));
6a488035 143
cf1d2db7
PN
144 $errors = array();
145 $errors['is_error'] = 1;
146 $errors['error_message'] = $message;
147 return $errors;
148 }
6a488035
TO
149 }
150
151 //delete from financial Type table
6a488035
TO
152 $financialType->delete();
153
154 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount( );
155 $entityFinancialType->entity_id = $financialTypeId;
156 $entityFinancialType->entity_table = 'civicrm_financial_type';
71e5aa5c 157 $entityFinancialType->delete();
6a488035
TO
158 return FALSE;
159 }
8ef12e64 160
6a488035 161 /**
100fef9d 162 * To fetch financial type having relationship as Income Account is
6a488035
TO
163 *
164 *
165 * @return array all financial type with income account is relationship
166 * @static
167 */
00be9182 168 public static function getIncomeFinancialType() {
6a488035
TO
169 // Financial Type
170 $financialType = CRM_Contribute_PseudoConstant::financialType();
171 $revenueFinancialType = array();
f743a6eb 172 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
8ef12e64 173 CRM_Core_PseudoConstant::populate(
6a488035
TO
174 $revenueFinancialType,
175 'CRM_Financial_DAO_EntityFinancialAccount',
8ef12e64 176 $all = True,
177 $retrieve = 'entity_id',
178 $filter = null,
179 "account_relationship = $relationTypeId AND entity_table = 'civicrm_financial_type' "
6a488035 180 );
8ef12e64 181
6a488035
TO
182 foreach ($financialType as $key => $financialTypeName) {
183 if (!in_array($key, $revenueFinancialType)) {
184 unset($financialType[$key]);
8ef12e64 185 }
6a488035
TO
186 }
187 return $financialType;
188 }
189}