/** * Build and run the query to select all contributions * matching the criteria, and try to create a snapshot * * @return snapshot creation result/error */ public static function createSnapshot($values) { // prepare timestamps $raw_from_ts = $values['donrec_contribution_horizon_from']; $raw_to_ts = $values['donrec_contribution_horizon_to']; $date_from = CRM_Utils_DonrecHelper::convertDate($raw_from_ts, -1); $date_to = CRM_Utils_DonrecHelper::convertDate($raw_to_ts, 1); $formatted_date_from = date('Y-m-d H:i:s', $date_from); $formatted_date_to = date('Y-m-d H:i:s', $date_to); $query_date_limit = ""; if ($date_from) { $query_date_limit .= "AND `receive_date` >= '{$formatted_date_from}'"; } if ($date_to) { $query_date_limit .= " AND `receive_date` <= '{$formatted_date_to}'"; } // get table- and column name $table_query = "SELECT `cg`.`table_name`,\n `cf`.`column_name`\n FROM `civicrm_custom_group` AS cg,\n `civicrm_custom_field` AS cf\n WHERE `cg`.`name` = 'zwb_donation_receipt_item'\n AND `cf`.`custom_group_id` = `cg`.`id`\n AND `cf`.`name` = 'status'"; $results = CRM_Core_DAO::executeQuery($table_query); $custom_group_table = NULL; $status_column = NULL; while ($results->fetch()) { $custom_group_table = $results->table_name; $status_column = $results->column_name; } if ($custom_group_table == NULL || $status_column == NULL) { // something went wrong here CRM_Core_Error::debug_log_message("de.systopia.donrec: error: custom_group_table or status_column is empty!"); return array(); } // calculate main selector clause if (!empty($values['contact_id'])) { $contact_id = (int) $values['contact_id']; $main_selector = "`contact_id` = {$contact_id}"; } elseif (!empty($values['contact_ids'])) { $contact_ids = implode(',', $values['contact_ids']); $main_selector = "`contact_id` IN ({$contact_ids})"; } elseif (!empty($values['contribution_ids'])) { $contribution_ids = implode(',', $values['contribution_ids']); $main_selector = "`civicrm_contribution`.`id` IN ({$contribution_ids})"; } else { CRM_Core_Error::debug_log_message("de.systopia.donrec: error: no selector data found in params!"); $main_selector = "FALSE"; } // get financial type selector clause $profile = new CRM_Donrec_Logic_Profile($values['profile']); $financialTypeClause = $profile->getContributionTypesClause(); // run the main query $query = "SELECT `civicrm_contribution`.`id`\n FROM (`civicrm_contribution`)\n LEFT JOIN `{$custom_group_table}` AS existing_receipt\n ON `civicrm_contribution`.`id` = existing_receipt.`entity_id`\n AND existing_receipt.`{$status_column}` = 'ORIGINAL'\n WHERE\n ({$main_selector})\n {$query_date_limit}\n AND {$financialTypeClause}\n AND (`non_deductible_amount` = 0 OR `non_deductible_amount` IS NULL)\n AND `contribution_status_id` = 1\n AND `is_test` = 0\n AND `currency` = 'EUR'\n AND existing_receipt.`entity_id` IS NULL;"; // execute the query $result = CRM_Core_DAO::executeQuery($query); // build array $contributionIds = array(); while ($result->fetch()) { $contributionIds[] = $result->id; } // finally, build the snapshot with it return CRM_Donrec_Logic_Snapshot::create($contributionIds, CRM_Donrec_Logic_Settings::getLoggedInContactID(), $formatted_date_from, $formatted_date_to, $values['profile']); }
function buildQuickForm() { CRM_Utils_System::setTitle(ts('Issue Donation Receipts', array('domain' => 'de.systopia.donrec'))); $this->addElement('hidden', 'rsid'); $options = array('current_year' => ts('This Year', array('domain' => 'de.systopia.donrec')), 'last_year' => ts('last year', array('domain' => 'de.systopia.donrec')), 'customized_period' => ts('Choose Date Range', array('domain' => 'de.systopia.donrec'))); $this->addElement('select', 'time_period', 'Time Period:', $options, array('class' => 'crm-select2')); $this->addDateRange('donrec_contribution_horizon', '_from', '_to', ts('From:', array('domain' => 'de.systopia.donrec')), 'searchDate', TRUE, FALSE); // add profile selector $this->addElement('select', 'profile', ts('Profile', array('domain' => 'de.systopia.donrec')), CRM_Donrec_Logic_Profile::getAllNames(), array('class' => 'crm-select2')); // call the (overwritten) Form's method, so the continue button is on the right... CRM_Core_Form::addDefaultButtons(ts('Continue', array('domain' => 'de.systopia.donrec'))); }
/** * Get the profile connected to this snapshot */ public function getProfile() { if ($this->_profile == NULL) { $profile_name = CRM_Core_DAO::singleValueQuery("SELECT profile FROM donrec_snapshot WHERE snapshot_id = %1 LIMIT 1;", array(1 => array($this->Id, 'Integer'))); $this->_profile = CRM_Donrec_Logic_Profile::getProfile($profile_name, TRUE); } return $this->_profile; }
/** * get the profile object that was used to create this receipt */ public function getProfile() { CRM_Donrec_Logic_ReceiptItem::getCustomFields(); $receipt_table_name = CRM_Donrec_DataStructure::getTableName('zwb_donation_receipt'); $profile_column_name = CRM_Donrec_DataStructure::getCustomFields('zwb_donation_receipt')['profile']; $profile = CRM_Core_DAO::singleValueQuery("SELECT `{$profile_column_name}` FROM `{$receipt_table_name}` WHERE `id` = %1", array(1 => array($this->Id, 'Integer'))); return CRM_Donrec_Logic_Profile::getProfile($profile, TRUE); }
/** * Upgrade to 1.4: * - update to new, prefixed, settings names * - (on 4.6) if no profile exists, create default (from legacy indivudual values) * - (on 4.6) migrate all profiles into a "settings bag" * * REMARK: using the CRM_Core_BAO_Setting::getItem in order to evaluate the group_name * * @return TRUE on success * @throws Exception */ public function upgrade_0140() { $OLD_SETTINGS_GROUP = 'Donation Receipt Profiles'; // STEP 1: Migrate old general settings to prefixed ones $settings_migration = array('default_profile' => 'donrec_default_profile', 'packet_size' => 'donrec_packet_size', 'pdfinfo_path' => 'donrec_pdfinfo_path'); $migrated_values = array(); foreach ($settings_migration as $old_key => $new_key) { $new_value = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $new_key); if ($new_value === NULL) { $old_value = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $old_key); if ($old_value !== NULL) { $migrated_values[$new_key] = $old_value; } } } if (!empty($migrated_values)) { civicrm_api3('Setting', 'create', $migrated_values); } // Migrate profiles // (only works on 4.6. With 4.7 the group_name was dropped, and we cannot find the profiles any more) $existing_profiles = civicrm_api3('Setting', 'getvalue', array('name' => 'donrec_profiles')); if (empty($existing_profiles) && version_compare(CRM_Utils_System::version(), '4.6', '<=')) { // FIXME: is there a better way than a SQL query? $profiles = array(); $query = CRM_Core_DAO::executeQuery("SELECT name FROM civicrm_setting WHERE group_name = '{$OLD_SETTINGS_GROUP}'"); while ($query->fetch()) { $profile_data = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $query->name); if (is_array($profile_data)) { $profiles[$query->name] = $profile_data; } else { $this->ctx->log->warn('Profile "{$query->name}" seems to be broken and is lost.'); } } // if there is no default profile, create one and copy legacy (pre 1.3) values if (empty($profiles['Default'])) { $default_profile = new CRM_Donrec_Logic_Profile('Default'); $profile_data = $default_profile->getData(); foreach (array_keys($profile_data) as $field_name) { $legacy_value = CRM_Core_BAO_Setting::getItem(CRM_Donrec_Logic_Settings::$SETTINGS_GROUP, $field_name); if ($legacy_value !== NULL) { $profile_data[$field_name] = $legacy_value; } } $legacy_contribution_types = CRM_Core_BAO_Setting::getItem(CRM_Donrec_Logic_Settings::$SETTINGS_GROUP, 'contribution_types'); if ($legacy_contribution_types !== NULL && $legacy_contribution_types != 'all') { $profile_data['financial_types'] = explode(',', $legacy_contribution_types); } $profiles['Default'] = $profile_data; $this->ctx->log->warn('Created default profile.'); } CRM_Donrec_Logic_Profile::setAllData($profiles); $profiles_migrated = count($profiles); $this->ctx->log->info('Migrated {$profiles_migrated} profiles.'); } return TRUE; }
function postProcess() { // process all form values and save valid settings $values = $this->exportValues(); // save generic settings CRM_Donrec_Logic_Settings::set('donrec_packet_size', $values['packet_size']); if ($values['pdfinfo_path']) { CRM_Donrec_Logic_Settings::set('donrec_pdfinfo_path', $values['pdfinfo_path']); } // first, update current values into slected profile if (!empty($values['selected_profile'])) { $profile = $values['selected_profile']; $profile_data = json_decode($values['profile_data'], 1); $profile_defaults = CRM_Donrec_Logic_Profile::defaultProfileData(); foreach (array_keys($profile_defaults) as $field_name) { $value = CRM_Utils_Array::value($field_name, $values, NULL); if ($value != NULL) { $profile_data[$profile][$field_name] = $value; } } // verify some stuff foreach ($profile_data as $profile_name => $profile) { // test the ID pattern try { $generator = new CRM_Donrec_Logic_IDGenerator($profile['id_pattern'], false); } catch (Exception $e) { $session = CRM_Core_Session::singleton(); $session->setStatus(ts("One of the Receipt ID patterns are invalid! Changes NOT saved!", array('domain' => 'de.systopia.donrec')), ts('Error', array('domain' => 'de.systopia.donrec')), 'error'); return; } } // then store the profiles CRM_Donrec_Logic_Profile::setAllData($profile_data); } $session = CRM_Core_Session::singleton(); $session->setStatus(ts("Settings successfully saved", array('domain' => 'de.systopia.donrec')), ts('Settings', array('domain' => 'de.systopia.donrec')), 'success'); $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/setting/donrec')); }
public static function addDynamicTokens(&$values) { if (!empty($values['issued_by'])) { // add created_by_display_name try { $creator = civicrm_api3('Contact', 'getsingle', array('id' => $values['issued_by'])); $values['issued_by_display_name'] = $creator['display_name']; } catch (Exception $e) { CRM_Core_Error::debug_log_message('de.systopia.donrec - ' . print_r($e, 1)); } } // add the legacy 'today' token if (!empty($values['issued_on'])) { $values['today'] = $values['issued_on']; } // add the monetary tokens: 'total', 'totaltext', 'totalmoney' if (isset($values['total_amount'])) { // format total_amount $values['total_amount'] = number_format((double) $values['total_amount'], 2, '.', ''); $values['total'] = $values['total_amount']; $values['totaltext'] = CRM_Utils_DonrecHelper::convert_number_to_words($values['total_amount']); $values['totalmoney'] = CRM_Utils_Money::format($values['total_amount'], ''); } // add financial type name $financialTypes = CRM_Contribute_PseudoConstant::financialType(); if (is_array($values['lines'])) { foreach ($values['lines'] as $key => $line) { if (!empty($line['financial_type_id'])) { $values['lines'][$key]['financial_type'] = $financialTypes[$line['financial_type_id']]; } } } // sort contribution lines by receive date (#1497) $receive_dates = array(); $sorted_lines = $values['lines']; foreach ($sorted_lines as $key => $line) { $sorted_lines[$key]['id'] = $key; $receive_dates[$key] = $line['receive_date']; } array_multisort($receive_dates, SORT_ASC, $sorted_lines); $values['lines'] = array(); foreach ($sorted_lines as $key => $line) { $values['lines'][$line['id']] = $line; } // add legacy 'items' if (count($values['lines']) > 1) { $values['items'] = $values['lines']; } // add organisation address if (empty($values['organisation'])) { $domain = CRM_Core_BAO_Domain::getDomain(); $values['organisation'] = self::lookupAddressTokens($domain->contact_id, 0, 0); } // ADD watermarks $profile = CRM_Donrec_Logic_Profile::getProfile($values['profile']); if ($values['status'] == 'ORIGINAL') { // nothing to to in this case.. } elseif ($values['status'] == 'COPY') { $values['watermark'] = $profile->get('copy_text'); } else { // in all other cases, it's INVALID/DRAFT: $values['watermark'] = $profile->get('draft_text'); } // copy contributor values to addressee, if not set separately if (!isset($values['addressee']['display_name'])) { $values['addressee']['display_name'] = $values['contributor']['display_name']; } if (!isset($values['addressee']['addressee_display'])) { $values['addressee']['addressee_display'] = $values['contributor']['addressee_display']; } // add URL to view original file, if it exists if (!empty($values['original_file'])) { $values['view_url'] = CRM_Donrec_Logic_File::getPermanentURL($values['original_file'], $values['contributor']['id']); } // TODO: call Token hooks? Currently done by PDF generator }