Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into afform-email-conf…
Browse files Browse the repository at this point in the history
…irmation
  • Loading branch information
kurund committed Dec 6, 2023
2 parents c8cd976 + 12f7a46 commit 32437ed
Show file tree
Hide file tree
Showing 107 changed files with 4,415 additions and 4,204 deletions.
7 changes: 7 additions & 0 deletions CRM/Admin/Form/PreferencesDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
class CRM_Admin_Form_PreferencesDate extends CRM_Admin_Form {

/**
* @return string
*/
public function getDefaultEntity(): string {
return 'PreferencesDate';
}

/**
* Build the form object.
*/
Expand Down
2 changes: 1 addition & 1 deletion CRM/Api4/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function execute(string $entity, string $action, array $params = [], $in
$status = $statusMap[get_class($e)] ?? 500;
// Send error code (but don't overwrite success code if there are multiple calls and one was successful)
$this->httpResponseCode = $this->httpResponseCode ?: $status;
if (CRM_Core_Permission::check('view debug output') || ($e->getErrorData()['show_detailed_error'] ?? FALSE)) {
if (CRM_Core_Permission::check('view debug output') || (method_exists($e, 'getErrorData') && ($e->getErrorData()['show_detailed_error'] ?? FALSE))) {
$response['error_code'] = $e->getCode();
$response['error_message'] = $e->getMessage();
if (!empty($params['debug']) && CRM_Core_Permission::check('view debug output')) {
Expand Down
20 changes: 10 additions & 10 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function import(array $values): void {
}
}

[$formatted, $params] = $this->processContact($params, $formatted, TRUE);
$formatted['id'] = $params['id'] = $this->processContact($params, TRUE);

//format common data, CRM-4062
$this->formatCommonData($params, $formatted);
Expand All @@ -177,7 +177,8 @@ public function import(array $values): void {

//relationship contact insert
foreach ($this->getRelatedContactsParams($params) as $key => $field) {
[$formatting, $field] = $this->processContact($field, $field, FALSE);
$field['id'] = $this->processContact($field, FALSE);
$formatting = $field;
//format common data, CRM-4062
$this->formatCommonData($field, $formatting);
$isUpdate = empty($formatting['id']) ? 'new' : 'updated';
Expand Down Expand Up @@ -1546,25 +1547,24 @@ protected function lookupContactID(array $params, bool $isMainContact): ?int {

/**
* @param array $params
* @param array $formatted
* @param bool $isMainContact
*
* @return array[]
* @return int|null
* @throws \CRM_Core_Exception
*/
protected function processContact(array $params, array $formatted, bool $isMainContact): array {
$params['id'] = $formatted['id'] = $this->lookupContactID($params, $isMainContact);
if ($params['id'] && !empty($params['contact_sub_type'])) {
protected function processContact(array $params, bool $isMainContact): ?int {
$contactID = $this->lookupContactID($params, $isMainContact);
if ($contactID && !empty($params['contact_sub_type'])) {
$contactSubType = Contact::get(FALSE)
->addWhere('id', '=', $params['id'])
->addWhere('id', '=', $contactID)
->addSelect('contact_sub_type')
->execute()
->first()['contact_sub_type'];
if (!empty($contactSubType) && $contactSubType[0] !== $params['contact_sub_type'] && !CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType[0])) {
if (!empty($contactSubType) && $contactSubType[0] !== $params['contact_sub_type'] && !CRM_Contact_BAO_ContactType::isAllowEdit($contactID, $contactSubType[0])) {
throw new CRM_Core_Exception('Mismatched contact SubTypes :', CRM_Import_Parser::NO_MATCH);
}
}
return [$formatted, $params];
return $contactID;
}

/**
Expand Down
30 changes: 21 additions & 9 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4212,11 +4212,7 @@ public static function updateMembershipBasedOnCompletionOfContribution($contribu
}
// @todo remove all this stuff in favour of letting the api call further down handle in
// (it is a duplication of what the api does).
$dates = array_fill_keys([
'join_date',
'start_date',
'end_date',
], NULL);
$dates = [];
if ($currentMembership) {
/*
* Fixed FOR CRM-4433
Expand All @@ -4239,9 +4235,9 @@ public static function updateMembershipBasedOnCompletionOfContribution($contribu
}
else {
//get the status for membership.
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'],
$dates['end_date'],
$dates['join_date'],
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'] ?? NULL,
$dates['end_date'] ?? NULL,
$dates['join_date'] ?? NULL,
'now',
TRUE,
$membershipParams['membership_type_id'],
Expand All @@ -4255,7 +4251,23 @@ public static function updateMembershipBasedOnCompletionOfContribution($contribu
//so make status override false.
$membershipParams['is_override'] = FALSE;
$membershipParams['status_override_end_date'] = 'null';
civicrm_api3('Membership', 'create', $membershipParams);
$membership = civicrm_api3('Membership', 'create', $membershipParams);
$membership = $membership['values'][$membership['id']];
// Update activity to Completed.
// Perhaps this should be in Membership::create? Test cover in
// api_v3_ContributionTest.testPendingToCompleteContribution.
$priorMembershipStatus = $memberships[$membership['id']]['status_id'] ?? NULL;
Activity::update(FALSE)->setValues([
'status_id:name' => 'Completed',
'subject' => ts('Status changed from %1 to %2'), [
1 => CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $priorMembershipStatus),
2 => CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']),
],

])->addWhere('source_record_id', '=', $membership['id'])
->addWhere('status_id:name', '=', 'Scheduled')
->addWhere('activity_type_id:name', 'IN', ['Membership Signup', 'Membership Renewal'])
->execute();
}
}

Expand Down
88 changes: 59 additions & 29 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Payment\Exception\PaymentProcessorException;

/**
* This form records additional payments needed when event/contribution is partially paid.
*/
Expand Down Expand Up @@ -48,7 +50,14 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract

protected $_paymentType = NULL;

protected $_contributionId = NULL;
/**
* Internal property for contribution ID - use getContributionID().
*
* @var int
*
* @internal
*/
protected $_contributionId;

protected $fromEmailId = NULL;

Expand All @@ -72,17 +81,11 @@ public function preProcess() {
$this->assign('id', $this->_id);
$this->assign('suppressPaymentFormButtons', $this->isBeingCalledFromSelectorContext());

if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
if ($this->_view === 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
$title = $this->assignPaymentInfoBlock();
$this->setTitle($title);
return;
}
if ($this->_component == 'event') {
$this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
}
else {
$this->_contributionId = $this->_id;
}

$paymentAmt = $this->getAmountDue();

Expand All @@ -96,7 +99,7 @@ public function preProcess() {
throw new CRM_Core_Exception(ts('Credit card payment is not for Refund payments use'));
}

list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
[$this->_contributorDisplayName, $this->_contributorEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);

$this->assign('contributionMode', $this->_mode);
$this->assign('contactId', $this->_contactID);
Expand Down Expand Up @@ -134,7 +137,7 @@ protected function isBeingCalledFromSelectorContext() {
* @throws \CRM_Core_Exception
*/
public function setDefaultValues() {
if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
if ($this->_view === 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
return NULL;
}
$defaults = [];
Expand Down Expand Up @@ -309,9 +312,6 @@ public function postProcess() {
public function submit($submittedValues) {
$this->_params = $submittedValues;
$this->beginPostProcess();
// _contributorContactID may no longer need to be set - setting it here
// was for use in processBillingAddress
$this->_contributorContactID = $this->_contactID;
$this->processBillingAddress($this->_contactID, (string) $this->_contributorEmail);
$participantId = NULL;
if ($this->_component === 'event') {
Expand All @@ -334,13 +334,13 @@ public function submit($submittedValues) {
$trxnsData['is_send_contribution_notification'] = FALSE;
$paymentID = civicrm_api3('Payment', 'create', $trxnsData)['id'];

if ($this->_contributionId && CRM_Core_Permission::access('CiviMember')) {
if ($this->getContributionID() && CRM_Core_Permission::access('CiviMember')) {
$membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', ['contribution_id' => $this->_contributionId]);
if ($membershipPaymentCount) {
$this->ajaxResponse['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactID);
}
}
if ($this->_contributionId && CRM_Core_Permission::access('CiviEvent')) {
if ($this->getContributionID() && CRM_Core_Permission::access('CiviEvent')) {
$participantPaymentCount = civicrm_api3('ParticipantPayment', 'getCount', ['contribution_id' => $this->_contributionId]);
if ($participantPaymentCount) {
$this->ajaxResponse['updateTabs']['#tab_participant'] = CRM_Contact_BAO_Contact::getCountComponent('participant', $this->_contactID);
Expand All @@ -359,7 +359,7 @@ public function submit($submittedValues) {
CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
}

public function processCreditCard() {
public function processCreditCard(): ?array {
$config = CRM_Core_Config::singleton();
$session = CRM_Core_Session::singleton();

Expand Down Expand Up @@ -409,11 +409,11 @@ public function processCreditCard() {

if ($paymentParams['amount'] > 0.0) {
try {
// force a reset of the payment processor in case the form changed it, CRM-7179
$payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
// Re-retrieve the payment processor in case the form changed it, CRM-7179
$payment = \Civi\Payment\System::singleton()->getById($this->getPaymentProcessorID());
$result = $payment->doPayment($paymentParams);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
catch (PaymentProcessorException $e) {
Civi::log()->error('Payment processor exception: ' . $e->getMessage());
$urlParams = "action=add&cid={$this->_contactId}&id={$this->_contributionId}&component={$this->_component}&mode={$this->_mode}";
CRM_Core_Error::statusBounce($e->getMessage(), CRM_Utils_System::url('civicrm/payment/add', $urlParams));
Expand All @@ -425,27 +425,26 @@ public function processCreditCard() {
}

$this->set('params', $this->_params);

// set source if not set
if (empty($this->_params['source'])) {
$userID = $session->get('userID');
$userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID,
'sort_name'
);
$this->_params['source'] = ts('Submit Credit Card Payment by: %1', [1 => $userSortName]);
}
return [
'fee_amount' => $result['fee_amount'] ?? 0,
'trxn_id' => $result['trxn_id'] ?? NULL,
'trxn_result_code' => $result['trxn_result_code'] ?? NULL,
];
}

/**
* Wrapper for unit testing the post process submit function.
*
* @deprecated since 5.69 will be removed around 5.75
*
* @param array $params
* @param string|null $creditCardMode
* @param string $entityType
*
* @throws \CRM_Core_Exception
*/
public function testSubmit($params, $creditCardMode = NULL, $entityType = 'contribute') {
CRM_Core_Error::deprecatedFunctionWarning('use FormTrait in tests');
$this->_bltID = 5;
// Required because processCreditCard calls set method on this.
$_SERVER['REQUEST_METHOD'] = 'GET';
Expand Down Expand Up @@ -516,9 +515,40 @@ protected function isARefund() {
*/
protected function getAmountDue(): float {
if (!isset($this->amountDue)) {
$this->amountDue = CRM_Contribute_BAO_Contribution::getContributionBalance($this->_contributionId);
$this->amountDue = CRM_Contribute_BAO_Contribution::getContributionBalance($this->getContributionID());
}
return $this->amountDue;
}

/**
* Get the selected Contribution ID.
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpUnhandledExceptionInspection
*/
public function getContributionID(): int {
if (!$this->_contributionId) {
$component = CRM_Utils_Request::retrieve('component', 'String', $this, FALSE, 'contribution');
if ($component === 'event') {
$this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
}
else {
$this->_contributionId = $this->_id;
}
}
return (int) $this->_contributionId;
}

/**
* Get the payment processor ID.
*
* @return int
*/
public function getPaymentProcessorID(): int {
return (int) ($this->getSubmittedValue('payment_processor_id') ?: $this->_paymentProcessor['id']);
}

}
45 changes: 24 additions & 21 deletions CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,6 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {

$fields = [
'financial_type_id',
'contribution_status_id',
'payment_instrument_id',
'cancel_reason',
'source',
Expand All @@ -1985,6 +1984,21 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
foreach ($fields as $f) {
$params[$f] = $formValues[$f] ?? NULL;
}
if ($this->_id && $action & CRM_Core_Action::UPDATE) {
// Can only be updated to contribution which is handled via Payment.create
$params['contribution_status_id'] = $this->getSubmittedValue('contribution_status_id');

// Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
// else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041
if ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')) {
$params['is_pay_later'] = 1;
}
elseif ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) {
// @todo - if the contribution is new then it should be Pending status & then we use
// Payment.create to update to Completed.
$params['is_pay_later'] = 0;
}
}

$params['revenue_recognition_date'] = NULL;
if (!empty($formValues['revenue_recognition_date'])) {
Expand All @@ -2005,17 +2019,6 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
$params['cancel_date'] = $params['cancel_reason'] = 'null';
}

// Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
// else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041
if ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')) {
$params['is_pay_later'] = 1;
}
elseif ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) {
// @todo - if the contribution is new then it should be Pending status & then we use
// Payment.create to update to Completed.
$params['is_pay_later'] = 0;
}

// Add Additional common information to formatted params.
CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this);
if ($pId) {
Expand All @@ -2036,21 +2039,21 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
if (!empty($params['note']) && !empty($submittedValues['note'])) {
unset($params['note']);
}
$contribution = CRM_Contribute_BAO_Contribution::create($params);

$previousStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $this->_values['contribution_status_id'] ?? NULL);
// process associated membership / participant, CRM-4395
if ($contribution->id && $action & CRM_Core_Action::UPDATE
if ($this->getContributionID() && $this->getAction() & CRM_Core_Action::UPDATE
&& in_array($previousStatus, ['Pending', 'Partially paid'], TRUE)
&& 'Completed' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $this->getSubmittedValue('contribution_status_id'))) {
// @todo use Payment.create to do this, remove transitioncomponents function
// if contribution is being created with a completed status it should be
// created pending & then Payment.create adds the payment
CRM_Contribute_BAO_Contribution::transitionComponents([
'contribution_id' => $contribution->id,
'receive_date' => $contribution->receive_date,
// @todo make users use add payment form.
civicrm_api3('Payment', 'create', [
'contribution_id' => $this->getContributionID(),
'total_amount' => $this->getContributionValue('balance_amount'),
'currency' => $this->getSubmittedValue('currency'),
'payment_instrument_id' => $this->getSubmittedValue('payment_instrument_id'),
'check_number' => $this->getSubmittedValue('check_number'),
]);
}
$contribution = CRM_Contribute_BAO_Contribution::create($params);

array_unshift($this->statusMessage, ts('The contribution record has been saved.'));

Expand Down
Loading

0 comments on commit 32437ed

Please sign in to comment.