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