Skip to content

Commit

Permalink
Fix line item 'title' determination
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 2, 2022
1 parent 2b14980 commit 72f9134
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions CRM/Financial/BAO/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,6 @@ public function getPriceSetID(): int {
foreach ($this->getPriceOptions() as $fieldID => $valueID) {
$this->setPriceSetIDFromSelectedField($fieldID);
}
if (!$this->priceSetID && $this->getTemplateContributionID()) {
// Load the line items from the contribution.
foreach ($this->getLineItems() as $lineItem) {
return $lineItem['price_field_id.price_set_id'];
}
}
}
return $this->priceSetID;
}
Expand Down Expand Up @@ -819,6 +813,10 @@ protected function calculateLineItems(): array {
$params['financial_type_id'] = 0;
if ($this->getTemplateContributionID()) {
$lineItems = $this->getLinesFromTemplateContribution();
// Set the price set ID from the first line item (we need to set this here
// to prevent a loop later when we retrieve the price field metadata to
// set the 'title' (as accessed from workflow message templates).
$this->setPriceSetID($lineItems[0]['price_field_id.price_set_id']);
}
else {
foreach ($this->getPriceOptions() as $fieldID => $valueID) {
Expand Down Expand Up @@ -855,7 +853,7 @@ protected function calculateLineItems(): array {
elseif ($taxRate) {
$lineItem['tax_amount'] = ($taxRate / 100) * $lineItem['line_total'];
}
$lineItem['title'] = $lineItem['label'];
$lineItem['title'] = $this->getLineItemTitle($lineItem);
}
return $lineItems;
}
Expand Down Expand Up @@ -1001,15 +999,7 @@ public function setLineItem(array $lineItem, $index): void {
}
}
if (empty($lineItem['title'])) {
// Title is used in output for workflow templates.
$htmlType = !empty($this->priceFieldMetadata) ? $this->getPriceFieldSpec($lineItem['price_field_id'])['html_type'] : NULL;
$lineItem['title'] = (!$htmlType || $htmlType === 'Text') ? $lineItem['label'] : $this->getPriceFieldSpec($lineItem['price_field_id'])['label'] . ' : ' . $lineItem['label'];
if (!empty($lineItem['price_field_value_id'])) {
$description = $this->priceFieldValueMetadata[$lineItem['price_field_value_id']]['description'] ?? '';
if ($description) {
$lineItem['title'] .= ' ' . CRM_Utils_String::ellipsify($description, 30);
}
}
$lineItem['title'] = $this->getLineItemTitle($lineItem);
}
$this->lineItems[$index] = $lineItem;
}
Expand Down Expand Up @@ -1211,4 +1201,27 @@ protected function fillDefaultContributionLine(array &$lineItem): void {
$lineItem = array_merge($defaults, $lineItem);
}

/**
* Get a 'title' for the line item.
*
* This descriptor is used in message templates. It could conceivably
* by used elsewhere but if so determination would likely move to the api.
*
* @param array $lineItem
*
* @return string
*/
private function getLineItemTitle(array $lineItem): string {
// Title is used in output for workflow templates.
$htmlType = $this->getPriceFieldSpec($lineItem['price_field_id'])['html_type'] ?? NULL;
$lineItemTitle = (!$htmlType || $htmlType === 'Text') ? $lineItem['label'] : $this->getPriceFieldSpec($lineItem['price_field_id'])['label'] . ' - ' . $lineItem['label'];
if (!empty($lineItem['price_field_value_id'])) {
$description = $this->priceFieldValueMetadata[$lineItem['price_field_value_id']]['description'] ?? '';
if ($description) {
$lineItemTitle .= ' ' . CRM_Utils_String::ellipsify($description, 30);
}
}
return $lineItemTitle;
}

}

0 comments on commit 72f9134

Please sign in to comment.