/**
  * (Queue Task Callback)
  *
  * Find any contribution records and create corresponding line-item
  * records.
  *
  * @param CRM_Queue_TaskContext $ctx
  * @param int $startId
  *   the first/lowest contribution ID to convert.
  * @param int $endId
  *   the last/highest contribution ID to convert.
  *
  * @return bool
  */
 public static function task_4_2_alpha1_convertContributions(CRM_Queue_TaskContext $ctx, $startId, $endId)
 {
     $upgrade = new CRM_Upgrade_Form();
     $query = "\n INSERT INTO civicrm_line_item(`entity_table` ,`entity_id` ,`price_field_id` ,`label` , `qty` ,`unit_price` ,`line_total` ,`participant_count` ,`price_field_value_id`)\n SELECT 'civicrm_contribution',cc.id, cpf.id as price_field_id, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value\n FROM civicrm_membership_payment cmp\n LEFT JOIN `civicrm_contribution` cc ON cc.id = cmp.contribution_id\n LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id and cli.entity_table = 'civicrm_contribution'\n LEFT JOIN civicrm_membership cm ON cm.id=cmp.membership_id\n LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id\n LEFT JOIN civicrm_price_field cpf ON BINARY cpf.name = cmt.member_of_contact_id\n LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id AND cpf.id = cpfv.price_field_id\n WHERE (cc.id BETWEEN %1 AND %2) AND cli.entity_id IS NULL ;\n ";
     $sqlParams = array(1 => array($startId, 'Integer'), 2 => array($endId, 'Integer'));
     CRM_Core_DAO::executeQuery($query, $sqlParams);
     // create lineitems for contribution done for membership
     $sql = "\nSELECT    cc.id, cmp.membership_id, cpse.price_set_id, cc.total_amount\nFROM      civicrm_contribution cc\nLEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id AND cli.entity_table = 'civicrm_contribution'\nLEFT JOIN civicrm_membership_payment cmp ON cc.id = cmp.contribution_id\nLEFT JOIN civicrm_participant_payment cpp ON cc.id = cpp.contribution_id\nLEFT JOIN civicrm_price_set_entity cpse on cpse.entity_table = 'civicrm_contribution_page' AND cpse.entity_id = cc.contribution_page_id\nWHERE     (cc.id BETWEEN %1 AND %2)\nAND       cli.entity_id IS NULL AND cc.contribution_page_id IS NOT NULL AND cpp.contribution_id IS NULL\nGROUP BY  cc.id\n";
     $result = CRM_Core_DAO::executeQuery($sql, $sqlParams);
     while ($result->fetch()) {
         $sql = "\nSELECT    cpf.id, cpfv.id as price_field_value_id, cpfv.label, cpfv.amount, cpfv.count\nFROM      civicrm_price_field cpf\nLEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id\nWHERE     cpf.price_set_id = %1\n";
         $lineParams = array('entity_table' => 'civicrm_contribution', 'entity_id' => $result->id);
         if ($result->membership_id) {
             $sql .= " AND cpf.name = %2 AND cpfv.membership_type_id = %3 ";
             $params = array('1' => array($result->price_set_id, 'Integer'), '2' => array('membership_amount', 'String'), '3' => array(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $result->membership_id, 'membership_type_id'), 'Integer'));
             $res = CRM_Core_DAO::executeQuery($sql, $params);
             if ($res->fetch()) {
                 $lineParams += array('price_field_id' => $res->id, 'label' => $res->label, 'qty' => 1, 'unit_price' => $res->amount, 'line_total' => $res->amount, 'participant_count' => $res->count ? $res->count : 0, 'price_field_value_id' => $res->price_field_value_id);
             } else {
                 $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $result->price_set_id, 'id', 'price_set_id');
                 $lineParams['label'] = 'Membership Amount';
                 $lineParams['qty'] = 1;
                 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
                 $lineParams['participant_count'] = 0;
             }
         } else {
             $sql .= "AND cpfv.amount = %2";
             //CRM-12273
             //check if price_set_id is exist, if not use the default contribution amount
             if (isset($result->price_set_id)) {
                 $priceSetId = $result->price_set_id;
             } else {
                 $defaultPriceSets = CRM_Price_BAO_PriceSet::getDefaultPriceSet();
                 foreach ($defaultPriceSets as $key => $pSet) {
                     if ($pSet['name'] == 'contribution_amount') {
                         $priceSetId = $pSet['setID'];
                     }
                 }
             }
             $params = array('1' => array($priceSetId, 'Integer'), '2' => array($result->total_amount, 'String'));
             $res = CRM_Core_DAO::executeQuery($sql, $params);
             if ($res->fetch()) {
                 $lineParams += array('price_field_id' => $res->id, 'label' => $res->label, 'qty' => 1, 'unit_price' => $res->amount, 'line_total' => $res->amount, 'participant_count' => $res->count ? $res->count : 0, 'price_field_value_id' => $res->price_field_value_id);
             } else {
                 $params = array('price_set_id' => $priceSetId, 'name' => 'other_amount');
                 $defaults = array();
                 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::retrieve($params, $defaults);
                 if (!empty($defaults)) {
                     $lineParams['price_field_id'] = $defaults['id'];
                     $lineParams['label'] = $defaults['label'];
                     $lineParams['price_field_value_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $defaults['id'], 'id', 'price_field_id');
                 } else {
                     $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $priceSetId, 'id', 'price_set_id');
                     $lineParams['label'] = 'Contribution Amount';
                 }
                 $lineParams['qty'] = 1;
                 $lineParams['participant_count'] = 0;
                 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
             }
         }
         CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
     }
     return TRUE;
 }
Beispiel #2
0
 /**
  * This function for building custom fields.
  *
  * @param CRM_Core_Form $qf
  *   Form object (reference).
  * @param string $elementName
  *   Name of the custom field.
  * @param int $fieldId
  * @param bool $inactiveNeeded
  * @param bool $useRequired
  *   True if required else false.
  * @param string $label
  *   Label for custom field.
  * @param null $fieldOptions
  * @param array $freezeOptions
  *
  * @return null
  */
 public static function addQuickFormElement(&$qf, $elementName, $fieldId, $inactiveNeeded, $useRequired = TRUE, $label = NULL, $fieldOptions = NULL, $freezeOptions = array())
 {
     $field = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field();
     $field->id = $fieldId;
     if (!$field->find(TRUE)) {
         /* FIXME: failure! */
         return NULL;
     }
     $otherAmount = $qf->get('values');
     $config = CRM_Core_Config::singleton();
     $qf->assign('currencySymbol', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Currency', $config->defaultCurrency, 'symbol', 'name'));
     // get currency name for price field and option attributes
     $currencyName = $config->defaultCurrency;
     if (!isset($label)) {
         $label = property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $field->name == 'contribution_amount' && empty($otherAmount['is_allow_other_amount']) ? ts('Additional Contribution') : $field->label;
     }
     if ($field->name == 'contribution_amount') {
         $qf->_contributionAmount = 1;
     }
     if (isset($qf->_online) && $qf->_online) {
         $useRequired = FALSE;
     }
     $customOption = $fieldOptions;
     if (!is_array($customOption)) {
         $customOption = CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::getOptions($field->id, $inactiveNeeded);
     }
     //use value field.
     $valueFieldName = 'amount';
     $seperator = '|';
     switch ($field->html_type) {
         case 'Text':
             $optionKey = key($customOption);
             $count = CRM_Utils_Array::value('count', $customOption[$optionKey], '');
             $max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], '');
             $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value));
             $extra = array();
             if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf, '_contributionAmount') && $qf->_contributionAmount) {
                 $qf->assign('priceset', $elementName);
                 $extra = array('onclick' => 'useAmountOther();');
             }
             // if separate membership payment is used with quick config priceset then change the other amount label
             if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $qf->_quickConfig && $field->name == 'other_amount' && !property_exists($qf, '_contributionAmount')) {
                 $label = ts('Additional Contribution');
                 $useRequired = 0;
             } elseif (!empty($fieldOptions[$optionKey]['label'])) {
                 $label = $fieldOptions[$optionKey]['label'];
             }
             if ($field->is_display_amounts) {
                 $label .= ' - ';
                 $label .= CRM_Utils_Money::format(CRM_Utils_Array::value($valueFieldName, $customOption[$optionKey]));
             }
             $element =& $qf->add('text', $elementName, $label, array_merge($extra, array('price' => json_encode(array($optionKey, $priceVal)), 'size' => '4')), $useRequired && $field->is_required);
             // CRM-6902
             if (in_array($optionKey, $freezeOptions)) {
                 $element->freeze();
             }
             //CRM-10117
             if (property_exists($qf, '_quickConfig') && $qf->_quickConfig) {
                 $message = ts("Please enter a valid amount.");
                 $type = "money";
             } else {
                 $message = ts('%1 must be an integer (whole number).', array(1 => $label));
                 $type = "positiveInteger";
             }
             // integers will have numeric rule applied to them.
             $qf->addRule($elementName, $message, $type);
             break;
         case 'Radio':
             $choice = array();
             if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && property_exists($qf, '_contributionAmount') && $qf->_contributionAmount) {
                 $qf->assign('contriPriceset', $elementName);
             }
             foreach ($customOption as $opId => $opt) {
                 if ($field->is_display_amounts) {
                     $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : '';
                     $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
                 }
                 $count = CRM_Utils_Array::value('count', $opt, '');
                 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
                 $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
                 $extra = array('price' => json_encode(array($elementName, $priceVal)), 'data-amount' => $opt[$valueFieldName], 'data-currency' => $currencyName);
                 if (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'contribution_amount') {
                     $extra += array('onclick' => 'clearAmountOther();');
                 } elseif (property_exists($qf, '_quickConfig') && $qf->_quickConfig && $field->name == 'membership_amount') {
                     $extra += array('onclick' => "return showHideAutoRenew({$opt['membership_type_id']});", 'membership-type' => $opt['membership_type_id']);
                     $qf->assign('membershipFieldID', $field->id);
                 }
                 $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra);
                 // CRM-6902
                 if (in_array($opId, $freezeOptions)) {
                     $choice[$opId]->freeze();
                 }
             }
             if (property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock['is_separate_payment']) && $field->name == 'contribution_amount') {
                 $choice[] = $qf->createElement('radio', NULL, '', 'No thank you', '-1', array('onclick' => 'clearAmountOther();'));
             }
             if (!$field->is_required) {
                 // add "none" option
                 if (!empty($otherAmount['is_allow_other_amount']) && $field->name == 'contribution_amount') {
                     $none = ts('Other Amount');
                 } elseif (property_exists($qf, '_membershipBlock') && empty($qf->_membershipBlock['is_required']) && $field->name == 'membership_amount') {
                     $none = ts('No thank you');
                 } else {
                     $none = ts('-none-');
                 }
                 $choice[] = $qf->createElement('radio', NULL, '', $none, '0', array('price' => json_encode(array($elementName, "0"))));
             }
             $element =& $qf->addGroup($choice, $elementName, $label);
             // make contribution field required for quick config when membership block is enabled
             if (($field->name == 'contribution_amount' || $field->name == 'membership_amount') && property_exists($qf, '_membershipBlock') && !empty($qf->_membershipBlock) && !$field->is_required) {
                 $useRequired = $field->is_required = TRUE;
             }
             if ($useRequired && $field->is_required) {
                 $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
             }
             break;
         case 'Select':
             $selectOption = $allowedOptions = $priceVal = array();
             foreach ($customOption as $opt) {
                 $count = CRM_Utils_Array::value('count', $opt, '');
                 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
                 $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
                 if ($field->is_display_amounts) {
                     $opt['label'] .= ' - ';
                     $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
                 }
                 $selectOption[$opt['id']] = $opt['label'];
                 if (!in_array($opt['id'], $freezeOptions)) {
                     $allowedOptions[] = $opt['id'];
                 }
             }
             $element =& $qf->add('select', $elementName, $label, array('' => ts('- select -')) + $selectOption, $useRequired && $field->is_required, array('price' => json_encode($priceVal)));
             // CRM-6902
             $button = substr($qf->controller->getButtonName(), -4);
             if (!empty($freezeOptions) && $button != 'skip') {
                 $qf->addRule($elementName, ts('Sorry, this option is currently sold out.'), 'regex', "/" . implode('|', $allowedOptions) . "/");
             }
             break;
         case 'CheckBox':
             $check = array();
             foreach ($customOption as $opId => $opt) {
                 $count = CRM_Utils_Array::value('count', $opt, '');
                 $max_value = CRM_Utils_Array::value('max_value', $opt, '');
                 $priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
                 if ($field->is_display_amounts) {
                     $opt['label'] .= ' - ';
                     $opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
                 }
                 $check[$opId] =& $qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], array('price' => json_encode(array($opt['id'], $priceVal)), 'data-amount' => $opt[$valueFieldName], 'data-currency' => $currencyName));
                 // CRM-6902
                 if (in_array($opId, $freezeOptions)) {
                     $check[$opId]->freeze();
                 }
             }
             $element =& $qf->addGroup($check, $elementName, $label);
             if ($useRequired && $field->is_required) {
                 $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
             }
             break;
     }
     if (isset($qf->_online) && $qf->_online) {
         $element->freeze();
     }
 }
Beispiel #3
0
 /**
  * Build the price set form.
  *
  * @param CRM_Core_Form $form
  *
  * @return void
  */
 public static function buildPriceSet(&$form)
 {
     $priceSetId = $form->get('priceSetId');
     $userid = $form->getVar('_userID');
     if (!$priceSetId) {
         return;
     }
     $validFieldsOnly = TRUE;
     $className = CRM_Utils_System::getClassName($form);
     if (in_array($className, array('CRM_Contribute_Form_Contribution', 'CRM_Member_Form_Membership'))) {
         $validFieldsOnly = FALSE;
     }
     $priceSet = self::getSetDetail($priceSetId, TRUE, $validFieldsOnly);
     $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
     $form->_quickConfig = $quickConfig = 0;
     if (CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Set', $priceSetId, 'is_quick_config')) {
         $quickConfig = 1;
     }
     $form->assign('quickConfig', $quickConfig);
     if ($className == "CRM_Contribute_Form_Contribution_Main") {
         $form->_quickConfig = $quickConfig;
     }
     $form->assign('priceSet', $form->_priceSet);
     $component = 'contribution';
     if ($className == 'CRM_Member_Form_Membership') {
         $component = 'membership';
     }
     if ($className == 'CRM_Contribute_Form_Contribution_Main') {
         $feeBlock =& $form->_values['fee'];
         if (!empty($form->_useForMember)) {
             $component = 'membership';
         }
     } else {
         $feeBlock =& $form->_priceSet['fields'];
     }
     // call the hook.
     CRM_Utils_Hook::buildAmount($component, $form, $feeBlock);
     foreach ($feeBlock as $field) {
         if (CRM_Utils_Array::value('visibility', $field) == 'public' || !$validFieldsOnly) {
             $options = CRM_Utils_Array::value('options', $field);
             if ($className == 'CRM_Contribute_Form_Contribution_Main' && ($component = 'membership')) {
                 $checklifetime = self::checkCurrentMembership($options, $userid);
                 if ($checklifetime) {
                     $form->assign('ispricelifetime', TRUE);
                 }
             }
             if (!is_array($options)) {
                 continue;
             }
             CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::addQuickFormElement($form, 'price_' . $field['id'], $field['id'], FALSE, CRM_Utils_Array::value('is_required', $field, FALSE), NULL, $options);
         }
     }
 }