Commit 95c8444c authored by Monish Deb's avatar Monish Deb
Browse files

Additional fixes and unit test

parent 4e9abcb2
......@@ -53,17 +53,11 @@ class CRM_Lineitemedit_Form_Edit extends CRM_Core_Form {
'name'
);
$this->_isQuickConfig = empty($this->_lineitemInfo['price_field_id']) || CRM_Core_DAO::getFieldValue(
'CRM_Price_DAO_PriceSet',
CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_lineitemInfo['price_field_id'], 'price_set_id'),
'is_quick_config'
);
$this->_isQuickConfig = empty($this->_lineitemInfo['price_field_id']);
try {
if (!empty($this->_lineitemInfo['price_field_id'])) {
$this->_priceFieldInfo = civicrm_api3('PriceField', 'getsingle', array('id' => $this->_lineitemInfo['price_field_id']));
}
catch(CiviCRM_API3_Exception $e) {
// Exception here means there was no price field ID.
$this->_isQuickConfig = (bool) CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceFieldInfo['price_set_id'], 'is_quick_config');
}
if ($this->_isQuickConfig || empty($this->_priceFieldInfo['is_enter_qty'])) {
......@@ -200,7 +194,7 @@ class CRM_Lineitemedit_Form_Edit extends CRM_Core_Form {
$this->_lineitemInfo
);
if (in_array($this->_lineitemInfo['entity_table'], ['civicrm_membership', 'civicrm_participant'])) {
if (in_array($this->_lineitemInfo['entity_table'], ['civicrm_membership', 'civicrm_participant']) && !empty($lineItem['entity_id'])) {
$this->updateEntityRecord($this->_lineitemInfo);
$entityTab = ($this->_lineitemInfo['entity_table'] == 'civicrm_membership') ? 'member' : 'participant';
if (!$isTest) {
......@@ -210,32 +204,28 @@ class CRM_Lineitemedit_Form_Edit extends CRM_Core_Form {
}
protected function updateEntityRecord($lineItem) {
if(empty($lineItem['entity_id']))
return;
if (($lineItem['entity_table'] == 'civicrm_membership') || ($lineItem['entity_table'] == 'membership')) {
$memParams = array(
'id' => $lineItem['entity_id'],
);
if(!empty($lineItem['price_field_value_id'])) {
$params = ['id' => $lineItem['entity_id']];
if (($lineItem['entity_table'] == 'civicrm_membership')) {
if (!empty($lineItem['price_field_value_id'])) {
$memberNumTerms = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $lineItem['price_field_value_id'], 'membership_num_terms');
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $lineItem['price_field_value_id'], 'membership_type_id');
$memberNumTerms = empty($memberNumTerms) ? 1 : $memberNumTerms;
$memberNumTerms = $lineItem['qty'] * $memberNumTerms;
$memParams['num_terms'] = $memberNumTerms;
$memParams['membership_type_id'] = $membershipTypeId;
$params['num_terms'] = $memberNumTerms;
$params['membership_type_id'] = $membershipTypeId;
}
if ($lineItem['qty'] == 0) {
$memParams['status_id'] = 'Cancelled';
$memParams['is_override'] = TRUE;
$params['status_id'] = 'Cancelled';
$params['is_override'] = TRUE;
}
else {
$memParams['skipStatusCal'] = FALSE;
$params['skipStatusCal'] = FALSE;
}
civicrm_api3('Membership', 'create', $memParams);
civicrm_api3('Membership', 'create', $params);
}
elseif (($lineItem['entity_table'] == 'civicrm_participant') || ($lineItem['entity_table'] == 'participant')) {
$partUpdateFeeAmt = ['id' => $lineItem['entity_id']];
else {
$line = array();
$lineTotal = 0;
$getUpdatedLineItems = CRM_Utils_SQL_Select::from('civicrm_line_item')
->where([
"entity_table = '!et'",
......@@ -246,16 +236,14 @@ class CRM_Lineitemedit_Form_Edit extends CRM_Core_Form {
->param('#eid', $lineItem['entity_id'])
->execute()
->fetchAll();
$line = array();
$lineTotal = 0;
foreach ($getUpdatedLineItems as $updatedLineItem) {
$line[] = $updatedLineItem['label'] . ' - ' . (float) $updatedLineItem['qty'];
$lineTotal += $updatedLineItem['line_total'] + $updatedLineItem['tax_amount'];
}
$partUpdateFeeAmt['fee_level'] = implode(', ', $line);
$partUpdateFeeAmt['fee_amount'] = $lineTotal;
civicrm_api3('Participant', 'create', $partUpdateFeeAmt);
$params['fee_level'] = implode(', ', $line);
$params['fee_amount'] = $lineTotal;
civicrm_api3('Participant', 'create', $params);
//activity creation
CRM_Event_BAO_Participant::addActivityForSelection($lineItem['entity_id'], 'Change Registration');
......
......@@ -94,6 +94,45 @@ class CRM_Lineitemedit_Form_EditTest extends CRM_Lineitemedit_Form_BaseTest {
$this->checkArrayEqualsByAttributes($expectedFinancialTrxnEntries, $actualFinancialTrxnEntries);
}
public function testWithoutPriceFieldID() {
// Contribution amount and status before LineItem edit
$this->assertEquals('Completed', $this->_contribution['contribution_status']);
$this->assertEquals(100.00, $this->_contribution['total_amount']);
$form = new CRM_Lineitemedit_Form_Edit();
$lineItemInfo = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $this->_contributionID));
$this->callAPISuccess('LineItem', 'create', array('id' => $lineItemInfo['id'], 'price_field_id' => '', 'price_field_value_id' => ''));
$lineItemInfo = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $this->_contributionID));
$lineItemInfo['qty'] += 1; // increase lineitem qty to 2
$lineItemInfo['line_total'] *= $lineItemInfo['qty'];
$form->testSubmit($lineItemInfo);
// Contribution amount and status after LineItem edit
$contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionID));
$this->assertEquals('Partially paid', $contribution['contribution_status']);
$this->assertEquals(200.00, $contribution['total_amount']);
$check = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$actualFinancialItemEntries = $this->getFinancialItemsByContributionID($this->_contributionID);
$expectedFinancialItemEntries = array(
array(
'contact_id' => $this->_contactID,
'description' => 'Contribution Amount',
'amount' => 100.00,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid'),
),
array(
'contact_id' => $this->_contactID,
'description' => '2.00 of Contribution Amount',
'amount' => 100.00,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid'),
),
);
$this->checkArrayEqualsByAttributes($expectedFinancialItemEntries, $actualFinancialItemEntries);
}
/**
* Example: Test that a version is returned.
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment