Skip to content

Commit

Permalink
COMCL-770: Fix line item creations
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Shahrukh committed Sep 8, 2024
1 parent 99958b2 commit c3aecb0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions CRM/Financeextras/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,30 @@ public function upgrade_1002() {
return TRUE;
}

/**
* This upgrade updates line items that have empty price field values
*/
public function upgrade_1003() {
$this->ctx->log->info('Applying update 1003');

try {
$priceSetId = \CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
$priceSet = current(\CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
$priceFieldID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($priceSet);
$priceFieldValueID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($priceSet);

\Civi\Api4\LineItem::update(false)
->addValue('price_field_id', $priceFieldID)
->addValue('price_field_value_id', $priceFieldValueID)
->addClause('OR', ['price_field_id', 'IS NULL'], ['price_field_value_id', 'IS NULL'])
->execute();

return true;
} catch (\Throwable $e) {
$this->ctx->log->info($e->getMessage());

return false;
}
}

}
49 changes: 49 additions & 0 deletions Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Civi\Financeextras\Hook\Post;

class UpdateLineItemPriceFieldValues {

public function __construct(
private string $op,
private string $objectName,
private int $objectId,
private $objectRef,
) {
}

public function run() {
$this->updatePriceFieldValues();
}

private function updatePriceFieldValues(): void
{
try {
$record = (array) $this->objectRef;

if ($this->objectName !== 'LineItem' || !in_array($this->op, ['create', 'edit'])
|| ((!in_array($record['price_field_id'], [NULL, 'null'])) && (!in_array($record['price_field_value_id'], [NULL, 'null'])))) {
return;
}

$priceSetId = \CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
$priceSet = current(\CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
$priceFieldID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($priceSet);
$priceFieldValueID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($priceSet);

$lineItem = \Civi\Api4\LineItem::update(FALSE);

if (in_array($record['price_field_id'], [NULL, 'null'])) {
$lineItem->addValue('price_field_id', $priceFieldID);
}
if (in_array($record['price_field_value_id'], [NULL, 'null'])) {
$lineItem->addValue('price_field_value_id', $priceFieldValueID);
}

$lineItem->addWhere('id', '=', $this->objectId)->execute();

} catch (\Throwable $e) {
}
}

}
1 change: 1 addition & 0 deletions financeextras.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function financeextras_civicrm_post($op, $objectName, $objectId, &$objectRef) {

(new \Civi\Financeextras\Hook\Post\UpdateContributionExchangeRate($objectId))->run();
}
(new \Civi\Financeextras\Hook\Post\UpdateLineItemPriceFieldValues($op, $objectName, $objectId, $objectRef))->run();
}

/**
Expand Down

0 comments on commit c3aecb0

Please sign in to comment.