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