Exemplo n.º 1
0
 /**
  * Function to setDefaults according to Pledge Id
  * for batch entry pledges
  */
 public function getPledgeDefaults()
 {
     $details = array();
     if (!empty($_POST['pid'])) {
         $pledgeID = CRM_Utils_Type::escape($_POST['pid'], 'Integer');
         $details = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
     }
     CRM_Utils_JSON::output($details);
 }
Exemplo n.º 2
0
Arquivo: AJAX.php Projeto: kidaa30/yes
 /**
  * Function to setDefaults according to Pledge Id
  * for batch entry pledges
  */
 public function getPledgeDefaults()
 {
     $details = array();
     if (!empty($_POST['pid'])) {
         $pledgeID = CRM_Utils_Type::escape($_POST['pid'], 'Integer');
         $details = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
     }
     echo json_encode($details);
     CRM_Utils_System::civiExit();
 }
/**
 * Add or update a plege payment. Pledge Payment API doesn't actually add a pledge
 *  if the request is to 'create' and 'id' is not passed in
 * the oldest pledge with no associated contribution is updated
 *
 * @todo possibly add ability to add payment if there are less payments than pledge installments
 * @todo possibily add ability to recalc dates if the schedule is changed
 *
 * @param  array   $params    input parameters
 * {@getfields PledgePayment_create}
 * @example PledgePaymentCreate.php
 *
 * @return array API Result
 * @static void
 * @access public
 */
function civicrm_api3_pledge_payment_create($params)
{
    $paymentParams = $params;
    if (empty($params['id']) && !CRM_Utils_Array::value('option.create_new', $params)) {
        $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']);
        if (empty($paymentDetails)) {
            return civicrm_api3_create_error("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one");
        } elseif (is_array($paymentDetails)) {
            $paymentParams = array_merge($params, $paymentDetails);
        }
    }
    $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams);
    _civicrm_api3_object_to_array($dao, $result[$dao->id]);
    //update pledge status
    CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($params['pledge_id']);
    return civicrm_api3_create_success($result, $params, 'pledge_payment', 'create', $dao);
}
/**
 * Add or update a plege payment
 *
 * @param  array   $params           (reference ) input parameters
 *
 * @return array (reference )        pledge_id of created or updated record
 * @static void
 * @access public
 */
function &civicrm_pledge_payment_create(&$params)
{
    _civicrm_initialize();
    //GAP - update doesn't recalculate payment dates on existing payment schedule  - not the sure the code is in Civi to leverage
    if (empty($params)) {
        return civicrm_create_error(ts('No input parameters present'));
    }
    if (!is_array($params)) {
        return civicrm_create_error(ts('Input parameters is not an array'));
    }
    $error = _civicrm_pledgepayment_check_params($params);
    if (civicrm_error($error)) {
        return $error;
    }
    $values = array();
    require_once 'CRM/Pledge/BAO/PledgePayment.php';
    $error = _civicrm_pledgepayment_format_params($params, $values);
    if (civicrm_error($error)) {
        return $error;
    }
    $pledge = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']);
    $params['id'] = $pledge['id'];
    //params ID needs to be pledge payment ID
    // pledge payment isn't retrieved if only one exists - the status is not set correctly causing this so let's get it for now as a cludgey make it work
    // copied from getOldestPayment function
    if (!$params['id']) {
        $query = "\nSELECT civicrm_pledge_payment.id id, civicrm_pledge_payment.scheduled_amount amount\nFROM civicrm_pledge, civicrm_pledge_payment\nWHERE civicrm_pledge.id = civicrm_pledge_payment.pledge_id\n  AND civicrm_pledge.id = %1\nLIMIT 0, 1  \n";
        $params[1] = array($params['pledge_id'], 'Integer');
        $payment = CRM_Core_DAO::executeQuery($query, $params);
        $paymentDetails = NULL;
        if ($payment->fetch()) {
            $params['id'] = $payment->id;
        }
    }
    CRM_Pledge_BAO_PledgePayment::add($params);
    //update pledge status
    CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($params['pledge_id']);
    return $errors;
}
Exemplo n.º 5
0
 function updateRecurLinkedPledge(&$contribution)
 {
     $returnProperties = array('id', 'pledge_id');
     $paymentDetails = $paymentIDs = array();
     if (CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $contribution->id, $paymentDetails, $returnProperties)) {
         foreach ($paymentDetails as $key => $value) {
             $paymentIDs[] = $value['id'];
             $pledgeId = $value['pledge_id'];
         }
     } else {
         //payment is not already linked - if it is linked with a pledge we need to create a link.
         // return if it is not recurring contribution
         if (!$contribution->contribution_recur_id) {
             return;
         }
         $relatedContributions = new CRM_Contribute_DAO_Contribution();
         $relatedContributions->contribution_recur_id = $contribution->contribution_recur_id;
         $relatedContributions->find();
         while ($relatedContributions->fetch()) {
             CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'contribution_id', $relatedContributions->id, $paymentDetails, $returnProperties);
         }
         if (empty($paymentDetails)) {
             // payment is not linked with a pledge and neither are any other contributions on this
             return;
         }
         foreach ($paymentDetails as $key => $value) {
             $pledgeId = $value['pledge_id'];
         }
         // we have a pledge now we need to get the oldest unpaid payment
         $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
         if (empty($paymentDetails['id'])) {
             // we can assume this pledge is now completed
             // return now so we don't create a core error & roll back
             return;
         }
         $paymentDetails['contribution_id'] = $contribution->id;
         $paymentDetails['status_id'] = $contribution->contribution_status_id;
         $paymentDetails['actual_amount'] = $contribution->total_amount;
         // put contribution against it
         $payment = CRM_Pledge_BAO_PledgePayment::add($paymentDetails);
         $paymentIDs[] = $payment->id;
     }
     // update pledge and corresponding payment statuses
     CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIDs, $contribution->contribution_status_id, NULL, $contribution->total_amount);
 }
Exemplo n.º 6
0
 public function preProcessPledge()
 {
     //get the payment values associated with given pledge payment id OR check for payments due.
     $this->_pledgeValues = array();
     if ($this->_ppID) {
         $payParams = array('id' => $this->_ppID);
         CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']);
         $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']);
         $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']);
         $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']);
         //get all status
         $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
         if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) {
             CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or  'Overdue'."));
         }
         //get the pledge values associated with given pledge payment.
         $ids = array();
         $pledgeParams = array('id' => $this->_pledgeID);
         CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids);
         $this->assign('ppID', $this->_ppID);
     } else {
         // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206
         if (isset($this->_contactID)) {
             $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID);
             if (!empty($contactPledges)) {
                 $payments = $paymentsDue = NULL;
                 $multipleDue = FALSE;
                 foreach ($contactPledges as $key => $pledgeId) {
                     $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
                     if ($payments) {
                         if ($paymentsDue) {
                             $multipleDue = TRUE;
                             break;
                         } else {
                             $paymentsDue = $payments;
                         }
                     }
                 }
                 if ($multipleDue) {
                     // Show link to pledge tab since more than one pledge has a payment due
                     $pledgeTab = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge");
                     CRM_Core_Session::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)), ts('Notice'), 'alert');
                 } elseif ($paymentsDue) {
                     // Show user link to oldest Pending or Overdue pledge payment
                     $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']);
                     $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
                     if ($this->_mode) {
                         $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live");
                     } else {
                         $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge");
                     }
                     CRM_Core_Session::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array(1 => $ppUrl, 2 => $ppAmountDue, 3 => $ppSchedDate)), ts('Notice'), 'alert');
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 /**
  * Build the form object.
  *
  *
  * @return void
  */
 public function buildQuickForm()
 {
     if (!$this->_profileId) {
         CRM_Core_Error::fatal(ts('Profile for bulk data entry is missing.'));
     }
     $this->addElement('hidden', 'batch_id', $this->_batchId);
     $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
     // get the profile information
     if ($this->_batchInfo['type_id'] == $batchTypes['Contribution']) {
         CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions'));
         $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
     } elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
         CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships'));
         $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
     } elseif ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
         CRM_Utils_System::setTitle(ts('Batch Data Entry for Pledge Payments'));
         $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
     }
     $this->_fields = array();
     $this->_fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW);
     // remove file type field and then limit fields
     $suppressFields = FALSE;
     $removehtmlTypes = array('File', 'Autocomplete-Select');
     foreach ($this->_fields as $name => $field) {
         if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && in_array($this->_fields[$name]['html_type'], $removehtmlTypes)) {
             $suppressFields = TRUE;
             unset($this->_fields[$name]);
         }
         //fix to reduce size as we are using this field in grid
         if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) {
             //shrink class to "form-text-medium"
             $this->_fields[$name]['attributes']['size'] = 19;
         }
     }
     $this->addFormRule(array('CRM_Batch_Form_Entry', 'formRule'), $this);
     // add the force save button
     $forceSave = $this->getButtonName('upload', 'force');
     $this->addElement('submit', $forceSave, ts('Ignore Mismatch & Process the Batch?'));
     $this->addButtons(array(array('type' => 'upload', 'name' => ts('Validate & Process the Batch'), 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Save & Continue Later'))));
     $this->assign('rowCount', $this->_batchInfo['item_count'] + 1);
     $fileFieldExists = FALSE;
     $preserveDefaultsArray = array('first_name', 'last_name', 'middle_name', 'organization_name', 'household_name');
     $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
     $contactReturnProperties = array();
     $config = CRM_Core_Config::singleton();
     for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) {
         $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array('create' => TRUE, 'placeholder' => ts('- select -')));
         // special field specific to membership batch udpate
         if ($this->_batchInfo['type_id'] == 2) {
             $options = array(1 => ts('Add Membership'), 2 => ts('Renew Membership'));
             $this->add('select', "member_option[{$rowNumber}]", '', $options);
         }
         if ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
             $options = array('' => '-select-');
             $optionTypes = array('1' => ts('Adjust Pledge Payment Schedule?'), '2' => ts('Adjust Total Pledge Amount?'));
             $this->add('select', "option_type[{$rowNumber}]", NULL, $optionTypes);
             if (!empty($this->_batchId) && !empty($this->_batchInfo['data']) && !empty($rowNumber)) {
                 $dataValues = json_decode($this->_batchInfo['data'], TRUE);
                 if (!empty($dataValues['values']['primary_contact_id'][$rowNumber])) {
                     $pledgeIDs = CRM_Pledge_BAO_Pledge::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
                     foreach ($pledgeIDs as $pledgeID) {
                         $pledgePayment = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
                         $options += array($pledgeID => CRM_Utils_Date::customFormat($pledgePayment['schedule_date'], '%m/%d/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
                     }
                 }
             }
             $this->add('select', "open_pledges[{$rowNumber}]", '', $options);
         }
         foreach ($this->_fields as $name => $field) {
             if (in_array($field['field_type'], $contactTypes)) {
                 $fld = explode('-', $field['name']);
                 $contactReturnProperties[$field['name']] = $fld[0];
             }
             CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber);
             if (in_array($field['name'], $preserveDefaultsArray)) {
                 $this->_preserveDefault = FALSE;
             }
         }
     }
     $this->assign('fields', $this->_fields);
     CRM_Core_Resources::singleton()->addSetting(array('contact' => array('return' => implode(',', $contactReturnProperties), 'fieldmap' => array_flip($contactReturnProperties))));
     // don't set the status message when form is submitted.
     $buttonName = $this->controller->getButtonName('submit');
     if ($suppressFields && $buttonName != '_qf_Entry_next') {
         CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Some Fields Excluded'), 'info');
     }
 }
Exemplo n.º 8
0
 /**
  * Process the form submission.
  *
  *
  * @return void
  */
 public function postProcess()
 {
     //get the submitted form values.
     $formValues = $this->controller->exportValues($this->_name);
     $params = array();
     $formValues['scheduled_date'] = CRM_Utils_Date::processDate($formValues['scheduled_date']);
     $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']);
     $params['currency'] = CRM_Utils_Array::value('currency', $formValues);
     $now = date('Ymd');
     $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) {
         $params['status_id'] = array_search('Overdue', $contributionStatus);
     } else {
         $params['status_id'] = array_search('Pending', $contributionStatus);
     }
     $params['id'] = $this->_id;
     $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
     CRM_Pledge_BAO_PledgePayment::add($params);
     $adjustTotalAmount = FALSE;
     if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
         $adjustTotalAmount = TRUE;
     }
     $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'scheduled_amount', 'id');
     $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2);
     if ($oldestPaymentAmount['count'] != 1 && $oldestPaymentAmount['id'] == $params['id']) {
         $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
     }
     if ($formValues['scheduled_amount'] - $pledgeScheduledAmount >= $oldestPaymentAmount['amount']) {
         $adjustTotalAmount = TRUE;
     }
     //update pledge status
     CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, array($params['id']), $params['status_id'], NULL, $formValues['scheduled_amount'], $adjustTotalAmount);
     $statusMsg = ts('Pledge Payment Schedule has been updated.');
     CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
 }
Exemplo n.º 9
0
/**
 * take the input parameter list as specified in the data model and
 * convert it into the same format that we use in QF and BAO object
 *
 * @param array $params
 *   Associative array of property name/value.
 *                             pairs to insert in new contact.
 * @param array $values
 *   The reformatted properties that we can use internally.
 *                            '
 *
 * @param bool $create
 * @param null $onDuplicate
 *
 * @return array|CRM_Error
 */
function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = NULL)
{
    // copy all the contribution fields as is
    $fields = CRM_Contribute_DAO_Contribution::fields();
    _civicrm_api3_store_values($fields, $params, $values);
    require_once 'CRM/Core/OptionGroup.php';
    $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        // Handling Custom Data
        if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
            $values[$key] = $value;
            $type = $customFields[$customFieldID]['html_type'];
            if ($type == 'CheckBox' || $type == 'Multi-Select') {
                $mulValues = explode(',', $value);
                $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
                $values[$key] = array();
                foreach ($mulValues as $v1) {
                    foreach ($customOption as $customValueID => $customLabel) {
                        $customValue = $customLabel['value'];
                        if (strtolower($customLabel['label']) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            if ($type == 'CheckBox') {
                                $values[$key][$customValue] = 1;
                            } else {
                                $values[$key][] = $customValue;
                            }
                        }
                    }
                }
            } elseif ($type == 'Select' || $type == 'Radio' || $type == 'Autocomplete-Select' && $customFields[$customFieldID]['data_type'] == 'String') {
                $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
                foreach ($customOption as $customFldID => $customValue) {
                    $val = CRM_Utils_Array::value('value', $customValue);
                    $label = CRM_Utils_Array::value('label', $customValue);
                    $label = strtolower($label);
                    $value = strtolower(trim($value));
                    if ($value == $label || $value == strtolower($val)) {
                        $values[$key] = $val;
                    }
                }
            }
        }
        switch ($key) {
            case 'contribution_contact_id':
                if (!CRM_Utils_Rule::integer($value)) {
                    return civicrm_api3_create_error("contact_id not valid: {$value}");
                }
                $dao = new CRM_Core_DAO();
                $qParams = array();
                $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = {$value}", $qParams);
                if (!isset($svq)) {
                    return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                } elseif ($svq == 1) {
                    return civicrm_api3_create_error("Invalid Contact ID: contact_id {$value} is a soft-deleted contact.");
                }
                $values['contact_id'] = $values['contribution_contact_id'];
                unset($values['contribution_contact_id']);
                break;
            case 'contact_type':
                // import contribution record according to select contact type
                require_once 'CRM/Contact/DAO/Contact.php';
                $contactType = new CRM_Contact_DAO_Contact();
                // when insert mode check contact id or external identifier
                if (!empty($params['contribution_contact_id']) || !empty($params['external_identifier'])) {
                    if (!empty($params['contribution_contact_id'])) {
                        $contactType->id = CRM_Utils_Array::value('contribution_contact_id', $params);
                    } elseif (!empty($params['external_identifier'])) {
                        $contactType->external_identifier = $params['external_identifier'];
                    }
                    if ($contactType->find(TRUE)) {
                        if ($params['contact_type'] != $contactType->contact_type) {
                            return civicrm_api3_create_error("Contact Type is wrong: {$contactType->contact_type}");
                        }
                    }
                } elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) {
                    // when update mode check contribution id or trxn id or
                    // invoice id
                    $contactId = new CRM_Contribute_DAO_Contribution();
                    if (!empty($params['contribution_id'])) {
                        $contactId->id = $params['contribution_id'];
                    } elseif (!empty($params['trxn_id'])) {
                        $contactId->trxn_id = $params['trxn_id'];
                    } elseif (!empty($params['invoice_id'])) {
                        $contactId->invoice_id = $params['invoice_id'];
                    }
                    if ($contactId->find(TRUE)) {
                        $contactType->id = $contactId->contact_id;
                        if ($contactType->find(TRUE)) {
                            if ($params['contact_type'] != $contactType->contact_type) {
                                return civicrm_api3_create_error("Contact Type is wrong: {$contactType->contact_type}");
                            }
                        }
                    }
                } else {
                    if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
                        return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
                    } else {
                        return civicrm_api3_create_error("Empty Contact and External ID. Row was skipped.");
                    }
                }
                break;
            case 'receive_date':
            case 'cancel_date':
            case 'receipt_date':
            case 'thankyou_date':
                if (!CRM_Utils_Rule::dateTime($value)) {
                    return civicrm_api3_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'non_deductible_amount':
            case 'total_amount':
            case 'fee_amount':
            case 'net_amount':
                if (!CRM_Utils_Rule::money($value)) {
                    return civicrm_api3_create_error("{$key} not a valid amount: {$value}");
                }
                break;
            case 'currency':
                if (!CRM_Utils_Rule::currencyCode($value)) {
                    return civicrm_api3_create_error("currency not a valid code: {$value}");
                }
                break;
            case 'financial_type':
                require_once 'CRM/Contribute/PseudoConstant.php';
                $contriTypes = CRM_Contribute_PseudoConstant::financialType();
                foreach ($contriTypes as $val => $type) {
                    if (strtolower($value) == strtolower($type)) {
                        $values['financial_type_id'] = $val;
                        break;
                    }
                }
                if (empty($values['financial_type_id'])) {
                    return civicrm_api3_create_error("Financial Type is not valid: {$value}");
                }
                break;
            case 'payment_instrument':
                require_once 'CRM/Core/OptionGroup.php';
                $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value);
                if (empty($values['payment_instrument_id'])) {
                    return civicrm_api3_create_error("Payment Instrument is not valid: {$value}");
                }
                break;
            case 'contribution_status_id':
                require_once 'CRM/Core/OptionGroup.php';
                if (!($values['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', $value))) {
                    return civicrm_api3_create_error("Contribution Status is not valid: {$value}");
                }
                break;
            case 'soft_credit':
                // import contribution record according to select contact type
                // validate contact id and external identifier.
                $value[$key] = $mismatchContactType = $softCreditContactIds = '';
                if (isset($params[$key]) && is_array($params[$key])) {
                    foreach ($params[$key] as $softKey => $softParam) {
                        $contactId = CRM_Utils_Array::value('contact_id', $softParam);
                        $externalId = CRM_Utils_Array::value('external_identifier', $softParam);
                        $email = CRM_Utils_Array::value('email', $softParam);
                        if ($contactId || $externalId) {
                            require_once 'CRM/Contact/DAO/Contact.php';
                            $contact = new CRM_Contact_DAO_Contact();
                            $contact->id = $contactId;
                            $contact->external_identifier = $externalId;
                            $errorMsg = NULL;
                            if (!$contact->find(TRUE)) {
                                $field = $contactId ? ts('Contact ID') : ts('External ID');
                                $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.", array(1 => $field, 2 => $contactId ? $contactId : $externalId));
                            }
                            if ($errorMsg) {
                                return civicrm_api3_create_error($errorMsg, $value[$key]);
                            }
                            // finally get soft credit contact id.
                            $values[$key][$softKey] = $softParam;
                            $values[$key][$softKey]['contact_id'] = $contact->id;
                        } elseif ($email) {
                            if (!CRM_Utils_Rule::email($email)) {
                                return civicrm_api3_create_error("Invalid email address {$email} provided for Soft Credit. Row was skipped");
                            }
                            // get the contact id from duplicate contact rule, if more than one contact is returned
                            // we should return error, since current interface allows only one-one mapping
                            $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']);
                            $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
                            if (!$checkDedupe['is_error']) {
                                return civicrm_api3_create_error("Invalid email address(doesn't exist) {$email} for Soft Credit. Row was skipped");
                            } else {
                                $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
                                if (count($matchingContactIds) > 1) {
                                    return civicrm_api3_create_error("Invalid email address(duplicate) {$email} for Soft Credit. Row was skipped");
                                } elseif (count($matchingContactIds) == 1) {
                                    $contactId = $matchingContactIds[0];
                                    unset($softParam['email']);
                                    $values[$key][$softKey] = $softParam + array('contact_id' => $contactId);
                                }
                            }
                        }
                    }
                }
                break;
            case 'pledge_payment':
            case 'pledge_id':
                // giving respect to pledge_payment flag.
                if (empty($params['pledge_payment'])) {
                    continue;
                }
                // get total amount of from import fields
                $totalAmount = CRM_Utils_Array::value('total_amount', $params);
                $onDuplicate = CRM_Utils_Array::value('onDuplicate', $params);
                // we need to get contact id $contributionContactID to
                // retrieve pledge details as well as to validate pledge ID
                // first need to check for update mode
                if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id'])) {
                    $contribution = new CRM_Contribute_DAO_Contribution();
                    if ($params['contribution_id']) {
                        $contribution->id = $params['contribution_id'];
                    } elseif ($params['trxn_id']) {
                        $contribution->trxn_id = $params['trxn_id'];
                    } elseif ($params['invoice_id']) {
                        $contribution->invoice_id = $params['invoice_id'];
                    }
                    if ($contribution->find(TRUE)) {
                        $contributionContactID = $contribution->contact_id;
                        if (!$totalAmount) {
                            $totalAmount = $contribution->total_amount;
                        }
                    } else {
                        return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                    }
                } else {
                    // first get the contact id for given contribution record.
                    if (!empty($params['contribution_contact_id'])) {
                        $contributionContactID = $params['contribution_contact_id'];
                    } elseif (!empty($params['external_identifier'])) {
                        require_once 'CRM/Contact/DAO/Contact.php';
                        $contact = new CRM_Contact_DAO_Contact();
                        $contact->external_identifier = $params['external_identifier'];
                        if ($contact->find(TRUE)) {
                            $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id;
                        } else {
                            return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                        }
                    } else {
                        // we need to get contribution contact using de dupe
                        $error = _civicrm_api3_deprecated_check_contact_dedupe($params);
                        if (isset($error['error_message']['params'][0])) {
                            $matchedIDs = explode(',', $error['error_message']['params'][0]);
                            // check if only one contact is found
                            if (count($matchedIDs) > 1) {
                                return civicrm_api3_create_error($error['error_message']['message'], 'pledge_payment');
                            } else {
                                $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
                            }
                        } else {
                            return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                        }
                    }
                }
                if (!empty($params['pledge_id'])) {
                    if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
                        return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment');
                    }
                    $values['pledge_id'] = $params['pledge_id'];
                } else {
                    // check if there are any pledge related to this contact, with payments pending or in progress
                    require_once 'CRM/Pledge/BAO/Pledge.php';
                    $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID);
                    if (empty($pledgeDetails)) {
                        return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.', 'pledge_payment');
                    } elseif (count($pledgeDetails) > 1) {
                        return civicrm_api3_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.', 'pledge_payment');
                    }
                    // this mean we have only one pending / in progress pledge
                    $values['pledge_id'] = $pledgeDetails[0];
                }
                // we need to check if oldest payment amount equal to contribution amount
                require_once 'CRM/Pledge/BAO/PledgePayment.php';
                $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
                if ($pledgePaymentDetails['amount'] == $totalAmount) {
                    $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
                } else {
                    return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', 'pledge_payment');
                }
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    if ($create) {
        // CRM_Contribute_BAO_Contribution::add() handles contribution_source
        // So, if $values contains contribution_source, convert it to source
        $changes = array('contribution_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return NULL;
}
 /**
  * Function to set variables up before form is built
  *
  * @return void
  * @access public
  */
 public function preProcess()
 {
     //check permission for action.
     if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) {
         CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
     }
     $this->_cdType = CRM_Utils_Array::value('type', $_GET);
     $this->assign('cdType', FALSE);
     if ($this->_cdType) {
         $this->assign('cdType', TRUE);
         return CRM_Custom_Form_CustomData::preProcess($this);
     }
     $this->_formType = CRM_Utils_Array::value('formType', $_GET);
     // get price set id.
     $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET);
     $this->set('priceSetId', $this->_priceSetId);
     $this->assign('priceSetId', $this->_priceSetId);
     //get the pledge payment id
     $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this);
     //get the contact id
     $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
     //get the action.
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
     $this->assign('action', $this->_action);
     //get the contribution id if update
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
     $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
     $this->assign('context', $this->_context);
     $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
     //set the contribution mode.
     $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
     $this->assign('contributionMode', $this->_mode);
     $this->_paymentProcessor = array('billing_mode' => 1);
     $this->assign('showCheckNumber', FALSE);
     $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
     //ensure that processor has a valid config
     //only valid processors get display to user
     if ($this->_mode) {
         $validProcessors = array();
         $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 )");
         foreach ($processors as $ppID => $label) {
             $paymentProcessor = CRM_Core_BAO_PaymentProcessor::getPayment($ppID, $this->_mode);
             // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
             // to show receive date
             $processorsSupportingFutureStartDate = array('AuthNet');
             if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
                 $this->assign('processorSupportsFutureStartDate', TRUE);
             }
             if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
                 continue;
             } elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode == 'live') {
                 continue;
             } else {
                 $paymentObject = CRM_Core_Payment::singleton($this->_mode, $paymentProcessor, $this);
                 $error = $paymentObject->checkConfig();
                 if (empty($error)) {
                     $validProcessors[$ppID] = $label;
                 }
                 $paymentObject = NULL;
             }
         }
         if (empty($validProcessors)) {
             CRM_Core_Error::fatal(ts('You will need to configure the %1 settings for your Payment Processor before you can submit credit card transactions.', array(1 => $this->_mode)));
         } else {
             $this->_processors = $validProcessors;
         }
         //get the valid recurring processors.
         $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
         $this->_recurPaymentProcessors = array_intersect_assoc($this->_processors, $recurring);
     }
     $this->assign('recurringPaymentProcessorIds', empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)));
     // this required to show billing block
     $this->assign_by_ref('paymentProcessor', $paymentProcessor);
     $this->assign('hidePayPalExpress', TRUE);
     if ($this->_contactID) {
         list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
         $this->assign('displayName', $this->userDisplayName);
     }
     // also check for billing information
     // get the billing location type
     $locationTypes = CRM_Core_PseudoConstant::locationType();
     $this->_bltID = array_search('Billing', $locationTypes);
     if (!$this->_bltID) {
         CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
     }
     $this->set('bltID', $this->_bltID);
     $this->assign('bltID', $this->_bltID);
     $this->_fields = array();
     // payment fields are depending on payment type
     if (CRM_Utils_Array::value('payment_type', $this->_processors) & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT) {
         CRM_Core_Payment_Form::setDirectDebitFields($this);
     } else {
         CRM_Core_Payment_Form::setCreditCardFields($this);
     }
     if ($this->_action & CRM_Core_Action::DELETE) {
         return;
     }
     $config = CRM_Core_Config::singleton();
     if (in_array('CiviPledge', $config->enableComponents) && !$this->_formType) {
         //get the payment values associated with given pledge payment id OR check for payments due.
         $this->_pledgeValues = array();
         if ($this->_ppID) {
             $payParams = array('id' => $this->_ppID);
             CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']);
             $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']);
             $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']);
             $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']);
             //get all status
             $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
             if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) {
                 CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or  'Overdue'."));
             }
             //get the pledge values associated with given pledge payment.
             $ids = array();
             $pledgeParams = array('id' => $this->_pledgeID);
             CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids);
             $this->assign('ppID', $this->_ppID);
         } else {
             // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206
             if (isset($this->_contactID)) {
                 $contactPledges = array();
                 $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID);
                 if (!empty($contactPledges)) {
                     $payments = $paymentsDue = NULL;
                     $multipleDue = FALSE;
                     foreach ($contactPledges as $key => $pledgeId) {
                         $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
                         if ($payments) {
                             if ($paymentsDue) {
                                 $multipleDue = TRUE;
                                 break;
                             } else {
                                 $paymentsDue = $payments;
                             }
                         }
                     }
                     if ($multipleDue) {
                         // Show link to pledge tab since more than one pledge has a payment due
                         $pledgeTab = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge");
                         CRM_Core_Session::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)));
                     } elseif ($paymentsDue) {
                         // Show user link to oldest Pending or Overdue pledge payment
                         $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']);
                         $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
                         if ($this->_mode) {
                             $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live");
                         } else {
                             $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge");
                         }
                         CRM_Core_Session::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array(1 => $ppUrl, 2 => $ppAmountDue, 3 => $ppSchedDate)));
                     }
                 }
             }
         }
     }
     $this->_values = array();
     // current contribution id
     if ($this->_id) {
         //to get Premium id
         $sql = "\nSELECT *\nFROM   civicrm_contribution_product\nWHERE  contribution_id = {$this->_id}\n";
         $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
         if ($dao->fetch()) {
             $this->_premiumID = $dao->id;
             $this->_productDAO = $dao;
         }
         $dao->free();
         $ids = array();
         $params = array('id' => $this->_id);
         CRM_Contribute_BAO_Contribution::getValues($params, $this->_values, $ids);
         //do check for online / recurring contributions
         $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnIds($this->_id, 'civicrm_contribution');
         $this->_online = CRM_Utils_Array::value('entityFinancialTrxnId', $fids);
         //don't allow to update all fields for recuring contribution.
         if (!$this->_online) {
             $this->_online = CRM_Utils_Array::value('contribution_recur_id', $this->_values);
         }
         $this->assign('isOnline', $this->_online ? TRUE : FALSE);
         //unset the honor type id:when delete the honor_contact_id
         //and edit the contribution, honoree infomation pane open
         //since honor_type_id is present
         if (!CRM_Utils_Array::value('honor_contact_id', $this->_values)) {
             unset($this->_values['honor_type_id']);
         }
         //to get note id
         $daoNote = new CRM_Core_BAO_Note();
         $daoNote->entity_table = 'civicrm_contribution';
         $daoNote->entity_id = $this->_id;
         if ($daoNote->find(TRUE)) {
             $this->_noteID = $daoNote->id;
             $this->_values['note'] = $daoNote->note;
         }
         $this->_contributionType = $this->_values['contribution_type_id'];
         $csParams = array('contribution_id' => $this->_id);
         $softCredit = CRM_Contribute_BAO_Contribution::getSoftContribution($csParams, TRUE);
         if (CRM_Utils_Array::value('soft_credit_to', $softCredit)) {
             $softCredit['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $softCredit['soft_credit_to'], 'sort_name');
         }
         $this->_values['soft_credit_to'] = CRM_Utils_Array::value('sort_name', $softCredit);
         $this->_values['softID'] = CRM_Utils_Array::value('soft_credit_id', $softCredit);
         $this->_values['soft_contact_id'] = CRM_Utils_Array::value('soft_credit_to', $softCredit);
         if (CRM_Utils_Array::value('pcp_id', $softCredit)) {
             $pcpId = CRM_Utils_Array::value('pcp_id', $softCredit);
             $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
             $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute');
             $this->_values['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $softCredit) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
             $this->_values['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $softCredit);
             $this->_values['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $softCredit);
             $this->_values['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $softCredit);
             $this->_values['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $softCredit);
         }
         //display check number field only if its having value or its offline mode.
         if (CRM_Utils_Array::value('payment_instrument_id', $this->_values) == CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name') || CRM_Utils_Array::value('check_number', $this->_values)) {
             $this->assign('showCheckNumber', TRUE);
         }
     }
     // when custom data is included in this page
     if (CRM_Utils_Array::value('hidden_custom', $_POST)) {
         $this->set('type', 'Contribution');
         $this->set('subType', CRM_Utils_Array::value('contribution_type_id', $_POST));
         $this->set('entityId', $this->_id);
         CRM_Custom_Form_CustomData::preProcess($this);
         CRM_Custom_Form_CustomData::buildQuickForm($this);
         CRM_Custom_Form_CustomData::setDefaultValues($this);
     }
     $this->_lineItems = array();
     if ($this->_id) {
         $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1);
         empty($lineItem) ? null : ($this->_lineItems[] = $lineItem);
     }
     $this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems);
 }
Exemplo n.º 11
0
/**
 * take the input parameter list as specified in the data model and
 * convert it into the same format that we use in QF and BAO object
 *
 * @param array  $params       Associative array of property name/value
 *                             pairs to insert in new contact.
 * @param array  $values       The reformatted properties that we can use internally
 *                            '
 *
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_contribute_formatted_param(&$params, &$values, $create = FALSE)
{
    // copy all the contribution fields as is
    $fields = CRM_Contribute_DAO_Contribution::fields();
    _civicrm_store_values($fields, $params, $values);
    require_once 'CRM/Core/OptionGroup.php';
    $customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        //Handling Custom Data
        _civicrm_generic_handle_custom_data($key, $value, $values, $customFields);
        switch ($key) {
            case 'contribution_contact_id':
                if (!CRM_Utils_Rule::integer($value)) {
                    return civicrm_create_error("contact_id not valid: {$value}");
                }
                $dao = new CRM_Core_DAO();
                $qParams = array();
                $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams);
                if (!$svq) {
                    return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
                $values['contact_id'] = $values['contribution_contact_id'];
                unset($values['contribution_contact_id']);
                break;
            case 'contact_type':
                //import contribution record according to select contact type
                require_once 'CRM/Contact/DAO/Contact.php';
                $contactType = new CRM_Contact_DAO_Contact();
                //when insert mode check contact id or external identifire
                if ($params['contribution_contact_id'] || $params['external_identifier']) {
                    if ($params['contribution_contact_id']) {
                        $contactType->id = $params['contribution_contact_id'];
                    } elseif ($params['external_identifier']) {
                        $contactType->external_identifier = $params['external_identifier'];
                    }
                    if ($contactType->find(TRUE)) {
                        if ($params['contact_type'] != $contactType->contact_type) {
                            return civicrm_create_error("Contact Type is wrong: {$contactType->contact_type}");
                        }
                    }
                } elseif ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id']) {
                    //when update mode check contribution id or trxn id or
                    //invoice id
                    $contactId = new CRM_Contribute_DAO_Contribution();
                    if ($params['contribution_id']) {
                        $contactId->id = $params['contribution_id'];
                    } elseif ($params['trxn_id']) {
                        $contactId->trxn_id = $params['trxn_id'];
                    } elseif ($params['invoice_id']) {
                        $contactId->invoice_id = $params['invoice_id'];
                    }
                    if ($contactId->find(TRUE)) {
                        $contactType->id = $contactId->contact_id;
                        if ($contactType->find(TRUE)) {
                            if ($params['contact_type'] != $contactType->contact_type) {
                                return civicrm_create_error("Contact Type is wrong: {$contactType->contact_type}");
                            }
                        }
                    }
                }
                break;
            case 'receive_date':
            case 'cancel_date':
            case 'receipt_date':
            case 'thankyou_date':
                if (!CRM_Utils_Rule::date($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'non_deductible_amount':
            case 'total_amount':
            case 'fee_amount':
            case 'net_amount':
                if (!CRM_Utils_Rule::money($value)) {
                    return civicrm_create_error("{$key} not a valid amount: {$value}");
                }
                break;
            case 'currency':
                if (!CRM_Utils_Rule::currencyCode($value)) {
                    return civicrm_create_error("currency not a valid code: {$value}");
                }
                break;
            case 'contribution_type':
                require_once 'CRM/Contribute/PseudoConstant.php';
                $contriTypes = CRM_Contribute_PseudoConstant::contributionType();
                foreach ($contriTypes as $val => $type) {
                    if (strtolower($value) == strtolower($type)) {
                        $values['contribution_type_id'] = $val;
                        break;
                    }
                }
                if (!CRM_Utils_Array::value('contribution_type_id', $values)) {
                    return civicrm_create_error("Contribution Type is not valid: {$value}");
                }
                break;
            case 'payment_instrument':
                require_once 'CRM/Core/OptionGroup.php';
                $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value);
                if (!CRM_Utils_Array::value('payment_instrument_id', $values)) {
                    return civicrm_create_error("Payment Instrument is not valid: {$value}");
                }
                break;
            case 'contribution_status_id':
                require_once 'CRM/Core/OptionGroup.php';
                if (!($values['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', $value))) {
                    return civicrm_create_error("Contribution Status is not valid: {$value}");
                }
                break;
            case 'honor_type_id':
                require_once 'CRM/Core/OptionGroup.php';
                $values['honor_type_id'] = CRM_Core_OptionGroup::getValue('honor_type', $value);
                if (!CRM_Utils_Array::value('honor_type_id', $values)) {
                    return civicrm_create_error("Honor Type is not valid: {$value}");
                }
                break;
            case 'soft_credit':
                //import contribution record according to select contact type
                // validate contact id and external identifier.
                $contactId = CRM_Utils_Array::value('contact_id', $params['soft_credit']);
                $externalId = CRM_Utils_Array::value('external_identifier', $params['soft_credit']);
                if ($contactId || $externalId) {
                    require_once 'CRM/Contact/DAO/Contact.php';
                    $contact = new CRM_Contact_DAO_Contact();
                    $contact->id = $contactId;
                    $contact->external_identifier = $externalId;
                    $errorMsg = NULL;
                    if (!$contact->find(TRUE)) {
                        $errorMsg = ts("No match found for specified Soft Credit contact data. Row was skipped.");
                    } elseif ($params['contact_type'] != $contact->contact_type) {
                        $errorMsg = ts("Soft Credit Contact Type is wrong: %1", array(1 => $contact->contact_type));
                    }
                    if ($errorMsg) {
                        return civicrm_create_error($errorMsg, 'soft_credit');
                    }
                    // finally get soft credit contact id.
                    $values['soft_credit_to'] = $contact->id;
                } else {
                    // get the contact id from dupicate contact rule, if more than one contact is returned
                    // we should return error, since current interface allows only one-one mapping
                    $softParams = $params['soft_credit'];
                    $softParams['contact_type'] = $params['contact_type'];
                    $error = _civicrm_duplicate_formatted_contact($softParams);
                    if (isset($error['error_message']['params'][0])) {
                        $matchedIDs = explode(',', $error['error_message']['params'][0]);
                        // check if only one contact is found
                        if (count($matchedIDs) > 1) {
                            return civicrm_create_error($error['error_message']['message'], 'soft_credit');
                        } else {
                            $values['soft_credit_to'] = $matchedIDs[0];
                        }
                    } else {
                        return civicrm_create_error('No match found for specified Soft Credit contact data. Row was skipped.', 'soft_credit');
                    }
                }
                break;
            case 'pledge_payment':
            case 'pledge_id':
                //giving respect to pledge_payment flag.
                if (!CRM_Utils_Array::value('pledge_payment', $params)) {
                    continue;
                }
                //get total amount of from import fields
                $totalAmount = CRM_Utils_Array::value('total_amount', $params);
                $onDuplicate = CRM_Utils_Array::value('onDuplicate', $params);
                //we need to get contact id $contributionContactID to
                //retrieve pledge details as well as to validate pledge ID
                //first need to check for update mode
                if ($onDuplicate == CRM_Contribute_Import_Parser::DUPLICATE_UPDATE && ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id'])) {
                    $contribution = new CRM_Contribute_DAO_Contribution();
                    if ($params['contribution_id']) {
                        $contribution->id = $params['contribution_id'];
                    } elseif ($params['trxn_id']) {
                        $contribution->trxn_id = $params['trxn_id'];
                    } elseif ($params['invoice_id']) {
                        $contribution->invoice_id = $params['invoice_id'];
                    }
                    if ($contribution->find(TRUE)) {
                        $contributionContactID = $contribution->contact_id;
                        if (!$totalAmount) {
                            $totalAmount = $contribution->total_amount;
                        }
                    } else {
                        return civicrm_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                    }
                } else {
                    // first get the contact id for given contribution record.
                    if (CRM_Utils_Array::value('contribution_contact_id', $params)) {
                        $contributionContactID = $params['contribution_contact_id'];
                    } elseif (CRM_Utils_Array::value('external_identifier', $params)) {
                        require_once 'CRM/Contact/DAO/Contact.php';
                        $contact = new CRM_Contact_DAO_Contact();
                        $contact->external_identifier = $params['external_identifier'];
                        if ($contact->find(TRUE)) {
                            $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id;
                        } else {
                            return civicrm_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                        }
                    } else {
                        // we  need to get contribution contact using de dupe
                        $error = civicrm_check_contact_dedupe($params);
                        if (isset($error['error_message']['params'][0])) {
                            $matchedIDs = explode(',', $error['error_message']['params'][0]);
                            // check if only one contact is found
                            if (count($matchedIDs) > 1) {
                                return civicrm_create_error($error['error_message']['message'], 'pledge_payment');
                            } else {
                                $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
                            }
                        } else {
                            return civicrm_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
                        }
                    }
                }
                if (CRM_Utils_Array::value('pledge_id', $params)) {
                    if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
                        return civicrm_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment');
                    }
                    $values['pledge_id'] = $params['pledge_id'];
                } else {
                    //check if there are any pledge related to this contact, with payments pending or in progress
                    require_once 'CRM/Pledge/BAO/Pledge.php';
                    $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID);
                    if (empty($pledgeDetails)) {
                        return civicrm_create_error('No open pledges found for this contact. Contribution row was skipped.', 'pledge_payment');
                    } elseif (count($pledgeDetails) > 1) {
                        return civicrm_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.', 'pledge_payment');
                    }
                    // this mean we have only one pending / in progress pledge
                    $values['pledge_id'] = $pledgeDetails[0];
                }
                //we need to check if oldest payment amount equal to contribution amount
                require_once 'CRM/Pledge/BAO/PledgePayment.php';
                $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
                if ($pledgePaymentDetails['amount'] == $totalAmount) {
                    $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
                } else {
                    return civicrm_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', 'pledge_payment');
                }
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    if ($create) {
        // CRM_Contribute_BAO_Contribution::add() handles contribution_source
        // So, if $values contains contribution_source, convert it to source
        $changes = array('contribution_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return NULL;
}