X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContribute%2FBAO%2FContribution.php;h=5ad3b2c6c4f36e8e7ef6dab99aa5bc3f3027b41f;hb=f527e0121d4da74f6bd7d43fe2f4d6e6af7f6f3f;hp=2f759741f0097843678ab85171fe60f472b53eef;hpb=01d14675700160c19a23af37c4ef2f1af7c99a18;p=civicrm-core.git diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2f759741f0..5ad3b2c6c4 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -184,7 +184,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { $partialAmtPay = $params['partial_amount_pay']; $params['total_amount'] = $partialAmtTotal; if ($partialAmtPay < $partialAmtTotal) { - $params['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name'); + $params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'); $params['is_pay_later'] = 0; $setPrevContribution = FALSE; } @@ -288,7 +288,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { 'payment_instrument_id' => key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1') ), - 'contribution_status_id' => CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'), + 'contribution_status_id' => CRM_Core_Pseudoconstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), ); } @@ -842,31 +842,31 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { $softCreditFields = array( 'contribution_soft_credit_name' => array( 'name' => 'contribution_soft_credit_name', - 'title' => 'Soft Credit For', + 'title' => ts('Soft Credit For'), 'where' => 'civicrm_contact_d.display_name', 'data_type' => CRM_Utils_Type::T_STRING, ), 'contribution_soft_credit_amount' => array( 'name' => 'contribution_soft_credit_amount', - 'title' => 'Soft Credit Amount', + 'title' => ts('Soft Credit Amount'), 'where' => 'civicrm_contribution_soft.amount', 'data_type' => CRM_Utils_Type::T_MONEY, ), 'contribution_soft_credit_type' => array( 'name' => 'contribution_soft_credit_type', - 'title' => 'Soft Credit Type', + 'title' => ts('Soft Credit Type'), 'where' => 'contribution_softcredit_type.label', 'data_type' => CRM_Utils_Type::T_STRING, ), 'contribution_soft_credit_contribution_id' => array( 'name' => 'contribution_soft_credit_contribution_id', - 'title' => 'Soft Credit For Contribution ID', + 'title' => ts('Soft Credit For Contribution ID'), 'where' => 'civicrm_contribution_soft.contribution_id', 'data_type' => CRM_Utils_Type::T_INT, ), 'contribution_soft_credit_contact_id' => array( 'name' => 'contribution_soft_credit_contact_id', - 'title' => 'Soft Credit For Contact ID', + 'title' => ts('Soft Credit For Contact ID'), 'where' => 'civicrm_contact_d.id', 'data_type' => CRM_Utils_Type::T_INT, ), @@ -1173,31 +1173,31 @@ LEFT JOIN civicrm_line_item i ON ( i.contribution_id = c.id AND i.entity_table $extraFields = array( 'contribution_soft_credit_name' => array( 'name' => 'contribution_soft_credit_name', - 'title' => 'Soft Credit Name', + 'title' => ts('Soft Credit Name'), 'headerPattern' => '/^soft_credit_name$/i', 'where' => 'civicrm_contact_d.display_name', ), 'contribution_soft_credit_email' => array( 'name' => 'contribution_soft_credit_email', - 'title' => 'Soft Credit Email', + 'title' => ts('Soft Credit Email'), 'headerPattern' => '/^soft_credit_email$/i', 'where' => 'soft_email.email', ), 'contribution_soft_credit_phone' => array( 'name' => 'contribution_soft_credit_phone', - 'title' => 'Soft Credit Phone', + 'title' => ts('Soft Credit Phone'), 'headerPattern' => '/^soft_credit_phone$/i', 'where' => 'soft_phone.phone', ), 'contribution_soft_credit_contact_id' => array( 'name' => 'contribution_soft_credit_contact_id', - 'title' => 'Soft Credit Contact ID', + 'title' => ts('Soft Credit Contact ID'), 'headerPattern' => '/^soft_credit_contact_id$/i', 'where' => 'civicrm_contribution_soft.contact_id', ), 'contribution_pcp_title' => array( 'name' => 'contribution_pcp_title', - 'title' => 'Personal Campaign Page Title', + 'title' => ts('Personal Campaign Page Title'), 'headerPattern' => '/^contribution_pcp_title$/i', 'where' => 'contribution_pcp.title', ), @@ -2330,7 +2330,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $ids )); - if (!$paymentProcessorID && $this->contribution_page_id) { + if (!isset($input['payment_processor_id']) && !$paymentProcessorID && $this->contribution_page_id) { $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor' @@ -3115,16 +3115,14 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $partialAmtPay = CRM_Utils_Rule::cleanMoney($params['partial_amount_pay']); $partialAmtTotal = CRM_Utils_Rule::cleanMoney($params['partial_payment_total']); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $fromFinancialAccountId = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); - $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); + $fromFinancialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], 'Accounts Receivable Account is'); + $statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); $params['total_amount'] = $partialAmtPay; $balanceTrxnInfo = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($params['contribution']->id, $params['financial_type_id']); if (empty($balanceTrxnInfo['trxn_id'])) { // create new balance transaction record - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], 'Accounts Receivable Account is'); $balanceTrxnParams['total_amount'] = $partialAmtTotal; $balanceTrxnParams['to_financial_account_id'] = $toFinancialAccount; @@ -3137,6 +3135,8 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $balanceTrxnParams['status_id'] = $statusId; $balanceTrxnParams['payment_instrument_id'] = $params['contribution']->payment_instrument_id; $balanceTrxnParams['check_number'] = CRM_Utils_Array::value('check_number', $params); + $balanceTrxnParams['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $params); + $balanceTrxnParams['card_type_id'] = CRM_Utils_Array::value('card_type_id', $params); if (!empty($balanceTrxnParams['from_financial_account_id']) && ($statusId == array_search('Completed', $contributionStatuses) || $statusId == array_search('Partially paid', $contributionStatuses)) ) { @@ -3169,7 +3169,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac ); } elseif (!empty($params['payment_processor'])) { - $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($params['payment_processor'], 'civicrm_payment_processor', 'financial_account_id'); + $params['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['payment_processor'], NULL, 'civicrm_payment_processor'); $params['payment_instrument_id'] = civicrm_api3('PaymentProcessor', 'getvalue', array( 'id' => $params['payment_processor'], 'return' => 'payment_instrument_id', @@ -3201,6 +3201,8 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac 'status_id' => $statusId, 'payment_instrument_id' => CRM_Utils_Array::value('payment_instrument_id', $params, $params['contribution']->payment_instrument_id), 'check_number' => CRM_Utils_Array::value('check_number', $params), + 'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $params), + 'card_type_id' => CRM_Utils_Array::value('card_type_id', $params), ); if ($contributionStatus == 'Refunded' || $contributionStatus == 'Chargeback' || $contributionStatus == 'Cancelled') { $trxnParams['trxn_date'] = !empty($params['contribution']->cancel_date) ? $params['contribution']->cancel_date : date('YmdHis'); @@ -3225,6 +3227,9 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (!empty($financialTrxnValues)) { $trxnParams = array_merge($trxnParams, $financialTrxnValues); } + if (empty($trxnParams['payment_processor_id'])) { + unset($trxnParams['payment_processor_id']); + } $params['trxnParams'] = $trxnParams; @@ -3258,14 +3263,13 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (!empty($params['revenue_recognition_date']) || $params['prevContribution']->revenue_recognition_date) { $accountRelationship = 'Deferred Revenue Account is'; } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '$accountRelationship' ")); - $oldFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId); - $newFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + $oldFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['prevContribution']->financial_type_id, $accountRelationship); + $newFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], $accountRelationship); if ($oldFinancialAccount != $newFinancialAccount) { $params['total_amount'] = 0; if (in_array($params['contribution']->contribution_status_id, $pendingStatus)) { - $params['trxnParams']['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( - $params['prevContribution']->financial_type_id, $relationTypeId); + $params['trxnParams']['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount( + $params['prevContribution']->financial_type_id, $accountRelationship); } else { $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC'); @@ -3374,7 +3378,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac civicrm_api3('FinancialTrxn', 'create', array('id' => $refundIDs['financialTrxnId'], 'trxn_id' => $params['refund_trxn_id'])); } } - $cardType = CRM_Utils_Array::value('card_type', $params); + $cardType = CRM_Utils_Array::value('card_type_id', $params); $panTruncation = CRM_Utils_Array::value('pan_truncation', $params); CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($params['contribution']->id, $panTruncation, $cardType); } @@ -3384,6 +3388,8 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac // records finanical trxn and entity financial trxn // also make it available as return value self::recordAlwaysAccountsReceivable($trxnParams, $params); + $trxnParams['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $params); + $trxnParams['card_type_id'] = CRM_Utils_Array::value('card_type_id', $params); $return = $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); $params['entity_id'] = $financialTxn->id; if (empty($params['partial_payment_total']) && empty($params['partial_amount_pay'])) { @@ -3477,8 +3483,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac && $params['prevContribution']->is_pay_later) || $previousContributionStatus == 'In Progress' ) { $financialTypeID = CRM_Utils_Array::value('financial_type_id', $params) ? $params['financial_type_id'] : $params['prevContribution']->financial_type_id; - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $arAccountId = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeID, $relationTypeId); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeID, 'Accounts Receivable Account is'); if ($params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) { $params['trxnParams']['to_financial_account_id'] = $arAccountId; @@ -3498,8 +3503,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $params['trxnParams']['net_amount'] = $params['trxnParams']['total_amount']; $deferredFinancialAccount = CRM_Utils_Array::value('deferred_financial_account_id', $params); if (empty($deferredFinancialAccount)) { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); - $deferredFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId); + $deferredFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['prevContribution']->financial_type_id, 'Deferred Revenue Account is'); } $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC', FALSE, NULL, $deferredFinancialAccount); if (!empty($lastFinancialTrxnId['financialTrxnId'])) { @@ -3559,6 +3563,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac return; } } + $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); $params['entity_id'] = $trxn->id; if ($context != 'changePaymentInstrument') { @@ -3766,8 +3771,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac * @return array|bool */ public static function validateFinancialType($financialTypeId, $relationName = 'Expense Account is') { - $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '{$relationName}' ")); - $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); + $financialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, $relationName); if (!$financialAccount) { return CRM_Contribute_PseudoConstant::financialType($financialTypeId); @@ -3791,7 +3795,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac * @return null|object */ public static function recordAdditionalPayment($contributionId, $trxnsData, $paymentType = 'owed', $participantId = NULL, $updateStatus = TRUE) { - $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); + $statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); $getInfoOf['id'] = $contributionId; $defaults = array(); $contributionDAO = CRM_Contribute_BAO_Contribution::retrieve($getInfoOf, $defaults, CRM_Core_DAO::$_nullArray); @@ -3799,21 +3803,22 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $contributionId, 'participant_id', 'contribution_id'); } + // build params for recording financial trxn entry + $params['contribution'] = $contributionDAO; + $params = array_merge($defaults, $params); + $params['skipLineItem'] = TRUE; + $trxnsData['trxn_date'] = !empty($trxnsData['trxn_date']) ? $trxnsData['trxn_date'] : date('YmdHis'); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is'); if ($paymentType == 'owed') { - // build params for recording financial trxn entry - $params['contribution'] = $contributionDAO; - $params = array_merge($defaults, $params); - $params['skipLineItem'] = TRUE; $params['partial_payment_total'] = $contributionDAO->total_amount; $params['partial_amount_pay'] = $trxnsData['total_amount']; - $trxnsData['trxn_date'] = !empty($trxnsData['trxn_date']) ? $trxnsData['trxn_date'] : date('YmdHis'); $trxnsData['net_amount'] = !empty($trxnsData['net_amount']) ? $trxnsData['net_amount'] : $trxnsData['total_amount']; + $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $trxnsData); + $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $trxnsData); // record the entry $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionDAO->financial_type_id, $relationTypeId); - + $toFinancialAccount = $arAccountId; $trxnId = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId, $contributionDAO->financial_type_id); if (!empty($trxnId)) { $trxnId = $trxnId['trxn_id']; @@ -3890,17 +3895,9 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) } } elseif ($paymentType == 'refund') { - // build params for recording financial trxn entry - $params['contribution'] = $contributionDAO; - $params = array_merge($defaults, $params); - $params['skipLineItem'] = TRUE; - $trxnsData['trxn_date'] = !empty($trxnsData['trxn_date']) ? $trxnsData['trxn_date'] : date('YmdHis'); $trxnsData['total_amount'] = -$trxnsData['total_amount']; - - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $trxnsData['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType($contributionDAO->financial_type_id, $relationTypeId); - // @todo remove deprecated fn call. - $trxnsData['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); + $trxnsData['from_financial_account_id'] = $arAccountId; + $trxnsData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); // record the entry $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData); @@ -3958,8 +3955,9 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) $activityType = ($paymentType == 'refund') ? 'Refund' : 'Payment'; self::addActivityForPayment($entityObj, $financialTrxn, $activityType, $component, $contributionId); + return $financialTrxn; } - return $financialTrxn; + } /** @@ -4061,9 +4059,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total); $contributionIsPayLater = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'is_pay_later'); - $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); - $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); + $feeFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is'); if ($paymentBalance == 0 && $contributionIsPayLater) { $paymentBalance = $total; @@ -4082,7 +4079,7 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) SELECT GROUP_CONCAT(fa.`name`) as financial_account, ft.total_amount, ft.payment_instrument_id, - ft.trxn_date, ft.trxn_id, ft.status_id, ft.check_number, ft.currency, ft.pan_truncation, ft.card_type + ft.trxn_date, ft.trxn_id, ft.status_id, ft.check_number, ft.currency, ft.pan_truncation, ft.card_type_id FROM civicrm_contribution con LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.entity_id = con.id AND eft.entity_table = 'civicrm_contribution') @@ -4104,8 +4101,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) while ($resultDAO->fetch()) { $paidByLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $resultDAO->payment_instrument_id); $paidByName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $resultDAO->payment_instrument_id); - if ($resultDAO->card_type) { - $creditCardType = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_FinancialTrxn', 'card_type', $resultDAO->card_type); + if ($resultDAO->card_type_id) { + $creditCardType = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $resultDAO->card_type_id); $pantruncation = ''; if ($resultDAO->pan_truncation) { $pantruncation = ": {$resultDAO->pan_truncation}"; @@ -4447,14 +4444,10 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) * @param CRM_Core_Transaction $transaction * @param int $recur * @param CRM_Contribute_BAO_Contribution $contribution - * @param bool $isRecurring - * Duplication of param needs review. Only used by AuthorizeNetIPN - * @param int $isFirstOrLastRecurringPayment - * Deprecated param only used by AuthorizeNetIPN. * * @return array */ - public static function completeOrder(&$input, &$ids, $objects, $transaction, $recur, $contribution, $isRecurring, $isFirstOrLastRecurringPayment) { + public static function completeOrder(&$input, &$ids, $objects, $transaction, $recur, $contribution) { $primaryContributionID = isset($contribution->id) ? $contribution->id : $objects['first_contribution']->id; // The previous details are used when calculating line items so keep it before any code that 'does something' if (!empty($contribution->id)) { @@ -4472,6 +4465,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) 'receive_date', 'receipt_date', 'contribution_status_id', + 'card_type_id', + 'pan_truncation', ); if (self::isSingleLineItem($primaryContributionID)) { $inputContributionWhiteList[] = 'financial_type_id'; @@ -4585,43 +4580,52 @@ LIMIT 1;"; } $dao->free(); - $membershipParams['num_terms'] = $contribution->getNumTermsByContributionAndMembershipType( - $membershipParams['membership_type_id'], - $primaryContributionID - ); - $dates = array_fill_keys(array('join_date', 'start_date', 'end_date'), NULL); - if ($currentMembership) { - /* - * Fixed FOR CRM-4433 - * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status - * when Contribution mode is notify and membership is for renewal ) - */ - CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeDate); - - // @todo - we should pass membership_type_id instead of null here but not - // adding as not sure of testing - $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membershipParams['id'], - $changeDate, NULL, $membershipParams['num_terms'] - ); - - $dates['join_date'] = $currentMembership['join_date']; + if (CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $input)) === 'Pending') { + $membershipParams['num_terms'] = 0; } + else { + $membershipParams['num_terms'] = $contribution->getNumTermsByContributionAndMembershipType( + $membershipParams['membership_type_id'], + $primaryContributionID + ); + // @todo remove all this stuff in favour of letting the api call further down handle in + // (it is a duplication of what the api does). + $dates = array_fill_keys(array('join_date', 'start_date', 'end_date'), NULL); + if ($currentMembership) { + /* + * Fixed FOR CRM-4433 + * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status + * when Contribution mode is notify and membership is for renewal ) + */ + CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeDate); + + // @todo - we should pass membership_type_id instead of null here but not + // adding as not sure of testing + $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membershipParams['id'], + $changeDate, NULL, $membershipParams['num_terms'] + ); + + $dates['join_date'] = $currentMembership['join_date']; + } - //get the status for membership. - $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], - $dates['end_date'], - $dates['join_date'], - 'today', - TRUE, - $membershipParams['membership_type_id'], - $membershipParams - ); + //get the status for membership. + $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], + $dates['end_date'], + $dates['join_date'], + 'today', + TRUE, + $membershipParams['membership_type_id'], + $membershipParams + ); - $membershipParams['status_id'] = CRM_Utils_Array::value('id', $calcStatus, 'New'); - //we might be renewing membership, - //so make status override false. - $membershipParams['is_override'] = FALSE; + unset($dates['end_date']); + $membershipParams['status_id'] = CRM_Utils_Array::value('id', $calcStatus, 'New'); + //we might be renewing membership, + //so make status override false. + $membershipParams['is_override'] = FALSE; + } //CRM-17723 - reset static $relatedContactIds array() + // @todo move it to Civi Statics. $var = TRUE; CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE); civicrm_api3('Membership', 'create', $membershipParams); @@ -4708,10 +4712,6 @@ LIMIT 1;"; } CRM_Core_Error::debug_log_message("Success: Database updated"); - if ($isRecurring) { - CRM_Contribute_BAO_ContributionRecur::sendRecurringStartOrEndNotification($ids, $recur, - $isFirstOrLastRecurringPayment); - } return $contributionResult; } @@ -4869,11 +4869,10 @@ LIMIT 1;"; ); $statusId = array_search('Completed', $contributionStatuses); if (in_array(CRM_Utils_Array::value('contribution_status_id', $contribution), $pendingStatus)) { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $balanceTrxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId); + $balanceTrxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'); } elseif (!empty($params['payment_processor'])) { - $balanceTrxnParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($contribution['payment_processor'], 'civicrm_payment_processor', 'financial_account_id'); + $balanceTrxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['payment_processor'], NULL, 'civicrm_payment_processor'); } elseif (!empty($params['payment_instrument_id'])) { $balanceTrxnParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($contribution['payment_instrument_id']); @@ -4883,8 +4882,7 @@ LIMIT 1;"; $queryParams = array(1 => array($relationTypeId, 'Integer')); $balanceTrxnParams['to_financial_account_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams); } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $fromFinancialAccountId = CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId); + $fromFinancialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'); $balanceTrxnParams['from_financial_account_id'] = $fromFinancialAccountId; $balanceTrxnParams['total_amount'] = $params['total_amount']; $balanceTrxnParams['contribution_id'] = $params['contribution_id']; @@ -5347,8 +5345,7 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co $params = $trxnParams; $financialTypeID = CRM_Utils_Array::value('financial_type_id', $contributionParams) ? $contributionParams['financial_type_id'] : $contributionParams['prevContribution']->financial_type_id; - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $arAccountId = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeID, $relationTypeId); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeID, 'Accounts Receivable Account is'); $params['to_financial_account_id'] = $arAccountId; $params['status_id'] = array_search('Pending', $contributionStatuses); $params['is_payment'] = FALSE; @@ -5529,4 +5526,45 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co return TRUE; } + /** + * Function to replace contribution tokens. + * + * @param array $contributionIds + * + * @param string $subject + * + * @param array $subjectToken + * + * @param string $text + * + * @param string $html + * + * @param array $messageToken + * + * @param bool $escapeSmarty + * + * @return array + */ + public static function replaceContributionTokens( + $contributionIds, + $subject, + $subjectToken, + $text, + $html, + $messageToken, + $escapeSmarty + ) { + if (empty($contributionIds)) { + return array(); + } + $contributionDetails = array(); + foreach ($contributionIds as $id) { + $result = civicrm_api3('Contribution', 'get', array('id' => $id)); + $contributionDetails[$result['values'][$result['id']]['contact_id']]['subject'] = CRM_Utils_Token::replaceContributionTokens($subject, $result, FALSE, $subjectToken, FALSE, $escapeSmarty); + $contributionDetails[$result['values'][$result['id']]['contact_id']]['text'] = CRM_Utils_Token::replaceContributionTokens($text, $result, FALSE, $messageToken, FALSE, $escapeSmarty); + $contributionDetails[$result['values'][$result['id']]['contact_id']]['html'] = CRM_Utils_Token::replaceContributionTokens($html, $result, FALSE, $messageToken, FALSE, $escapeSmarty); + } + return $contributionDetails; + } + }