/** * takes an associative array and creates a contribution object * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contribute_BAO_Contribution object * @access public * @static */ static function &create(&$params, &$ids) { // FIXME: a cludgy hack to fix the dates to MySQL format $dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date'); foreach ($dateFields as $df) { if (isset($params[$df])) { $params[$df] = CRM_Utils_Date::isoToMysql($params[$df]); } } if (CRM_Utils_Array::value('contribution', $ids) && !CRM_Utils_Array::value('softID', $params)) { if ($softID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionSoft', $ids['contribution'], 'id', 'contribution_id')) { $params['softID'] = $softID; } } $transaction = new CRM_Core_Transaction(); // delete the soft credit record if no soft credit contact ID AND no PCP is set in the form if (CRM_Utils_Array::value('contribution', $ids) && (!CRM_Utils_Array::value('soft_credit_to', $params) && !CRM_Utils_Array::value('pcp_made_through_id', $params)) && CRM_Utils_Array::value('softID', $params)) { $softCredit = new CRM_Contribute_DAO_ContributionSoft(); $softCredit->id = $params['softID']; $softCredit->delete(); } $contribution = self::add($params, $ids); if (is_a($contribution, 'CRM_Core_Error')) { $transaction->rollback(); return $contribution; } $params['contribution_id'] = $contribution->id; if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) { CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id); } $session = CRM_Core_Session::singleton(); if (CRM_Utils_Array::value('note', $params)) { $noteParams = array('entity_table' => 'civicrm_contribution', 'note' => $params['note'], 'entity_id' => $contribution->id, 'contact_id' => $session->get('userID'), 'modified_date' => date('Ymd')); if (!$noteParams['contact_id']) { $noteParams['contact_id'] = $params['contact_id']; } CRM_Core_BAO_Note::add($noteParams, CRM_Utils_Array::value('note', $ids)); } // make entry in batch entity batch table if (CRM_Utils_Array::value('batch_id', $params)) { $entityParams = array('batch_id' => $params['batch_id'], 'entity_table' => 'civicrm_contribution', 'entity_id' => $contribution->id); // in some update cases we need to get extra fields - ie an update that doesn't pass in all these params $titleFields = array('contact_id', 'total_amount', 'currency', 'contribution_type_id'); $retrieverequired = 0; foreach ($titleFields as $titleField) { if (!isset($contribution->{$titleField})) { $retrieverequired = 1; break; } } if ($retrieverequired == 1) { $contribution->find(true); } CRM_Core_BAO_Batch::addBatchEntity($entityParams); } // check if activity record exist for this contribution, if // not add activity $activity = new CRM_Activity_DAO_Activity(); $activity->source_record_id = $contribution->id; $activity->activity_type_id = CRM_Core_OptionGroup::getValue('activity_type', 'Contribution', 'name'); if (!$activity->find()) { CRM_Activity_BAO_Activity::addActivity($contribution, 'Offline'); } // Handle soft credit and / or link to personal campaign page if (CRM_Utils_Array::value('soft_credit_to', $params) || CRM_Utils_Array::value('pcp_made_through_id', $params)) { $csParams = array(); if ($id = CRM_Utils_Array::value('softID', $params)) { $csParams['id'] = $params['softID']; } $csParams['contribution_id'] = $contribution->id; // If pcp_made_through_id set, we define soft_credit_to contact based on selected PCP, // else use passed soft_credit_to if (CRM_Utils_Array::value('pcp_made_through_id', $params)) { $csParams['pcp_display_in_roll'] = $params['pcp_display_in_roll'] ? 1 : 0; foreach (array('pcp_roll_nickname', 'pcp_personal_note') as $val) { $csParams[$val] = $params[$val]; } $csParams['pcp_id'] = CRM_Utils_Array::value('pcp_made_through_id', $params); $csParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $csParams['pcp_id'], 'contact_id'); } else { $csParams['contact_id'] = $params['soft_credit_to']; $csParams['pcp_id'] = ''; } // first stage: we register whole amount as credited to given person $csParams['amount'] = $contribution->total_amount; self::addSoftContribution($csParams); } $transaction->commit(); // do not add to recent items for import, CRM-4399 if (!CRM_Utils_Array::value('skipRecentView', $params)) { $url = CRM_Utils_System::url('civicrm/contact/view/contribution', "action=view&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home"); // in some update cases we need to get extra fields - ie an update that doesn't pass in all these params $titleFields = array('contact_id', 'total_amount', 'currency', 'contribution_type_id'); $retrieverequired = 0; foreach ($titleFields as $titleField) { if (!isset($contribution->{$titleField})) { $retrieverequired = 1; break; } } if ($retrieverequired == 1) { $contribution->find(true); } $contributionTypes = CRM_Contribute_PseudoConstant::contributionType(); $title = CRM_Contact_BAO_Contact::displayName($contribution->contact_id) . ' - (' . CRM_Utils_Money::format($contribution->total_amount, $contribution->currency) . ' ' . ' - ' . $contributionTypes[$contribution->contribution_type_id] . ')'; $recentOther = array(); if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) { $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution', "action=update&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home"); } if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) { $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution', "action=delete&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home"); } // add the recently created Contribution CRM_Utils_Recent::add($title, $url, $contribution->id, 'Contribution', $contribution->contact_id, NULL, $recentOther); } return $contribution; }
/** * Function to delete soft credits * * @param int $contributionTypeId * @static */ static function del($params) { //delete from contribution soft table $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); foreach ($params as $column => $value) { $contributionSoft->{$column} = $value; } $contributionSoft->delete(); }
/** * Delete soft credits. * * @param array $params * */ public static function del($params) { //delete from contribution soft table $contributionSoft = new CRM_Contribute_DAO_ContributionSoft(); $contributionSoft->id = $params['id']; if (!$contributionSoft->find()) { return FALSE; } unset($params['id']); foreach ($params as $column => $value) { $contributionSoft->{$column} = $value; } $contributionSoft->delete(); return TRUE; }