From bc2eeabb41290adb36e944a7e403dae8228a28dd Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Thu, 16 Jan 2014 07:15:01 +0530 Subject: [PATCH] CRM-13973 - work on the UI implementation, CRM-13965 - improvements to BAOs --- CRM/Contribute/BAO/Contribution.php | 17 +- CRM/Contribute/Page/PaymentInfo.php | 2 +- CRM/Core/BAO/FinancialTrxn.php | 47 ++--- CRM/Event/Form/Participant.php | 11 +- CRM/Event/Form/ParticipantFeeSelection.php | 178 ++++++++++++++++++ CRM/Event/Form/ParticipantView.php | 10 +- CRM/Event/xml/Menu/Event.xml | 6 + CRM/Price/BAO/LineItem.php | 10 + templates/CRM/Contribute/Page/PaymentInfo.tpl | 49 +++-- templates/CRM/Event/Form/Participant.tpl | 6 +- .../Event/Form/ParticipantFeeSelection.tpl | 91 +++++++++ templates/CRM/Event/Form/ParticipantView.tpl | 4 +- templates/CRM/Price/Form/Calculate.tpl | 3 +- 13 files changed, 355 insertions(+), 79 deletions(-) create mode 100644 CRM/Event/Form/ParticipantFeeSelection.php create mode 100644 templates/CRM/Event/Form/ParticipantFeeSelection.tpl diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 20a1fe5479..6f2477672e 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3148,17 +3148,23 @@ WHERE eft.financial_trxn_id = {$trxnId} $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'contribution_id', 'participant_id'); } $total = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); - $baseTrxnId = $total['trxn_id']; - $total = $total['total_amount']; - $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE); + $baseTrxnId = NULL; + if (empty($total)) { + $total = CRM_Price_BAO_LineItem::getLineTotal($id, 'civicrm_participant'); + } + else { + $baseTrxnId = $total['trxn_id']; + $total = $total['total_amount']; + } + $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total); $info['total'] = $total; $info['paid'] = $total - $paymentBalance; $info['balance'] = $paymentBalance; $info['id'] = $id; $info['component'] = $component; - - if ($getTrxnInfo) { + $rows = array(); + if ($getTrxnInfo && $baseTrxnId) { $sql = " SELECT ft.total_amount, con.financial_type_id, ft.payment_instrument_id, ft.trxn_date, ft.trxn_id, ft.status_id FROM civicrm_contribution con @@ -3168,7 +3174,6 @@ WHERE ft.id != {$baseTrxnId} AND con.id = {$contributionId} "; $resultDAO = CRM_Core_DAO::executeQuery($sql); - $rows = array(); while($resultDAO->fetch()) { $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); $statuses = CRM_Contribute_PseudoConstant::contributionStatus(); diff --git a/CRM/Contribute/Page/PaymentInfo.php b/CRM/Contribute/Page/PaymentInfo.php index 1bc1f662e4..ccf502e332 100644 --- a/CRM/Contribute/Page/PaymentInfo.php +++ b/CRM/Contribute/Page/PaymentInfo.php @@ -53,7 +53,7 @@ class CRM_Contribute_Page_PaymentInfo extends CRM_Core_Page { $this->assign('paymentInfo', $paymentInfo); } else { - $rows = $paymentInfo['transaction']; + $rows = CRM_Utils_Array::value('transaction', $paymentInfo); $this->assign('rows', $rows); } } diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index e0b2100fcf..f8c46ce15c 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -398,48 +398,38 @@ WHERE ceft.entity_id = %1"; * return @array : payment type => amount * payment type : 'amount_owed' or 'refund_due' */ - static function getPartialPaymentWithType($entityId, $entityName = 'pariticpant', $returnType = TRUE) { + static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) { $value = NULL; if (empty($entityName)) { return $value; } if ($entityName == 'participant') { - $partialPaymentStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name'); - $sql = "SELECT pp.contribution_id, con.financial_type_id FROM civicrm_participant_payment pp -INNER JOIN civicrm_contribution con ON con.id = pp.contribution_id -WHERE pp.participant_id = %1 AND con.contribution_status_id = %2 -"; - $qParams[1] = array($entityId, 'Integer'); - $qParams[2] = array($partialPaymentStatusId, 'Integer'); - $dao = CRM_Core_DAO::executeQuery($sql, $qParams); - if ($dao->fetch()) { - $contributionId = $dao->contribution_id; - $financialTypeId = $dao->financial_type_id; - } + $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id'); + $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $entityId, 'financial_type_id'); if ($contributionId && $financialTypeId) { $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); - $sqlLineItemTotal = "SELECT SUM(li.line_total) -FROM civicrm_line_item li -INNER JOIN civicrm_participant_payment pp ON ( li.entity_id = pp.participant_id -AND li.entity_table = 'civicrm_participant' -AND li.entity_id = {$entityId})"; - $lineItemTotal = CRM_Core_DAO::singleValueQuery($sqlLineItemTotal); - - $sqlFtTotalAmt = "SELECT SUM(ft.total_amount) + if (empty($lineItemTotal)) { + $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant'); + } + $sqlFtTotalAmt = " +SELECT SUM(ft.total_amount) FROM civicrm_financial_trxn ft -LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') -LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) -LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) -WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.status_id = {$statusId} + LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') + LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) + LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) +WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} + AND ft.from_financial_account_id = {$toFinancialAccount} AND ft.status_id = {$statusId} "; $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); - - $value = $paymentVal = $lineItemTotal - $ftTotalAmt; + $value = 0; + if ($ftTotalAmt) { + $value = $paymentVal = $lineItemTotal - $ftTotalAmt; + } if ($returnType) { $value = array(); if ($paymentVal < 0) { @@ -448,6 +438,9 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina elseif ($paymentVal > 0) { $value['amount_owed'] = $paymentVal; } + elseif ($lineItemTotal == $ftTotalAmt) { + $value['full_paid'] = $ftTotalAmt; + } } } } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index bf7a833219..bbf750ca2c 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -195,8 +195,6 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { */ public $_participantRoleIds = array(); - - protected $_feePaymenBlock = NULL; /** * Function to set variables up before form is built * @@ -234,13 +232,8 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { if ($this->_id) { $this->assign('participantId', $this->_id); - $this->_feePaymenBlock = FALSE; $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_id, 'status_id', 'id'); - if (in_array($participantStatuses[$statusId], array('Partially paid', 'Pending refund'))) { - $this->_feePaymenBlock = TRUE; - } - $this->assign('feePaymentBlock', $this->_feePaymenBlock); $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'id', 'participant_id' ); @@ -679,9 +672,7 @@ SELECT civicrm_custom_group.name as name, * @access public */ public function buildQuickForm() { - if ($this->_feePaymenBlock) { - CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); - } + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); if ($this->_showFeeBlock) { return CRM_Event_Form_EventFees::buildQuickForm($this); } diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php new file mode 100644 index 0000000000..b87e850ef9 --- /dev/null +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -0,0 +1,178 @@ +_participantId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); + $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); + $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); + $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id'); + $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id'); + if ($this->_contributionId) { + $this->_isPaidEvent = TRUE; + } + $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, TRUE); + + list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId); + $this->assign('displayName', $this->_contributorDisplayName); + //set the payment mode - _mode property is defined in parent class + $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); + + $this->assign('contactId', $this->_contactId); + $this->assign('id', $this->_participantId); + + $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event'); + $this->assign('paymentInfo', $paymentInfo); + CRM_Core_Resources::singleton()->addSetting(array('feePaid' => $paymentInfo['paid'])); + + $title = "Change selections for {$this->_contributorDisplayName}"; + if ($title) { + CRM_Utils_System::setTitle(ts('%1', array(1 => $title))); + } + } + + public function setDefaultValues() { + $params = array('id' => $this->_participantId); + + CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); + $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId); + + $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId); + if (!empty($priceSetValues)) { + $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues); + } + + $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId])); + if ($this->_action == CRM_Core_Action::UPDATE) { + $fee_level = $defaults[$this->_participantId]['fee_level']; + CRM_Event_BAO_Participant::fixEventLevel($fee_level); + $this->assign('fee_level', $fee_level); + $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId])); + } + $defaults = $defaults[$this->_participantId]; + return $defaults; + } + + public function buildQuickForm() { + $config = CRM_Core_Config::singleton(); + $this->assign('currencySymbol', $config->defaultCurrencySymbol); + + // line items block + $lineItem = $event = array(); + $params = array('id' => $this->_eventId); + CRM_Event_BAO_Event::retrieve($params, $event); + + //retrieve custom information + $this->_values = array(); + CRM_Event_Form_Registration::initEventFee($this, $event['id']); + CRM_Event_Form_Registration_Register::buildAmount($this, TRUE); + + if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) { + $lineItem[] = $this->_values['line_items']; + } + $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem); + $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId); + $this->assign('eventName', $event[$this->_eventId]); + + $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); + $this->add('select', 'status_id', ts('Participant Status'), + array( + '' => ts('- select -')) + $statusOptions, + TRUE + ); + + $buttons[] = array( + 'type' => 'upload', + 'name' => ts('Save'), + 'isDefault' => TRUE, + ); + + $buttons[] = array( + 'type' => 'cancel', + 'name' => ts('Cancel'), + ); + + $this->addButtons($buttons); + $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this); + } + + static function formRule($fields, $files, $self) { + $errors = array(); + return $errors; + } + + public function postProcess() { + $params = $this->controller->exportValues($this->_name); + } + + static function emailReceipt(&$form, &$params) { + // email receipt sending + } +} \ No newline at end of file diff --git a/CRM/Event/Form/ParticipantView.php b/CRM/Event/Form/ParticipantView.php index 41dda2e3f3..b8da44ab59 100644 --- a/CRM/Event/Form/ParticipantView.php +++ b/CRM/Event/Form/ParticipantView.php @@ -45,7 +45,6 @@ class CRM_Event_Form_ParticipantView extends CRM_Core_Form { * @return void * @access public */ - protected $_feePaymentBlock = NULL; public function preProcess() { $values = $ids = array(); $participantID = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); @@ -69,13 +68,8 @@ class CRM_Event_Form_ParticipantView extends CRM_Core_Form { $this->assign('contactId', $contactID); $this->assign('participantId', $participantID); - $this->_feePaymentBlock = FALSE; $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'status_id', 'id'); $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); - if (in_array($participantStatuses[$statusId], array('Partially paid', 'Pending refund'))) { - $this->_feePaymentBlock = TRUE; - } - $this->assign('feePaymentBlock', $this->_feePaymentBlock); if ($values[$participantID]['is_test']) { $values[$participantID]['status'] .= ' (test) '; @@ -208,9 +202,7 @@ class CRM_Event_Form_ParticipantView extends CRM_Core_Form { * @access public */ public function buildQuickForm() { - if ($this->_feePaymentBlock) { - CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); - } + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); $this->addButtons(array( array( 'type' => 'cancel', diff --git a/CRM/Event/xml/Menu/Event.xml b/CRM/Event/xml/Menu/Event.xml index 125e71e19a..be95ac376b 100644 --- a/CRM/Event/xml/Menu/Event.xml +++ b/CRM/Event/xml/Menu/Event.xml @@ -330,4 +330,10 @@ true false + + civicrm/event/participant/feeselection + Change Registration Selections + CRM_Event_Form_ParticipantFeeSelection + access CiviEvent + diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index 98a831ebc6..741a10c74d 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -92,6 +92,16 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem { return NULL; } + static function getLineTotal($entityId, $entityTable) { + $sqlLineItemTotal = "SELECT SUM(li.line_total) +FROM civicrm_line_item li +INNER JOIN civicrm_participant_payment pp ON ( li.entity_id = pp.participant_id +AND li.entity_table = '{$entityTable}' +AND li.entity_id = {$entityId})"; + $lineItemTotal = CRM_Core_DAO::singleValueQuery($sqlLineItemTotal); + return $lineItemTotal; + } + /** * Given a participant id/contribution id, * return contribution/fee line items diff --git a/templates/CRM/Contribute/Page/PaymentInfo.tpl b/templates/CRM/Contribute/Page/PaymentInfo.tpl index 37a339e0d5..444250e354 100644 --- a/templates/CRM/Contribute/Page/PaymentInfo.tpl +++ b/templates/CRM/Contribute/Page/PaymentInfo.tpl @@ -60,24 +60,33 @@
{ts}Record Payment{/ts}
{/if} {elseif $context eq 'transaction'} - - - - - - - - - - {foreach from=$rows item=row} - - - - - - - - - {/foreach} -
{ts}Amount{/ts}{ts}Type{/ts}{ts}Paid By{/ts}{ts}Received{/ts}{ts}Transaction ID{/ts}{ts}Status{/ts}
{$row.total_amount|crmMoney}{$row.financial_type}{$row.payment_instrument}{$row.receive_date|crmDate}{$row.trxn_id}{$row.status}
+ {if !empty($rows)} +
+ + + + + + + + + {foreach from=$rows item=row} + + + + + + + + + {/foreach} +
{ts}Amount{/ts}{ts}Type{/ts}{ts}Paid By{/ts}{ts}Received{/ts}{ts}Transaction ID{/ts}{ts}Status{/ts}
{$row.total_amount|crmMoney}{$row.financial_type}{$row.payment_instrument}{$row.receive_date|crmDate}{$row.trxn_id}{$row.status}
+ {else} + {if $component eq 'event'} + {assign var='entity' value='participant'} + {else} + {assign var='entity' value=$component} + {/if} + {ts 1=$entity}No additional payments found for this %1 record{/ts} + {/if} {/if} \ No newline at end of file diff --git a/templates/CRM/Event/Form/Participant.tpl b/templates/CRM/Event/Form/Participant.tpl index e4c76163bd..bb6705bf65 100644 --- a/templates/CRM/Event/Form/Participant.tpl +++ b/templates/CRM/Event/Form/Participant.tpl @@ -24,7 +24,7 @@ +--------------------------------------------------------------------+ *} {* This template is used for adding/editing/deleting offline Event Registrations *} -{if $showFeeBlock } +{if $showFeeBlock} {if $priceSet}
{literal} @@ -165,7 +165,7 @@ {/literal} {/if} - {if $participantId and $feePaymentBlock} + {if $participantId} {include file="CRM/Contribute/Page/PaymentInfo.tpl" show='event-payment'} {/if} {include file="CRM/Event/Form/EventFees.tpl"} @@ -291,7 +291,7 @@ {ts}Source for this registration (if applicable).{/ts}
- {if $participantId and $feePaymentBlock} + {if $participantId} diff --git a/templates/CRM/Event/Form/ParticipantFeeSelection.tpl b/templates/CRM/Event/Form/ParticipantFeeSelection.tpl new file mode 100644 index 0000000000..ea9641b069 --- /dev/null +++ b/templates/CRM/Event/Form/ParticipantFeeSelection.tpl @@ -0,0 +1,91 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.4 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2013 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +{* This template is used to change selections of fees for a participant *} + +

Change Registration Selections

+ +
+
{include file="CRM/common/formButtons.tpl" location="bottom"}
+
{ts}Fees{/ts}
+ + + + + + + + + + + {if $lineItem} + + + + + {/if} +
{ts}Participant{/ts}{$displayName}
{ts}Event{/ts}{$eventName}
{$form.status_id.label}{$form.status_id.html}
{ts}Current Selections{/ts}{include file="CRM/Price/Page/LineItem.tpl" context="Event"}
+ + {if $priceSet.fields} +
+ + + + + + {if $paymentInfo} +
{$form.amount.label}{include file="CRM/Price/Form/PriceSet.tpl" extends="Event"}
+
+
{ts}Updated Fee(s){/ts}
+
{ts}Total Paid{/ts}
+ +
{ts}Balance Owed{/ts}
+
+ {include file='CRM/Price/Form/Calculate.tpl' currencySymbol=$currencySymbol noCalcValueDisplay='false'} + {/if} +
+
+ {/if} +
{include file="CRM/common/formButtons.tpl" location="bottom"}
+ +{literal} + +{/literal} \ No newline at end of file diff --git a/templates/CRM/Event/Form/ParticipantView.tpl b/templates/CRM/Event/Form/ParticipantView.tpl index 5b57e56d5e..b889f590e8 100644 --- a/templates/CRM/Event/Form/ParticipantView.tpl +++ b/templates/CRM/Event/Form/ParticipantView.tpl @@ -99,7 +99,7 @@ {ts}Event Source{/ts}{$source}  {/if} - {if $participantId and $feePaymentBlock} + {if $participantId} {ts}Fees{/ts} @@ -122,7 +122,7 @@ {/if} {/foreach} - {if $participantId and $feePaymentBlock} + {if $participantId} {include file="CRM/Contribute/Page/PaymentInfo.tpl" show='event-payment'} {/if} {include file="CRM/Custom/Page/CustomDataView.tpl"} diff --git a/templates/CRM/Price/Form/Calculate.tpl b/templates/CRM/Price/Form/Calculate.tpl index 8e3d29c47e..d8397508c3 100644 --- a/templates/CRM/Price/Form/Calculate.tpl +++ b/templates/CRM/Price/Form/Calculate.tpl @@ -23,6 +23,7 @@ | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ *} +{if $noCalcValueDisplay neq 'false'}
- +{/if}