}
// @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
}
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'],
//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();
}
}
$fields = [
'financial_type_id',
- 'contribution_status_id',
'payment_instrument_id',
'cancel_reason',
'source',
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'])) {
$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) {
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.'));
// Check if Membership is updated to New.
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
- $this->assertEquals($membership['status_id'], array_search('New', $memStatus));
+ $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
}
/**
'membership_id' => $this->getMembershipID(),
]);
$this->assertEquals(4, $logs['count']);
- //Assert only three activities are created.
+ //Assert activities are created.
$activityNames = (array) ActivityContact::get(FALSE)
- ->addWhere('contact_id', '=', $this->_ids['contact'])
+ ->addWhere('contact_id', '=', $membership['contact_id'])
->addSelect('activity_id.activity_type_id:name')->execute()->indexBy('activity_id.activity_type_id:name');
$this->assertArrayHasKey('Contribution', $activityNames);
$this->assertArrayHasKey('Membership Signup', $activityNames);
*/
public function testPendingToCompleteContribution(): void {
$this->createPriceSetWithPage('membership');
- $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
+ $this->setUpPendingContribution($this->_ids['price_field_value'][0], 'new');
$this->callAPISuccess('membership', 'getsingle', ['id' => $this->_ids['membership']]);
// Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
$activity = $this->callAPISuccess('Activity', 'get', [
$this->assertEquals(date('Y-m-d', strtotime('yesterday + 5 years')), $membership['end_date']);
}
- public function cleanUpAfterPriceSets(): void {
- $this->quickCleanUpFinancialEntities();
- $this->contactDelete($this->_ids['contact']);
- }
-
/**
* Set up a pending transaction with a specific price field id.
*
* @param array $membershipParams
*/
public function setUpPendingContribution(int $priceFieldValueID, string $key = 'first', array $contributionParams = [], array $lineParams = [], array $membershipParams = []): void {
- $contactID = $this->individualCreate();
+ $contactID = $this->ids['Contact']['individual_0'] ?? $this->individualCreate();
$membershipParams = array_merge([
'contact_id' => $contactID,
'membership_type_id' => $this->_ids['membership_type'],
],
], $contributionParams));
- $this->_ids['contact'] = $contactID;
- $this->ids['contribution'][$key] = $contribution['id'];
+ $this->ids['Contribution'][$key] = $contribution['id'];
$this->_ids['membership'] = $this->callAPISuccessGetValue('MembershipPayment', ['return' => 'membership_id', 'contribution_id' => $contribution['id']]);
}
* @return int
*/
protected function getContributionID(string $key = 'first'): int {
- return (int) $this->ids['contribution'][$key];
+ if (count($this->ids['Contribution']) === 1) {
+ return reset($this->ids['Contribution']);
+ }
+ return (int) $this->ids['Contribution'][$key];
}
/**