function generatexml($id) { $xml = ""; $template = CRM_Core_Smarty::singleton(); $this->get((int) $id); $template->assign("file", $this->toArray()); $txgroup = new CRM_Sepa_BAO_SEPATransactionGroup(); $txgroup->sdd_file_id = $this->id; $txgroup->find(); $total = 0; $nbtransactions = 0; $fileFormats = array(); while ($txgroup->fetch()) { $xml .= $txgroup->generateXML(); $total += $txgroup->total; $nbtransactions += $txgroup->nbtransactions; $fileFormats[] = $txgroup->fileFormat; } if (count(array_unique($fileFormats)) > 1) { throw new Exception('Creditors with mismatching File Formats cannot be mixed in same File'); } else { $fileFormatName = CRM_Utils_SepaOptionGroupTools::sanitizeFileFormat(reset($fileFormats)); } $template->assign("file", $this->toArray()); $template->assign("total", $total); $template->assign("nbtransactions", $nbtransactions); $head = $template->fetch('../formats/' . $fileFormatName . '/transaction-header.tpl'); $footer = $template->fetch('../formats/' . $fileFormatName . '/transaction-footer.tpl'); return $head . $xml . $footer; }
function run() { if (!isset($_REQUEST['mid'])) { die(ts("This page needs a mandate id ('mid') parameter.")); } else { $mandate_id = (int) $_REQUEST['mid']; } if (isset($_REQUEST['action'])) { if ($_REQUEST['action'] == 'delete') { $this->deleteMandate($mandate_id); $this->assign('deleted_mandate', $mandate_id); parent::run(); return; } else { if ($_REQUEST['action'] == 'end') { $this->endMandate($mandate_id); } else { if ($_REQUEST['action'] == 'cancel') { $this->cancelMandate($mandate_id); } else { if ($_REQUEST['action'] == 'adjustamount') { $this->adjustAmount($mandate_id); } else { CRM_Core_Session::setStatus(sprintf(ts("Unkown action '%s'. Ignored."), $_REQUEST['action']), ts('Error'), 'error'); } } } } } // first, load the mandate $mandate = civicrm_api("SepaMandate", "getsingle", array('id' => $mandate_id, 'version' => 3)); if (isset($mandate['is_error']) && $mandate['is_error']) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read mandate [%s]. Error was: '%s'"), $mandate_id, $mandate['error_message']), ts('Error'), 'error'); die(sprintf(ts("Cannot find mandate [%s]."), $mandate_id)); } // load the contribution $contribution_id = $mandate['entity_id']; $contribution_type = $mandate['entity_table'] == 'civicrm_contribution' ? 'Contribution' : 'ContributionRecur'; $contribution = civicrm_api($contribution_type, "getsingle", array('id' => $contribution_id, 'version' => 3)); if (isset($contribution['is_error']) && $contribution['is_error']) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read contribution [%s]. Error was: '%s'"), $contribution_id, $contribution['error_message']), ts('Error'), 'error'); } // load the mandate's contact $contact1 = civicrm_api("Contact", "getsingle", array('id' => $mandate['contact_id'], 'version' => 3)); if (isset($contact1['is_error']) && $contact1['is_error']) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read contact [%s]. Error was: '%s'"), $contact1, $contact1['error_message']), ts('Error'), 'error'); } // load the contribtion's contact if ($mandate['contact_id'] == $contribution['contact_id']) { $contact2 = $contact1; } else { $contact2 = civicrm_api("Contact", "getsingle", array('id' => $contribution['contact_id'], 'version' => 3)); if (isset($contact2['is_error']) && $contact2['is_error']) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read contact [%s]. Error was: '%s'"), $contact2, $contact2['error_message']), ts('Error'), 'error'); } } // load the creditor if (!empty($mandate['creditor_id'])) { $creditor = civicrm_api("SepaCreditor", "getsingle", array('id' => $mandate['creditor_id'], 'version' => 3)); if (!empty($creditor['is_error'])) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read creditor [%s]. Error was: '%s'"), $mandate['creditor_id'], $creditor['error_message']), ts('Error'), 'error'); } else { $mandate['creditor_name'] = $creditor['name']; } } // load the campaign if (isset($contribution['contribution_campaign_id']) && $contribution['contribution_campaign_id']) { $campaign_id = $contribution['contribution_campaign_id']; } elseif (isset($contribution['campaign_id']) && $contribution['campaign_id']) { $campaign_id = $contribution['campaign_id']; } if (isset($campaign_id)) { $campaign = civicrm_api("Campaign", "getsingle", array('id' => $campaign_id, 'version' => 3)); if (isset($campaign['is_error'])) { CRM_Core_Session::setStatus(sprintf(ts("Cannot read contact [%s]. Error was: '%s'"), $campaign, $campaign['error_message']), ts('Error'), 'error'); } else { $contribution['campaign'] = $campaign['title']; } } else { $contribution['campaign'] = ''; } // prepare the data $financial_types = CRM_Contribute_PseudoConstant::financialType(); $contact1['link'] = CRM_Utils_System::url('civicrm/contact/view', "&reset=1&cid=" . $contact1['id']); $contact2['link'] = CRM_Utils_System::url('civicrm/contact/view', "&reset=1&cid=" . $contact2['id']); $contribution['financial_type'] = $financial_types[$contribution['financial_type_id']]; if (isset($contribution['amount']) && $contribution['amount']) { // this is a recurring contribution $contribution['link'] = CRM_Utils_System::url('civicrm/contact/view/contributionrecur', "&reset=1&id=" . $contribution['id'] . "&cid=" . $contact2['id']); $contribution['currency'] = $contribution['currency']; $contribution['cycle'] = CRM_Utils_SepaOptionGroupTools::getFrequencyText($contribution['frequency_interval'], $contribution['frequency_unit'], true); if (isset($contribution['end_date']) && $contribution['end_date']) { $contribution['default_end_date'] = date('Y-m-d', strtotime($contribution['end_date'])); } else { $contribution['default_end_date'] = date('Y-m-d'); } } else { // this is a simple contribution $contribution['link'] = CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&action=view&id=" . $contribution['id'] . "&cid=" . $contact2['id']); $contribution['amount'] = $contribution['total_amount']; $contribution['currency'] = $contribution['currency']; } // load eligeble templates // first: the dafault template $template_entry = civicrm_api('OptionValue', 'getsingle', array('version' => 3, 'option_group_name' => 'msg_tpl_workflow_contribution', 'name' => 'sepa_mandate_pdf')); $tpl_ids = array(); $query = "SELECT `id`, `msg_title`, `msg_subject`\n FROM `civicrm_msg_template`\n WHERE `is_active` = 1\n AND ( (`workflow_id` = '{$template_entry['id']}')\n OR (`msg_title` LIKE 'SEPA%' AND `workflow_id` IS NULL) );"; $result = CRM_Core_DAO::executeQuery($query); while ($result->fetch()) { $tpl_ids[] = array($result->id, $result->msg_title); } $this->assign('sepa', $mandate); $this->assign('contribution', $contribution); $this->assign('contact1', $contact1); $this->assign('contact2', $contact2); $this->assign('can_delete', CRM_Core_Permission::check('administer CiviCRM')); $this->assign('can_modify', CRM_Sepa_Logic_Settings::getSetting('allow_mandate_modification')); $this->assign('sepa_templates', $tpl_ids); parent::run(); }
/** * generate the HTML text, and assign all the required variables (tokens) * this is a precondition for PDF generation as well as emails */ function generateHTML($mandate, $template_id) { // init API wrapper object if (!isset($this->api)) { $this->api = new civicrm_api3(); } $api = $this->api; // LOAD INFORMATION and set tokens (smarty variables) // fix mandate. @X+: Why? if (is_array($mandate)) { $mandate = json_decode(json_encode($mandate), FALSE); } $this->mandate = $mandate; $this->assign("sepa", (array) $mandate); $this->assign("mandate", (array) $mandate); // load the associated contribution switch ($mandate->entity_table) { case "civicrm_contribution_recur": $api->ContributionRecur->getsingle(array("id" => $mandate->entity_id)); $recur = $api->result; $this->assign("recur", (array) $recur); $this->assign("contactId", $recur->contact_id); $this->addContactTokens($recur->contact_id, 'contact'); // first_conttribution if (!empty($mandate->first_contribution_id)) { $api->Contribution->getsingle(array("id" => $mandate->first_contribution_id)); $this->assign("first_contribution", (array) $api->result); } // some more extra information: $recur_extra = array(); $recur_extra['frequency_text'] = CRM_Utils_SepaOptionGroupTools::getFrequencyText($recur->frequency_interval, $recur->frequency_unit, false); $recur_extra['frequency_text_l10n'] = CRM_Utils_SepaOptionGroupTools::getFrequencyText($recur->frequency_interval, $recur->frequency_unit, true); $recur_extra['yearly_amount'] = $recur->amount; if ($recur->frequency_unit == 'month') { $recur_extra['yearly_amount'] = $recur_extra['yearly_amount'] * (12 / $recur->frequency_interval); } elseif ($recur->frequency_unit == 'year') { $recur_extra['yearly_amount'] = $recur_extra['yearly_amount'] / $recur->frequency_interval; } else { // we don't support other units unset($recur_extra['yearly_amount']); } $this->assign("recur_extra", (array) $recur_extra); break; case "civicrm_contribution": $api->Contribution->getsingle(array("id" => $mandate->entity_id)); $contribution = $api->result; $this->assign("contribution", (array) $contribution); $this->assign("contactId", $contribution->contact_id); $this->addContactTokens($contribution->contact_id, 'contact'); break; default: return CRM_Core_Error::fatal("We don't know how to handle mandates for " . $mandate->entity_table); } // add creditor information $api->SepaCreditor->getsingle(array('id' => $mandate->creditor_id)); $creditor = $api->result; $this->assign("mandate_creditor", $creditor); // add payment processor information if (!empty($recur->payment_processor_id)) { $payment_processor_id = $recur->payment_processor_id; } if (!empty($contribution->payment_processor_id)) { $payment_processor_id = $contribution->payment_processor_id; } if (!empty($payment_processor_id)) { $api->PaymentProcessor->getsingle(array('id' => $payment_processor_id)); if (isset($api->{$result})) { $this->assign("payment_processor", $api->{$result}); // LEGACY: set 'creditor' to the following (@X+: why?) $this->assign("creditor", $api->{$result}->user_name); } } // set some more basic information $this->addContactTokens($mandate->contact_id, 'mandate_contact'); $this->addContactTokens($creditor->creditor_id, 'creditor_contact'); $this->addContactTokens(CRM_Core_Session::singleton()->get('userID'), 'user'); // Load the template and run SMARTY $msg = $this->getMessage($template_id); CRM_Utils_System::setTitle($msg["msg_title"] . " " . $mandate->reference); $this->html = $this->getTemplate()->fetch("string:" . $msg["msg_html"]); }
/** * This method will create the SDD file for the given group * * @param txgroup_id the transaction group for which the file should be created * @param override if true, will override an already existing file and create a new one * * @return int id of the sepa file entity created, or an error message string */ static function createFile($txgroup_id, $override = false) { $txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id' => $txgroup_id, 'version' => 3)); if (isset($txgroup['is_error']) && $txgroup['is_error']) { return "Cannot find transaction group " . $txgroup_id; } $creditor = civicrm_api("SepaCreditor", "getsingle", array("sequential" => 1, "version" => 3, "id" => $txgroup["sdd_creditor_id"])); $fileFormatName = CRM_Core_OptionGroup::getValue('sepa_file_format', $creditor['sepa_file_format_id'], 'value', 'String', 'name'); if ($override || (!isset($txgroup['sdd_file_id']) || !$txgroup['sdd_file_id'])) { $fileFormatName = CRM_Utils_SepaOptionGroupTools::sanitizeFileFormat($fileFormatName); self::loadFormatClass($fileFormatName); $format_class = 'CRM_Sepa_Logic_Format_' . $fileFormatName; $format = new $format_class(); // find an available txgroup reference $available_name = $name = $format->getDDFilePrefix() . $txgroup['reference']; $counter = 1; $test_sql = "SELECT id FROM civicrm_sdd_file WHERE reference='%s';"; while (CRM_Core_DAO::executeQuery(sprintf($test_sql, $available_name))->fetch()) { // i.e. available_name is already taken, modify it $available_name = $name . '--' . $counter; $counter += 1; if ($counter > 1000) { return "Cannot create file! Unable to find an available file reference."; } } $group_status_id_closed = (int) CRM_Core_OptionGroup::getValue('batch_status', 'Closed', 'name'); // now that we found an available reference, create the file $sepa_file = civicrm_api('SepaSddFile', 'create', array('version' => 3, 'reference' => $available_name, 'filename' => $format->getFilename($available_name), 'latest_submission_date' => $txgroup['latest_submission_date'], 'created_date' => date('YmdHis'), 'created_id' => CRM_Core_Session::singleton()->get('userID'), 'status_id' => $group_status_id_closed)); if (isset($sepa_file['is_error']) && $sepa_file['is_error']) { return sprintf(ts("Cannot create file! Error was: '%s'"), $sepa_file['error_message']); } else { // update the txgroup object $result = civicrm_api('SepaTransactionGroup', 'create', array('id' => $txgroup_id, 'sdd_file_id' => $sepa_file['id'], 'version' => 3)); if (isset($result['is_error']) && $result['is_error']) { sprintf(ts("Cannot update transaction group! Error was: '%s'"), $result['error_message']); } return $sepa_file['id']; } } }
/** * This function checks to see if we have the right config values * * @return string the error message if any * @public */ function checkConfig() { // TODO: check urls (creditor IDs) CRM_Utils_SepaOptionGroupTools::checkRecurringFrequencyUnits(TRUE, TRUE); return NULL; }