X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContribute%2FBAO%2FContribution.php;h=f6252eb62fec0fae7bee26bacd1abcfdbeb4e37a;hb=8d20d89cf15dc3fd35e42140dcbe3ffa0f7e8a2e;hp=263dd7dffd0c65d0db7b237dd0540817990fddc8;hpb=7a7971bd6efc4c713933abb705d165bb677344b3;p=civicrm-core.git diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 263dd7dffd..f6252eb62f 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -133,6 +133,19 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } + //if contribution is created with cancelled or refunded status, add credit note id + if (!empty($params['contribution_status_id'])) { + $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + + if (($params['contribution_status_id'] == array_search('Refunded', $contributionStatus) + || $params['contribution_status_id'] == array_search('Cancelled', $contributionStatus)) + ) { + if (empty($params['creditnote_id']) || $params['creditnote_id'] == "null") { + $params['creditnote_id'] = self::createCreditNoteId(); + } + } + } + //set defaults in create mode if (!$contributionID) { CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); @@ -158,6 +171,9 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { $setPrevContribution = FALSE; } } + if ($contributionID && $setPrevContribution) { + $params['prevContribution'] = self::getValues(array('id' => $contributionID), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); + } if ($contributionID) { CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params); @@ -178,10 +194,6 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } - if ($contributionID && $setPrevContribution) { - $params['prevContribution'] = self::getValues(array('id' => $contributionID), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); - } - $result = $contribution->save(); // Add financial_trxn details as part of fix for CRM-4724 @@ -390,6 +402,19 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } + //if contribution is created with cancelled or refunded status, add credit note id + if (!empty($params['contribution_status_id'])) { + $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + + if (($params['contribution_status_id'] == array_search('Refunded', $contributionStatus) + || $params['contribution_status_id'] == array_search('Cancelled', $contributionStatus)) + ) { + if (empty($params['creditnote_id']) || $params['creditnote_id'] == "null") { + $params['creditnote_id'] = self::createCreditNoteId(); + } + } + } + $transaction = new CRM_Core_Transaction(); $contribution = self::add($params, $ids); @@ -444,62 +469,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { } } - // Handle soft credit and / or link to personal campaign page - $softIDs = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id); - - $pcpId = CRM_Contribute_BAO_ContributionSoft::getSoftCreditIds($contribution->id, TRUE); - - if ($pcp = CRM_Utils_Array::value('pcp', $params)) { - $softParams = array(); - $softParams['id'] = $pcpId ? $pcpId : NULL; - $softParams['contribution_id'] = $contribution->id; - $softParams['pcp_id'] = $pcp['pcp_made_through_id']; - $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', - $pcp['pcp_made_through_id'], 'contact_id' - ); - $softParams['currency'] = $contribution->currency; - $softParams['amount'] = $contribution->total_amount; - $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp); - $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp); - $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp); - $softParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); - $contributionSoft = CRM_Contribute_BAO_ContributionSoft::add($softParams); - //Send notification to owner for PCP - if ($contributionSoft->pcp_id && empty($pcpId)) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); - } - } - //Delete PCP against this contribution and create new on submitted PCP information - elseif (array_key_exists('pcp', $params) && $pcpId) { - $deleteParams = array('id' => $pcpId); - CRM_Contribute_BAO_ContributionSoft::del($deleteParams); - } - if (isset($params['soft_credit'])) { - $softParams = $params['soft_credit']; - foreach ($softParams as $softParam) { - if (!empty($softIDs)) { - $key = key($softIDs); - $softParam['id'] = $softIDs[$key]; - unset($softIDs[$key]); - } - $softParam['contribution_id'] = $contribution->id; - $softParam['currency'] = $contribution->currency; - //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default - if (empty($softParam['amount'])) { - $softParam['amount'] = $contribution->total_amount; - } - CRM_Contribute_BAO_ContributionSoft::add($softParam); - } - - if (!empty($softIDs)) { - foreach ($softIDs as $softID) { - if (!in_array($softID, $params['soft_credit_ids'])) { - $deleteParams = array('id' => $softID); - CRM_Contribute_BAO_ContributionSoft::del($deleteParams); - } - } - } - } + CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); $transaction->commit(); @@ -1160,7 +1130,7 @@ GROUP BY p.id $contributionDAO->id = $honorDAO->contribution_id; if ($contributionDAO->find(TRUE)) { - $params[$contributionDAO->id]['honor_type'] = CRM_Core_OptionGroup::getLabel('soft_credit_type', $honorDAO->soft_credit_type_id, 'value'); + $params[$contributionDAO->id]['honor_type'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $honorDAO->soft_credit_type_id); $params[$contributionDAO->id]['honorId'] = $contributionDAO->contact_id; $params[$contributionDAO->id]['display_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contributionDAO->contact_id, 'display_name'); $params[$contributionDAO->id]['type'] = $type[$contributionDAO->financial_type_id]; @@ -1227,12 +1197,14 @@ WHERE civicrm_contribution.contact_id = civicrm_contact.id $nextYear = $year + 1; if ($config->fiscalYearStart) { - if ($config->fiscalYearStart['M'] < 10) { - $config->fiscalYearStart['M'] = '0' . $config->fiscalYearStart['M']; + $newFiscalYearStart = $config->fiscalYearStart; + if ($newFiscalYearStart['M'] < 10) { + $newFiscalYearStart['M'] = '0' . $newFiscalYearStart['M']; } - if ($config->fiscalYearStart['d'] < 10) { - $config->fiscalYearStart['d'] = '0' . $config->fiscalYearStart['d']; + if ($newFiscalYearStart['d'] < 10) { + $newFiscalYearStart['d'] = '0' . $newFiscalYearStart['d']; } + $config->fiscalYearStart = $newFiscalYearStart; $monthDay = $config->fiscalYearStart['M'] . $config->fiscalYearStart['d']; } else { @@ -2481,6 +2453,11 @@ WHERE contribution_id = %1 "; $values['address'] = $addressDetails[0]['display']; } if ($this->_component == 'contribute') { + //get soft contributions + $softContributions = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($this->id, TRUE); + if (!empty($softContributions)) { + $values['softContributions'] = $softContributions['soft_credit']; + } if (isset($this->contribution_page_id)) { CRM_Contribute_BAO_ContributionPage::setValues( $this->contribution_page_id, @@ -2753,6 +2730,9 @@ WHERE contribution_id = %1 "; if (!empty($values['customGroup'])) { $template->assign('customGroup', $values['customGroup']); } + if (!empty($values['softContributions'])) { + $template->assign('softContributions', $values['softContributions']); + } if ($this->_component == 'event') { $template->assign('title', $values['event']['title']); $participantRoles = CRM_Event_PseudoConstant::participantRole(); @@ -3237,18 +3217,29 @@ WHERE contribution_id = %1 "; || $params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) ) { $params['trxnParams']['total_amount'] = -$params['total_amount']; + if (empty($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") { + $creditNoteId = self::createCreditNoteId(); + CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId); + } } elseif (($params['prevContribution']->contribution_status_id == array_search('Pending', $contributionStatus) && $params['prevContribution']->is_pay_later) || $params['prevContribution']->contribution_status_id == array_search('In Progress', $contributionStatus) ) { $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); + if ($params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) { - $params['trxnParams']['to_financial_account_id'] = NULL; + $params['trxnParams']['to_financial_account_id'] = $arAccountId; $params['trxnParams']['total_amount'] = -$params['total_amount']; + if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") { + $creditNoteId = self::createCreditNoteId(); + CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId); + } + } + else { + $params['trxnParams']['from_financial_account_id'] = $arAccountId; } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $params['trxnParams']['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( - $financialTypeID, $relationTypeId); } $itemAmount = $params['trxnParams']['total_amount']; } @@ -4120,8 +4111,7 @@ WHERE con.id = {$contributionId} * @param int $isFirstOrLastRecurringPayment * Deprecated param only used by AuthorizeNetIPN. */ - public static function completeOrder(&$input, &$ids, $objects, $transaction, $recur, $contribution, $isRecurring, - $isFirstOrLastRecurringPayment) { + public static function completeOrder(&$input, &$ids, $objects, $transaction, $recur, $contribution, $isRecurring, $isFirstOrLastRecurringPayment) { $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)) { @@ -4178,6 +4168,10 @@ WHERE con.id = {$contributionId} $values['receipt_from_email'] = $domainValues[1]; } + if (empty($contributionParams['receive_date']) && $changeDate) { + $contributionParams['receive_date'] = $changeDate; + } + if ($recurContrib && $recurContrib->id && !isset($input['is_email_receipt'])) { //CRM-13273 - is_email_receipt setting on recurring contribution should take precedence over contribution page setting // but CRM-16124 if $input['is_email_receipt'] is set then that should not be overridden. @@ -4268,7 +4262,6 @@ LIMIT 1;"; if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) { $eventDetail = civicrm_api3('Event', 'getsingle', array('id' => $objects['event']->id)); $contributionParams['source'] = ts('Online Event Registration') . ': ' . $eventDetail['title']; - if ($eventDetail['is_email_confirm']) { // @todo this should be set by the function that sends the mail after sending. $contributionParams['receipt_date'] = $changeDate; @@ -4406,4 +4399,28 @@ LIMIT 1;"; return $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText); } + /** + * Generate credit note id with next avaible number + * + * @return string + * Credit Note Id. + */ + public static function createCreditNoteId() { + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); + + $creditNoteNum = CRM_Core_DAO::singleValueQuery("SELECT count(creditnote_id) as creditnote_number FROM civicrm_contribution"); + $creditNoteId = NULL; + + do { + $creditNoteNum++; + $creditNoteId = CRM_Utils_Array::value('credit_notes_prefix', $prefixValue) . "" . $creditNoteNum; + $result = civicrm_api3('Contribution', 'getcount', array( + 'sequential' => 1, + 'creditnote_id' => $creditNoteId, + )); + } while ($result > 0); + + return $creditNoteId; + } + }