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