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