Merge pull request #6812 from eileenmcnaughton/CRM-17075
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
CommitLineData
a45a51e3 1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
a45a51e3 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
a45a51e3 32 */
33class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft {
34
8cb2c285 35 /**
fe482240 36 * Construct method.
8cb2c285 37 */
00be9182 38 public function __construct() {
a45a51e3 39 parent::__construct();
40 }
41
42 /**
fe482240 43 * Add contribution soft credit record.
a45a51e3 44 *
014c4014
TO
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
a45a51e3 47 *
a6c01b45
CW
48 * @return object
49 * soft contribution of object that is added
a45a51e3 50 */
07a88eec 51 public static function add(&$params) {
a45a51e3 52 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
53 $contributionSoft->copyValues($params);
6dcc4d9d 54
55 // set currency for CRM-1496
56 if (!isset($contributionSoft->currency)) {
57 $config = CRM_Core_Config::singleton();
58 $contributionSoft->currency = $config->defaultCurrency;
59 }
a45a51e3 60 return $contributionSoft->save();
61 }
62
7a13735b 63 /**
64 * Process the soft contribution and/or link to personal campaign page.
65 *
66 * @param array $params
67 * @param object $contribution CRM_Contribute_DAO_Contribution
68 *
69 */
70 public static function processSoftContribution($params, $contribution) {
71 //retrieve existing soft-credit and pcp id(s) if any against $contribution
72 $softIDs = self::getSoftCreditIds($contribution->id);
73 $pcpId = self::getSoftCreditIds($contribution->id, TRUE);
74
75 if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
76 $softParams = array();
77 $softParams['id'] = $pcpId ? $pcpId : NULL;
78 $softParams['contribution_id'] = $contribution->id;
79 $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
80 $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
81 $pcp['pcp_made_through_id'], 'contact_id'
82 );
83 $softParams['currency'] = $contribution->currency;
84 $softParams['amount'] = $contribution->total_amount;
85 $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp);
86 $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp);
87 $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp);
88 $softParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name');
89 $contributionSoft = self::add($softParams);
90 //Send notification to owner for PCP
91 if ($contributionSoft->pcp_id && empty($pcpId)) {
92 CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft);
93 }
94 }
95 //Delete PCP against this contribution and create new on submitted PCP information
96 elseif (array_key_exists('pcp', $params) && $pcpId) {
97 $deleteParams = array('id' => $pcpId);
98 CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
99 }
100 if (isset($params['soft_credit'])) {
101 $softParams = $params['soft_credit'];
102 foreach ($softParams as $softParam) {
103 if (!empty($softIDs)) {
104 $key = key($softIDs);
105 $softParam['id'] = $softIDs[$key];
106 unset($softIDs[$key]);
107 }
108 $softParam['contribution_id'] = $contribution->id;
109 $softParam['currency'] = $contribution->currency;
110 //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
111 if (empty($softParam['amount'])) {
112 $softParam['amount'] = $contribution->total_amount;
113 }
114 CRM_Contribute_BAO_ContributionSoft::add($softParam);
115 }
116
117 // delete any extra soft-credit while updating back-office contribution
118 foreach ((array) $softIDs as $softID) {
119 if (!in_array($softID, $params['soft_credit_ids'])) {
120 $deleteParams = array('id' => $softID);
121 CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
122 }
123 }
124 }
125 }
126
127 /**
128 * Function used to save pcp / soft credit entry.
129 *
130 * This is used by contribution and also event pcps
131 *
132 * @param array $params
133 * @param object $form
134 * Form object.
135 */
136 public static function formatSoftCreditParams(&$params, &$form) {
137 $pcp = $softParams = $softIDs = array();
138 if (!empty($params['pcp_made_through_id'])) {
139 $fields = array(
140 'pcp_made_through_id',
141 'pcp_display_in_roll',
142 'pcp_roll_nickname',
143 'pcp_personal_note',
144 );
145 foreach ($fields as $f) {
146 $pcp[$f] = CRM_Utils_Array::value($f, $params);
147 }
148 }
149
4779abb3 150 if (!empty($form->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
7a13735b 151 $honorId = NULL;
152
153 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name');
154 //check if there is any duplicate contact
4779abb3 155 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']);
7a13735b 156 $dedupeParams = CRM_Dedupe_Finder::formatParams($params['honor'], $profileContactType);
157 $dedupeParams['check_permission'] = FALSE;
158 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $profileContactType);
159 if (count($ids)) {
160 $honorId = CRM_Utils_Array::value(0, $ids);
161 }
162
163 $honorId = CRM_Contact_BAO_Contact::createProfileContact(
164 $params['honor'], CRM_Core_DAO::$_nullArray,
165 $honorId, NULL,
4779abb3 166 $form->_values['honoree_profile_id']
7a13735b 167 );
168 $softParams[] = array(
169 'contact_id' => $honorId,
170 'soft_credit_type_id' => $params['soft_credit_type_id'],
171 );
172
173 if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) {
174 $form->_values['honor'] = array(
175 'soft_credit_type' => CRM_Utils_Array::value(
176 $params['soft_credit_type_id'],
177 CRM_Core_OptionGroup::values("soft_credit_type")
178 ),
179 'honor_id' => $honorId,
4779abb3 180 'honor_profile_id' => $form->_values['honoree_profile_id'],
7a13735b 181 'honor_profile_values' => $params['honor'],
182 );
183 }
184 }
185 elseif (!empty($params['soft_credit_contact_id'])) {
186 //build soft credit params
187 foreach ($params['soft_credit_contact_id'] as $key => $val) {
188 if ($val && $params['soft_credit_amount'][$key]) {
189 $softParams[$key]['contact_id'] = $val;
190 $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]);
191 $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
192 if (!empty($params['soft_credit_id'][$key])) {
193 $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key];
194 }
195 }
196 }
197 }
198
199 $params['pcp'] = !empty($pcp) ? $pcp : NULL;
200 $params['soft_credit'] = $softParams;
201 $params['soft_credit_ids'] = $softIDs;
202 }
203
a45a51e3 204 /**
fe482240 205 * Fetch object based on array of properties.
a45a51e3 206 *
014c4014
TO
207 * @param array $params
208 * (reference ) an assoc array of name/value pairs.
209 * @param array $defaults
210 * (reference ) an assoc array to hold the flattened values.
a45a51e3 211 *
16b10e64 212 * @return CRM_Contribute_BAO_ContributionSoft
a45a51e3 213 */
00be9182 214 public static function retrieve(&$params, &$defaults) {
a45a51e3 215 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
216 $contributionSoft->copyValues($params);
217 if ($contributionSoft->find(TRUE)) {
218 CRM_Core_DAO::storeValues($contributionSoft, $defaults);
219 return $contributionSoft;
220 }
221 return NULL;
222 }
223
224 /**
fe482240 225 * Delete soft credits.
a45a51e3 226 *
c490a46a 227 * @param array $params
6c8f6e67 228 *
a45a51e3 229 */
00be9182 230 public static function del($params) {
a45a51e3 231 //delete from contribution soft table
232 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
22e263ad 233 foreach ($params as $column => $value) {
2cfc0f58 234 $contributionSoft->$column = $value;
235 }
a45a51e3 236 $contributionSoft->delete();
237 }
6dcc4d9d 238
186c9c17 239 /**
100fef9d 240 * @param int $contact_id
186c9c17
EM
241 * @param int $isTest
242 *
243 * @return array
244 */
00be9182 245 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
8cb2c285
KJ
246 $query = '
247 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
248 FROM civicrm_contribution_soft ccs
249 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
250 WHERE cc.is_test = %2 AND ccs.contact_id = %1
251 GROUP BY currency';
6dcc4d9d 252
874c9be7 253 $params = array(
353ffa53 254 1 => array($contact_id, 'Integer'),
389bcebf 255 2 => array($isTest, 'Integer'),
353ffa53 256 );
6dcc4d9d 257
258 $cs = CRM_Core_DAO::executeQuery($query, $params);
259
260 $count = 0;
261 $amount = $average = array();
262
263 while ($cs->fetch()) {
264 if ($cs->amount > 0) {
265 $count++;
91bb24a7 266 $amount[] = $cs->amount;
267 $average[] = $cs->average;
6dcc4d9d 268 $currency[] = $cs->currency;
269 }
270 }
271
272 if ($count > 0) {
91bb24a7 273 return array(
274 implode(',&nbsp;', $amount),
6dcc4d9d 275 implode(',&nbsp;', $average),
276 implode(',&nbsp;', $currency),
277 );
278 }
279 return array(0, 0);
280 }
281
282 /**
100fef9d 283 * Retrieve soft contributions for contribution record.
6dcc4d9d 284 *
100fef9d 285 * @param int $contributionID
014c4014
TO
286 * @param bool $all
287 * Include PCP data.
dd244018 288 *
a6c01b45 289 * @return array
16b10e64 290 * Array of soft contribution ids, amounts, and associated contact ids
6dcc4d9d 291 */
00be9182 292 public static function getSoftContribution($contributionID, $all = FALSE) {
17db9f82
KJ
293 $pcpFields = array(
294 'pcp_id',
b6545333 295 'pcp_title',
17db9f82
KJ
296 'pcp_display_in_roll',
297 'pcp_roll_nickname',
298 'pcp_personal_note',
299 );
300
7ccf8829 301 $query = '
b6545333 302 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 303 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
b6545333 304 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
7ccf8829
KJ
305 WHERE contribution_id = %1;
306 ';
307
308 $params = array(1 => array($contributionID, 'Integer'));
309
310 $dao = CRM_Core_DAO::executeQuery($query, $params);
311
312 $softContribution = array();
313 $count = 1;
314 while ($dao->fetch()) {
b6545333 315 if ($dao->pcp_id) {
6dcc4d9d 316 if ($all) {
17db9f82 317 foreach ($pcpFields as $val) {
b6545333 318 $softContribution[$val] = $dao->$val;
6dcc4d9d 319 }
b6545333 320 $softContribution['pcp_soft_credit_to_name'] = $dao->display_name;
321 $softContribution['pcp_soft_credit_to_id'] = $dao->contact_id;
6dcc4d9d 322 }
b6545333 323 }
324 else {
35729234 325 $softContribution['soft_credit'][$count] = array(
b6545333 326 'contact_id' => $dao->contact_id,
327 'soft_credit_id' => $dao->id,
328 'currency' => $dao->currency,
329 'amount' => $dao->amount,
330 'contact_name' => $dao->display_name,
331 'soft_credit_type' => $dao->soft_credit_type_id,
21dfd5f5 332 'soft_credit_type_label' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $dao->soft_credit_type_id),
35729234
KJ
333 );
334 $count++;
6dcc4d9d 335 }
336 }
7ccf8829 337
6dcc4d9d 338 return $softContribution;
339 }
340
186c9c17 341 /**
100fef9d 342 * @param int $contributionID
186c9c17
EM
343 * @param bool $isPCP
344 *
345 * @return array
346 */
874c9be7 347 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
91bb24a7 348 $query = "
b6545333 349 SELECT id
2cfc0f58 350 FROM civicrm_contribution_soft
351 WHERE contribution_id = %1
352 ";
b6545333 353
354 if ($isPCP) {
355 $query .= " AND pcp_id IS NOT NULL";
356 }
1221efe9 357 else {
358 $query .= " AND pcp_id IS NULL";
359 }
2cfc0f58 360 $params = array(1 => array($contributionID, 'Integer'));
361
362 $dao = CRM_Core_DAO::executeQuery($query, $params);
363 $id = array();
7f880799 364 $type = '';
2cfc0f58 365 while ($dao->fetch()) {
b6545333 366 if ($isPCP) {
367 return $dao->id;
2cfc0f58 368 }
369 $id[] = $dao->id;
370 }
b6545333 371 return $id;
2cfc0f58 372 }
91bb24a7 373
6dcc4d9d 374 /**
91bb24a7 375 * Function to retrieve the list of soft contributions for given contact.
6dcc4d9d 376 *
014c4014
TO
377 * @param int $contact_id
378 * Contact id.
014c4014 379 * @param string $filter
389bcebf 380 * @param int $isTest
014c4014 381 * Additional filter criteria, later used in where clause.
dd244018
EM
382 *
383 * @return array
6dcc4d9d 384 */
00be9182 385 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0) {
8cb2c285 386 $query = '
6dcc4d9d 387 SELECT ccs.id, ccs.amount as amount,
8cb2c285
KJ
388 ccs.contribution_id,
389 ccs.pcp_id,
390 ccs.pcp_display_in_roll,
391 ccs.pcp_roll_nickname,
392 ccs.pcp_personal_note,
51fa20cb 393 ccs.soft_credit_type_id,
8cb2c285
KJ
394 cc.receive_date,
395 cc.contact_id as contributor_id,
396 cc.contribution_status_id as contribution_status_id,
397 cp.title as pcp_title,
398 cc.currency,
399 contact.display_name,
400 cct.name as contributionType
401 FROM civicrm_contribution_soft ccs
402 LEFT JOIN civicrm_contribution cc
403 ON ccs.contribution_id = cc.id
404 LEFT JOIN civicrm_pcp cp
405 ON ccs.pcp_id = cp.id
406 LEFT JOIN civicrm_contact contact ON
407 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
408 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
91ef9be0 409 ';
410
411 $where = "
412 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
413 if ($filter) {
414 $where .= $filter;
415 }
416
417 $query .= "{$where} ORDER BY cc.receive_date DESC";
8cb2c285
KJ
418
419 $params = array(
420 1 => array($contact_id, 'Integer'),
21dfd5f5 421 2 => array($isTest, 'Integer'),
8cb2c285
KJ
422 );
423 $cs = CRM_Core_DAO::executeQuery($query, $params);
6dcc4d9d 424 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
8cb2c285 425 $result = array();
6dcc4d9d 426 while ($cs->fetch()) {
427 $result[$cs->id]['amount'] = $cs->amount;
428 $result[$cs->id]['currency'] = $cs->currency;
429 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
430 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
431 $result[$cs->id]['contributor_name'] = $cs->display_name;
432 $result[$cs->id]['financial_type'] = $cs->contributionType;
433 $result[$cs->id]['receive_date'] = $cs->receive_date;
434 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
435 $result[$cs->id]['pcp_title'] = $cs->pcp_title;
436 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
437 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
438 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
439 $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus);
51fa20cb 440 $result[$cs->id]['sct_label'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $cs->soft_credit_type_id);
6dcc4d9d 441
442 if ($isTest) {
443 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
444 }
445 }
446 return $result;
447 }
1421174e 448
186c9c17 449 /**
d424ffde
CW
450 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
451 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
452 * is assigned
453 *
c490a46a
CW
454 * @param CRM_Core_Form $form
455 * @param array $params
100fef9d 456 * @param int $honorId
186c9c17 457 */
4779abb3 458 public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) {
459 if (empty($form->_values['honoree_profile_id'])) {
460 return;
461 }
462 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']);
463 $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_values['honoree_profile_id']);
1421174e 464 $honoreeProfileFields = $values = array();
8af73472 465 $honorName = NULL;
1421174e 466
467 if ($honorId) {
468 CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params);
8af73472 469 if (empty($params)) {
470 foreach ($profileFields as $name => $field) {
471 $title = $field['title'];
472 $params[$field['name']] = $values[$title];
473 }
474 }
1421174e 475 }
476
477 //remove name related fields and construct name string with prefix/suffix
478 //which will be later assigned to template
479 switch ($profileContactType) {
480 case 'Individual':
481 if (array_key_exists('prefix_id', $params)) {
482 $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params),
483 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
484 );
485 unset($profileFields['prefix_id']);
486 }
874c9be7
TO
487 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
488 unset($profileFields['first_name']);
489 unset($profileFields['last_name']);
490 if (array_key_exists('suffix_id', $params)) {
491 $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params),
1421174e 492 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
493 );
874c9be7
TO
494 unset($profileFields['suffix_id']);
495 }
496 break;
497
498 case 'Organization':
499 $honorName = $params['organization_name'];
500 unset($profileFields['organization_name']);
501 break;
502
503 case 'Household':
504 $honorName = $params['household_name'];
505 unset($profileFields['household_name']);
506 break;
1421174e 507 }
508
509 if ($honorId) {
510 $honoreeProfileFields['Name'] = $honorName;
511 foreach ($profileFields as $name => $field) {
512 $title = $field['title'];
513 $honoreeProfileFields[$title] = $values[$title];
514 }
515 $form->assign('honoreeProfile', $honoreeProfileFields);
516 }
517 else {
518 $form->assign('honorName', $honorName);
519 }
520 }
96025800 521
a45a51e3 522}