Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn { | |
b5c2afd0 | 37 | /** |
100fef9d | 38 | * Class constructor |
b5c2afd0 | 39 | * |
b5c2afd0 EM |
40 | * @return \CRM_Financial_DAO_FinancialTrxn |
41 | */ | |
42 | /** | |
43 | * | |
44 | */ | |
00be9182 | 45 | public function __construct() { |
6a488035 TO |
46 | parent::__construct(); |
47 | } | |
48 | ||
49 | /** | |
100fef9d | 50 | * Takes an associative array and creates a financial transaction object |
6a488035 TO |
51 | * |
52 | * @param array $params (reference ) an assoc array of name/value pairs | |
53 | * | |
54 | * @param string $trxnEntityTable entity_table | |
55 | * | |
c490a46a | 56 | * @return CRM_Core_BAO_FinancialTrxn object |
6a488035 TO |
57 | * @static |
58 | */ | |
00be9182 | 59 | public static function create(&$params, $trxnEntityTable = null ) { |
6a488035 TO |
60 | $trxn = new CRM_Financial_DAO_FinancialTrxn(); |
61 | $trxn->copyValues($params); | |
62 | $fids = array(); | |
63 | if (!CRM_Utils_Rule::currencyCode($trxn->currency)) { | |
64 | $config = CRM_Core_Config::singleton(); | |
65 | $trxn->currency = $config->defaultCurrency; | |
66 | } | |
67 | ||
68 | $trxn->save(); | |
69 | ||
70 | // save to entity_financial_trxn table | |
71 | $entityFinancialTrxnParams = | |
72 | array( | |
73 | 'entity_table' => "civicrm_contribution", | |
74 | 'financial_trxn_id' => $trxn->id, | |
75 | 'amount' => $params['total_amount'], | |
76 | 'currency' => $trxn->currency, | |
77 | ); | |
78 | ||
79 | if (!empty($trxnEntityTable)) { | |
80 | $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table']; | |
81 | $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id']; | |
82 | } | |
83 | else { | |
84 | $entityFinancialTrxnParams['entity_id'] = $params['contribution_id']; | |
85 | } | |
86 | ||
87 | $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn(); | |
88 | $entityTrxn->copyValues($entityFinancialTrxnParams); | |
89 | $entityTrxn->save(); | |
90 | return $trxn; | |
91 | } | |
92 | ||
b5c2afd0 | 93 | /** |
100fef9d CW |
94 | * @param int $contributionId |
95 | * @param int $contributionFinancialTypeId | |
b5c2afd0 EM |
96 | * |
97 | * @return array | |
98 | */ | |
00be9182 | 99 | public static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) { |
29c61b58 PJ |
100 | if (!$contributionFinancialTypeId) { |
101 | $contributionFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id'); | |
102 | } | |
ede1935f PJ |
103 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
104 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId); | |
520ec403 | 105 | $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') WHERE eft.entity_id = %1 AND ft.to_financial_account_id = %2"; |
1010c4e1 | 106 | |
ede1935f PJ |
107 | $p[1] = array($contributionId, 'Integer'); |
108 | $p[2] = array($toFinancialAccount, 'Integer'); | |
1010c4e1 | 109 | |
ede1935f PJ |
110 | $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p); |
111 | $ret = array(); | |
112 | while($balanceAmtDAO->fetch()) { | |
113 | $ret['trxn_id'] = $balanceAmtDAO->id; | |
114 | $ret['total_amount'] = $balanceAmtDAO->total_amount; | |
115 | } | |
116 | ||
117 | return $ret; | |
118 | } | |
119 | ||
6a488035 | 120 | /** |
c490a46a | 121 | * Fetch object based on array of properties |
6a488035 TO |
122 | * |
123 | * @param array $params (reference ) an assoc array of name/value pairs | |
124 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
125 | * | |
c490a46a | 126 | * @return CRM_Contribute_BAO_ContributionType object |
6a488035 TO |
127 | * @static |
128 | */ | |
00be9182 | 129 | public static function retrieve( &$params, &$defaults ) { |
6a488035 TO |
130 | $financialItem = new CRM_Financial_DAO_FinancialTrxn( ); |
131 | $financialItem->copyValues($params); | |
132 | if ($financialItem->find(true)) { | |
133 | CRM_Core_DAO::storeValues( $financialItem, $defaults ); | |
134 | return $financialItem; | |
135 | } | |
136 | return null; | |
137 | } | |
138 | ||
139 | /** | |
6a488035 TO |
140 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. |
141 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
142 | * | |
c490a46a | 143 | * @param $entity_id id of the entity usually the contactID. |
dd244018 | 144 | * @param string $orderBy to get single trxn id for a entity table i.e last or first. |
dd244018 EM |
145 | * @param bool $newTrxn |
146 | * | |
c490a46a | 147 | * @return array $tag array of category id's the contact belongs to. |
6a488035 | 148 | * |
6a488035 TO |
149 | * @static |
150 | */ | |
00be9182 | 151 | public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) { |
6a488035 TO |
152 | $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL); |
153 | ||
154 | $condition = ""; | |
155 | if (!$newTrxn) { | |
156 | $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) "; | |
157 | } | |
21d32567 DL |
158 | |
159 | if ($orderBy) { | |
160 | $orderBy = CRM_Utils_Type::escape($orderBy, 'String'); | |
161 | } | |
162 | ||
6a488035 | 163 | $query = "SELECT ceft.id, ceft.financial_trxn_id FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 164 | LEFT JOIN civicrm_entity_financial_trxn ceft |
6a488035 TO |
165 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
166 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
167 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
168 | LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id | |
169 | WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL) | |
170 | {$condition} | |
171 | ORDER BY cft.id {$orderBy} | |
cbb7c7e0 | 172 | LIMIT 1;"; |
173 | ||
6a488035 TO |
174 | $params = array(1 => array($entity_id, 'Integer')); |
175 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
176 | if ($dao->fetch()) { | |
177 | $ids['entityFinancialTrxnId'] = $dao->id; | |
178 | $ids['financialTrxnId'] = $dao->financial_trxn_id; | |
179 | } | |
180 | return $ids; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
c490a46a | 185 | * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. |
da6b46f4 | 186 | * |
c490a46a | 187 | * @param int $entity_id id of the entity usually the contactID. |
6a488035 | 188 | * |
c490a46a | 189 | * @return array $tag array of catagory id's the contact belongs to. |
6a488035 | 190 | * |
6a488035 TO |
191 | * @static |
192 | */ | |
00be9182 | 193 | public static function getFinancialTrxnTotal($entity_id) { |
6a488035 TO |
194 | $query = " |
195 | SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft | |
cbb7c7e0 | 196 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id |
6a488035 TO |
197 | WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1 |
198 | "; | |
199 | ||
200 | $sqlParams = array(1 => array($entity_id, 'Integer')); | |
201 | return CRM_Core_DAO::singleValueQuery($query, $sqlParams); | |
202 | ||
203 | } | |
77b97be7 | 204 | |
6a488035 TO |
205 | /** |
206 | * Given an financial_trxn_id check for previous entity_financial_trxn. | |
207 | * | |
c490a46a | 208 | * @param $financial_trxn_id id of the latest payment. |
77b97be7 | 209 | * |
6a488035 | 210 | * |
c490a46a | 211 | * @return array $payment array of previous payments |
6a488035 | 212 | * |
6a488035 TO |
213 | * @static |
214 | */ | |
00be9182 | 215 | public static function getPayments($financial_trxn_id) { |
6a488035 TO |
216 | $query = " |
217 | SELECT ef1.financial_trxn_id, sum(ef1.amount) amount | |
218 | FROM civicrm_entity_financial_trxn ef1 | |
219 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id | |
220 | WHERE ef2.financial_trxn_id =%1 | |
221 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
222 | AND ef1.entity_table = 'civicrm_financial_item' | |
223 | GROUP BY ef1.financial_trxn_id | |
224 | UNION | |
225 | SELECT ef1.financial_trxn_id, ef1.amount | |
226 | FROM civicrm_entity_financial_trxn ef1 | |
227 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id | |
228 | WHERE ef2.financial_trxn_id =%1 | |
229 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
230 | AND ef1.entity_table = 'civicrm_financial_trxn'"; | |
231 | ||
232 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
233 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
234 | $i = 0; | |
235 | $result = array(); | |
236 | while ($dao->fetch()) { | |
237 | $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id; | |
238 | $result[$i]['amount'] = $dao->amount; | |
239 | $i++; | |
240 | } | |
241 | ||
242 | if (empty($result)) { | |
243 | $query = "SELECT sum( amount ) amount FROM civicrm_entity_financial_trxn WHERE financial_trxn_id =%1 AND entity_table = 'civicrm_financial_item'"; | |
244 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
245 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
246 | ||
247 | if ($dao->fetch()) { | |
248 | $result[0]['financial_trxn_id'] = $financial_trxn_id; | |
249 | $result[0]['amount'] = $dao->amount; | |
250 | } | |
251 | } | |
252 | return $result; | |
253 | } | |
254 | ||
255 | /** | |
256 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
257 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
258 | * | |
c490a46a CW |
259 | * @param $entity_id id of the entity usually the contactID. |
260 | * @param string $entity_table name of the entity table usually 'civicrm_contact' | |
6a488035 | 261 | * |
c490a46a | 262 | * @return array $tag array of catagory id's the contact belongs to. |
6a488035 | 263 | * |
6a488035 TO |
264 | * @static |
265 | */ | |
00be9182 | 266 | public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') { |
6a488035 TO |
267 | $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft |
268 | LEFT JOIN civicrm_financial_item AS fi ON fi.id = ft.entity_id AND fi.entity_table = 'civicrm_line_item' AND ft.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 269 | LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2 |
6a488035 TO |
270 | WHERE lt.entity_id = %1 "; |
271 | ||
272 | $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String')); | |
273 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
274 | while($dao->fetch()){ | |
275 | $result[$dao->financial_trxn_id][$dao->id] = $dao->amount; | |
276 | } | |
277 | if (!empty($result)) { | |
278 | return $result; | |
279 | } | |
280 | else { | |
281 | return null; | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Delete financial transaction | |
287 | * | |
100fef9d | 288 | * @param int $entity_id |
6a488035 | 289 | * @return true on success, false otherwise |
6a488035 TO |
290 | * @static |
291 | */ | |
00be9182 | 292 | public static function deleteFinancialTrxn($entity_id) { |
de1c25e1 | 293 | $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 294 | LEFT JOIN civicrm_entity_financial_trxn ceft |
de1c25e1 PN |
295 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
296 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
297 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 298 | LEFT JOIN civicrm_financial_item cfi |
de1c25e1 PN |
299 | ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id |
300 | WHERE ceft.entity_id = %1"; | |
301 | CRM_Core_DAO::executeQuery($query, array(1 => array($entity_id, 'Integer'))); | |
302 | return TRUE; | |
6a488035 TO |
303 | } |
304 | ||
305 | /** | |
100fef9d | 306 | * Create financial transaction for premium |
6a488035 | 307 | * |
6a488035 TO |
308 | * @static |
309 | */ | |
00be9182 | 310 | public static function createPremiumTrxn($params) { |
8cc574cf | 311 | if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) { |
6a488035 TO |
312 | return; |
313 | } | |
cbb7c7e0 | 314 | |
a7488080 | 315 | if (!empty($params['cost'])) { |
6a488035 TO |
316 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
317 | $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']); | |
f743a6eb | 318 | $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND label IN ('Premiums Inventory Account is', 'Cost of Sales Account is')"); |
0d8afee2 CW |
319 | $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; |
320 | $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is': 'Premiums Inventory Account is'; | |
6a488035 TO |
321 | $accountRelationship = array_flip($accountRelationship); |
322 | $financialtrxn = array( | |
323 | 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]], | |
324 | 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]], | |
325 | 'trxn_date' => date('YmdHis'), | |
326 | 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0, | |
327 | 'currency' => CRM_Utils_Array::value('currency', $params), | |
328 | 'status_id' => array_search('Completed', $contributionStatuses) | |
329 | ); | |
330 | $trxnEntityTable['entity_table'] = 'civicrm_contribution'; | |
331 | $trxnEntityTable['entity_id'] = $params['contributionId']; | |
332 | CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable); | |
333 | } | |
334 | ||
a7488080 | 335 | if (!empty($params['oldPremium'])) { |
6a488035 TO |
336 | $premiumParams = array( |
337 | 'id' => $params['oldPremium']['product_id'] | |
338 | ); | |
339 | $productDetails = array(); | |
340 | CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails); | |
341 | $params = array( | |
342 | 'cost' => CRM_Utils_Array::value('cost', $productDetails), | |
343 | 'currency' => CRM_Utils_Array::value('currency', $productDetails), | |
344 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails), | |
345 | 'contributionId' => $params['oldPremium']['contribution_id'], | |
346 | 'isDeleted' => TRUE | |
347 | ); | |
348 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params); | |
349 | } | |
350 | } | |
c490a46a | 351 | |
6a488035 | 352 | /** |
100fef9d | 353 | * Create financial trxn and items when fee is charged |
6a488035 | 354 | * |
c490a46a | 355 | * @param array $params to create trxn entries |
6a488035 | 356 | * |
6a488035 TO |
357 | * @static |
358 | */ | |
00be9182 | 359 | public static function recordFees($params) { |
f743a6eb | 360 | $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
6a488035 TO |
361 | $domainId = CRM_Core_Config::domainID(); |
362 | $amount = 0; | |
a7488080 | 363 | if (!empty($params['prevContribution'])) { |
6a488035 TO |
364 | $amount = $params['prevContribution']->fee_amount; |
365 | } | |
366 | $amount = $params['fee_amount'] - $amount; | |
78b79549 PN |
367 | if (!$amount) { |
368 | return FALSE; | |
369 | } | |
39383f5f PJ |
370 | $contributionId = isset($params['contribution']->id) ? $params['contribution']->id : $params['contribution_id']; |
371 | if (empty($params['financial_type_id'])) { | |
372 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id', 'id'); | |
373 | } | |
374 | else { | |
375 | $financialTypeId = $params['financial_type_id']; | |
376 | } | |
377 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); | |
378 | ||
6a488035 TO |
379 | $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id']; |
380 | $params['trxnParams']['to_financial_account_id'] = $financialAccount; | |
381 | $params['trxnParams']['total_amount'] = $amount; | |
cbb7c7e0 | 382 | $params['trxnParams']['fee_amount'] = |
6a488035 TO |
383 | $params['trxnParams']['net_amount'] = 0; |
384 | $params['trxnParams']['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status','Completed','name'); | |
39383f5f | 385 | $params['trxnParams']['contribution_id'] = $contributionId; |
6a488035 | 386 | $trxn = self::create($params['trxnParams']); |
a7488080 | 387 | if (empty($params['entity_id'])) { |
6a488035 TO |
388 | $financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC'); |
389 | $params['entity_id'] = $financialTrxnID['financialTrxnId']; | |
390 | } | |
cbb7c7e0 | 391 | $fItemParams = |
6a488035 TO |
392 | array( |
393 | 'financial_account_id' => $financialAccount, | |
394 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'), | |
395 | 'created_date' => date('YmdHis'), | |
396 | 'transaction_date' => date('YmdHis'), | |
397 | 'amount' => $amount, | |
398 | 'description' => 'Fee', | |
399 | 'status_id' => CRM_Core_OptionGroup::getValue('financial_item_status','Paid','name'), | |
400 | 'entity_table' => 'civicrm_financial_trxn', | |
401 | 'entity_id' => $params['entity_id'], | |
402 | 'currency' => $params['trxnParams']['currency'], | |
403 | ); | |
404 | $trxnIDS['id'] = $trxn->id; | |
405 | $financialItem = CRM_Financial_BAO_FinancialItem::create($fItemParams, NULL, $trxnIDS); | |
406 | } | |
6a488035 | 407 | |
0f602e3f | 408 | /* |
c490a46a | 409 | * get partial payment amount and type of it |
0f602e3f PJ |
410 | * return @array : payment type => amount |
411 | * payment type : 'amount_owed' or 'refund_due' | |
412 | */ | |
b5c2afd0 | 413 | /** |
100fef9d | 414 | * @param int $entityId |
b5c2afd0 EM |
415 | * @param string $entityName |
416 | * @param bool $returnType | |
417 | * @param null $lineItemTotal | |
418 | * | |
419 | * @return array|int|null|string | |
420 | */ | |
00be9182 | 421 | public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) { |
0f602e3f PJ |
422 | $value = NULL; |
423 | if (empty($entityName)) { | |
424 | return $value; | |
425 | } | |
426 | ||
427 | if ($entityName == 'participant') { | |
bc2eeabb | 428 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id'); |
e8cf3013 | 429 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); |
0f602e3f PJ |
430 | |
431 | if ($contributionId && $financialTypeId) { | |
432 | $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); | |
1010c4e1 PJ |
433 | $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); |
434 | ||
0f602e3f PJ |
435 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
436 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); | |
8cf01b22 DG |
437 | $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
438 | $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); | |
0f602e3f | 439 | |
bc2eeabb | 440 | if (empty($lineItemTotal)) { |
28e4e707 PJ |
441 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); |
442 | if (count($ids) > 1) { | |
443 | $total = 0; | |
444 | foreach ($ids as $val) { | |
445 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); | |
446 | } | |
447 | $lineItemTotal = $total; | |
448 | } | |
449 | else { | |
450 | $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant'); | |
451 | } | |
bc2eeabb PJ |
452 | } |
453 | $sqlFtTotalAmt = " | |
454 | SELECT SUM(ft.total_amount) | |
0f602e3f | 455 | FROM civicrm_financial_trxn ft |
bc2eeabb PJ |
456 | LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') |
457 | LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) | |
458 | LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) | |
8cf01b22 | 459 | WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount} |
1010c4e1 | 460 | AND ft.status_id IN ({$statusId}, {$refundStatusId}) |
0f602e3f PJ |
461 | "; |
462 | $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); | |
bc2eeabb PJ |
463 | $value = 0; |
464 | if ($ftTotalAmt) { | |
465 | $value = $paymentVal = $lineItemTotal - $ftTotalAmt; | |
466 | } | |
29c61b58 PJ |
467 | if ($returnType) { |
468 | $value = array(); | |
469 | if ($paymentVal < 0) { | |
470 | $value['refund_due'] = $paymentVal; | |
471 | } | |
472 | elseif ($paymentVal > 0) { | |
473 | $value['amount_owed'] = $paymentVal; | |
474 | } | |
bc2eeabb PJ |
475 | elseif ($lineItemTotal == $ftTotalAmt) { |
476 | $value['full_paid'] = $ftTotalAmt; | |
477 | } | |
0f602e3f PJ |
478 | } |
479 | } | |
480 | } | |
481 | return $value; | |
482 | } | |
0d8afee2 | 483 | } |