| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { |
| 36 | |
| 37 | /** |
| 38 | * Static field for all the pledge information that we can potentially export |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | static $_exportableFields = NULL; |
| 43 | |
| 44 | /** |
| 45 | * Class constructor |
| 46 | */ |
| 47 | public function __construct() { |
| 48 | parent::__construct(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Takes a bunch of params that are needed to match certain criteria and |
| 53 | * retrieves the relevant objects. Typically the valid params are only |
| 54 | * pledge id. We'll tweak this function to be more full featured over a period |
| 55 | * of time. This is the inverse function of create. It also stores all the retrieved |
| 56 | * values in the default array |
| 57 | * |
| 58 | * @param array $params |
| 59 | * (reference ) an assoc array of name/value pairs. |
| 60 | * @param array $defaults |
| 61 | * (reference ) an assoc array to hold the flattened values. |
| 62 | * |
| 63 | * @return CRM_Pledge_BAO_Pledge |
| 64 | */ |
| 65 | public static function retrieve(&$params, &$defaults) { |
| 66 | $pledge = new CRM_Pledge_DAO_Pledge(); |
| 67 | $pledge->copyValues($params); |
| 68 | if ($pledge->find(TRUE)) { |
| 69 | CRM_Core_DAO::storeValues($pledge, $defaults); |
| 70 | return $pledge; |
| 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add pledge |
| 77 | * |
| 78 | * @param array $params |
| 79 | * Reference array contains the values submitted by the form. |
| 80 | * |
| 81 | * |
| 82 | * @return object |
| 83 | */ |
| 84 | public static function add(&$params) { |
| 85 | if (!empty($params['id'])) { |
| 86 | CRM_Utils_Hook::pre('edit', 'Pledge', $params['id'], $params); |
| 87 | } |
| 88 | else { |
| 89 | CRM_Utils_Hook::pre('create', 'Pledge', NULL, $params); |
| 90 | } |
| 91 | |
| 92 | $pledge = new CRM_Pledge_DAO_Pledge(); |
| 93 | |
| 94 | // if pledge is complete update end date as current date |
| 95 | if ($pledge->status_id == 1) { |
| 96 | $pledge->end_date = date('Ymd'); |
| 97 | } |
| 98 | |
| 99 | $pledge->copyValues($params); |
| 100 | |
| 101 | // set currency for CRM-1496 |
| 102 | if (!isset($pledge->currency)) { |
| 103 | $config = CRM_Core_Config::singleton(); |
| 104 | $pledge->currency = $config->defaultCurrency; |
| 105 | } |
| 106 | |
| 107 | $result = $pledge->save(); |
| 108 | |
| 109 | if (!empty($params['id'])) { |
| 110 | CRM_Utils_Hook::post('edit', 'Pledge', $pledge->id, $pledge); |
| 111 | } |
| 112 | else { |
| 113 | CRM_Utils_Hook::post('create', 'Pledge', $pledge->id, $pledge); |
| 114 | } |
| 115 | |
| 116 | return $result; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Given the list of params in the params array, fetch the object |
| 121 | * and store the values in the values array |
| 122 | * |
| 123 | * @param array $params |
| 124 | * Input parameters to find object. |
| 125 | * @param array $values |
| 126 | * Output values of the object. |
| 127 | * @param array $returnProperties |
| 128 | * If you want to return specific fields. |
| 129 | * |
| 130 | * @return array |
| 131 | * associated array of field values |
| 132 | */ |
| 133 | public static function &getValues(&$params, &$values, $returnProperties = NULL) { |
| 134 | if (empty($params)) { |
| 135 | return NULL; |
| 136 | } |
| 137 | CRM_Core_DAO::commonRetrieve('CRM_Pledge_BAO_Pledge', $params, $values, $returnProperties); |
| 138 | return $values; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Takes an associative array and creates a pledge object |
| 143 | * |
| 144 | * @param array $params |
| 145 | * (reference ) an assoc array of name/value pairs. |
| 146 | * |
| 147 | * @return CRM_Pledge_BAO_Pledge |
| 148 | */ |
| 149 | public static function &create(&$params) { |
| 150 | //FIXME: a cludgy hack to fix the dates to MySQL format |
| 151 | $dateFields = array('start_date', 'create_date', 'acknowledge_date', 'modified_date', 'cancel_date', 'end_date'); |
| 152 | foreach ($dateFields as $df) { |
| 153 | if (isset($params[$df])) { |
| 154 | $params[$df] = CRM_Utils_Date::isoToMysql($params[$df]); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $transaction = new CRM_Core_Transaction(); |
| 159 | |
| 160 | $paymentParams = array(); |
| 161 | $paymentParams['status_id'] = CRM_Utils_Array::value('status_id', $params); |
| 162 | if (!empty($params['installment_amount'])) { |
| 163 | $params['amount'] = $params['installment_amount'] * $params['installments']; |
| 164 | } |
| 165 | |
| 166 | //get All Payments status types. |
| 167 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 168 | |
| 169 | //update the pledge status only if it does NOT come from form |
| 170 | if (!isset($params['pledge_status_id'])) { |
| 171 | if (isset($params['contribution_id'])) { |
| 172 | if ($params['installments'] > 1) { |
| 173 | $params['status_id'] = array_search('In Progress', $paymentStatusTypes); |
| 174 | } |
| 175 | } |
| 176 | else { |
| 177 | if (!empty($params['id'])) { |
| 178 | $params['status_id'] = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($params['id']); |
| 179 | } |
| 180 | else { |
| 181 | $params['status_id'] = array_search('Pending', $paymentStatusTypes); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | $pledge = self::add($params); |
| 187 | if (is_a($pledge, 'CRM_Core_Error')) { |
| 188 | $pledge->rollback(); |
| 189 | return $pledge; |
| 190 | } |
| 191 | |
| 192 | //handle custom data. |
| 193 | if (!empty($params['custom']) && |
| 194 | is_array($params['custom']) |
| 195 | ) { |
| 196 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pledge', $pledge->id); |
| 197 | } |
| 198 | |
| 199 | // skip payment stuff inedit mode |
| 200 | if (!isset($params['id']) || !empty($params['is_pledge_pending'])) { |
| 201 | |
| 202 | //if pledge is pending delete all payments and recreate. |
| 203 | if (!empty($params['is_pledge_pending'])) { |
| 204 | CRM_Pledge_BAO_PledgePayment::deletePayments($pledge->id); |
| 205 | } |
| 206 | |
| 207 | //building payment params |
| 208 | $paymentParams['pledge_id'] = $pledge->id; |
| 209 | $paymentKeys = array( |
| 210 | 'amount', |
| 211 | 'installments', |
| 212 | 'scheduled_date', |
| 213 | 'frequency_unit', |
| 214 | 'currency', |
| 215 | 'frequency_day', |
| 216 | 'frequency_interval', |
| 217 | 'contribution_id', |
| 218 | 'installment_amount', |
| 219 | 'actual_amount', |
| 220 | ); |
| 221 | foreach ($paymentKeys as $key) { |
| 222 | $paymentParams[$key] = CRM_Utils_Array::value($key, $params, NULL); |
| 223 | } |
| 224 | CRM_Pledge_BAO_PledgePayment::create($paymentParams); |
| 225 | } |
| 226 | |
| 227 | $transaction->commit(); |
| 228 | |
| 229 | $url = CRM_Utils_System::url('civicrm/contact/view/pledge', |
| 230 | "action=view&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" |
| 231 | ); |
| 232 | |
| 233 | $recentOther = array(); |
| 234 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::UPDATE)) { |
| 235 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', |
| 236 | "action=update&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" |
| 237 | ); |
| 238 | } |
| 239 | if (CRM_Core_Permission::checkActionPermission('CiviPledge', CRM_Core_Action::DELETE)) { |
| 240 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/pledge', |
| 241 | "action=delete&reset=1&id={$pledge->id}&cid={$pledge->contact_id}&context=home" |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
| 246 | $title = CRM_Contact_BAO_Contact::displayName($pledge->contact_id) . ' - (' . ts('Pledged') . ' ' . CRM_Utils_Money::format($pledge->amount, $pledge->currency) . ' - ' . CRM_Utils_Array::value($pledge->financial_type_id, $contributionTypes) . ')'; |
| 247 | |
| 248 | // add the recently created Pledge |
| 249 | CRM_Utils_Recent::add($title, |
| 250 | $url, |
| 251 | $pledge->id, |
| 252 | 'Pledge', |
| 253 | $pledge->contact_id, |
| 254 | NULL, |
| 255 | $recentOther |
| 256 | ); |
| 257 | |
| 258 | return $pledge; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Delete the pledge |
| 263 | * |
| 264 | * @param int $id |
| 265 | * Pledge id. |
| 266 | * |
| 267 | * @return mixed |
| 268 | */ |
| 269 | public static function deletePledge($id) { |
| 270 | CRM_Utils_Hook::pre('delete', 'Pledge', $id, CRM_Core_DAO::$_nullArray); |
| 271 | |
| 272 | $transaction = new CRM_Core_Transaction(); |
| 273 | |
| 274 | //check for no Completed Payment records with the pledge |
| 275 | $payment = new CRM_Pledge_DAO_PledgePayment(); |
| 276 | $payment->pledge_id = $id; |
| 277 | $payment->find(); |
| 278 | |
| 279 | while ($payment->fetch()) { |
| 280 | //also delete associated contribution. |
| 281 | if ($payment->contribution_id) { |
| 282 | CRM_Contribute_BAO_Contribution::deleteContribution($payment->contribution_id); |
| 283 | } |
| 284 | $payment->delete(); |
| 285 | } |
| 286 | |
| 287 | $dao = new CRM_Pledge_DAO_Pledge(); |
| 288 | $dao->id = $id; |
| 289 | $results = $dao->delete(); |
| 290 | |
| 291 | $transaction->commit(); |
| 292 | |
| 293 | CRM_Utils_Hook::post('delete', 'Pledge', $dao->id, $dao); |
| 294 | |
| 295 | // delete the recently created Pledge |
| 296 | $pledgeRecent = array( |
| 297 | 'id' => $id, |
| 298 | 'type' => 'Pledge', |
| 299 | ); |
| 300 | CRM_Utils_Recent::del($pledgeRecent); |
| 301 | |
| 302 | return $results; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get the amount details date wise. |
| 307 | * |
| 308 | * @param string $status |
| 309 | * @param string $startDate |
| 310 | * @param string $endDate |
| 311 | * |
| 312 | * @return array|null |
| 313 | */ |
| 314 | public static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { |
| 315 | $where = array(); |
| 316 | $select = $from = $queryDate = NULL; |
| 317 | //get all status |
| 318 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 319 | $statusId = array_search($status, $allStatus); |
| 320 | |
| 321 | switch ($status) { |
| 322 | case 'Completed': |
| 323 | $statusId = array_search('Cancelled', $allStatus); |
| 324 | $where[] = 'status_id != ' . $statusId; |
| 325 | break; |
| 326 | |
| 327 | case 'Cancelled': |
| 328 | $where[] = 'status_id = ' . $statusId; |
| 329 | break; |
| 330 | |
| 331 | case 'In Progress': |
| 332 | $where[] = 'status_id = ' . $statusId; |
| 333 | break; |
| 334 | |
| 335 | case 'Pending': |
| 336 | $where[] = 'status_id = ' . $statusId; |
| 337 | break; |
| 338 | |
| 339 | case 'Overdue': |
| 340 | $where[] = 'status_id = ' . $statusId; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if ($startDate) { |
| 345 | $where[] = "create_date >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; |
| 346 | } |
| 347 | if ($endDate) { |
| 348 | $where[] = "create_date <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; |
| 349 | } |
| 350 | |
| 351 | $whereCond = implode(' AND ', $where); |
| 352 | |
| 353 | $query = " |
| 354 | SELECT sum( amount ) as pledge_amount, count( id ) as pledge_count, currency |
| 355 | FROM civicrm_pledge |
| 356 | WHERE $whereCond AND is_test=0 |
| 357 | GROUP BY currency |
| 358 | "; |
| 359 | $start = substr($startDate, 0, 8); |
| 360 | $end = substr($endDate, 0, 8); |
| 361 | $pCount = 0; |
| 362 | $pamount = array(); |
| 363 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 364 | while ($dao->fetch()) { |
| 365 | $pCount += $dao->pledge_count; |
| 366 | $pamount[] = CRM_Utils_Money::format($dao->pledge_amount, $dao->currency); |
| 367 | } |
| 368 | |
| 369 | $pledge_amount = array( |
| 370 | 'pledge_amount' => implode(', ', $pamount), |
| 371 | 'pledge_count' => $pCount, |
| 372 | 'purl' => CRM_Utils_System::url('civicrm/pledge/search', |
| 373 | "reset=1&force=1&pstatus={$statusId}&pstart={$start}&pend={$end}&test=0" |
| 374 | ), |
| 375 | ); |
| 376 | |
| 377 | $where = array(); |
| 378 | $statusId = array_search($status, $allStatus); |
| 379 | switch ($status) { |
| 380 | case 'Completed': |
| 381 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; |
| 382 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; |
| 383 | $queryDate = 'receive_date'; |
| 384 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; |
| 385 | break; |
| 386 | |
| 387 | case 'Cancelled': |
| 388 | $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; |
| 389 | $where[] = 'cp.status_id = ' . $statusId . ' AND cp.contribution_id = cd.id AND cd.is_test=0'; |
| 390 | $queryDate = 'receive_date'; |
| 391 | $from = ' civicrm_contribution cd, civicrm_pledge_payment cp'; |
| 392 | break; |
| 393 | |
| 394 | case 'Pending': |
| 395 | $select = 'sum( scheduled_amount )as received_pledge , count( cp.id ) as received_count'; |
| 396 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; |
| 397 | $queryDate = 'scheduled_date'; |
| 398 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; |
| 399 | break; |
| 400 | |
| 401 | case 'Overdue': |
| 402 | $select = 'sum( scheduled_amount ) as received_pledge , count( cp.id ) as received_count'; |
| 403 | $where[] = 'cp.status_id = ' . $statusId . ' AND pledge.is_test=0'; |
| 404 | $queryDate = 'scheduled_date'; |
| 405 | $from = ' civicrm_pledge_payment cp INNER JOIN civicrm_pledge pledge on cp.pledge_id = pledge.id'; |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | if ($startDate) { |
| 410 | $where[] = " $queryDate >= '" . CRM_Utils_Type::escape($startDate, 'Timestamp') . "'"; |
| 411 | } |
| 412 | if ($endDate) { |
| 413 | $where[] = " $queryDate <= '" . CRM_Utils_Type::escape($endDate, 'Timestamp') . "'"; |
| 414 | } |
| 415 | |
| 416 | $whereCond = implode(' AND ', $where); |
| 417 | |
| 418 | $query = " |
| 419 | SELECT $select, cp.currency |
| 420 | FROM $from |
| 421 | WHERE $whereCond |
| 422 | GROUP BY cp.currency |
| 423 | "; |
| 424 | if ($select) { |
| 425 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 426 | $amount = array(); |
| 427 | $count = 0; |
| 428 | |
| 429 | while ($dao->fetch()) { |
| 430 | $count += $dao->received_count; |
| 431 | $amount[] = CRM_Utils_Money::format($dao->received_pledge, $dao->currency); |
| 432 | } |
| 433 | |
| 434 | if ($count) { |
| 435 | return array_merge($pledge_amount, array( |
| 436 | 'received_amount' => implode(', ', $amount), |
| 437 | 'received_count' => $count, |
| 438 | 'url' => CRM_Utils_System::url('civicrm/pledge/search', |
| 439 | "reset=1&force=1&status={$statusId}&start={$start}&end={$end}&test=0" |
| 440 | ), |
| 441 | )); |
| 442 | } |
| 443 | } |
| 444 | else { |
| 445 | return $pledge_amount; |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Get list of pledges In Honor of contact Ids |
| 452 | * |
| 453 | * @param int $honorId |
| 454 | * In Honor of Contact ID. |
| 455 | * |
| 456 | * @return array |
| 457 | * return the list of pledge fields |
| 458 | * |
| 459 | */ |
| 460 | public static function getHonorContacts($honorId) { |
| 461 | $params = array(); |
| 462 | $honorDAO = new CRM_Contribute_DAO_ContributionSoft(); |
| 463 | $honorDAO->contact_id = $honorId; |
| 464 | $honorDAO->find(); |
| 465 | |
| 466 | //get all status. |
| 467 | while ($honorDAO->fetch()) { |
| 468 | $pledgePaymentDAO = new CRM_Pledge_DAO_PledgePayment(); |
| 469 | $pledgePaymentDAO->contribution_id = $honorDAO->contribution_id; |
| 470 | if ($pledgePaymentDAO->find(TRUE)) { |
| 471 | $pledgeDAO = new CRM_Pledge_DAO_Pledge(); |
| 472 | $pledgeDAO->id = $pledgePaymentDAO->pledge_id; |
| 473 | if ($pledgeDAO->find(TRUE)) { |
| 474 | $params[$pledgeDAO->id] = array( |
| 475 | 'honor_type' => CRM_Core_OptionGroup::getLabel('soft_credit_type', $honorDAO->soft_credit_type_id, 'value'), |
| 476 | 'honorId' => $pledgeDAO->contact_id, |
| 477 | 'amount' => $pledgeDAO->amount, |
| 478 | 'status' => CRM_Contribute_PseudoConstant::contributionStatus($pledgeDAO->status_id), |
| 479 | 'create_date' => $pledgeDAO->create_date, |
| 480 | 'acknowledge_date' => $pledgeDAO->acknowledge_date, |
| 481 | 'type' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', |
| 482 | $pledgeDAO->financial_type_id, 'name' |
| 483 | ), |
| 484 | 'display_name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 485 | $pledgeDAO->contact_id, 'display_name' |
| 486 | ), |
| 487 | ); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return $params; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Send Acknowledgment and create activity. |
| 497 | * |
| 498 | * @param CRM_Core_Form $form |
| 499 | * Form object. |
| 500 | * @param array $params |
| 501 | * An assoc array of name/value pairs. |
| 502 | * |
| 503 | * @return void |
| 504 | */ |
| 505 | public static function sendAcknowledgment(&$form, $params) { |
| 506 | //handle Acknowledgment. |
| 507 | $allPayments = $payments = array(); |
| 508 | |
| 509 | //get All Payments status types. |
| 510 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 511 | $returnProperties = array('status_id', 'scheduled_amount', 'scheduled_date', 'contribution_id'); |
| 512 | //get all paymnets details. |
| 513 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $params['id'], $allPayments, $returnProperties); |
| 514 | |
| 515 | if (!empty($allPayments)) { |
| 516 | foreach ($allPayments as $payID => $values) { |
| 517 | $contributionValue = $contributionStatus = array(); |
| 518 | if (isset($values['contribution_id'])) { |
| 519 | $contributionParams = array('id' => $values['contribution_id']); |
| 520 | $returnProperties = array('contribution_status_id', 'receive_date'); |
| 521 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_Contribution', |
| 522 | $contributionParams, $contributionStatus, $returnProperties |
| 523 | ); |
| 524 | $contributionValue = array( |
| 525 | 'status' => CRM_Utils_Array::value('contribution_status_id', $contributionStatus), |
| 526 | 'receive_date' => CRM_Utils_Array::value('receive_date', $contributionStatus), |
| 527 | ); |
| 528 | } |
| 529 | $payments[$payID] = array_merge($contributionValue, |
| 530 | array( |
| 531 | 'amount' => CRM_Utils_Array::value('scheduled_amount', $values), |
| 532 | 'due_date' => CRM_Utils_Array::value('scheduled_date', $values), |
| 533 | ) |
| 534 | ); |
| 535 | |
| 536 | //get the first valid payment id. |
| 537 | if (!isset($form->paymentId) && ($paymentStatusTypes[$values['status_id']] == 'Pending' || |
| 538 | $paymentStatusTypes[$values['status_id']] == 'Overdue' |
| 539 | ) |
| 540 | ) { |
| 541 | $form->paymentId = $values['id']; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | //assign pledge fields value to template. |
| 547 | $pledgeFields = array( |
| 548 | 'create_date', |
| 549 | 'total_pledge_amount', |
| 550 | 'frequency_interval', |
| 551 | 'frequency_unit', |
| 552 | 'installments', |
| 553 | 'frequency_day', |
| 554 | 'scheduled_amount', |
| 555 | 'currency', |
| 556 | ); |
| 557 | foreach ($pledgeFields as $field) { |
| 558 | if (!empty($params[$field])) { |
| 559 | $form->assign($field, $params[$field]); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | //assign all payments details. |
| 564 | if ($payments) { |
| 565 | $form->assign('payments', $payments); |
| 566 | } |
| 567 | |
| 568 | //handle domain token values |
| 569 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 570 | $tokens = array( |
| 571 | 'domain' => array('name', 'phone', 'address', 'email'), |
| 572 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
| 573 | ); |
| 574 | $domainValues = array(); |
| 575 | foreach ($tokens['domain'] as $token) { |
| 576 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); |
| 577 | } |
| 578 | $form->assign('domain', $domainValues); |
| 579 | |
| 580 | //handle contact token values. |
| 581 | $ids = array($params['contact_id']); |
| 582 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields()), |
| 583 | array('display_name', 'checksum', 'contact_id') |
| 584 | ); |
| 585 | foreach ($fields as $key => $val) { |
| 586 | $returnProperties[$val] = TRUE; |
| 587 | } |
| 588 | $details = CRM_Utils_Token::getTokenDetails($ids, |
| 589 | $returnProperties, |
| 590 | TRUE, TRUE, NULL, |
| 591 | $tokens, |
| 592 | get_class($form) |
| 593 | ); |
| 594 | $form->assign('contact', $details[0][$params['contact_id']]); |
| 595 | |
| 596 | //handle custom data. |
| 597 | if (!empty($params['hidden_custom'])) { |
| 598 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Pledge', CRM_Core_DAO::$_nullObject, $params['id']); |
| 599 | $pledgeParams = array(array('pledge_id', '=', $params['id'], 0, 0)); |
| 600 | $customGroup = array(); |
| 601 | // retrieve custom data |
| 602 | foreach ($groupTree as $groupID => $group) { |
| 603 | $customFields = $customValues = array(); |
| 604 | if ($groupID == 'info') { |
| 605 | continue; |
| 606 | } |
| 607 | foreach ($group['fields'] as $k => $field) { |
| 608 | $field['title'] = $field['label']; |
| 609 | $customFields["custom_{$k}"] = $field; |
| 610 | } |
| 611 | |
| 612 | //to build array of customgroup & customfields in it |
| 613 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $pledgeParams); |
| 614 | $customGroup[$group['title']] = $customValues; |
| 615 | } |
| 616 | |
| 617 | $form->assign('customGroup', $customGroup); |
| 618 | } |
| 619 | |
| 620 | //handle acknowledgment email stuff. |
| 621 | list($pledgerDisplayName, |
| 622 | $pledgerEmail |
| 623 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); |
| 624 | |
| 625 | //check for online pledge. |
| 626 | $session = CRM_Core_Session::singleton(); |
| 627 | if (!empty($params['receipt_from_email'])) { |
| 628 | $userName = CRM_Utils_Array::value('receipt_from_name', $params); |
| 629 | $userEmail = CRM_Utils_Array::value('receipt_from_email', $params); |
| 630 | } |
| 631 | elseif (!empty($params['from_email_id'])) { |
| 632 | $receiptFrom = $params['from_email_id']; |
| 633 | } |
| 634 | elseif ($userID = $session->get('userID')) { |
| 635 | //check for logged in user. |
| 636 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); |
| 637 | } |
| 638 | else { |
| 639 | //set the domain values. |
| 640 | $userName = CRM_Utils_Array::value('name', $domainValues); |
| 641 | $userEmail = CRM_Utils_Array::value('email', $domainValues); |
| 642 | } |
| 643 | |
| 644 | if (!isset($receiptFrom)) { |
| 645 | $receiptFrom = "$userName <$userEmail>"; |
| 646 | } |
| 647 | |
| 648 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 649 | array( |
| 650 | 'groupName' => 'msg_tpl_workflow_pledge', |
| 651 | 'valueName' => 'pledge_acknowledge', |
| 652 | 'contactId' => $params['contact_id'], |
| 653 | 'from' => $receiptFrom, |
| 654 | 'toName' => $pledgerDisplayName, |
| 655 | 'toEmail' => $pledgerEmail, |
| 656 | ) |
| 657 | ); |
| 658 | |
| 659 | //check if activity record exist for this pledge |
| 660 | //Acknowledgment, if exist do not add activity. |
| 661 | $activityType = 'Pledge Acknowledgment'; |
| 662 | $activity = new CRM_Activity_DAO_Activity(); |
| 663 | $activity->source_record_id = $params['id']; |
| 664 | $activity->activity_type_id = CRM_Core_OptionGroup::getValue('activity_type', |
| 665 | $activityType, |
| 666 | 'name' |
| 667 | ); |
| 668 | $config = CRM_Core_Config::singleton(); |
| 669 | |
| 670 | // FIXME: Translate |
| 671 | $details = 'Total Amount ' . CRM_Utils_Money::format($params['total_pledge_amount'], CRM_Utils_Array::value('currency', $params)) . ' To be paid in ' . $params['installments'] . ' installments of ' . CRM_Utils_Money::format($params['scheduled_amount'], CRM_Utils_Array::value('currency', $params)) . ' every ' . $params['frequency_interval'] . ' ' . $params['frequency_unit'] . '(s)'; |
| 672 | |
| 673 | if (!$activity->find()) { |
| 674 | $activityParams = array( |
| 675 | 'subject' => $subject, |
| 676 | 'source_contact_id' => $params['contact_id'], |
| 677 | 'source_record_id' => $params['id'], |
| 678 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', |
| 679 | $activityType, |
| 680 | 'name' |
| 681 | ), |
| 682 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($params['acknowledge_date']), |
| 683 | 'is_test' => $params['is_test'], |
| 684 | 'status_id' => 2, |
| 685 | 'details' => $details, |
| 686 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), |
| 687 | ); |
| 688 | |
| 689 | //lets insert assignee record. |
| 690 | if (!empty($params['contact_id'])) { |
| 691 | $activityParams['assignee_contact_id'] = $params['contact_id']; |
| 692 | } |
| 693 | |
| 694 | if (is_a(CRM_Activity_BAO_Activity::create($activityParams), 'CRM_Core_Error')) { |
| 695 | CRM_Core_Error::fatal("Failed creating Activity for acknowledgment"); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Combine all the exportable fields from the lower levels object |
| 702 | * |
| 703 | * @return array |
| 704 | * array of exportable Fields |
| 705 | */ |
| 706 | public static function &exportableFields() { |
| 707 | if (!self::$_exportableFields) { |
| 708 | if (!self::$_exportableFields) { |
| 709 | self::$_exportableFields = array(); |
| 710 | } |
| 711 | |
| 712 | $fields = CRM_Pledge_DAO_Pledge::export(); |
| 713 | |
| 714 | $fields = array_merge($fields, CRM_Pledge_DAO_PledgePayment::export()); |
| 715 | |
| 716 | //set title to calculated fields |
| 717 | $calculatedFields = array( |
| 718 | 'pledge_total_paid' => array('title' => ts('Total Paid')), |
| 719 | 'pledge_balance_amount' => array('title' => ts('Balance Amount')), |
| 720 | 'pledge_next_pay_date' => array('title' => ts('Next Payment Date')), |
| 721 | 'pledge_next_pay_amount' => array('title' => ts('Next Payment Amount')), |
| 722 | 'pledge_payment_paid_amount' => array('title' => ts('Paid Amount')), |
| 723 | 'pledge_payment_paid_date' => array('title' => ts('Paid Date')), |
| 724 | 'pledge_payment_status' => array( |
| 725 | 'title' => ts('Pledge Payment Status'), |
| 726 | 'name' => 'pledge_payment_status', |
| 727 | 'data_type' => CRM_Utils_Type::T_STRING, |
| 728 | ), |
| 729 | ); |
| 730 | |
| 731 | $pledgeFields = array( |
| 732 | 'pledge_status' => array( |
| 733 | 'title' => 'Pledge Status', |
| 734 | 'name' => 'pledge_status', |
| 735 | 'data_type' => CRM_Utils_Type::T_STRING, |
| 736 | ), |
| 737 | 'pledge_frequency_unit' => array( |
| 738 | 'title' => 'Pledge Frequency Unit', |
| 739 | 'name' => 'pledge_frequency_unit', |
| 740 | 'data_type' => CRM_Utils_Type::T_ENUM, |
| 741 | ), |
| 742 | 'pledge_frequency_interval' => array( |
| 743 | 'title' => 'Pledge Frequency Interval', |
| 744 | 'name' => 'pledge_frequency_interval', |
| 745 | 'data_type' => CRM_Utils_Type::T_INT, |
| 746 | ), |
| 747 | 'pledge_contribution_page_id' => array( |
| 748 | 'title' => 'Pledge Contribution Page Id', |
| 749 | 'name' => 'pledge_contribution_page_id', |
| 750 | 'data_type' => CRM_Utils_Type::T_INT, |
| 751 | ), |
| 752 | ); |
| 753 | |
| 754 | $fields = array_merge($fields, $pledgeFields, $calculatedFields); |
| 755 | |
| 756 | // add custom data |
| 757 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Pledge')); |
| 758 | self::$_exportableFields = $fields; |
| 759 | } |
| 760 | |
| 761 | return self::$_exportableFields; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Get pending or in progress pledges |
| 766 | * |
| 767 | * @param int $contactID |
| 768 | * Contact id. |
| 769 | * |
| 770 | * @return array |
| 771 | * associated array of pledge id(s) |
| 772 | */ |
| 773 | public static function getContactPledges($contactID) { |
| 774 | $pledgeDetails = array(); |
| 775 | $pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 776 | |
| 777 | $status = array(); |
| 778 | |
| 779 | //get pending and in progress status |
| 780 | foreach (array( |
| 781 | 'Pending', |
| 782 | 'In Progress', |
| 783 | 'Overdue', |
| 784 | ) as $name) { |
| 785 | if ($statusId = array_search($name, $pledgeStatuses)) { |
| 786 | $status[] = $statusId; |
| 787 | } |
| 788 | } |
| 789 | if (empty($status)) { |
| 790 | return $pledgeDetails; |
| 791 | } |
| 792 | |
| 793 | $statusClause = " IN (" . implode(',', $status) . ")"; |
| 794 | |
| 795 | $query = " |
| 796 | SELECT civicrm_pledge.id id |
| 797 | FROM civicrm_pledge |
| 798 | WHERE civicrm_pledge.status_id {$statusClause} |
| 799 | AND civicrm_pledge.contact_id = %1 |
| 800 | "; |
| 801 | |
| 802 | $params[1] = array($contactID, 'Integer'); |
| 803 | $pledge = CRM_Core_DAO::executeQuery($query, $params); |
| 804 | |
| 805 | while ($pledge->fetch()) { |
| 806 | $pledgeDetails[] = $pledge->id; |
| 807 | } |
| 808 | |
| 809 | return $pledgeDetails; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Get pledge record count for a Contact |
| 814 | * |
| 815 | * @param int $contactID |
| 816 | * |
| 817 | * @return int |
| 818 | * count of pledge records |
| 819 | */ |
| 820 | public static function getContactPledgeCount($contactID) { |
| 821 | $query = "SELECT count(*) FROM civicrm_pledge WHERE civicrm_pledge.contact_id = {$contactID} AND civicrm_pledge.is_test = 0"; |
| 822 | return CRM_Core_DAO::singleValueQuery($query); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * @param array $params |
| 827 | * |
| 828 | * @return array |
| 829 | */ |
| 830 | public static function updatePledgeStatus($params) { |
| 831 | |
| 832 | $returnMessages = array(); |
| 833 | |
| 834 | $sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE); |
| 835 | |
| 836 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 837 | |
| 838 | //unset statues that we never use for pledges |
| 839 | foreach (array( |
| 840 | 'Completed', |
| 841 | 'Cancelled', |
| 842 | 'Failed', |
| 843 | ) as $statusKey) { |
| 844 | if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) { |
| 845 | unset($allStatus[$key]); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | $statusIds = implode(',', array_keys($allStatus)); |
| 850 | $updateCnt = 0; |
| 851 | |
| 852 | $query = " |
| 853 | SELECT pledge.contact_id as contact_id, |
| 854 | pledge.id as pledge_id, |
| 855 | pledge.amount as amount, |
| 856 | payment.scheduled_date as scheduled_date, |
| 857 | pledge.create_date as create_date, |
| 858 | payment.id as payment_id, |
| 859 | pledge.currency as currency, |
| 860 | pledge.contribution_page_id as contribution_page_id, |
| 861 | payment.reminder_count as reminder_count, |
| 862 | pledge.max_reminders as max_reminders, |
| 863 | payment.reminder_date as reminder_date, |
| 864 | pledge.initial_reminder_day as initial_reminder_day, |
| 865 | pledge.additional_reminder_day as additional_reminder_day, |
| 866 | pledge.status_id as pledge_status, |
| 867 | payment.status_id as payment_status, |
| 868 | pledge.is_test as is_test, |
| 869 | pledge.campaign_id as campaign_id, |
| 870 | SUM(payment.scheduled_amount) as amount_due, |
| 871 | ( SELECT sum(civicrm_pledge_payment.actual_amount) |
| 872 | FROM civicrm_pledge_payment |
| 873 | WHERE civicrm_pledge_payment.status_id = 1 |
| 874 | AND civicrm_pledge_payment.pledge_id = pledge.id |
| 875 | ) as amount_paid |
| 876 | FROM civicrm_pledge pledge, civicrm_pledge_payment payment |
| 877 | WHERE pledge.id = payment.pledge_id |
| 878 | AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} ) |
| 879 | GROUP By payment.id |
| 880 | "; |
| 881 | |
| 882 | $dao = CRM_Core_DAO::executeQuery($query); |
| 883 | |
| 884 | $now = date('Ymd'); |
| 885 | $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array(); |
| 886 | while ($dao->fetch()) { |
| 887 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id); |
| 888 | |
| 889 | $pledgeDetails[$dao->payment_id] = array( |
| 890 | 'scheduled_date' => $dao->scheduled_date, |
| 891 | 'amount_due' => $dao->amount_due, |
| 892 | 'amount' => $dao->amount, |
| 893 | 'amount_paid' => $dao->amount_paid, |
| 894 | 'create_date' => $dao->create_date, |
| 895 | 'contact_id' => $dao->contact_id, |
| 896 | 'pledge_id' => $dao->pledge_id, |
| 897 | 'checksumValue' => $checksumValue, |
| 898 | 'contribution_page_id' => $dao->contribution_page_id, |
| 899 | 'reminder_count' => $dao->reminder_count, |
| 900 | 'max_reminders' => $dao->max_reminders, |
| 901 | 'reminder_date' => $dao->reminder_date, |
| 902 | 'initial_reminder_day' => $dao->initial_reminder_day, |
| 903 | 'additional_reminder_day' => $dao->additional_reminder_day, |
| 904 | 'pledge_status' => $dao->pledge_status, |
| 905 | 'payment_status' => $dao->payment_status, |
| 906 | 'is_test' => $dao->is_test, |
| 907 | 'currency' => $dao->currency, |
| 908 | 'campaign_id' => $dao->campaign_id, |
| 909 | ); |
| 910 | |
| 911 | $contactIds[$dao->contact_id] = $dao->contact_id; |
| 912 | $pledgeStatus[$dao->pledge_id] = $dao->pledge_status; |
| 913 | |
| 914 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), |
| 915 | $now |
| 916 | ) && $dao->payment_status != array_search('Overdue', $allStatus) |
| 917 | ) { |
| 918 | $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // process the updating script... |
| 923 | |
| 924 | foreach ($pledgePayments as $pledgeId => $paymentIds) { |
| 925 | // 1. update the pledge /pledge payment status. returns new status when an update happens |
| 926 | $returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})"; |
| 927 | |
| 928 | $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, |
| 929 | array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE |
| 930 | ); |
| 931 | if ($newStatus != $pledgeStatus[$pledgeId]) { |
| 932 | $returnMessages[] = "- status updated to: {$allStatus[$newStatus]}"; |
| 933 | $updateCnt += 1; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if ($sendReminders) { |
| 938 | // retrieve domain tokens |
| 939 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 940 | $tokens = array( |
| 941 | 'domain' => array('name', 'phone', 'address', 'email'), |
| 942 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
| 943 | ); |
| 944 | |
| 945 | $domainValues = array(); |
| 946 | foreach ($tokens['domain'] as $token) { |
| 947 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); |
| 948 | } |
| 949 | |
| 950 | //get the domain email address, since we don't carry w/ object. |
| 951 | $domainValue = CRM_Core_BAO_Domain::getNameAndEmail(); |
| 952 | $domainValues['email'] = $domainValue[1]; |
| 953 | |
| 954 | // retrieve contact tokens |
| 955 | |
| 956 | // this function does NOT return Deceased contacts since we don't want to send them email |
| 957 | list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, |
| 958 | NULL, |
| 959 | FALSE, FALSE, NULL, |
| 960 | $tokens, 'CRM_UpdatePledgeRecord' |
| 961 | ); |
| 962 | |
| 963 | // assign domain values to template |
| 964 | $template = CRM_Core_Smarty::singleton(); |
| 965 | $template->assign('domain', $domainValues); |
| 966 | |
| 967 | //set receipt from |
| 968 | $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>'; |
| 969 | |
| 970 | foreach ($pledgeDetails as $paymentId => $details) { |
| 971 | if (array_key_exists($details['contact_id'], $contactDetails)) { |
| 972 | $contactId = $details['contact_id']; |
| 973 | $pledgerName = $contactDetails[$contactId]['display_name']; |
| 974 | } |
| 975 | else { |
| 976 | continue; |
| 977 | } |
| 978 | |
| 979 | if (empty($details['reminder_date'])) { |
| 980 | $nextReminderDate = new DateTime($details['scheduled_date']); |
| 981 | $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day"); |
| 982 | $nextReminderDate = $nextReminderDate->format("Ymd"); |
| 983 | } |
| 984 | else { |
| 985 | $nextReminderDate = new DateTime($details['reminder_date']); |
| 986 | $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day"); |
| 987 | $nextReminderDate = $nextReminderDate->format("Ymd"); |
| 988 | } |
| 989 | if (($details['reminder_count'] < $details['max_reminders']) |
| 990 | && ($nextReminderDate <= $now) |
| 991 | ) { |
| 992 | |
| 993 | $toEmail = $doNotEmail = $onHold = NULL; |
| 994 | |
| 995 | if (!empty($contactDetails[$contactId]['email'])) { |
| 996 | $toEmail = $contactDetails[$contactId]['email']; |
| 997 | } |
| 998 | |
| 999 | if (!empty($contactDetails[$contactId]['do_not_email'])) { |
| 1000 | $doNotEmail = $contactDetails[$contactId]['do_not_email']; |
| 1001 | } |
| 1002 | |
| 1003 | if (!empty($contactDetails[$contactId]['on_hold'])) { |
| 1004 | $onHold = $contactDetails[$contactId]['on_hold']; |
| 1005 | } |
| 1006 | |
| 1007 | // 2. send acknowledgement mail |
| 1008 | if ($toEmail && !($doNotEmail || $onHold)) { |
| 1009 | //assign value to template |
| 1010 | $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0); |
| 1011 | $template->assign('contact', $contactDetails[$contactId]); |
| 1012 | $template->assign('next_payment', $details['scheduled_date']); |
| 1013 | $template->assign('amount_due', $details['amount_due']); |
| 1014 | $template->assign('checksumValue', $details['checksumValue']); |
| 1015 | $template->assign('contribution_page_id', $details['contribution_page_id']); |
| 1016 | $template->assign('pledge_id', $details['pledge_id']); |
| 1017 | $template->assign('scheduled_payment_date', $details['scheduled_date']); |
| 1018 | $template->assign('amount', $details['amount']); |
| 1019 | $template->assign('create_date', $details['create_date']); |
| 1020 | $template->assign('currency', $details['currency']); |
| 1021 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 1022 | array( |
| 1023 | 'groupName' => 'msg_tpl_workflow_pledge', |
| 1024 | 'valueName' => 'pledge_reminder', |
| 1025 | 'contactId' => $contactId, |
| 1026 | 'from' => $receiptFrom, |
| 1027 | 'toName' => $pledgerName, |
| 1028 | 'toEmail' => $toEmail, |
| 1029 | ) |
| 1030 | ); |
| 1031 | |
| 1032 | // 3. update pledge payment details |
| 1033 | if ($mailSent) { |
| 1034 | CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId); |
| 1035 | $activityType = 'Pledge Reminder'; |
| 1036 | $activityParams = array( |
| 1037 | 'subject' => $subject, |
| 1038 | 'source_contact_id' => $contactId, |
| 1039 | 'source_record_id' => $paymentId, |
| 1040 | 'assignee_contact_id' => $contactId, |
| 1041 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1042 | $activityType, |
| 1043 | 'name' |
| 1044 | ), |
| 1045 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), |
| 1046 | 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), |
| 1047 | 'is_test' => $details['is_test'], |
| 1048 | 'status_id' => 2, |
| 1049 | 'campaign_id' => $details['campaign_id'], |
| 1050 | ); |
| 1051 | if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) { |
| 1052 | $returnMessages[] = "Failed creating Activity for acknowledgment"; |
| 1053 | return array('is_error' => 1, 'message' => $returnMessages); |
| 1054 | } |
| 1055 | $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}"; |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | // end foreach on $pledgeDetails |
| 1061 | } |
| 1062 | // end if ( $sendReminders ) |
| 1063 | $returnMessages[] = "{$updateCnt} records updated."; |
| 1064 | |
| 1065 | return array('is_error' => 0, 'messages' => implode("\n\r", $returnMessages)); |
| 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * Mark a pledge (and any outstanding payments) as cancelled. |
| 1070 | * |
| 1071 | * @param int $pledgeID |
| 1072 | */ |
| 1073 | public static function cancel($pledgeID) { |
| 1074 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); |
| 1075 | $paymentIDs = self::findCancelablePayments($pledgeID); |
| 1076 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $paymentIDs, NULL, |
| 1077 | $statuses['Cancelled'], 0, FALSE, TRUE |
| 1078 | ); |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Find payments which can be safely canceled. |
| 1083 | * |
| 1084 | * @param int $pledgeID |
| 1085 | * @return array |
| 1086 | * Array of int (civicrm_pledge_payment.id) |
| 1087 | */ |
| 1088 | public static function findCancelablePayments($pledgeID) { |
| 1089 | $statuses = array_flip(CRM_Contribute_PseudoConstant::contributionStatus()); |
| 1090 | |
| 1091 | $paymentDAO = new CRM_Pledge_DAO_PledgePayment(); |
| 1092 | $paymentDAO->pledge_id = $pledgeID; |
| 1093 | $paymentDAO->whereAdd(sprintf("status_id IN (%d,%d)", |
| 1094 | $statuses['Overdue'], |
| 1095 | $statuses['Pending'] |
| 1096 | )); |
| 1097 | $paymentDAO->find(); |
| 1098 | |
| 1099 | $paymentIDs = array(); |
| 1100 | while ($paymentDAO->fetch()) { |
| 1101 | $paymentIDs[] = $paymentDAO->id; |
| 1102 | } |
| 1103 | return $paymentIDs; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Is this pledge free from financial transactions (this is important to know as we allow editing |
| 1108 | * when no transactions have taken place - the editing process currently involves deleting all pledge payments & contributions |
| 1109 | * & recreating so we want to block that if appropriate |
| 1110 | * |
| 1111 | * @param int $pledgeID |
| 1112 | * @param int $pledgeStatusID |
| 1113 | * @return bool |
| 1114 | * do financial transactions exist for this pledge? |
| 1115 | */ |
| 1116 | public static function pledgeHasFinancialTransactions($pledgeID, $pledgeStatusID) { |
| 1117 | if (empty($pledgeStatusID)) { |
| 1118 | //why would this happen? If we can see where it does then we can see if we should look it up |
| 1119 | //but assuming from form code it CAN be empty |
| 1120 | return TRUE; |
| 1121 | } |
| 1122 | if (self::isTransactedStatus($pledgeStatusID)) { |
| 1123 | return TRUE; |
| 1124 | } |
| 1125 | |
| 1126 | return civicrm_api3('pledge_payment', 'getcount', array( |
| 1127 | 'pledge_id' => $pledgeID, |
| 1128 | 'status_id' => array('IN' => self::getTransactionalStatus()), |
| 1129 | )); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Does this pledge / pledge payment status mean that a financial transaction has taken place? |
| 1134 | * @param int $statusID |
| 1135 | * Pledge status id. |
| 1136 | * |
| 1137 | * @return bool |
| 1138 | * is it a transactional status? |
| 1139 | */ |
| 1140 | protected static function isTransactedStatus($statusID) { |
| 1141 | if (!in_array($statusID, self::getNonTransactionalStatus())) { |
| 1142 | return TRUE; |
| 1143 | } |
| 1144 | return FALSE; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Get array of non transactional statuses |
| 1149 | * @return array |
| 1150 | * non transactional status ids |
| 1151 | */ |
| 1152 | protected static function getNonTransactionalStatus() { |
| 1153 | $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 1154 | return array_flip(array_intersect($paymentStatus, array('Overdue', 'Pending'))); |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Get array of non transactional statuses |
| 1159 | * @return array |
| 1160 | * non transactional status ids |
| 1161 | */ |
| 1162 | protected static function getTransactionalStatus() { |
| 1163 | $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 1164 | return array_diff(array_flip($paymentStatus), self::getNonTransactionalStatus()); |
| 1165 | } |
| 1166 | |
| 1167 | } |