diff --git a/CRM/Financeextras/Upgrader.php b/CRM/Financeextras/Upgrader.php index b63f75c..d0dc56b 100644 --- a/CRM/Financeextras/Upgrader.php +++ b/CRM/Financeextras/Upgrader.php @@ -162,4 +162,31 @@ 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; + } + } + } diff --git a/Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php b/Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php new file mode 100644 index 0000000..7d42d25 --- /dev/null +++ b/Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php @@ -0,0 +1,49 @@ +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) { + } + } + +} diff --git a/financeextras.php b/financeextras.php index 91b9aa0..cb7a265 100644 --- a/financeextras.php +++ b/financeextras.php @@ -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(); } /**