Exemplo n.º 1
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.º 2
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.º 3
0
 /**
  * @param $data
  * @param $row
  * @param $type
  */
 public function _fillData($data, $row, $type)
 {
     if (!empty($data['contact'])) {
         $this->select2("s2id_primary_contact_id_{$row}", $data['contact']['email']);
     } else {
         $email = $data['first_name'] . '@example.com';
         $this->webtestNewDialogContact($data['first_name'], $data['last_name'], $email, 4, "s2id_primary_contact_id_{$row}", $row, 'primary');
     }
     if ($type == "Pledge Payment") {
         $this->select("field_{$row}_financial_type", $data['financial_type']);
         $this->type("field_{$row}_total_amount", $data['amount']);
         $this->webtestFillDateTime("field_{$row}_receive_date", "+1 week");
         $this->type("field_{$row}_contribution_source", substr(sha1(rand()), 0, 10));
         $this->select("field_{$row}_payment_instrument", "Check");
         $this->type("field_{$row}_check_number", rand());
         $this->click("field[{$row}][send_receipt]");
         $this->click("field_{$row}_invoice_id");
         $this->type("field_{$row}_invoice_id", substr(sha1(rand()), 0, 10));
         $softcreditemail = $data['soft_credit_first_name'] . '@example.com';
         $this->webtestNewDialogContact($data['soft_credit_first_name'], $data['soft_credit_last_name'], $softcreditemail, 4, "s2id_soft_credit_contact_id_{$row}", $row, 'soft_credit');
         $this->type("soft_credit_amount_{$row}", $data['soft_credit_amount']);
         $this->select("soft_credit_type_{$row}", $data['soft_credit_type']);
         if (!empty($data['contact'])) {
             $pledgeID = CRM_Pledge_BAO_Pledge::getContactPledges($data['contact']['id']);
             $this->select("open_pledges_{$row}", "value={$pledgeID[0]}");
             $this->click("css=span#{$row}.pledge-adjust-option a");
             sleep(5);
             $this->select("option_type_{$row}", "value=1");
             $this->click("css=span#{$row}.pledge-adjust-option a");
             $this->type("field_{$row}_total_amount", $data['amount']);
         }
     } elseif ($type == "Contribution") {
         $this->select("field_{$row}_financial_type", $data['financial_type']);
         $this->type("field_{$row}_total_amount", $data['amount']);
         $this->webtestFillDateTime("field_{$row}_receive_date", "+1 week");
         $this->type("field_{$row}_contribution_source", substr(sha1(rand()), 0, 10));
         $this->select("field_{$row}_payment_instrument", "Check");
         $this->type("field_{$row}_check_number", rand());
         $this->click("field[{$row}][send_receipt]");
         $this->click("field_{$row}_invoice_id");
         $this->type("field_{$row}_invoice_id", substr(sha1(rand()), 0, 10));
         $softcreditemail = $data['soft_credit_first_name'] . '@example.com';
         $this->webtestNewDialogContact($data['soft_credit_first_name'], $data['soft_credit_last_name'], $softcreditemail, 4, "s2id_soft_credit_contact_id_{$row}", $row, 'soft_credit');
         $this->type("soft_credit_amount_{$row}", $data['soft_credit_amount']);
         $this->select("soft_credit_type_{$row}", $data['soft_credit_type']);
     } elseif ($type == "Membership") {
         $this->select("field[{$row}][membership_type][0]", "value=1");
         $this->select("field[{$row}][membership_type][1]", $data['membership_type']);
         $this->webtestFillDate("field_{$row}_join_date", "now");
         $this->webtestFillDate("field_{$row}_membership_start_date", "now");
         $this->webtestFillDate("field_{$row}_membership_end_date", "+1 month");
         $this->type("field_{$row}_membership_source", substr(sha1(rand()), 0, 10));
         $this->click("field[{$row}][send_receipt]");
         $this->select("field_{$row}_financial_type", $data['financial_type']);
         $this->webtestFillDateTime("field_{$row}_receive_date", "+1 week");
         $this->select("field_{$row}_payment_instrument", "Check");
         $this->type("field_{$row}_check_number", rand());
         $this->select("field_{$row}_contribution_status_id", "Completed");
         $softcreditemail = $data['soft_credit_first_name'] . '@example.com';
         $this->webtestNewDialogContact($data['soft_credit_first_name'], $data['soft_credit_last_name'], $softcreditemail, 4, "s2id_soft_credit_contact_id_{$row}", $row, 'soft_credit');
         $this->type("soft_credit_amount_{$row}", $data['soft_credit_amount']);
         $this->select("soft_credit_type_{$row}", $data['soft_credit_type']);
     }
 }
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
 /** 
  * 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);
     //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);
     //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) {
             require_once 'CRM/Core/BAO/PaymentProcessor.php';
             require_once 'CRM/Core/Payment.php';
             $paymentProcessor =& CRM_Core_BAO_PaymentProcessor::getPayment($ppID, $this->_mode);
             if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
                 continue;
             } else {
                 if ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode == 'live') {
                     continue;
                 } else {
                     $paymentObject =& CRM_Core_Payment::singleton($this->_mode, 'Contribute', $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;
         }
     }
     // this required to show billing block
     $this->assign_by_ref('paymentProcessor', $paymentProcessor);
     $this->assign('hidePayPalExpress', true);
     if ($this->_contactID) {
         require_once 'CRM/Contact/BAO/Contact/Location.php';
         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();
     require_once 'CRM/Core/Payment.php';
     require_once 'CRM/Core/Payment/Form.php';
     // payment fields are depending on payment type
     if ($this->_processors['payment_type'] & 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);
             require_once "CRM/Pledge/BAO/Payment.php";
             CRM_Pledge_BAO_Payment::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();
             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.
             require_once 'CRM/Pledge/BAO/Pledge.php';
             $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 check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206
             if (isset($this->_contactID)) {
                 require_once "CRM/Pledge/BAO/Pledge.php";
                 $contactPledges = array();
                 $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID);
                 if (!empty($contactPledges)) {
                     $payments = null;
                     $paymentsDue = null;
                     $multipleDue = false;
                     require_once "CRM/Pledge/BAO/Payment.php";
                     foreach ($contactPledges as $key => $pledgeId) {
                         $payments = CRM_Pledge_BAO_Payment::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)));
                     } else {
                         if ($paymentsDue) {
                             // Show user link to oldest Pending or Overdue pledge payment
                             require_once 'CRM/Utils/Date.php';
                             require_once 'CRM/Utils/Money.php';
                             $ppAmountDue = CRM_Utils_Money::format($payments['amount']);
                             $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Payment', $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 apply this contribution as a pledge payment<a/>.', array(1 => $ppUrl, 2 => $ppAmountDue, 3 => $ppSchedDate)));
                         }
                     }
                 }
             }
         }
     }
     $this->_values = array();
     // current contribution id
     if ($this->_id) {
         $this->_online = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_FinancialTrxn', $this->_id, 'id', 'contribution_id');
         if ($this->_online) {
             $this->assign('isOnline', true);
         }
         //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);
         require_once "CRM/Contribute/BAO/Contribution.php";
         CRM_Contribute_BAO_Contribution::getValues($params, $this->_values, $ids);
         //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
         require_once 'CRM/Core/BAO/Note.php';
         $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'] = $softCredit['sort_name'];
         $this->_values['softID'] = $softCredit['soft_credit_id'];
         $this->_values['soft_contact_id'] = $softCredit['soft_credit_to'];
         $this->_values['pcp_made_through_id'] = $softCredit['pcp_id'];
         $this->_values['pcp_display_in_roll'] = $softCredit['pcp_display_in_roll'];
         $this->_values['pcp_roll_nickname'] = $softCredit['pcp_roll_nickname'];
         $this->_values['pcp_personal_note'] = $softCredit['pcp_personal_note'];
         //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', $this->_contributionType);
         $this->set('entityId', $this->_id);
         CRM_Custom_Form_Customdata::preProcess($this);
         CRM_Custom_Form_Customdata::buildQuickForm($this);
         CRM_Custom_Form_Customdata::setDefaultValues($this);
     }
     require_once 'CRM/Price/BAO/Set.php';
     $this->_lineItems = array();
     if ($this->_id && ($priceSetId = CRM_Price_BAO_Set::getFor('civicrm_contribution', $this->_id))) {
         $this->_priceSetId = $priceSetId;
         require_once 'CRM/Price/BAO/LineItem.php';
         $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution');
     }
     $this->assign('lineItem', empty($this->_lineItems) ? false : $this->_lineItems);
 }
/**
 * 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;
}