Commit | Line | Data |
---|---|---|
a45a51e3 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
a45a51e3 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
a45a51e3 | 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 |
a45a51e3 | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft { | |
36 | ||
8cb2c285 | 37 | /** |
a45a51e3 | 38 | * construct method |
8cb2c285 | 39 | */ |
a45a51e3 | 40 | function __construct() { |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
91bb24a7 | 45 | * function to add contribution soft credit record |
a45a51e3 | 46 | * |
47 | * @param array $params (reference ) an assoc array of name/value pairs | |
48 | * | |
49 | * @return object soft contribution of object that is added | |
50 | * @access public | |
51 | * | |
52 | */ | |
07a88eec | 53 | public static function add(&$params) { |
a45a51e3 | 54 | $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); |
55 | $contributionSoft->copyValues($params); | |
6dcc4d9d | 56 | |
57 | // set currency for CRM-1496 | |
58 | if (!isset($contributionSoft->currency)) { | |
59 | $config = CRM_Core_Config::singleton(); | |
60 | $contributionSoft->currency = $config->defaultCurrency; | |
61 | } | |
a45a51e3 | 62 | return $contributionSoft->save(); |
63 | } | |
64 | ||
65 | /** | |
66 | * Takes a bunch of params that are needed to match certain criteria and | |
67 | * retrieves the relevant objects. Typically the valid params are only | |
68 | * contact_id. We'll tweak this function to be more full featured over a period | |
69 | * of time. This is the inverse function of create. It also stores all the retrieved | |
70 | * values in the default array | |
71 | * | |
72 | * @param array $params (reference ) an assoc array of name/value pairs | |
73 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
74 | * | |
75 | * @return object CRM_Contribute_BAO_ContributionSoft object | |
76 | * @access public | |
77 | * @static | |
78 | */ | |
79 | static function retrieve(&$params, &$defaults) { | |
80 | $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); | |
81 | $contributionSoft->copyValues($params); | |
82 | if ($contributionSoft->find(TRUE)) { | |
83 | CRM_Core_DAO::storeValues($contributionSoft, $defaults); | |
84 | return $contributionSoft; | |
85 | } | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | /** | |
8cb2c285 | 90 | * Function to delete soft credits |
a45a51e3 | 91 | * |
6c8f6e67 EM |
92 | * @param $params |
93 | * | |
94 | * @internal param int $contributionTypeId | |
a45a51e3 | 95 | * @static |
96 | */ | |
2cfc0f58 | 97 | static function del($params) { |
a45a51e3 | 98 | //delete from contribution soft table |
99 | $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); | |
2cfc0f58 | 100 | foreach($params as $column => $value) { |
101 | $contributionSoft->$column = $value; | |
102 | } | |
a45a51e3 | 103 | $contributionSoft->delete(); |
104 | } | |
6dcc4d9d | 105 | |
186c9c17 EM |
106 | /** |
107 | * @param $contact_id | |
108 | * @param int $isTest | |
109 | * | |
110 | * @return array | |
111 | */ | |
6dcc4d9d | 112 | static function getSoftContributionTotals($contact_id, $isTest = 0) { |
8cb2c285 KJ |
113 | $query = ' |
114 | SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency | |
115 | FROM civicrm_contribution_soft ccs | |
116 | LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id | |
117 | WHERE cc.is_test = %2 AND ccs.contact_id = %1 | |
118 | GROUP BY currency'; | |
6dcc4d9d | 119 | |
120 | $params = array(1 => array($contact_id, 'Integer'), | |
121 | 2 => array($isTest, 'Integer')); | |
122 | ||
123 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
124 | ||
125 | $count = 0; | |
126 | $amount = $average = array(); | |
127 | ||
128 | while ($cs->fetch()) { | |
129 | if ($cs->amount > 0) { | |
130 | $count++; | |
91bb24a7 | 131 | $amount[] = $cs->amount; |
132 | $average[] = $cs->average; | |
6dcc4d9d | 133 | $currency[] = $cs->currency; |
134 | } | |
135 | } | |
136 | ||
137 | if ($count > 0) { | |
91bb24a7 | 138 | return array( |
139 | implode(', ', $amount), | |
6dcc4d9d | 140 | implode(', ', $average), |
141 | implode(', ', $currency), | |
142 | ); | |
143 | } | |
144 | return array(0, 0); | |
145 | } | |
146 | ||
147 | /** | |
148 | * Function to retrieve soft contributions for contribution record. | |
6dcc4d9d | 149 | * |
dd244018 EM |
150 | * @param $contributionID |
151 | * @param boolean $all include PCP data | |
152 | * | |
153 | * @internal param array $params an associated array | |
154 | * @return array of soft contribution ids, amounts, and associated contact ids | |
155 | * @static | |
6dcc4d9d | 156 | */ |
7ccf8829 | 157 | static function getSoftContribution($contributionID, $all = FALSE) { |
17db9f82 KJ |
158 | $pcpFields = array( |
159 | 'pcp_id', | |
b6545333 | 160 | 'pcp_title', |
17db9f82 KJ |
161 | 'pcp_display_in_roll', |
162 | 'pcp_roll_nickname', | |
163 | 'pcp_personal_note', | |
164 | ); | |
165 | ||
7ccf8829 | 166 | $query = ' |
b6545333 | 167 | SELECT ccs.id, pcp_id, cpcp.title as pcp_title, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, ccs.currency as currency, amount, ccs.contact_id as contact_id, c.display_name, ccs.soft_credit_type_id |
7ccf8829 | 168 | FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id |
b6545333 | 169 | LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id |
7ccf8829 KJ |
170 | WHERE contribution_id = %1; |
171 | '; | |
172 | ||
173 | $params = array(1 => array($contributionID, 'Integer')); | |
174 | ||
175 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
176 | ||
177 | $softContribution = array(); | |
178 | $count = 1; | |
179 | while ($dao->fetch()) { | |
b6545333 | 180 | if ($dao->pcp_id) { |
6dcc4d9d | 181 | if ($all) { |
17db9f82 | 182 | foreach ($pcpFields as $val) { |
b6545333 | 183 | $softContribution[$val] = $dao->$val; |
6dcc4d9d | 184 | } |
b6545333 | 185 | $softContribution['pcp_soft_credit_to_name'] = $dao->display_name; |
186 | $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id; | |
6dcc4d9d | 187 | } |
b6545333 | 188 | } |
189 | else { | |
35729234 | 190 | $softContribution['soft_credit'][$count] = array( |
b6545333 | 191 | 'contact_id' => $dao->contact_id, |
192 | 'soft_credit_id' => $dao->id, | |
193 | 'currency' => $dao->currency, | |
194 | 'amount' => $dao->amount, | |
195 | 'contact_name' => $dao->display_name, | |
196 | 'soft_credit_type' => $dao->soft_credit_type_id, | |
197 | 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id) | |
35729234 KJ |
198 | ); |
199 | $count++; | |
6dcc4d9d | 200 | } |
201 | } | |
7ccf8829 | 202 | |
6dcc4d9d | 203 | return $softContribution; |
204 | } | |
205 | ||
186c9c17 EM |
206 | /** |
207 | * @param $contributionID | |
208 | * @param bool $isPCP | |
209 | * | |
210 | * @return array | |
211 | */ | |
b6545333 | 212 | static function getSoftCreditIds($contributionID , $isPCP = FALSE) { |
91bb24a7 | 213 | $query = " |
b6545333 | 214 | SELECT id |
2cfc0f58 | 215 | FROM civicrm_contribution_soft |
216 | WHERE contribution_id = %1 | |
217 | "; | |
b6545333 | 218 | |
219 | if ($isPCP) { | |
220 | $query .= " AND pcp_id IS NOT NULL"; | |
221 | } | |
1221efe9 | 222 | else { |
223 | $query .= " AND pcp_id IS NULL"; | |
224 | } | |
2cfc0f58 | 225 | $params = array(1 => array($contributionID, 'Integer')); |
226 | ||
227 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
228 | $id = array(); | |
7f880799 | 229 | $type = ''; |
2cfc0f58 | 230 | while ($dao->fetch()) { |
b6545333 | 231 | if ($isPCP) { |
232 | return $dao->id; | |
2cfc0f58 | 233 | } |
234 | $id[] = $dao->id; | |
235 | } | |
b6545333 | 236 | return $id; |
2cfc0f58 | 237 | } |
91bb24a7 | 238 | |
6dcc4d9d | 239 | /** |
91bb24a7 | 240 | * Function to retrieve the list of soft contributions for given contact. |
6dcc4d9d | 241 | * |
91ef9be0 | 242 | * @param int $contact_id contact id |
243 | * @param int $isTest | |
244 | * @param string $filter additional filter criteria, later used in where clause | |
dd244018 EM |
245 | * |
246 | * @return array | |
247 | * @static | |
6dcc4d9d | 248 | */ |
91ef9be0 | 249 | static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) { |
8cb2c285 | 250 | $query = ' |
6dcc4d9d | 251 | SELECT ccs.id, ccs.amount as amount, |
8cb2c285 KJ |
252 | ccs.contribution_id, |
253 | ccs.pcp_id, | |
254 | ccs.pcp_display_in_roll, | |
255 | ccs.pcp_roll_nickname, | |
256 | ccs.pcp_personal_note, | |
51fa20cb | 257 | ccs.soft_credit_type_id, |
8cb2c285 KJ |
258 | cc.receive_date, |
259 | cc.contact_id as contributor_id, | |
260 | cc.contribution_status_id as contribution_status_id, | |
261 | cp.title as pcp_title, | |
262 | cc.currency, | |
263 | contact.display_name, | |
264 | cct.name as contributionType | |
265 | FROM civicrm_contribution_soft ccs | |
266 | LEFT JOIN civicrm_contribution cc | |
267 | ON ccs.contribution_id = cc.id | |
268 | LEFT JOIN civicrm_pcp cp | |
269 | ON ccs.pcp_id = cp.id | |
270 | LEFT JOIN civicrm_contact contact ON | |
271 | ccs.contribution_id = cc.id AND cc.contact_id = contact.id | |
272 | LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id | |
91ef9be0 | 273 | '; |
274 | ||
275 | $where = " | |
276 | WHERE cc.is_test = %2 AND ccs.contact_id = %1"; | |
277 | if ($filter) { | |
278 | $where .= $filter; | |
279 | } | |
280 | ||
281 | $query .= "{$where} ORDER BY cc.receive_date DESC"; | |
8cb2c285 KJ |
282 | |
283 | $params = array( | |
284 | 1 => array($contact_id, 'Integer'), | |
285 | 2 => array($isTest, 'Integer') | |
286 | ); | |
287 | $cs = CRM_Core_DAO::executeQuery($query, $params); | |
6dcc4d9d | 288 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
8cb2c285 | 289 | $result = array(); |
6dcc4d9d | 290 | while ($cs->fetch()) { |
291 | $result[$cs->id]['amount'] = $cs->amount; | |
292 | $result[$cs->id]['currency'] = $cs->currency; | |
293 | $result[$cs->id]['contributor_id'] = $cs->contributor_id; | |
294 | $result[$cs->id]['contribution_id'] = $cs->contribution_id; | |
295 | $result[$cs->id]['contributor_name'] = $cs->display_name; | |
296 | $result[$cs->id]['financial_type'] = $cs->contributionType; | |
297 | $result[$cs->id]['receive_date'] = $cs->receive_date; | |
298 | $result[$cs->id]['pcp_id'] = $cs->pcp_id; | |
299 | $result[$cs->id]['pcp_title'] = $cs->pcp_title; | |
300 | $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll; | |
301 | $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname; | |
302 | $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note; | |
303 | $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus); | |
51fa20cb | 304 | $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id); |
6dcc4d9d | 305 | |
306 | if ($isTest) { | |
307 | $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)'; | |
308 | } | |
309 | } | |
310 | return $result; | |
311 | } | |
1421174e | 312 | |
313 | /* | |
314 | * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id) | |
315 | * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName | |
316 | * is assigned | |
317 | * | |
318 | * @static | |
319 | */ | |
320 | ||
186c9c17 EM |
321 | /** |
322 | * @param $form | |
323 | * @param $params | |
324 | * @param $honoreeprofileId | |
325 | * @param null $honorId | |
326 | */ | |
1421174e | 327 | static function formatHonoreeProfileFields($form, $params, $honoreeprofileId, $honorId = NULL) { |
328 | $profileContactType = CRM_Core_BAO_UFGroup::getContactType($honoreeprofileId); | |
329 | $profileFields = CRM_Core_BAO_UFGroup::getFields($honoreeprofileId); | |
330 | $honoreeProfileFields = $values = array(); | |
8af73472 | 331 | $honorName = NULL; |
1421174e | 332 | |
333 | if ($honorId) { | |
334 | CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params); | |
8af73472 | 335 | if (empty($params)) { |
336 | foreach ($profileFields as $name => $field) { | |
337 | $title = $field['title']; | |
338 | $params[$field['name']] = $values[$title]; | |
339 | } | |
340 | } | |
1421174e | 341 | } |
342 | ||
343 | //remove name related fields and construct name string with prefix/suffix | |
344 | //which will be later assigned to template | |
345 | switch ($profileContactType) { | |
346 | case 'Individual': | |
347 | if (array_key_exists('prefix_id', $params)) { | |
348 | $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params), | |
349 | CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id') | |
350 | ); | |
351 | unset($profileFields['prefix_id']); | |
352 | } | |
353 | $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name']; | |
354 | unset($profileFields['first_name']); | |
355 | unset($profileFields['last_name']); | |
356 | if (array_key_exists('suffix_id', $params)) { | |
357 | $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params), | |
358 | CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id') | |
359 | ); | |
360 | unset($profileFields['suffix_id']); | |
361 | } | |
362 | break; | |
363 | case 'Organization': | |
364 | $honorName = $params['organization_name']; | |
365 | unset($profileFields['organization_name']); | |
366 | break; | |
367 | case 'Household': | |
368 | $honorName = $params['household_name']; | |
369 | unset($profileFields['household_name']); | |
370 | break; | |
371 | } | |
372 | ||
373 | if ($honorId) { | |
374 | $honoreeProfileFields['Name'] = $honorName; | |
375 | foreach ($profileFields as $name => $field) { | |
376 | $title = $field['title']; | |
377 | $honoreeProfileFields[$title] = $values[$title]; | |
378 | } | |
379 | $form->assign('honoreeProfile', $honoreeProfileFields); | |
380 | } | |
381 | else { | |
382 | $form->assign('honorName', $honorName); | |
383 | } | |
384 | } | |
a45a51e3 | 385 | } |
386 |