Ejemplo n.º 1
0
 /**
  * Takes an associative array and creates a contribution object.
  *
  * the function extract all the params it needs to initialize the create a
  * contribution object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array $params
  *   (reference ) an assoc array of name/value pairs.
  * @param array $ids
  *   The array that holds all the db ids.
  *
  * @return CRM_Contribute_BAO_Contribution|void
  */
 public static function add(&$params, $ids = array())
 {
     if (empty($params)) {
         return NULL;
     }
     //per http://wiki.civicrm.org/confluence/display/CRM/Database+layer we are moving away from $ids array
     $contributionID = CRM_Utils_Array::value('contribution', $ids, CRM_Utils_Array::value('id', $params));
     $duplicates = array();
     if (self::checkDuplicate($params, $duplicates, $contributionID)) {
         $error = CRM_Core_Error::singleton();
         $d = implode(', ', $duplicates);
         $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: {$d}");
         return $error;
     }
     // first clean up all the money fields
     $moneyFields = array('total_amount', 'net_amount', 'fee_amount', 'non_deductible_amount');
     //if priceset is used, no need to cleanup money
     if (!empty($params['skipCleanMoney'])) {
         unset($moneyFields[0]);
     }
     foreach ($moneyFields as $field) {
         if (isset($params[$field])) {
             $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
         }
     }
     //set defaults in create mode
     if (!$contributionID) {
         CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
     }
     self::calculateMissingAmountParams($params, $contributionID);
     if (!empty($params['payment_instrument_id'])) {
         $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
         if ($params['payment_instrument_id'] != array_search('Check', $paymentInstruments)) {
             $params['check_number'] = 'null';
         }
     }
     $setPrevContribution = TRUE;
     // CRM-13964 partial payment
     if (!empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) {
         $partialAmtTotal = $params['partial_payment_total'];
         $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['is_pay_later'] = 0;
             $setPrevContribution = FALSE;
         }
     }
     if ($contributionID) {
         CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params);
     }
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->copyValues($params);
     $contribution->id = $contributionID;
     if (empty($contribution->id)) {
         // (only) on 'create', make sure that a valid currency is set (CRM-16845)
         if (!CRM_Utils_Rule::currencyCode($contribution->currency)) {
             $config = CRM_Core_Config::singleton();
             $contribution->currency = $config->defaultCurrency;
         }
     }
     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
     $contribution->trxn_result_code = CRM_Utils_Array::value('trxn_result_code', $params);
     $contribution->payment_processor = CRM_Utils_Array::value('payment_processor', $params);
     //add Account details
     $params['contribution'] = $contribution;
     self::recordFinancialAccounts($params);
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($contributionID) {
         CRM_Utils_Hook::post('edit', 'Contribution', $contribution->id, $contribution);
     } else {
         CRM_Utils_Hook::post('create', 'Contribution', $contribution->id, $contribution);
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * Creates a new entry in the database.
  *
  * @param array $params
  *   (reference), array $ids.
  *
  * @param $ids
  *
  * @return CRM_Price_DAO_PriceFieldValue
  */
 public static function create(&$params, $ids = array())
 {
     $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
     if (!is_array($params) || empty($params)) {
         return NULL;
     }
     if (!$id && empty($params['name'])) {
         $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
     }
     if ($id && !empty($params['weight'])) {
         if (isset($params['name'])) {
             unset($params['name']);
         }
         $oldWeight = NULL;
         if ($id) {
             $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
         }
         $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
         $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
     } else {
         if (!$id) {
             CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
             if (empty($params['name'])) {
                 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
             }
         }
     }
     return self::add($params, $ids);
 }
Ejemplo n.º 3
0
 /**
  * Add the membership types.
  *
  * @param array $params
  *   Reference array contains the values submitted by the form.
  * @param array $ids
  *   Array contains the id - this param is deprecated.
  *
  *
  * @return object
  */
 public static function add(&$params, $ids = array())
 {
     $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipStatus', $ids));
     if (!$id) {
         CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
         //copy name to label when not passed.
         if (empty($params['label']) && !empty($params['name'])) {
             $params['label'] = $params['name'];
         }
         if (empty($params['name']) && !empty($params['label'])) {
             $params['name'] = $params['label'];
         }
     }
     // set all other defaults to false.
     if (!empty($params['is_default'])) {
         $query = "UPDATE civicrm_membership_status SET is_default = 0";
         CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
     }
     // action is taken depending upon the mode
     $membershipStatus = new CRM_Member_DAO_MembershipStatus();
     $membershipStatus->copyValues($params);
     $membershipStatus->id = $id;
     $membershipStatus->save();
     return $membershipStatus;
 }
Ejemplo n.º 4
0
 /**
  * Takes an associative array and creates a contribution object.
  *
  * the function extract all the params it needs to initialize the create a
  * contribution object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array $params
  *   (reference ) an assoc array of name/value pairs.
  * @param array $ids
  *   The array that holds all the db ids.
  *
  * @return CRM_Contribute_BAO_Contribution|void
  */
 public static function add(&$params, $ids = array())
 {
     if (empty($params)) {
         return NULL;
     }
     //per http://wiki.civicrm.org/confluence/display/CRM/Database+layer we are moving away from $ids array
     $contributionID = CRM_Utils_Array::value('contribution', $ids, CRM_Utils_Array::value('id', $params));
     $duplicates = array();
     if (self::checkDuplicate($params, $duplicates, $contributionID)) {
         $error = CRM_Core_Error::singleton();
         $d = implode(', ', $duplicates);
         $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: {$d}");
         return $error;
     }
     // first clean up all the money fields
     $moneyFields = array('total_amount', 'net_amount', 'fee_amount', 'non_deductible_amount');
     //if priceset is used, no need to cleanup money
     if (!empty($params['skipCleanMoney'])) {
         unset($moneyFields[0]);
     }
     foreach ($moneyFields as $field) {
         if (isset($params[$field])) {
             $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
         }
     }
     //set defaults in create mode
     if (!$contributionID) {
         CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
     }
     $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     //if contribution is created with cancelled or refunded status, add credit note id
     if (!empty($params['contribution_status_id'])) {
         // @todo - should we include Chargeback? If so use self::isContributionStatusNegative($params['contribution_status_id'])
         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();
             }
         }
     } else {
         // Since the fee amount is expecting this (later on) ensure it is always set.
         // It would only not be set for an update where it is unchanged.
         $params['contribution_status_id'] = civicrm_api3('Contribution', 'getvalue', array('id' => $contributionID, 'return' => 'contribution_status_id'));
     }
     if (!$contributionID && CRM_Utils_Array::value('membership_id', $params) && self::checkContributeSettings('deferred_revenue_enabled')) {
         $memberStartDate = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $params['membership_id'], 'start_date');
         if ($memberStartDate) {
             $params['revenue_recognition_date'] = date('Ymd', strtotime($memberStartDate));
         }
     }
     self::calculateMissingAmountParams($params, $contributionID);
     if (!empty($params['payment_instrument_id'])) {
         $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
         if ($params['payment_instrument_id'] != array_search('Check', $paymentInstruments)) {
             $params['check_number'] = 'null';
         }
     }
     $setPrevContribution = TRUE;
     // CRM-13964 partial payment
     if (!empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) {
         $partialAmtTotal = $params['partial_payment_total'];
         $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['is_pay_later'] = 0;
             $setPrevContribution = FALSE;
         }
     }
     if ($contributionID && $setPrevContribution) {
         $params['prevContribution'] = self::getOriginalContribution($contributionID);
     }
     // CRM-16189
     CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params, $contributionID);
     if ($contributionID && !empty($params['revenue_recognition_date']) && !empty($params['prevContribution']) && !($contributionStatus[$params['prevContribution']->contribution_status_id] == 'Pending') && !self::allowUpdateRevenueRecognitionDate($contributionID)) {
         unset($params['revenue_recognition_date']);
     }
     if (!isset($params['tax_amount']) && $setPrevContribution && (isset($params['total_amount']) || isset($params['financial_type_id']))) {
         $params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params);
     }
     if ($contributionID) {
         CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params);
     }
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->copyValues($params);
     $contribution->id = $contributionID;
     if (empty($contribution->id)) {
         // (only) on 'create', make sure that a valid currency is set (CRM-16845)
         if (!CRM_Utils_Rule::currencyCode($contribution->currency)) {
             $contribution->currency = CRM_Core_Config::singleton()->defaultCurrency;
         }
     }
     $result = $contribution->save();
     // Add financial_trxn details as part of fix for CRM-4724
     $contribution->trxn_result_code = CRM_Utils_Array::value('trxn_result_code', $params);
     $contribution->payment_processor = CRM_Utils_Array::value('payment_processor', $params);
     //add Account details
     $params['contribution'] = $contribution;
     self::recordFinancialAccounts($params);
     if (self::isUpdateToRecurringContribution($params)) {
         CRM_Contribute_BAO_ContributionRecur::updateOnNewPayment(!empty($params['contribution_recur_id']) ? $params['contribution_recur_id'] : $params['prevContribution']->contribution_recur_id, $contributionStatus[$params['contribution_status_id']]);
     }
     CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
     if ($contributionID) {
         CRM_Utils_Hook::post('edit', 'Contribution', $contribution->id, $contribution);
     } else {
         CRM_Utils_Hook::post('create', 'Contribution', $contribution->id, $contribution);
     }
     return $result;
 }