/**
  * 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()
 {
     // 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);
     $year = $params['receipt_year'];
     if ($year) {
         $year = substr($year, strlen('issue_'));
         // e.g. issue_2012
     }
     $previewMode = FALSE;
     if (isset($params['is_preview']) && $params['is_preview'] == 1) {
         $previewMode = TRUE;
     }
     // start a PDF to collect receipts that cannot be emailed
     $receiptsForPrintingPDF = cdntaxreceipts_openCollectedPDF();
     $emailCount = 0;
     $printCount = 0;
     $failCount = 0;
     // TODO: Will need to change if multiple years allowed
     foreach ($this->_receipts['original'][$year]['contact_ids'] as $contact_id => $contribution_status) {
         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;
         }
         $contributions = $contribution_status['contributions'];
         $method = $contribution_status['issue_method'];
         if (empty($issuedOn) && count($contributions) > 0) {
             $ret = cdntaxreceipts_issueAggregateTaxReceipt($contact_id, $year, $contributions, $method, $receiptsForPrintingPDF, $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($receiptsForPrintingPDF, 'Receipts-To-Print-' . (int) $_SERVER['REQUEST_TIME'] . '.pdf');
     // EXITS.
 }