First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
1288
sites/all/modules/civicrm/CRM/Pledge/BAO/Pledge.php
Normal file
1288
sites/all/modules/civicrm/CRM/Pledge/BAO/Pledge.php
Normal file
File diff suppressed because it is too large
Load diff
351
sites/all/modules/civicrm/CRM/Pledge/BAO/PledgeBlock.php
Normal file
351
sites/all/modules/civicrm/CRM/Pledge/BAO/PledgeBlock.php
Normal file
|
@ -0,0 +1,351 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve DB object based on input parameters.
|
||||
*
|
||||
* It also stores all the retrieved values in the default array.
|
||||
*
|
||||
* @param array $params
|
||||
* (reference ) an assoc array of name/value pairs.
|
||||
* @param array $defaults
|
||||
* (reference ) an assoc array to hold the flattened values.
|
||||
*
|
||||
* @return CRM_Pledge_BAO_PledgeBlock
|
||||
*/
|
||||
public static function retrieve(&$params, &$defaults) {
|
||||
$pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
|
||||
$pledgeBlock->copyValues($params);
|
||||
if ($pledgeBlock->find(TRUE)) {
|
||||
CRM_Core_DAO::storeValues($pledgeBlock, $defaults);
|
||||
return $pledgeBlock;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an associative array and creates a pledgeBlock object.
|
||||
*
|
||||
* @param array $params
|
||||
* (reference ) an assoc array of name/value pairs.
|
||||
*
|
||||
* @return CRM_Pledge_BAO_PledgeBlock
|
||||
*/
|
||||
public static function &create(&$params) {
|
||||
$transaction = new CRM_Core_Transaction();
|
||||
$pledgeBlock = self::add($params);
|
||||
|
||||
if (is_a($pledgeBlock, 'CRM_Core_Error')) {
|
||||
$pledgeBlock->rollback();
|
||||
return $pledgeBlock;
|
||||
}
|
||||
|
||||
$params['id'] = $pledgeBlock->id;
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return $pledgeBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add pledgeBlock.
|
||||
*
|
||||
* @param array $params
|
||||
* Reference array contains the values submitted by the form.
|
||||
*
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function add(&$params) {
|
||||
|
||||
if (!empty($params['id'])) {
|
||||
CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params);
|
||||
}
|
||||
else {
|
||||
CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params);
|
||||
}
|
||||
|
||||
$pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
|
||||
|
||||
// fix for pledge_frequency_unit
|
||||
$freqUnits = CRM_Utils_Array::value('pledge_frequency_unit', $params);
|
||||
|
||||
if ($freqUnits && is_array($freqUnits)) {
|
||||
unset($params['pledge_frequency_unit']);
|
||||
$newFreqUnits = array();
|
||||
foreach ($freqUnits as $k => $v) {
|
||||
if ($v) {
|
||||
$newFreqUnits[$k] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
$freqUnits = $newFreqUnits;
|
||||
if (is_array($freqUnits) && !empty($freqUnits)) {
|
||||
$freqUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($freqUnits));
|
||||
$pledgeBlock->pledge_frequency_unit = $freqUnits;
|
||||
}
|
||||
else {
|
||||
$pledgeBlock->pledge_frequency_unit = '';
|
||||
}
|
||||
}
|
||||
|
||||
$pledgeBlock->copyValues($params);
|
||||
$result = $pledgeBlock->save();
|
||||
|
||||
if (!empty($params['id'])) {
|
||||
CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock);
|
||||
}
|
||||
else {
|
||||
CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the pledgeBlock.
|
||||
*
|
||||
* @param int $id
|
||||
* PledgeBlock id.
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function deletePledgeBlock($id) {
|
||||
CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
|
||||
|
||||
$transaction = new CRM_Core_Transaction();
|
||||
|
||||
$results = NULL;
|
||||
|
||||
$dao = new CRM_Pledge_DAO_PledgeBlock();
|
||||
$dao->id = $id;
|
||||
$results = $dao->delete();
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Pledge Block info in Contribution Pages.
|
||||
*
|
||||
* @param int $pageID
|
||||
* Contribution page id.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getPledgeBlock($pageID) {
|
||||
$pledgeBlock = array();
|
||||
|
||||
$dao = new CRM_Pledge_DAO_PledgeBlock();
|
||||
$dao->entity_table = 'civicrm_contribution_page';
|
||||
$dao->entity_id = $pageID;
|
||||
if ($dao->find(TRUE)) {
|
||||
CRM_Core_DAO::storeValues($dao, $pledgeBlock);
|
||||
}
|
||||
|
||||
return $pledgeBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Pledge Block in Contribution Pages.
|
||||
*
|
||||
* @param CRM_Core_Form $form
|
||||
*/
|
||||
public static function buildPledgeBlock($form) {
|
||||
//build pledge payment fields.
|
||||
if (!empty($form->_values['pledge_id'])) {
|
||||
//get all payments required details.
|
||||
$allPayments = array();
|
||||
$returnProperties = array(
|
||||
'status_id',
|
||||
'scheduled_date',
|
||||
'scheduled_amount',
|
||||
'currency',
|
||||
'pledge_start_date',
|
||||
);
|
||||
CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
|
||||
$form->_values['pledge_id'], $allPayments, $returnProperties
|
||||
);
|
||||
// get all status
|
||||
$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
|
||||
|
||||
$nextPayment = array();
|
||||
$isNextPayment = FALSE;
|
||||
$overduePayments = array();
|
||||
foreach ($allPayments as $payID => $value) {
|
||||
if ($allStatus[$value['status_id']] == 'Overdue') {
|
||||
$overduePayments[$payID] = array(
|
||||
'id' => $payID,
|
||||
'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
|
||||
'scheduled_amount_currency' => $value['currency'],
|
||||
'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
|
||||
'%B %d'
|
||||
),
|
||||
);
|
||||
}
|
||||
elseif (!$isNextPayment &&
|
||||
$allStatus[$value['status_id']] == 'Pending'
|
||||
) {
|
||||
// get the next payment.
|
||||
$nextPayment = array(
|
||||
'id' => $payID,
|
||||
'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
|
||||
'scheduled_amount_currency' => $value['currency'],
|
||||
'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
|
||||
'%B %d'
|
||||
),
|
||||
);
|
||||
$isNextPayment = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// build check box array for payments.
|
||||
$payments = array();
|
||||
if (!empty($overduePayments)) {
|
||||
foreach ($overduePayments as $id => $payment) {
|
||||
$label = ts("%1 - due on %2 (overdue)", array(
|
||||
1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)),
|
||||
2 => CRM_Utils_Array::value('scheduled_date', $payment),
|
||||
));
|
||||
$paymentID = CRM_Utils_Array::value('id', $payment);
|
||||
$payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $payment)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($nextPayment)) {
|
||||
$label = ts("%1 - due on %2", array(
|
||||
1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)),
|
||||
2 => CRM_Utils_Array::value('scheduled_date', $nextPayment),
|
||||
));
|
||||
$paymentID = CRM_Utils_Array::value('id', $nextPayment);
|
||||
$payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $nextPayment)));
|
||||
}
|
||||
// give error if empty or build form for payment.
|
||||
if (empty($payments)) {
|
||||
CRM_Core_Error::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
|
||||
}
|
||||
else {
|
||||
$form->assign('is_pledge_payment', TRUE);
|
||||
$form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
$pledgeBlock = self::getPledgeBlock($form->_id);
|
||||
|
||||
// build form for pledge creation.
|
||||
$pledgeOptions = array(
|
||||
'0' => ts('I want to make a one-time contribution'),
|
||||
'1' => ts('I pledge to contribute this amount every'),
|
||||
);
|
||||
$form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
|
||||
NULL, array('<br/>')
|
||||
);
|
||||
$form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
|
||||
|
||||
if (!empty($pledgeBlock['is_pledge_interval'])) {
|
||||
$form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock));
|
||||
$form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
|
||||
}
|
||||
else {
|
||||
$form->add('hidden', 'pledge_frequency_interval', 1);
|
||||
}
|
||||
// Frequency unit drop-down label suffixes switch from *ly to *(s)
|
||||
$freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']);
|
||||
$freqUnits = array();
|
||||
$frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
|
||||
foreach ($freqUnitVals as $key => $val) {
|
||||
if (array_key_exists($val, $frequencyUnits)) {
|
||||
$freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
|
||||
}
|
||||
}
|
||||
$form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);
|
||||
// CRM-18854
|
||||
if (CRM_Utils_Array::value('is_pledge_start_date_visible', $pledgeBlock)) {
|
||||
if (CRM_Utils_Array::value('pledge_start_date', $pledgeBlock)) {
|
||||
$defaults = array();
|
||||
$date = (array) json_decode($pledgeBlock['pledge_start_date']);
|
||||
list($field, $value) = each($date);
|
||||
switch ($field) {
|
||||
case 'contribution_date':
|
||||
$form->addDate('start_date', ts('First installment payment'));
|
||||
$paymentDate = $value = date('m/d/Y');
|
||||
list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(NULL);
|
||||
$form->assign('is_date', TRUE);
|
||||
break;
|
||||
|
||||
case 'calendar_date':
|
||||
$form->addDate('start_date', ts('First installment payment'));
|
||||
list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($value);
|
||||
$form->assign('is_date', TRUE);
|
||||
$paymentDate = $value;
|
||||
break;
|
||||
|
||||
case 'calendar_month':
|
||||
$month = CRM_Utils_Date::getCalendarDayOfMonth();
|
||||
$form->add('select', 'start_date', ts('Day of month installments paid'), $month);
|
||||
$paymentDate = CRM_Pledge_BAO_Pledge::getPaymentDate($value);
|
||||
list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($paymentDate);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
$form->setDefaults($defaults);
|
||||
$form->assign('start_date_display', $paymentDate);
|
||||
$form->assign('start_date_editable', FALSE);
|
||||
if (CRM_Utils_Array::value('is_pledge_start_date_editable', $pledgeBlock)) {
|
||||
$form->assign('start_date_editable', TRUE);
|
||||
if ($field == 'calendar_month') {
|
||||
$form->assign('is_date', FALSE);
|
||||
$form->setDefaults(array('start_date' => $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
904
sites/all/modules/civicrm/CRM/Pledge/BAO/PledgePayment.php
Normal file
904
sites/all/modules/civicrm/CRM/Pledge/BAO/PledgePayment.php
Normal file
|
@ -0,0 +1,904 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Pledge_BAO_PledgePayment extends CRM_Pledge_DAO_PledgePayment {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pledge payment details.
|
||||
*
|
||||
* @param int $pledgeId
|
||||
* Pledge id.
|
||||
*
|
||||
* @return array
|
||||
* associated array of pledge payment details
|
||||
*/
|
||||
public static function getPledgePayments($pledgeId) {
|
||||
$query = "
|
||||
SELECT civicrm_pledge_payment.id id,
|
||||
scheduled_amount,
|
||||
scheduled_date,
|
||||
reminder_date,
|
||||
reminder_count,
|
||||
actual_amount,
|
||||
receive_date,
|
||||
civicrm_pledge_payment.currency,
|
||||
civicrm_option_value.name as status,
|
||||
civicrm_option_value.label as label,
|
||||
civicrm_contribution.id as contribution_id
|
||||
FROM civicrm_pledge_payment
|
||||
|
||||
LEFT JOIN civicrm_contribution ON civicrm_pledge_payment.contribution_id = civicrm_contribution.id
|
||||
LEFT JOIN civicrm_option_group ON ( civicrm_option_group.name = 'contribution_status' )
|
||||
LEFT JOIN civicrm_option_value ON ( civicrm_pledge_payment.status_id = civicrm_option_value.value AND
|
||||
civicrm_option_group.id = civicrm_option_value.option_group_id )
|
||||
WHERE pledge_id = %1
|
||||
";
|
||||
|
||||
$params[1] = array($pledgeId, 'Integer');
|
||||
$payment = CRM_Core_DAO::executeQuery($query, $params);
|
||||
|
||||
$paymentDetails = array();
|
||||
while ($payment->fetch()) {
|
||||
$paymentDetails[$payment->id]['scheduled_amount'] = $payment->scheduled_amount;
|
||||
$paymentDetails[$payment->id]['scheduled_date'] = $payment->scheduled_date;
|
||||
$paymentDetails[$payment->id]['reminder_date'] = $payment->reminder_date;
|
||||
$paymentDetails[$payment->id]['reminder_count'] = $payment->reminder_count;
|
||||
$paymentDetails[$payment->id]['total_amount'] = $payment->actual_amount;
|
||||
$paymentDetails[$payment->id]['receive_date'] = $payment->receive_date;
|
||||
$paymentDetails[$payment->id]['status'] = $payment->status;
|
||||
$paymentDetails[$payment->id]['label'] = $payment->label;
|
||||
$paymentDetails[$payment->id]['id'] = $payment->id;
|
||||
$paymentDetails[$payment->id]['contribution_id'] = $payment->contribution_id;
|
||||
$paymentDetails[$payment->id]['currency'] = $payment->currency;
|
||||
}
|
||||
|
||||
return $paymentDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return pledge
|
||||
*/
|
||||
public static function create($params) {
|
||||
$transaction = new CRM_Core_Transaction();
|
||||
$overdueStatusID = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_PledgePayment', 'status_id', 'Overdue');
|
||||
$pendingStatusId = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_PledgePayment', 'status_id', 'Pending');
|
||||
|
||||
//calculate the scheduled date for every installment
|
||||
$now = date('Ymd') . '000000';
|
||||
$statues = $prevScheduledDate = array();
|
||||
$prevScheduledDate[1] = CRM_Utils_Date::processDate($params['scheduled_date']);
|
||||
|
||||
if (CRM_Utils_Date::overdue($prevScheduledDate[1], $now)) {
|
||||
$statues[1] = $overdueStatusID;
|
||||
}
|
||||
else {
|
||||
$statues[1] = $pendingStatusId;
|
||||
}
|
||||
|
||||
for ($i = 1; $i < $params['installments']; $i++) {
|
||||
$prevScheduledDate[$i + 1] = self::calculateNextScheduledDate($params, $i);
|
||||
if (CRM_Utils_Date::overdue($prevScheduledDate[$i + 1], $now)) {
|
||||
$statues[$i + 1] = $overdueStatusID;
|
||||
}
|
||||
else {
|
||||
$statues[$i + 1] = $pendingStatusId;
|
||||
}
|
||||
}
|
||||
|
||||
if ($params['installment_amount']) {
|
||||
$params['scheduled_amount'] = $params['installment_amount'];
|
||||
}
|
||||
else {
|
||||
$params['scheduled_amount'] = round(($params['amount'] / $params['installments']), 2);
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $params['installments']; $i++) {
|
||||
// calculate the scheduled amount for every installment.
|
||||
if ($i == $params['installments']) {
|
||||
$params['scheduled_amount'] = $params['amount'] - ($i - 1) * $params['scheduled_amount'];
|
||||
}
|
||||
if (!isset($params['contribution_id']) && $params['installments'] > 1) {
|
||||
$params['status_id'] = $statues[$i];
|
||||
}
|
||||
|
||||
$params['scheduled_date'] = $prevScheduledDate[$i];
|
||||
$payment = self::add($params);
|
||||
if (is_a($payment, 'CRM_Core_Error')) {
|
||||
$transaction->rollback();
|
||||
return $payment;
|
||||
}
|
||||
|
||||
// we should add contribution id to only first payment record
|
||||
if (isset($params['contribution_id'])) {
|
||||
unset($params['contribution_id']);
|
||||
unset($params['actual_amount']);
|
||||
}
|
||||
}
|
||||
|
||||
// update pledge status
|
||||
self::updatePledgePaymentStatus($params['pledge_id']);
|
||||
|
||||
$transaction->commit();
|
||||
return $payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add pledge payment.
|
||||
*
|
||||
* @param array $params
|
||||
* Associate array of field.
|
||||
*
|
||||
* @return CRM_Pledge_DAO_PledgePayment
|
||||
* pledge payment id
|
||||
*/
|
||||
public static function add($params) {
|
||||
if (!empty($params['id'])) {
|
||||
CRM_Utils_Hook::pre('edit', 'PledgePayment', $params['id'], $params);
|
||||
}
|
||||
else {
|
||||
CRM_Utils_Hook::pre('create', 'PledgePayment', NULL, $params);
|
||||
}
|
||||
|
||||
$payment = new CRM_Pledge_DAO_PledgePayment();
|
||||
$payment->copyValues($params);
|
||||
|
||||
// set currency for CRM-1496
|
||||
if (!isset($payment->currency)) {
|
||||
$payment->currency = CRM_Core_Config::singleton()->defaultCurrency;
|
||||
}
|
||||
|
||||
$result = $payment->save();
|
||||
|
||||
if (!empty($params['id'])) {
|
||||
CRM_Utils_Hook::post('edit', 'PledgePayment', $payment->id, $payment);
|
||||
}
|
||||
else {
|
||||
CRM_Utils_Hook::post('create', 'PledgePayment', $payment->id, $payment);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve DB object based on input parameters.
|
||||
*
|
||||
* It also stores all the retrieved values in the default array.
|
||||
*
|
||||
* @param array $params
|
||||
* (reference ) an assoc array of name/value pairs.
|
||||
* @param array $defaults
|
||||
* (reference ) an assoc array to hold the flattened values.
|
||||
*
|
||||
* @return CRM_Pledge_BAO_PledgePayment
|
||||
*/
|
||||
public static function retrieve(&$params, &$defaults) {
|
||||
$payment = new CRM_Pledge_BAO_PledgePayment();
|
||||
$payment->copyValues($params);
|
||||
if ($payment->find(TRUE)) {
|
||||
CRM_Core_DAO::storeValues($payment, $defaults);
|
||||
return $payment;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete pledge payment.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return int
|
||||
* pledge payment id
|
||||
*/
|
||||
public static function del($id) {
|
||||
$payment = new CRM_Pledge_DAO_PledgePayment();
|
||||
$payment->id = $id;
|
||||
if ($payment->find()) {
|
||||
$payment->fetch();
|
||||
|
||||
CRM_Utils_Hook::pre('delete', 'PledgePayment', $id, $payment);
|
||||
|
||||
$result = $payment->delete();
|
||||
|
||||
CRM_Utils_Hook::post('delete', 'PledgePayment', $id, $payment);
|
||||
|
||||
return $result;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all pledge payments.
|
||||
*
|
||||
* @param int $id
|
||||
* Pledge id.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function deletePayments($id) {
|
||||
if (!CRM_Utils_Rule::positiveInteger($id)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$transaction = new CRM_Core_Transaction();
|
||||
|
||||
$payment = new CRM_Pledge_DAO_PledgePayment();
|
||||
$payment->pledge_id = $id;
|
||||
|
||||
if ($payment->find()) {
|
||||
while ($payment->fetch()) {
|
||||
//also delete associated contribution.
|
||||
if ($payment->contribution_id) {
|
||||
CRM_Contribute_BAO_Contribution::deleteContribution($payment->contribution_id);
|
||||
}
|
||||
self::del($payment->id);
|
||||
}
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* On delete contribution record update associated pledge payment and pledge.
|
||||
*
|
||||
* @param int $contributionID
|
||||
* Contribution id.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function resetPledgePayment($contributionID) {
|
||||
$transaction = new CRM_Core_Transaction();
|
||||
|
||||
$payment = new CRM_Pledge_DAO_PledgePayment();
|
||||
$payment->contribution_id = $contributionID;
|
||||
if ($payment->find(TRUE)) {
|
||||
$payment->contribution_id = 'null';
|
||||
$payment->status_id = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
|
||||
$payment->scheduled_date = NULL;
|
||||
$payment->reminder_date = NULL;
|
||||
$payment->scheduled_amount = $payment->actual_amount;
|
||||
$payment->actual_amount = 'null';
|
||||
$payment->save();
|
||||
|
||||
//update pledge status.
|
||||
$pledgeID = $payment->pledge_id;
|
||||
$pledgeStatusID = self::calculatePledgeStatus($pledgeID);
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'status_id', $pledgeStatusID);
|
||||
|
||||
$payment->free();
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Pledge Payment Status.
|
||||
*
|
||||
* @param int $pledgeID
|
||||
* Id of pledge.
|
||||
* @param array $paymentIDs
|
||||
* Ids of pledge payment(s) to update.
|
||||
* @param int $paymentStatusID
|
||||
* Payment status to set.
|
||||
* @param int $pledgeStatusID
|
||||
* Pledge status to change (if needed).
|
||||
* @param float|int $actualAmount , actual amount being paid
|
||||
* @param bool $adjustTotalAmount
|
||||
* Is amount being paid different from scheduled amount?.
|
||||
* @param bool $isScriptUpdate
|
||||
* Is function being called from bin script?.
|
||||
*
|
||||
* @return int
|
||||
* $newStatus, updated status id (or 0)
|
||||
*/
|
||||
public static function updatePledgePaymentStatus(
|
||||
$pledgeID,
|
||||
$paymentIDs = NULL,
|
||||
$paymentStatusID = NULL,
|
||||
$pledgeStatusID = NULL,
|
||||
$actualAmount = 0,
|
||||
$adjustTotalAmount = FALSE,
|
||||
$isScriptUpdate = FALSE
|
||||
) {
|
||||
$totalAmountClause = '';
|
||||
$paymentContributionId = NULL;
|
||||
$editScheduled = FALSE;
|
||||
|
||||
// get all statuses
|
||||
$allStatus = CRM_Core_OptionGroup::values('pledge_status',
|
||||
FALSE, FALSE, FALSE, NULL, 'name', TRUE
|
||||
);
|
||||
|
||||
// if we get do not get contribution id means we are editing the scheduled payment.
|
||||
if (!empty($paymentIDs)) {
|
||||
$editScheduled = FALSE;
|
||||
$payments = implode(',', $paymentIDs);
|
||||
$paymentContributionId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$payments,
|
||||
'contribution_id',
|
||||
'id'
|
||||
);
|
||||
|
||||
if (!$paymentContributionId) {
|
||||
$editScheduled = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// if payment ids are passed, we update payment table first, since payments statuses are not dependent on pledge status
|
||||
$pledgeStatusName = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $pledgeStatusID);
|
||||
if ((!empty($paymentIDs) || $pledgeStatusName == 'Cancelled') && (!$editScheduled || $isScriptUpdate)) {
|
||||
if ($pledgeStatusName == 'Cancelled') {
|
||||
$paymentStatusID = $pledgeStatusID;
|
||||
}
|
||||
|
||||
self::updatePledgePayments($pledgeID, $paymentStatusID, $paymentIDs, $actualAmount, $paymentContributionId, $isScriptUpdate);
|
||||
}
|
||||
if (!empty($paymentIDs) && $actualAmount) {
|
||||
$payments = implode(',', $paymentIDs);
|
||||
$pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$payments,
|
||||
'scheduled_amount',
|
||||
'id'
|
||||
);
|
||||
|
||||
$pledgeStatusId = self::calculatePledgeStatus($pledgeID);
|
||||
// Actual Pledge Amount
|
||||
$actualPledgeAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge',
|
||||
$pledgeID,
|
||||
'amount',
|
||||
'id'
|
||||
);
|
||||
// while editing scheduled we need to check if we are editing last pending
|
||||
$lastPending = FALSE;
|
||||
if (!$paymentContributionId) {
|
||||
$checkPendingCount = self::getOldestPledgePayment($pledgeID, 2);
|
||||
if ($checkPendingCount['count'] == 1) {
|
||||
$lastPending = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// check if this is the last payment and adjust the actual amount.
|
||||
if ($pledgeStatusId && $pledgeStatusId == array_search('Completed', $allStatus) || $lastPending) {
|
||||
// last scheduled payment
|
||||
if ($actualAmount >= $pledgeScheduledAmount) {
|
||||
$adjustTotalAmount = TRUE;
|
||||
}
|
||||
elseif (!$adjustTotalAmount) {
|
||||
// actual amount is less than the scheduled amount, so enter new pledge payment record
|
||||
$pledgeFrequencyUnit = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_unit', 'id');
|
||||
$pledgeFrequencyInterval = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_interval', 'id');
|
||||
$pledgeScheduledDate = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_date', 'id');
|
||||
$scheduled_date = CRM_Utils_Date::processDate($pledgeScheduledDate);
|
||||
$date['year'] = (int) substr($scheduled_date, 0, 4);
|
||||
$date['month'] = (int) substr($scheduled_date, 4, 2);
|
||||
$date['day'] = (int) substr($scheduled_date, 6, 2);
|
||||
$newDate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
|
||||
$ScheduledDate = CRM_Utils_Date::format(CRM_Utils_Date::intervalAdd($pledgeFrequencyUnit,
|
||||
$pledgeFrequencyInterval, $newDate
|
||||
));
|
||||
$pledgeParams = array(
|
||||
'status_id' => array_search('Pending', $allStatus),
|
||||
'pledge_id' => $pledgeID,
|
||||
'scheduled_amount' => ($pledgeScheduledAmount - $actualAmount),
|
||||
'scheduled_date' => $ScheduledDate,
|
||||
);
|
||||
$payment = self::add($pledgeParams);
|
||||
// while editing schedule, after adding a new pledge payemnt update the scheduled amount of the current payment
|
||||
if (!$paymentContributionId) {
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (!$adjustTotalAmount) {
|
||||
// not last schedule amount and also not selected to adjust Total
|
||||
$paymentContributionId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$payments,
|
||||
'contribution_id',
|
||||
'id'
|
||||
);
|
||||
self::adjustPledgePayment($pledgeID, $actualAmount, $pledgeScheduledAmount, $paymentContributionId, $payments, $paymentStatusID);
|
||||
// while editing schedule, after adding a new pledge payemnt update the scheduled amount of the current payment
|
||||
if (!$paymentContributionId) {
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
|
||||
}
|
||||
// after adjusting all payments check if the actual amount was greater than the actual remaining amount , if so then update the total pledge amount.
|
||||
$pledgeStatusId = self::calculatePledgeStatus($pledgeID);
|
||||
$balanceQuery = "
|
||||
SELECT sum( civicrm_pledge_payment.actual_amount )
|
||||
FROM civicrm_pledge_payment
|
||||
WHERE civicrm_pledge_payment.pledge_id = %1
|
||||
AND civicrm_pledge_payment.status_id = 1
|
||||
";
|
||||
$totalPaidParams = array(1 => array($pledgeID, 'Integer'));
|
||||
$totalPaidAmount = CRM_Core_DAO::singleValueQuery($balanceQuery, $totalPaidParams);
|
||||
$remainingTotalAmount = ($actualPledgeAmount - $totalPaidAmount);
|
||||
if (($pledgeStatusId && $pledgeStatusId == array_search('Completed', $allStatus)) && (($actualAmount > $remainingTotalAmount) || ($actualAmount >= $actualPledgeAmount))) {
|
||||
$totalAmountClause = ", civicrm_pledge.amount = {$totalPaidAmount}";
|
||||
}
|
||||
}
|
||||
if ($adjustTotalAmount) {
|
||||
$newTotalAmount = ($actualPledgeAmount + ($actualAmount - $pledgeScheduledAmount));
|
||||
$totalAmountClause = ", civicrm_pledge.amount = {$newTotalAmount}";
|
||||
if (!$paymentContributionId) {
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $payments, 'scheduled_amount', $actualAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$cancelDateClause = $endDateClause = NULL;
|
||||
// update pledge and payment status if status is Completed/Cancelled.
|
||||
if ($pledgeStatusID && $pledgeStatusID == array_search('Cancelled', $allStatus)) {
|
||||
$paymentStatusID = $pledgeStatusID;
|
||||
$cancelDateClause = ", civicrm_pledge.cancel_date = CURRENT_TIMESTAMP ";
|
||||
}
|
||||
else {
|
||||
// get pledge status
|
||||
$pledgeStatusID = self::calculatePledgeStatus($pledgeID);
|
||||
}
|
||||
|
||||
if ($pledgeStatusID == array_search('Completed', $allStatus)) {
|
||||
$endDateClause = ", civicrm_pledge.end_date = CURRENT_TIMESTAMP ";
|
||||
}
|
||||
|
||||
// update pledge status
|
||||
$query = "
|
||||
UPDATE civicrm_pledge
|
||||
SET civicrm_pledge.status_id = %1
|
||||
{$cancelDateClause} {$endDateClause} {$totalAmountClause}
|
||||
WHERE civicrm_pledge.id = %2
|
||||
";
|
||||
|
||||
$params = array(
|
||||
1 => array($pledgeStatusID, 'Integer'),
|
||||
2 => array($pledgeID, 'Integer'),
|
||||
);
|
||||
|
||||
CRM_Core_DAO::executeQuery($query, $params);
|
||||
|
||||
return $pledgeStatusID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the base scheduled date. This function effectively 'rounds' the $params['scheduled_date'] value
|
||||
* to the first payment date with respect to the frequency day - ie. if payments are on the 15th of the month the date returned
|
||||
* will be the 15th of the relevant month. Then to calculate the payments you can use intervalAdd ie.
|
||||
* CRM_Utils_Date::intervalAdd( $params['frequency_unit'], $i * ($params['frequency_interval']) , calculateBaseScheduledDate( &$params )))
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* Next scheduled date as an array
|
||||
*/
|
||||
public static function calculateBaseScheduleDate(&$params) {
|
||||
$date = array();
|
||||
$scheduled_date = CRM_Utils_Date::processDate($params['scheduled_date']);
|
||||
$date['year'] = (int) substr($scheduled_date, 0, 4);
|
||||
$date['month'] = (int) substr($scheduled_date, 4, 2);
|
||||
$date['day'] = (int) substr($scheduled_date, 6, 2);
|
||||
// calculation of schedule date according to frequency day of period
|
||||
// frequency day is not applicable for daily installments
|
||||
if ($params['frequency_unit'] != 'day' && $params['frequency_unit'] != 'year') {
|
||||
if ($params['frequency_unit'] != 'week') {
|
||||
// CRM-18316: To calculate pledge scheduled dates at the end of a month.
|
||||
$date['day'] = $params['frequency_day'];
|
||||
$lastDayOfMonth = date('t', mktime(0, 0, 0, $date['month'], 1, $date['year']));
|
||||
if ($lastDayOfMonth < $date['day']) {
|
||||
$date['day'] = $lastDayOfMonth;
|
||||
}
|
||||
}
|
||||
elseif ($params['frequency_unit'] == 'week') {
|
||||
|
||||
// for week calculate day of week ie. Sunday,Monday etc. as next payment date
|
||||
$dayOfWeek = date('w', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
|
||||
$frequencyDay = $params['frequency_day'] - $dayOfWeek;
|
||||
|
||||
$scheduleDate = explode("-", date('n-j-Y', mktime(0, 0, 0, $date['month'],
|
||||
$date['day'] + $frequencyDay, $date['year']
|
||||
)));
|
||||
$date['month'] = $scheduleDate[0];
|
||||
$date['day'] = $scheduleDate[1];
|
||||
$date['year'] = $scheduleDate[2];
|
||||
}
|
||||
}
|
||||
$newdate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
|
||||
return $newdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate next scheduled pledge payment date. Function calculates next pledge payment date.
|
||||
*
|
||||
* @param array $params
|
||||
* must include frequency unit & frequency interval
|
||||
* @param int $paymentNo
|
||||
* number of payment in sequence (e.g. 1 for first calculated payment (treat initial payment as 0)
|
||||
* @param string $basePaymentDate
|
||||
* date to calculate payments from. This would normally be the
|
||||
* first day of the pledge (default) & is calculated off the 'scheduled date' param. Returned date will
|
||||
* be equal to basePaymentDate normalised to fit the 'pledge pattern' + number of installments
|
||||
*
|
||||
* @return string
|
||||
* formatted date
|
||||
*/
|
||||
public static function calculateNextScheduledDate(&$params, $paymentNo, $basePaymentDate = NULL) {
|
||||
$interval = $paymentNo * ($params['frequency_interval']);
|
||||
if (!$basePaymentDate) {
|
||||
$basePaymentDate = self::calculateBaseScheduleDate($params);
|
||||
}
|
||||
|
||||
//CRM-18316 - change $basePaymentDate for the end dates of the month eg: 29, 30 or 31.
|
||||
if ($params['frequency_unit'] == 'month' && in_array($params['frequency_day'], array(29, 30, 31))) {
|
||||
$frequency = $params['frequency_day'];
|
||||
extract(date_parse($basePaymentDate));
|
||||
$lastDayOfMonth = date('t', mktime($hour, $minute, $second, $month + $interval, 1, $year));
|
||||
// Take the last day in case the current month is Feb or frequency_day is set to 31.
|
||||
if (in_array($lastDayOfMonth, array(28, 29)) || $frequency == 31) {
|
||||
$frequency = 0;
|
||||
$interval++;
|
||||
}
|
||||
$basePaymentDate = array(
|
||||
'M' => $month,
|
||||
'd' => $frequency,
|
||||
'Y' => $year,
|
||||
);
|
||||
}
|
||||
|
||||
return CRM_Utils_Date::format(
|
||||
CRM_Utils_Date::intervalAdd(
|
||||
$params['frequency_unit'],
|
||||
$interval,
|
||||
$basePaymentDate
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the pledge status.
|
||||
*
|
||||
* @param int $pledgeId
|
||||
* Pledge id.
|
||||
*
|
||||
* @return int
|
||||
* $statusId calculated status id of pledge
|
||||
*/
|
||||
public static function calculatePledgeStatus($pledgeId) {
|
||||
$paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
|
||||
$pledgeStatusTypes = CRM_Pledge_BAO_Pledge::buildOptions('status_id', 'validate');
|
||||
|
||||
//return if the pledge is cancelled.
|
||||
$currentPledgeStatusId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeId, 'status_id', 'id', TRUE);
|
||||
if ($currentPledgeStatusId == array_search('Cancelled', $pledgeStatusTypes)) {
|
||||
return $currentPledgeStatusId;
|
||||
}
|
||||
|
||||
// retrieve all pledge payments for this particular pledge
|
||||
$allPledgePayments = $allStatus = array();
|
||||
$returnProperties = array('status_id');
|
||||
CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', $pledgeId, $allPledgePayments, $returnProperties);
|
||||
|
||||
// build pledge payment statuses
|
||||
foreach ($allPledgePayments as $key => $value) {
|
||||
$allStatus[$value['id']] = $paymentStatusTypes[$value['status_id']];
|
||||
}
|
||||
|
||||
if (array_search('Overdue', $allStatus)) {
|
||||
$statusId = array_search('Overdue', $pledgeStatusTypes);
|
||||
}
|
||||
elseif (array_search('Completed', $allStatus)) {
|
||||
if (count(array_count_values($allStatus)) == 1) {
|
||||
$statusId = array_search('Completed', $pledgeStatusTypes);
|
||||
}
|
||||
else {
|
||||
$statusId = array_search('In Progress', $pledgeStatusTypes);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$statusId = array_search('Pending', $pledgeStatusTypes);
|
||||
}
|
||||
|
||||
return $statusId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update pledge payment table.
|
||||
*
|
||||
* @param int $pledgeId
|
||||
* Pledge id.
|
||||
* @param int $paymentStatusId
|
||||
* Payment status id to set.
|
||||
* @param array $paymentIds
|
||||
* Payment ids to be updated.
|
||||
* @param float|int $actualAmount , actual amount being paid
|
||||
* @param int $contributionId
|
||||
* , Id of associated contribution when payment is recorded.
|
||||
* @param bool $isScriptUpdate
|
||||
* , is function being called from bin script?.
|
||||
*
|
||||
*/
|
||||
public static function updatePledgePayments(
|
||||
$pledgeId,
|
||||
$paymentStatusId,
|
||||
$paymentIds = NULL,
|
||||
$actualAmount = 0,
|
||||
$contributionId = NULL,
|
||||
$isScriptUpdate = FALSE
|
||||
) {
|
||||
$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
|
||||
$paymentClause = NULL;
|
||||
if (!empty($paymentIds)) {
|
||||
$payments = implode(',', $paymentIds);
|
||||
$paymentClause = " AND civicrm_pledge_payment.id IN ( {$payments} )";
|
||||
}
|
||||
elseif ($paymentStatusId == array_search('Cancelled', $allStatus)) {
|
||||
$completedStatus = array_search('Completed', $allStatus);
|
||||
$paymentClause = " AND civicrm_pledge_payment.status_id != {$completedStatus}";
|
||||
}
|
||||
$actualAmountClause = NULL;
|
||||
$contributionIdClause = NULL;
|
||||
if (isset($contributionId) && !$isScriptUpdate) {
|
||||
$contributionIdClause = ", civicrm_pledge_payment.contribution_id = {$contributionId}";
|
||||
$actualAmountClause = ", civicrm_pledge_payment.actual_amount = {$actualAmount}";
|
||||
}
|
||||
|
||||
$query = "
|
||||
UPDATE civicrm_pledge_payment
|
||||
SET civicrm_pledge_payment.status_id = {$paymentStatusId}
|
||||
{$actualAmountClause} {$contributionIdClause}
|
||||
WHERE civicrm_pledge_payment.pledge_id = %1
|
||||
{$paymentClause}
|
||||
";
|
||||
|
||||
CRM_Core_DAO::executeQuery($query, array(1 => array($pledgeId, 'Integer')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update pledge payment table when reminder is sent.
|
||||
*
|
||||
* @param int $paymentId
|
||||
* Payment id.
|
||||
*/
|
||||
public static function updateReminderDetails($paymentId) {
|
||||
$query = "
|
||||
UPDATE civicrm_pledge_payment
|
||||
SET civicrm_pledge_payment.reminder_date = CURRENT_TIMESTAMP,
|
||||
civicrm_pledge_payment.reminder_count = civicrm_pledge_payment.reminder_count + 1
|
||||
WHERE civicrm_pledge_payment.id = {$paymentId}
|
||||
";
|
||||
$dao = CRM_Core_DAO::executeQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get oldest pending or in progress pledge payments.
|
||||
*
|
||||
* @param int $pledgeID
|
||||
* Pledge id.
|
||||
*
|
||||
* @param int $limit
|
||||
*
|
||||
* @return array
|
||||
* associated array of pledge details
|
||||
*/
|
||||
public static function getOldestPledgePayment($pledgeID, $limit = 1) {
|
||||
// get pending / overdue statuses
|
||||
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
|
||||
FALSE, FALSE, FALSE, NULL, 'name'
|
||||
);
|
||||
|
||||
// get pending and overdue payments
|
||||
$status[] = array_search('Pending', $pledgeStatuses);
|
||||
$status[] = array_search('Overdue', $pledgeStatuses);
|
||||
|
||||
$statusClause = " IN (" . implode(',', $status) . ")";
|
||||
|
||||
$query = "
|
||||
SELECT civicrm_pledge_payment.id id, civicrm_pledge_payment.scheduled_amount amount, civicrm_pledge_payment.currency, civicrm_pledge_payment.scheduled_date,civicrm_pledge.financial_type_id
|
||||
FROM civicrm_pledge, civicrm_pledge_payment
|
||||
WHERE civicrm_pledge.id = civicrm_pledge_payment.pledge_id
|
||||
AND civicrm_pledge_payment.status_id {$statusClause}
|
||||
AND civicrm_pledge.id = %1
|
||||
ORDER BY civicrm_pledge_payment.scheduled_date ASC
|
||||
LIMIT 0, %2
|
||||
";
|
||||
|
||||
$params[1] = array($pledgeID, 'Integer');
|
||||
$params[2] = array($limit, 'Integer');
|
||||
$payment = CRM_Core_DAO::executeQuery($query, $params);
|
||||
$count = 1;
|
||||
$paymentDetails = array();
|
||||
while ($payment->fetch()) {
|
||||
$paymentDetails[] = array(
|
||||
'id' => $payment->id,
|
||||
'amount' => $payment->amount,
|
||||
'currency' => $payment->currency,
|
||||
'schedule_date' => $payment->scheduled_date,
|
||||
'financial_type_id' => $payment->financial_type_id,
|
||||
'count' => $count,
|
||||
);
|
||||
$count++;
|
||||
}
|
||||
return end($paymentDetails);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pledgeID
|
||||
* @param $actualAmount
|
||||
* @param $pledgeScheduledAmount
|
||||
* @param int $paymentContributionId
|
||||
* @param int $pPaymentId
|
||||
* @param int $paymentStatusID
|
||||
*/
|
||||
public static function adjustPledgePayment($pledgeID, $actualAmount, $pledgeScheduledAmount, $paymentContributionId = NULL, $pPaymentId = NULL, $paymentStatusID = NULL) {
|
||||
$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
|
||||
$paymentStatusName = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_PledgePayment', 'status_id', $paymentStatusID);
|
||||
if ($paymentStatusName == 'Cancelled'|| $paymentStatusName == 'Refunded') {
|
||||
$query = "
|
||||
SELECT civicrm_pledge_payment.id id
|
||||
FROM civicrm_pledge_payment
|
||||
WHERE civicrm_pledge_payment.contribution_id = {$paymentContributionId}
|
||||
";
|
||||
$paymentsAffected = CRM_Core_DAO::executeQuery($query);
|
||||
$paymentIDs = array();
|
||||
while ($paymentsAffected->fetch()) {
|
||||
$paymentIDs[] = $paymentsAffected->id;
|
||||
}
|
||||
// Reset the affected values by the amount paid more than the scheduled amount
|
||||
foreach ($paymentIDs as $key => $value) {
|
||||
$payment = new CRM_Pledge_DAO_PledgePayment();
|
||||
$payment->id = $value;
|
||||
if ($payment->find(TRUE)) {
|
||||
$payment->contribution_id = 'null';
|
||||
$payment->status_id = array_search('Pending', $allStatus);
|
||||
$payment->scheduled_date = NULL;
|
||||
$payment->reminder_date = NULL;
|
||||
$payment->scheduled_amount = $pledgeScheduledAmount;
|
||||
$payment->actual_amount = 'null';
|
||||
$payment->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Cancel the initial paid amount
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', reset($paymentIDs), 'status_id', $paymentStatusID, 'id');
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', reset($paymentIDs), 'actual_amount', $actualAmount, 'id');
|
||||
|
||||
// Add new payment after the last payment for the pledge
|
||||
$allPayments = self::getPledgePayments($pledgeID);
|
||||
$lastPayment = array_pop($allPayments);
|
||||
|
||||
$pledgeFrequencyUnit = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_unit', 'id');
|
||||
$pledgeFrequencyInterval = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeID, 'frequency_interval', 'id');
|
||||
$pledgeScheduledDate = $lastPayment['scheduled_date'];
|
||||
$scheduled_date = CRM_Utils_Date::processDate($pledgeScheduledDate);
|
||||
$date['year'] = (int) substr($scheduled_date, 0, 4);
|
||||
$date['month'] = (int) substr($scheduled_date, 4, 2);
|
||||
$date['day'] = (int) substr($scheduled_date, 6, 2);
|
||||
$newDate = date('YmdHis', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
|
||||
$ScheduledDate = CRM_Utils_Date::format(CRM_Utils_Date::intervalAdd($pledgeFrequencyUnit, $pledgeFrequencyInterval, $newDate));
|
||||
$pledgeParams = array(
|
||||
'status_id' => array_search('Pending', $allStatus),
|
||||
'pledge_id' => $pledgeID,
|
||||
'scheduled_amount' => $pledgeScheduledAmount,
|
||||
'scheduled_date' => $ScheduledDate,
|
||||
);
|
||||
$payment = self::add($pledgeParams);
|
||||
}
|
||||
else {
|
||||
$nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID);
|
||||
if (!$paymentContributionId) {
|
||||
// means we are editing payment scheduled payment, so get the second pending to update.
|
||||
$nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID, 2);
|
||||
if (($nextPledgeInstallmentDue['count'] != 1) && ($nextPledgeInstallmentDue['id'] == $pPaymentId)) {
|
||||
$nextPledgeInstallmentDue = self::getOldestPledgePayment($pledgeID);
|
||||
}
|
||||
}
|
||||
|
||||
if ($nextPledgeInstallmentDue) {
|
||||
// not the last scheduled payment and the actual amount is less than the expected , add it to oldest pending.
|
||||
if (($actualAmount != $pledgeScheduledAmount) && (($actualAmount < $pledgeScheduledAmount) || (($actualAmount - $pledgeScheduledAmount) < $nextPledgeInstallmentDue['amount']))) {
|
||||
$oldScheduledAmount = $nextPledgeInstallmentDue['amount'];
|
||||
$newScheduledAmount = $oldScheduledAmount + ($pledgeScheduledAmount - $actualAmount);
|
||||
// store new amount in oldest pending payment record.
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$nextPledgeInstallmentDue['id'],
|
||||
'scheduled_amount',
|
||||
$newScheduledAmount
|
||||
);
|
||||
if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'contribution_id', 'id')) {
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$nextPledgeInstallmentDue['id'],
|
||||
'contribution_id',
|
||||
$paymentContributionId
|
||||
);
|
||||
}
|
||||
}
|
||||
elseif (($actualAmount > $pledgeScheduledAmount) && (($actualAmount - $pledgeScheduledAmount) >= $nextPledgeInstallmentDue['amount'])) {
|
||||
// here the actual amount is greater than expected and also greater than the next installment amount, so update the next installment as complete and again add it to next subsequent pending payment
|
||||
// set the actual amount of the next pending to '0', set contribution Id to current contribution Id and status as completed
|
||||
$paymentId = array($nextPledgeInstallmentDue['id']);
|
||||
self::updatePledgePayments($pledgeID, array_search('Completed', $allStatus), $paymentId, 0, $paymentContributionId);
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $nextPledgeInstallmentDue['id'], 'scheduled_amount', 0, 'id');
|
||||
if (!$paymentContributionId) {
|
||||
// means we are editing payment scheduled payment.
|
||||
$oldestPaymentAmount = self::getOldestPledgePayment($pledgeID, 2);
|
||||
}
|
||||
$newActualAmount = round(($actualAmount - $pledgeScheduledAmount), CRM_Utils_Money::getCurrencyPrecision());
|
||||
$newPledgeScheduledAmount = $nextPledgeInstallmentDue['amount'];
|
||||
if (!$paymentContributionId) {
|
||||
$newActualAmount = ($actualAmount - $pledgeScheduledAmount);
|
||||
$newPledgeScheduledAmount = $oldestPaymentAmount['amount'];
|
||||
// means we are editing payment scheduled payment, so update scheduled amount.
|
||||
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment',
|
||||
$oldestPaymentAmount['id'],
|
||||
'scheduled_amount',
|
||||
$newActualAmount
|
||||
);
|
||||
}
|
||||
if ($newActualAmount > 0) {
|
||||
self::adjustPledgePayment($pledgeID, $newActualAmount, $newPledgeScheduledAmount, $paymentContributionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override buildOptions to hack out some statuses.
|
||||
*
|
||||
* @todo instead of using & hacking the shared optionGroup contribution_status use a separate one.
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @param string $context
|
||||
* @param array $props
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public static function buildOptions($fieldName, $context = NULL, $props = array()) {
|
||||
$result = parent::buildOptions($fieldName, $context, $props);
|
||||
if ($fieldName == 'status_id') {
|
||||
$result = CRM_Pledge_BAO_Pledge::buildOptions($fieldName, $context, $props);
|
||||
$result = array_diff($result, array('Failed', 'In Progress'));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
605
sites/all/modules/civicrm/CRM/Pledge/BAO/Query.php
Normal file
605
sites/all/modules/civicrm/CRM/Pledge/BAO/Query.php
Normal file
|
@ -0,0 +1,605 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Pledge_BAO_Query extends CRM_Core_BAO_Query {
|
||||
/**
|
||||
* Get pledge fields.
|
||||
*
|
||||
* @param bool $checkPermission
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getFields($checkPermission = TRUE) {
|
||||
return CRM_Pledge_BAO_Pledge::exportableFields($checkPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build select for Pledge.
|
||||
*
|
||||
* @param CRM_Contact_BAO_Query $query
|
||||
*/
|
||||
public static function select(&$query) {
|
||||
|
||||
$statusId = implode(',', array_keys(CRM_Core_PseudoConstant::accountOptionValues("contribution_status", NULL, " AND v.name IN ('Pending', 'Overdue')")));
|
||||
if (($query->_mode & CRM_Contact_BAO_Query::MODE_PLEDGE) || !empty($query->_returnProperties['pledge_id'])) {
|
||||
$query->_select['pledge_id'] = 'civicrm_pledge.id as pledge_id';
|
||||
$query->_element['pledge_id'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
// add pledge select
|
||||
if (!empty($query->_returnProperties['pledge_amount'])) {
|
||||
$query->_select['pledge_amount'] = 'civicrm_pledge.amount as pledge_amount';
|
||||
$query->_element['pledge_amount'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_original_installment_amount'])) {
|
||||
$query->_select['pledge_original_installment_amount'] = 'civicrm_pledge.original_installment_amount as pledge_original_installment_amount';
|
||||
$query->_element['pledge_original_installment_amount'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['installments'])) {
|
||||
$query->_select['installments'] = 'civicrm_pledge.installments as installments';
|
||||
$query->_element['installments'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_create_date'])) {
|
||||
$query->_select['pledge_create_date'] = 'civicrm_pledge.create_date as pledge_create_date';
|
||||
$query->_element['pledge_create_date'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_start_date'])) {
|
||||
$query->_select['pledge_start_date'] = 'civicrm_pledge.start_date as pledge_start_date';
|
||||
$query->_element['pledge_start_date'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_status_id'])) {
|
||||
$query->_select['pledge_status_id'] = 'pledge_status.value as pledge_status_id';
|
||||
$query->_element['pledge_status'] = 1;
|
||||
$query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_status'])) {
|
||||
$query->_select['pledge_status'] = 'pledge_status.label as pledge_status';
|
||||
$query->_element['pledge_status'] = 1;
|
||||
$query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_total_paid'])) {
|
||||
$query->_select['pledge_total_paid'] = ' (SELECT sum(civicrm_pledge_payment.actual_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 1 ) as pledge_total_paid';
|
||||
$query->_element['pledge_total_paid'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_next_pay_date'])) {
|
||||
$query->_select['pledge_next_pay_date'] = " (SELECT civicrm_pledge_payment.scheduled_date FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_date";
|
||||
$query->_element['pledge_next_pay_date'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_next_pay_amount'])) {
|
||||
$query->_select['pledge_next_pay_amount'] = " (SELECT civicrm_pledge_payment.scheduled_amount FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_amount";
|
||||
$query->_element['pledge_next_pay_amount'] = 1;
|
||||
|
||||
$query->_select['pledge_outstanding_amount'] = " (SELECT sum(civicrm_pledge_payment.scheduled_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 6 ) as pledge_outstanding_amount";
|
||||
$query->_element['pledge_outstanding_amount'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_financial_type'])) {
|
||||
$query->_select['pledge_financial_type'] = "(SELECT civicrm_financial_type.name FROM civicrm_financial_type WHERE civicrm_financial_type.id = civicrm_pledge.financial_type_id) as pledge_financial_type";
|
||||
$query->_element['pledge_financial_type'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_contribution_page_id'])) {
|
||||
$query->_select['pledge_contribution_page_id'] = 'civicrm_pledge.contribution_page_id as pledge_contribution_page_id';
|
||||
$query->_element['pledge_contribution_page_id'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_id'])) {
|
||||
$query->_select['pledge_payment_id'] = 'civicrm_pledge_payment.id as pledge_payment_id';
|
||||
$query->_element['pledge_payment_id'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_scheduled_amount'])) {
|
||||
$query->_select['pledge_payment_scheduled_amount'] = 'civicrm_pledge_payment.scheduled_amount as pledge_payment_scheduled_amount';
|
||||
$query->_element['pledge_payment_scheduled_amount'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_scheduled_date'])) {
|
||||
$query->_select['pledge_payment_scheduled_date'] = 'civicrm_pledge_payment.scheduled_date as pledge_payment_scheduled_date';
|
||||
$query->_element['pledge_payment_scheduled_date'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_paid_amount'])) {
|
||||
$query->_select['pledge_payment_paid_amount'] = 'civicrm_pledge_payment.actual_amount as pledge_payment_paid_amount';
|
||||
$query->_element['pledge_payment_paid_amount'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_paid_date'])) {
|
||||
$query->_select['pledge_payment_paid_date'] = 'payment_contribution.receive_date as pledge_payment_paid_date';
|
||||
$query->_element['pledge_payment_paid_date'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
$query->_tables['payment_contribution'] = $query->_whereTables['payment_contribution'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_reminder_date'])) {
|
||||
$query->_select['pledge_payment_reminder_date'] = 'civicrm_pledge_payment.reminder_date as pledge_payment_reminder_date';
|
||||
$query->_element['pledge_payment_reminder_date'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_reminder_count'])) {
|
||||
$query->_select['pledge_payment_reminder_count'] = 'civicrm_pledge_payment.reminder_count as pledge_payment_reminder_count';
|
||||
$query->_element['pledge_payment_reminder_count'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_status_id'])) {
|
||||
$query->_select['pledge_payment_status_id'] = 'payment_status.name as pledge_payment_status_id';
|
||||
$query->_element['pledge_payment_status_id'] = 1;
|
||||
$query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_payment_status'])) {
|
||||
$query->_select['pledge_payment_status'] = 'payment_status.label as pledge_payment_status';
|
||||
$query->_element['pledge_payment_status'] = 1;
|
||||
$query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_frequency_interval'])) {
|
||||
$query->_select['pledge_frequency_interval'] = 'civicrm_pledge.frequency_interval as pledge_frequency_interval';
|
||||
$query->_element['pledge_frequency_interval'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_frequency_unit'])) {
|
||||
$query->_select['pledge_frequency_unit'] = 'civicrm_pledge.frequency_unit as pledge_frequency_unit';
|
||||
$query->_element['pledge_frequency_unit'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_is_test'])) {
|
||||
$query->_select['pledge_is_test'] = 'civicrm_pledge.is_test as pledge_is_test';
|
||||
$query->_element['pledge_is_test'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
if (!empty($query->_returnProperties['pledge_campaign_id'])) {
|
||||
$query->_select['pledge_campaign_id'] = 'civicrm_pledge.campaign_id as pledge_campaign_id';
|
||||
$query->_element['pledge_campaign_id'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
|
||||
if (!empty($query->_returnProperties['pledge_currency'])) {
|
||||
$query->_select['pledge_currency'] = 'civicrm_pledge.currency as pledge_currency';
|
||||
$query->_element['pledge_currency'] = 1;
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
*/
|
||||
public static function where(&$query) {
|
||||
$grouping = NULL;
|
||||
foreach (array_keys($query->_params) as $id) {
|
||||
if (empty($query->_params[$id][0])) {
|
||||
continue;
|
||||
}
|
||||
if (substr($query->_params[$id][0], 0, 7) == 'pledge_') {
|
||||
if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
|
||||
$query->_useDistinct = TRUE;
|
||||
}
|
||||
$grouping = $query->_params[$id][3];
|
||||
self::whereClauseSingle($query->_params[$id], $query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $values
|
||||
* @param $query
|
||||
*/
|
||||
public static function whereClauseSingle(&$values, &$query) {
|
||||
list($name, $op, $value, $grouping, $wildcard) = $values;
|
||||
|
||||
switch ($name) {
|
||||
case 'pledge_create_date_low':
|
||||
case 'pledge_create_date_high':
|
||||
// process to / from date
|
||||
$query->dateQueryBuilder($values,
|
||||
'civicrm_pledge', 'pledge_create_date', 'create_date', 'Pledge Made'
|
||||
);
|
||||
case 'pledge_start_date_low':
|
||||
case 'pledge_start_date_high':
|
||||
// process to / from date
|
||||
$query->dateQueryBuilder($values,
|
||||
'civicrm_pledge', 'pledge_start_date', 'start_date', 'Pledge Start Date'
|
||||
);
|
||||
return;
|
||||
|
||||
case 'pledge_end_date_low':
|
||||
case 'pledge_end_date_high':
|
||||
// process to / from date
|
||||
$query->dateQueryBuilder($values,
|
||||
'civicrm_pledge', 'pledge_end_date', 'end_date', 'Pledge End Date'
|
||||
);
|
||||
return;
|
||||
|
||||
case 'pledge_payment_date_low':
|
||||
case 'pledge_payment_date_high':
|
||||
// process to / from date
|
||||
$query->dateQueryBuilder($values,
|
||||
'civicrm_pledge_payment', 'pledge_payment_date', 'scheduled_date', 'Payment Scheduled'
|
||||
);
|
||||
return;
|
||||
|
||||
case 'pledge_amount':
|
||||
case 'pledge_amount_low':
|
||||
case 'pledge_amount_high':
|
||||
// process min/max amount
|
||||
$query->numberRangeBuilder($values,
|
||||
'civicrm_pledge', 'pledge_amount', 'amount', 'Pledge Amount'
|
||||
);
|
||||
return;
|
||||
|
||||
case 'pledge_installments_low':
|
||||
case 'pledge_installments_high':
|
||||
// process min/max amount
|
||||
$query->numberRangeBuilder($values,
|
||||
'civicrm_pledge', 'pledge_installments', 'installments', 'Number of Installments'
|
||||
);
|
||||
return;
|
||||
|
||||
case 'pledge_acknowledge_date_is_not_null':
|
||||
if ($value) {
|
||||
$op = "IS NOT NULL";
|
||||
$query->_qill[$grouping][] = ts('Pledge Acknowledgement Sent');
|
||||
}
|
||||
else {
|
||||
$op = "IS NULL";
|
||||
$query->_qill[$grouping][] = ts('Pledge Acknowledgement Not Sent');
|
||||
}
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.acknowledge_date", $op);
|
||||
return;
|
||||
|
||||
case 'pledge_payment_status_id':
|
||||
case 'pledge_status_id':
|
||||
if ($name == 'pledge_status_id') {
|
||||
$tableName = 'civicrm_pledge';
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
$label = "Pledge Status";
|
||||
$qillDAO = 'CRM_Pledge_DAO_Pledge';
|
||||
$qillField = 'status_id';
|
||||
}
|
||||
else {
|
||||
$tableName = 'civicrm_pledge_payment';
|
||||
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
|
||||
$label = "Pledge Payment Status";
|
||||
$qillDAO = 'CRM_Contribute_DAO_Contribution';
|
||||
$qillField = 'contribution_status_id';
|
||||
}
|
||||
$name = 'status_id';
|
||||
if (!empty($value) && is_array($value) && !in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
|
||||
$value = array('IN' => $value);
|
||||
}
|
||||
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name",
|
||||
$op,
|
||||
$value,
|
||||
'Integer'
|
||||
);
|
||||
list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($qillDAO, $qillField, $value, $op);
|
||||
$query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $label, 2 => $qillop, 3 => $qillVal));
|
||||
return;
|
||||
|
||||
case 'pledge_test':
|
||||
case 'pledge_is_test':
|
||||
// We dont want to include all tests for sql OR CRM-7827
|
||||
if (!$value || $query->getOperator() != 'OR') {
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.is_test',
|
||||
$op,
|
||||
$value,
|
||||
'Boolean'
|
||||
);
|
||||
if ($value) {
|
||||
$query->_qill[$grouping][] = ts('Pledge is a Test');
|
||||
}
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
}
|
||||
return;
|
||||
|
||||
case 'pledge_financial_type_id':
|
||||
$type = CRM_Contribute_PseudoConstant::financialType($value);
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.financial_type_id',
|
||||
$op,
|
||||
$value,
|
||||
'Integer'
|
||||
);
|
||||
$query->_qill[$grouping][] = ts('Financial Type - %1', array(1 => $type));
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
|
||||
case 'pledge_contribution_page_id':
|
||||
$page = CRM_Contribute_PseudoConstant::contributionPage($value);
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.contribution_page_id',
|
||||
$op,
|
||||
$value,
|
||||
'Integer'
|
||||
);
|
||||
$query->_qill[$grouping][] = ts('Financial Page - %1', array(1 => $page));
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
|
||||
case 'pledge_id':
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.id",
|
||||
$op,
|
||||
$value,
|
||||
"Integer"
|
||||
);
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
|
||||
case 'pledge_frequency_interval':
|
||||
$query->_where[$grouping][] = "civicrm_pledge.frequency_interval $op $value";
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
|
||||
case 'pledge_frequency_unit':
|
||||
$query->_where[$grouping][] = "civicrm_pledge.frequency_unit $op $value";
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
|
||||
case 'pledge_contact_id':
|
||||
case 'pledge_campaign_id':
|
||||
$name = str_replace('pledge_', '', $name);
|
||||
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.$name", $op, $value, 'Integer');
|
||||
list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Pledge_DAO_Pledge', $name, $value, $op);
|
||||
$label = ($name == 'campaign_id') ? 'Campaign' : 'Contact ID';
|
||||
$query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $label, 2 => $op, 3 => $value));
|
||||
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* From clause.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $mode
|
||||
* @param string $side
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public static function from($name, $mode, $side) {
|
||||
$from = NULL;
|
||||
|
||||
switch ($name) {
|
||||
case 'civicrm_pledge':
|
||||
$from = " $side JOIN civicrm_pledge ON civicrm_pledge.contact_id = contact_a.id ";
|
||||
break;
|
||||
|
||||
case 'pledge_status':
|
||||
$from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'pledge_status')";
|
||||
$from .= " $side JOIN civicrm_option_value pledge_status ON (civicrm_pledge.status_id = pledge_status.value AND option_group_pledge_status.id = pledge_status.option_group_id ) ";
|
||||
break;
|
||||
|
||||
case 'pledge_financial_type':
|
||||
$from .= " $side JOIN civicrm_financial_type ON civicrm_pledge.financial_type_id = civicrm_financial_type.id ";
|
||||
break;
|
||||
|
||||
case 'civicrm_pledge_payment':
|
||||
$from .= " $side JOIN civicrm_pledge_payment ON civicrm_pledge_payment.pledge_id = civicrm_pledge.id ";
|
||||
break;
|
||||
|
||||
case 'payment_contribution':
|
||||
$from .= " $side JOIN civicrm_contribution payment_contribution ON civicrm_pledge_payment.contribution_id = payment_contribution.id ";
|
||||
break;
|
||||
|
||||
case 'payment_status':
|
||||
$from .= " $side JOIN civicrm_option_group option_group_payment_status ON (option_group_payment_status.name = 'contribution_status')";
|
||||
$from .= " $side JOIN civicrm_option_value payment_status ON (civicrm_pledge_payment.status_id = payment_status.value AND option_group_payment_status.id = payment_status.option_group_id ) ";
|
||||
break;
|
||||
}
|
||||
|
||||
return $from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ideally this function should include fields that are displayed in the selector.
|
||||
*
|
||||
* @param int $mode
|
||||
* @param bool $includeCustomFields
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function defaultReturnProperties(
|
||||
$mode,
|
||||
$includeCustomFields = TRUE
|
||||
) {
|
||||
$properties = NULL;
|
||||
|
||||
if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
|
||||
$properties = array(
|
||||
'contact_type' => 1,
|
||||
'contact_sub_type' => 1,
|
||||
'sort_name' => 1,
|
||||
'display_name' => 1,
|
||||
'pledge_id' => 1,
|
||||
'pledge_amount' => 1,
|
||||
'pledge_total_paid' => 1,
|
||||
'pledge_create_date' => 1,
|
||||
'pledge_start_date' => 1,
|
||||
'pledge_next_pay_date' => 1,
|
||||
'pledge_next_pay_amount' => 1,
|
||||
'pledge_status' => 1,
|
||||
'pledge_status_id' => 1,
|
||||
'pledge_is_test' => 1,
|
||||
'pledge_contribution_page_id' => 1,
|
||||
'pledge_financial_type' => 1,
|
||||
'pledge_frequency_interval' => 1,
|
||||
'pledge_frequency_unit' => 1,
|
||||
'pledge_currency' => 1,
|
||||
'pledge_campaign_id' => 1,
|
||||
);
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* This includes any extra fields that might need for export etc.
|
||||
*
|
||||
* @param string $mode
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function extraReturnProperties($mode) {
|
||||
$properties = NULL;
|
||||
|
||||
if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
|
||||
$properties = array(
|
||||
'pledge_balance_amount' => 1,
|
||||
'pledge_payment_id' => 1,
|
||||
'pledge_payment_scheduled_amount' => 1,
|
||||
'pledge_payment_scheduled_date' => 1,
|
||||
'pledge_payment_paid_amount' => 1,
|
||||
'pledge_payment_paid_date' => 1,
|
||||
'pledge_payment_reminder_date' => 1,
|
||||
'pledge_payment_reminder_count' => 1,
|
||||
'pledge_payment_status_id' => 1,
|
||||
'pledge_payment_status' => 1,
|
||||
);
|
||||
|
||||
// also get all the custom pledge properties
|
||||
$fields = CRM_Core_BAO_CustomField::getFieldsForImport('Pledge');
|
||||
if (!empty($fields)) {
|
||||
foreach ($fields as $name => $dontCare) {
|
||||
$properties[$name] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CRM_Core_Form $form
|
||||
*/
|
||||
public static function buildSearchForm(&$form) {
|
||||
// pledge related dates
|
||||
CRM_Core_Form_Date::buildDateRange($form, 'pledge_start_date', 1, '_low', '_high', ts('From'), FALSE);
|
||||
CRM_Core_Form_Date::buildDateRange($form, 'pledge_end_date', 1, '_low', '_high', ts('From'), FALSE);
|
||||
CRM_Core_Form_Date::buildDateRange($form, 'pledge_create_date', 1, '_low', '_high', ts('From'), FALSE);
|
||||
|
||||
// pledge payment related dates
|
||||
CRM_Core_Form_Date::buildDateRange($form, 'pledge_payment_date', 1, '_low', '_high', ts('From'), FALSE);
|
||||
|
||||
$form->addYesNo('pledge_test', ts('Pledge is a Test?'), TRUE);
|
||||
$form->add('text', 'pledge_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
|
||||
$form->addRule('pledge_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
|
||||
|
||||
$form->add('text', 'pledge_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
|
||||
$form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
|
||||
|
||||
$form->add('select', 'pledge_status_id',
|
||||
ts('Pledge Status'), CRM_Pledge_BAO_Pledge::buildOptions('status_id'),
|
||||
FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple')
|
||||
);
|
||||
|
||||
$form->addYesNo('pledge_acknowledge_date_is_not_null', ts('Acknowledgement sent?'), TRUE);
|
||||
|
||||
$form->add('text', 'pledge_installments_low', ts('From'), array('size' => 8, 'maxlength' => 8));
|
||||
$form->addRule('pledge_installments_low', ts('Please enter a number'), 'integer');
|
||||
|
||||
$form->add('text', 'pledge_installments_high', ts('To'), array('size' => 8, 'maxlength' => 8));
|
||||
$form->addRule('pledge_installments_high', ts('Please enter number.'), 'integer');
|
||||
|
||||
$form->add('select', 'pledge_payment_status_id',
|
||||
ts('Pledge Payment Status'), CRM_Pledge_BAO_PledgePayment::buildOptions('status_id'),
|
||||
FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple')
|
||||
);
|
||||
|
||||
$form->add('select', 'pledge_financial_type_id',
|
||||
ts('Financial Type'),
|
||||
array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType(),
|
||||
FALSE, array('class' => 'crm-select2')
|
||||
);
|
||||
|
||||
$form->add('select', 'pledge_contribution_page_id',
|
||||
ts('Contribution Page'),
|
||||
array('' => ts('- any -')) + CRM_Contribute_PseudoConstant::contributionPage(),
|
||||
FALSE, array('class' => 'crm-select2')
|
||||
);
|
||||
|
||||
// add fields for pledge frequency
|
||||
$form->add('text', 'pledge_frequency_interval', ts('Every'), array('size' => 8, 'maxlength' => 8));
|
||||
$form->addRule('pledge_frequency_interval', ts('Please enter valid Pledge Frequency Interval'), 'integer');
|
||||
$frequencies = CRM_Core_OptionGroup::values('recur_frequency_units');
|
||||
foreach ($frequencies as $val => $label) {
|
||||
$freqUnitsDisplay["'{$val}'"] = ts('%1(s)', array(1 => $label));
|
||||
}
|
||||
|
||||
$form->add('select', 'pledge_frequency_unit',
|
||||
ts('Pledge Frequency'),
|
||||
array('' => ts('- any -')) + $freqUnitsDisplay
|
||||
);
|
||||
|
||||
self::addCustomFormFields($form, array('Pledge'));
|
||||
|
||||
CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'pledge_campaign_id');
|
||||
|
||||
$form->assign('validCiviPledge', TRUE);
|
||||
$form->setDefaults(array('pledge_test' => 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tables
|
||||
*/
|
||||
public static function tableNames(&$tables) {
|
||||
// add status table
|
||||
if (!empty($tables['pledge_status']) || !empty($tables['civicrm_pledge_payment'])) {
|
||||
$tables = array_merge(array('civicrm_pledge' => 1), $tables);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue