예제 #1
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);
 }
예제 #2
0
파일: utils.php 프로젝트: ksecor/civicrm
/**
 * 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
        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;
                            }
                        }
                    }
                }
            } else {
                if ($type == 'Select' || $type == 'Radio') {
                    $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                    foreach ($customOption as $customValue => $customLabel) {
                        if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            $values[$key] = $customValue;
                        }
                    }
                }
            }
        }
        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'];
                    } else {
                        if ($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}");
                        }
                    }
                } else {
                    if ($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'];
                        } else {
                            if ($params['trxn_id']) {
                                $contactId->trxn_id = $params['trxn_id'];
                            } else {
                                if ($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';
                $values['contribution_type_id'] = CRM_Utils_Array::key(ucfirst($value), CRM_Contribute_PseudoConstant::contributionType());
                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.");
                    } else {
                        if ($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'];
                    } else {
                        if ($params['trxn_id']) {
                            $contribution->trxn_id = $params['trxn_id'];
                        } else {
                            if ($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'];
                    } else {
                        if (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');
                    } else {
                        if (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/Payment.php';
                $pledgePaymentDetails = CRM_Pledge_BAO_Payment::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;
}
예제 #3
0
 static function adjustPledgePayment($pledgeID, $actualAmount, $pledgeScheduledAmount, $paymentContributionId = null, $pPaymentId = null)
 {
     $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(null, 'name');
     $oldestPayment = self::getOldestPledgePayment($pledgeID);
     if (!$paymentContributionId) {
         // means we are editing payment scheduled payment, so get the second pending to update.
         $oldestPayment = self::getOldestPledgePayment($pledgeID, 2);
         if ($oldestPayment['count'] != 1 && $oldestPayment['id'] == $pPaymentId) {
             $oldestPayment = CRM_Pledge_BAO_Payment::getOldestPledgePayment($pledgeID);
         }
     }
     if ($oldestPayment) {
         // not the last scheduled payment and the actual amount is less than the expected , add it to oldest pending.
         if ($actualAmount != $pledgeScheduledAmount && ($actualAmount < $pledgeScheduledAmount || $actualAmount - $pledgeScheduledAmount < $oldestPayment['amount'])) {
             $oldScheduledAmount = $oldestPayment['amount'];
             $newScheduledAmount = $oldScheduledAmount + ($pledgeScheduledAmount - $actualAmount);
             //store new amount in oldest pending payment record.
             CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_Payment', $oldestPayment['id'], 'scheduled_amount', $newScheduledAmount);
         } elseif ($actualAmount > $pledgeScheduledAmount && $actualAmount - $pledgeScheduledAmount >= $oldestPayment['amount']) {
             // here the actual amount is greater than expected and also greater than the next installment amount, so update the next installment as complete and again add it to next subsequent pending payment
             // set the actual amount of the next pending to '0', set contribution Id to current contribution Id and status as completed
             $paymentId = array($oldestPayment['id']);
             self::updatePledgePayments($pledgeID, array_search('Completed', $allStatus), $paymentId, 0, $paymentContributionId);
             CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_Payment', $oldestPayment['id'], 'scheduled_amount', 0, 'id');
             $oldestPayment = self::getOldestPledgePayment($pledgeID);
             if (!$paymentContributionId) {
                 // means we are editing payment scheduled payment.
                 $oldestPaymentAmount = self::getOldestPledgePayment($pledgeID, 2);
             }
             $newActualAmount = $actualAmount - $pledgeScheduledAmount;
             $newPledgeScheduledAmount = $oldestPayment['amount'];
             if (!$paymentContributionId) {
                 $newActualAmount = $actualAmount - $pledgeScheduledAmount;
                 $newPledgeScheduledAmount = $oldestPaymentAmount['amount'];
                 // means we are editing payment scheduled payment, so update scheduled amount.
                 CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_Payment', $oldestPaymentAmount['id'], 'scheduled_amount', $newActualAmount);
             }
             if ($newActualAmount > 0) {
                 self::adjustPledgePayment($pledgeID, $newActualAmount, $newPledgeScheduledAmount, $paymentContributionId);
             }
         }
     }
 }