worked on CRM-12463, more cleanup
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
CommitLineData
a45a51e3 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class 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 /**
45 * funtion to add contribution soft credit record
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 *
92 * @param int $contributionTypeId
93 * @static
94 */
95 static function del($contributionID) {
96 //delete from contribution soft table
97 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
07a88eec 98 $contributionSoft->contribution_id = $contributionID;
a45a51e3 99 $contributionSoft->delete();
100 }
6dcc4d9d 101
6dcc4d9d 102 static function getSoftContributionTotals($contact_id, $isTest = 0) {
8cb2c285
KJ
103 $query = '
104 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
105 FROM civicrm_contribution_soft ccs
106 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
107 WHERE cc.is_test = %2 AND ccs.contact_id = %1
108 GROUP BY currency';
6dcc4d9d 109
110 $params = array(1 => array($contact_id, 'Integer'),
111 2 => array($isTest, 'Integer'));
112
113 $cs = CRM_Core_DAO::executeQuery($query, $params);
114
115 $count = 0;
116 $amount = $average = array();
117
118 while ($cs->fetch()) {
119 if ($cs->amount > 0) {
120 $count++;
121 $amount[] = $cs->amount;
122 $average[] = $cs->average;
123 $currency[] = $cs->currency;
124 }
125 }
126
127 if ($count > 0) {
128 return array(implode(',&nbsp;', $amount),
129 implode(',&nbsp;', $average),
130 implode(',&nbsp;', $currency),
131 );
132 }
133 return array(0, 0);
134 }
135
136 /**
137 * Function to retrieve soft contributions for contribution record.
138 * @param array $params an associated array
139 * @param boolean $all include PCP data
140 *
141 * @return array of soft contribution ids, amounts, and associated contact ids
142 * @static
143 */
7ccf8829 144 static function getSoftContribution($contributionID, $all = FALSE) {
17db9f82
KJ
145 $pcpFields = array(
146 'pcp_id',
147 'pcp_display_in_roll',
148 'pcp_roll_nickname',
149 'pcp_personal_note',
150 );
151
7ccf8829
KJ
152 $query = '
153 SELECT ccs.id, pcp_id, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, amount, contact_id, c.display_name
154 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
155 WHERE contribution_id = %1;
156 ';
157
158 $params = array(1 => array($contributionID, 'Integer'));
159
160 $dao = CRM_Core_DAO::executeQuery($query, $params);
161
162 $softContribution = array();
163 $count = 1;
164 while ($dao->fetch()) {
165 if ($all) {
166 foreach ($pcpFields as $val) {
167 $softContribution['pcp'][$val] = $dao->$val;
168 }
169 }
170
171 $softContribution['soft_credit'][$count] = array(
172 'contact_id' => $dao->contact_id,
173 'soft_credit_id' => $dao->id,
174 'amount' => $dao->amount,
175 'contact_name' => $dao->display_name
176 );
177 $count++;
178 }
179
180 /*
181 * FIX API before deleting this
6dcc4d9d 182 $cs = new CRM_Contribute_DAO_ContributionSoft();
183 $cs->copyValues($params);
184 $softContribution = array();
185 $cs->find();
17db9f82 186
6dcc4d9d 187 if ($cs->N > 0) {
35729234 188 $count = 1;
6dcc4d9d 189 while ($cs->fetch()) {
6dcc4d9d 190 if ($all) {
17db9f82
KJ
191 foreach ($pcpFields as $val) {
192 $softContribution['pcp'][$val] = $cs->$val;
6dcc4d9d 193 }
194 }
35729234
KJ
195
196 $softContribution['soft_credit'][$count] = array(
197 'soft_credit_to' => $cs->contact_id,
198 'soft_credit_id' => $cs->id,
199 'soft_credit_amount' => $cs->amount,
200 );
201 $count++;
6dcc4d9d 202 }
203 }
7ccf8829
KJ
204 */
205
6dcc4d9d 206 return $softContribution;
207 }
208
209 /**
210 * Function to retrieve the list of soft contributons for given contact.
211 * @param int $contact_id contact id
212 *
213 * @return array
214 * @static
215 */
216 static function getSoftContributionList($contact_id, $isTest = 0) {
8cb2c285 217 $query = '
6dcc4d9d 218 SELECT ccs.id, ccs.amount as amount,
8cb2c285
KJ
219 ccs.contribution_id,
220 ccs.pcp_id,
221 ccs.pcp_display_in_roll,
222 ccs.pcp_roll_nickname,
223 ccs.pcp_personal_note,
224 cc.receive_date,
225 cc.contact_id as contributor_id,
226 cc.contribution_status_id as contribution_status_id,
227 cp.title as pcp_title,
228 cc.currency,
229 contact.display_name,
230 cct.name as contributionType
231 FROM civicrm_contribution_soft ccs
232 LEFT JOIN civicrm_contribution cc
233 ON ccs.contribution_id = cc.id
234 LEFT JOIN civicrm_pcp cp
235 ON ccs.pcp_id = cp.id
236 LEFT JOIN civicrm_contact contact ON
237 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
238 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
239 WHERE cc.is_test = %2 AND ccs.contact_id = %1
240 ORDER BY cc.receive_date DESC';
241
242 $params = array(
243 1 => array($contact_id, 'Integer'),
244 2 => array($isTest, 'Integer')
245 );
246 $cs = CRM_Core_DAO::executeQuery($query, $params);
6dcc4d9d 247 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
8cb2c285 248 $result = array();
6dcc4d9d 249 while ($cs->fetch()) {
250 $result[$cs->id]['amount'] = $cs->amount;
251 $result[$cs->id]['currency'] = $cs->currency;
252 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
253 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
254 $result[$cs->id]['contributor_name'] = $cs->display_name;
255 $result[$cs->id]['financial_type'] = $cs->contributionType;
256 $result[$cs->id]['receive_date'] = $cs->receive_date;
257 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
258 $result[$cs->id]['pcp_title'] = $cs->pcp_title;
259 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
260 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
261 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
262 $result[$cs->id]['contribution_status'] = CRM_Utils_Array::value($cs->contribution_status_id, $contributionStatus);
263
264 if ($isTest) {
265 $result[$cs->id]['contribution_status'] = $result[$cs->id]['contribution_status'] . '<br /> (test)';
266 }
267 }
268 return $result;
269 }
a45a51e3 270}
271