From 15f4ec6529a9eb2bdf6e9f2c8a6b3a14781b8d73 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Tue, 19 Sep 2017 23:36:46 +0530 Subject: [PATCH] CRM-20569: Add priceset validation on a partial payment use-case and other improvements --- CRM/Price/BAO/PriceField.php | 7 +++++++ CRM/Price/BAO/PriceSet.php | 10 +++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 025f62e3ee..aaff2d3414 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -836,9 +836,16 @@ WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; list($componentName) = explode(':', $fields['_qf_default']); // now we have all selected amount in hand. $totalAmount = array_sum($selectedAmounts); + $actualTotalAmount = CRM_Utils_Array::value('partial_payment_total', $fields, CRM_Utils_Array::value('total_amount', $fields)); if ($totalAmount < 0) { $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName)); } + elseif ($totalAmount > 0 && + $actualTotalAmount >= $totalAmount && // if total amount is equal to all selected amount in hand + (CRM_Utils_Array::value('contribution_status_id', $fields) == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Partially paid')) + ) { + $error['total_amount'] = ts('For partially paid contribution, amount must be less then the sum of all selected amount in hand'); + } } else { if ($allowNoneSelection) { diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index e1dabf5202..3af46ffbde 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -703,8 +703,8 @@ WHERE id = %1"; } if ($priceSetID) { $priceFields = self::filterPriceFieldsFromParams($priceSetID, $params); - if (count($priceFields) == 1 && !empty($params['total_amount'])) { - $amount_override = $params['total_amount']; + if (count($priceFields) == 1) { + $amount_override = CRM_Utils_Array::value('partial_payment_total', $params, CRM_Utils_Array::value('total_amount', $params)); } } foreach ($fields as $id => $field) { @@ -719,7 +719,7 @@ WHERE id = %1"; case 'Text': $firstOption = reset($field['options']); $params["price_{$id}"] = array($firstOption['id'] => $params["price_{$id}"]); - CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); + CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem, CRM_Utils_Array::value('partial_payment_total', $params)); $optionValueId = key($field['options']); if (CRM_Utils_Array::value('name', $field['options'][$optionValueId]) == 'contribution_amount') { @@ -767,7 +767,7 @@ WHERE id = %1"; $params["price_{$id}"] = array($params["price_{$id}"] => 1); $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]); - CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); + CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem, CRM_Utils_Array::value('partial_payment_total', $params)); if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) { $lineItem = self::setLineItem($field, $lineItem, $optionValueId, $totalTax); } @@ -783,7 +783,7 @@ WHERE id = %1"; case 'CheckBox': - CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); + CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem, CRM_Utils_Array::value('partial_payment_total', $params)); foreach ($params["price_{$id}"] as $optionId => $option) { if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionId])) { $lineItem = self::setLineItem($field, $lineItem, $optionId, $totalTax); -- 2.25.1