/**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return None
  */
 function postProcess()
 {
     // lets get around the time limit issue if possible
     if (!ini_get('safe_mode')) {
         set_time_limit(0);
     }
     // Issue 1895204: Turn off geocoding to avoid hitting Google API limits
     $config =& CRM_Core_Config::singleton();
     $oldGeocode = $config->geocodeMethod;
     unset($config->geocodeMethod);
     $params = $this->controller->exportValues($this->_name);
     $originalOnly = FALSE;
     if ($params['receipt_option'] == 'original_only') {
         $originalOnly = TRUE;
     }
     $previewMode = FALSE;
     if (isset($params['is_preview']) && $params['is_preview'] == 1) {
         $previewMode = TRUE;
     }
     /**
      * Drupal module include
      */
     //module_load_include('.inc','civicrm_cdntaxreceipts','civicrm_cdntaxreceipts');
     //module_load_include('.module','civicrm_cdntaxreceipts','civicrm_cdntaxreceipts');
     // start a PDF to collect receipts that cannot be emailed
     $receiptsForPrinting = cdntaxreceipts_openCollectedPDF();
     $emailCount = 0;
     $printCount = 0;
     $failCount = 0;
     foreach ($this->_contributionIds as $item => $contributionId) {
         if ($emailCount + $printCount + $failCount >= self::MAX_RECEIPT_COUNT) {
             $status = ts('Maximum of %1 tax receipt(s) were sent. Please repeat to continue processing.', array(1 => self::MAX_RECEIPT_COUNT, 'domain' => 'org.civicrm.cdntaxreceipts'));
             CRM_Core_Session::setStatus($status, '', 'info');
             break;
         }
         // 1. Load Contribution information
         $contribution = new CRM_Contribute_DAO_Contribution();
         $contribution->id = $contributionId;
         if (!$contribution->find(TRUE)) {
             CRM_Core_Error::fatal("CDNTaxReceipts: Could not find corresponding contribution id.");
         }
         // 2. If Contribution is eligible for receipting, issue the tax receipt.  Otherwise ignore.
         if (cdntaxreceipts_eligibleForReceipt($contribution->id)) {
             list($issued_on, $receipt_id) = cdntaxreceipts_issued_on($contribution->id);
             if (empty($issued_on) || !$originalOnly) {
                 list($ret, $method) = cdntaxreceipts_issueTaxReceipt($contribution, $receiptsForPrinting, $previewMode);
                 if ($ret == 0) {
                     $failCount++;
                 } elseif ($method == 'email') {
                     $emailCount++;
                 } else {
                     $printCount++;
                 }
             }
         }
     }
     // 3. Set session status
     if ($previewMode) {
         $status = ts('%1 tax receipt(s) have been previewed.  No receipts have been issued.', array(1 => $printCount, 'domain' => 'org.civicrm.cdntaxreceipts'));
         CRM_Core_Session::setStatus($status, '', 'success');
     } else {
         $status = ts('%1 tax receipt(s) were sent by email.', array(1 => $emailCount, 'domain' => 'org.civicrm.cdntaxreceipts'));
         CRM_Core_Session::setStatus($status, '', 'success');
         $status = ts('%1 tax receipt(s) need to be printed.', array(1 => $printCount, 'domain' => 'org.civicrm.cdntaxreceipts'));
         CRM_Core_Session::setStatus($status, '', 'success');
     }
     if ($failCount > 0) {
         $status = ts('%1 tax receipt(s) failed to process.', array(1 => $failCount, 'domain' => 'org.civicrm.cdntaxreceipts'));
         CRM_Core_Session::setStatus($status, '', 'error');
     }
     // Issue 1895204: Reset geocoding
     $config->geocodeMethod = $oldGeocode;
     // 4. send the collected PDF for download
     // NB: This exits if a file is sent.
     cdntaxreceipts_sendCollectedPDF($receiptsForPrinting, 'Receipts-To-Print-' . (int) $_SERVER['REQUEST_TIME'] . '.pdf');
     // EXITS.
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return None
  */
 function postProcess()
 {
     // ensure the user has permission to issue the tax receipt.
     if (!CRM_Core_Permission::check('issue cdn tax receipts')) {
         CRM_Core_Error::fatal(ts('You do not have permission to access this page', array('domain' => 'org.civicrm.cdntaxreceipts')));
     }
     // load the contribution
     $contributionId = $this->get('contribution_id');
     $contactId = $this->get('contact_id');
     $contribution = new CRM_Contribute_DAO_Contribution();
     $contribution->id = $contributionId;
     if (!$contribution->find(TRUE)) {
         CRM_Core_Error::fatal("CDNTaxReceipts: Could not retrieve details for this contribution");
     }
     // issue tax receipt, or report error if ineligible
     if (!cdntaxreceipts_eligibleForReceipt($contribution->id)) {
         $statusMsg = ts('This contribution is not tax deductible and/or not completed. No receipt has been issued.', array('domain' => 'org.civicrm.cdntaxreceipts'));
         CRM_Core_Session::setStatus($statusMsg);
     } else {
         list($result, $method, $pdf) = cdntaxreceipts_issueTaxReceipt($contribution);
         if ($result == TRUE) {
             if ($method == 'email') {
                 $statusMsg = ts('Tax Receipt has been emailed to the contributor.', array('domain' => 'org.civicrm.cdntaxreceipts'));
             } else {
                 $statusMsg = ts('Tax Receipt has been generated for printing.', array('domain' => 'org.civicrm.cdntaxreceipts'));
             }
             CRM_Core_Session::setStatus($statusMsg);
         } else {
             $statusMsg = ts('Encountered an error. Tax receipt has not been issued.', array('domain' => 'org.civicrm.cdntaxreceipts'));
             CRM_Core_Session::setStatus($statusMsg);
             unset($pdf);
         }
     }
     // refresh the form, with file stored in session if we need it.
     $urlParams = array('reset=1', 'cid=' . $contactId, 'id=' . $contributionId);
     if ($method == 'print' && isset($pdf)) {
         $session = CRM_Core_Session::singleton();
         $session->set("pdf_file_" . $contributionId . "_" . $contactId, $pdf, 'cdntaxreceipts');
         $urlParams[] = 'file=1';
     }
     $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), implode('&', $urlParams));
     CRM_Utils_System::redirect($url);
 }