Ejemplo n.º 1
0
 function testCopy()
 {
     // prerequisites
     $contributions = $this->generateContributions(3);
     $this->assertEquals(3, count($contributions));
     // generate a new snapshot
     $snapshot = CRM_Donrec_Logic_Snapshot::create($contributions, 1);
     $this->assertNotNull($snapshot, "CRM_Donrec_Logic_Snapshot::create() returned NULL");
     // create a receipt
     $snapshot_line_id = 1;
     $params = array();
     $result = CRM_Donrec_Logic_Receipt::createSingleFromSnapshot($snapshot, $snapshot_line_id, $params);
     $this->assertEquals(TRUE, $result, "CRM_Donrec_Logic_Receipt::createSingleFromSnapshot returned FALSE");
     // clone it
 }
Ejemplo n.º 2
0
 function run()
 {
     $contact_id = empty($_REQUEST['cid']) ? NULL : $_REQUEST['cid'];
     $scroll_to_receipt = empty($_REQUEST['rid']) ? NULL : $_REQUEST['rid'];
     if ($contact_id && CRM_Core_Permission::check('view and copy receipts')) {
         $params = array();
         $receipts = CRM_Donrec_Logic_Receipt::getReceiptsForContact($contact_id, $params);
         $display_receipts = array();
         foreach ($receipts as $rec) {
             $display_receipts[$rec->getId()] = $rec->getAllTokens();
         }
         $this->assign('cid', $contact_id);
         $this->assign('display_receipts', $display_receipts);
         if ($scroll_to_receipt) {
             $this->assign('scroll_to', $scroll_to_receipt);
         }
     }
     // permissions
     $this->assign('can_view_copy', CRM_Core_Permission::check('view and copy receipts'));
     $this->assign('can_create_withdraw', CRM_Core_Permission::check('create and withdraw receipts'));
     $this->assign('can_delete', CRM_Core_Permission::check('delete receipts'));
     parent::run();
 }
Ejemplo n.º 3
0
 /**
  * Updates the class attribute to contain all custom fields of the
  * donation receipt database table.
  */
 public static function getCustomFields()
 {
     if (self::$_custom_fields === NULL) {
         // get the ids of all relevant custom fields
         $params = array('version' => 3, 'name' => 'zwb_donation_receipt');
         $custom_group = civicrm_api('CustomGroup', 'getsingle', $params);
         if (isset($custom_group['is_error'])) {
             CRM_Core_Error::debug_log_message(sprintf('de.systopia.donrec: getCustomFields: error: %s', $custom_group['error_message']));
             return NULL;
         }
         self::$_custom_group_id = $custom_group['id'];
         $params = array('version' => 3, 'custom_group_id' => $custom_group['id'], 'option.limit' => 999);
         $custom_fields = civicrm_api('CustomField', 'get', $params);
         if ($custom_fields['is_error'] != 0) {
             CRM_Core_Error::debug_log_message(sprintf('de.systopia.donrec: getCustomFields: error: %s', $custom_fields['error_message']));
             return NULL;
         }
         self::$_custom_fields = array();
         foreach ($custom_fields['values'] as $field) {
             self::$_custom_fields[$field['name']] = $field['column_name'];
         }
     }
     return self::$_custom_fields;
 }
function civicrm_api3_donation_receipt_details($params)
{
    // check for missing receipt id parameter
    if (empty($params['rid'])) {
        return civicrm_api3_create_error(ts("No 'rid' parameter given.", array('domain' => 'de.systopia.donrec')));
    }
    $receipt = CRM_Donrec_Logic_Receipt::get($params['rid']);
    if (!empty($receipt)) {
        $details = $receipt->getDetails();
    } else {
        return civicrm_api3_create_error(sprintf(ts("Receipt with id %d does not exist.", array('domain' => 'de.systopia.donrec')), $params['rid']));
    }
    // and return the result
    return civicrm_api3_create_success($details);
}
Ejemplo n.º 5
0
 /**
  * Rule set for the rebooking forms
  */
 static function rebookRules($values)
 {
     $errors = array();
     $contactId = trim($values['contactId']);
     $contributionIds = $values['contributionIds'];
     if (!preg_match('/^\\d+$/', $contactId)) {
         // check if is int
         $errors['contactId'] = ts('Please enter a CiviCRM ID!', array('domain' => 'de.systopia.donrec'));
         return empty($errors) ? TRUE : $errors;
     }
     // validation for contact
     $contact = new CRM_Contact_BAO_Contact();
     $contact->id = (int) $contactId;
     if (!$contact->find(true)) {
         $errors['contactId'] = ts('A contact with CiviCRM ID %1 doesn\'t exist!', array(1 => $contactId, 'domain' => 'de.systopia.donrec'));
         return empty($errors) ? TRUE : $errors;
     }
     // Der Kontakt, auf den umgebucht wird, darf kein Haushalt sein.
     $contactType = $contact->getContactType($contactId);
     if (!empty($contactType) && $contactType == 'Household') {
         $errors['contactId'] = ts('The target contact can not be a household!', array('domain' => 'de.systopia.donrec'));
         return empty($errors) ? TRUE : $errors;
     }
     // Der Kontakt, auf den umgebucht wird, darf nicht im Papierkorb sein.
     $contactIsDeleted = $contact->is_deleted;
     if ($contactIsDeleted == 1) {
         $errors['contactId'] = ts('The target contact can not be in trash!', array('domain' => 'de.systopia.donrec'));
         return empty($errors) ? TRUE : $errors;
     }
     // Check contributions
     $completed = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
     $arr = explode(",", $contributionIds);
     foreach ($arr as $contributionId) {
         $contribution = new CRM_Contribute_DAO_Contribution();
         $contribution->id = $contributionId;
         if ($contribution->find(true)) {
             // only 'completed' contributions can be rebooked
             if ($contribution->contribution_status_id != $completed) {
                 $errors['contactId'] = ts('The contribution with ID %1 is not completed!', array(1 => $contributionId, 'domain' => 'de.systopia.donrec'));
                 return empty($errors) ? TRUE : $errors;
             }
             // receipted contributions can NOT be rebooked
             if (CRM_Donrec_Logic_Receipt::isContributionLocked($contributionId)) {
                 $errors['contactId'] = ts('The contribution with ID %1 cannot be rebooked, because it has a valid contribution receipt.', array(1 => $contributionId, 'domain' => 'de.systopia.donrec'));
                 return empty($errors) ? TRUE : $errors;
             }
         }
     }
     return empty($errors) ? TRUE : $errors;
 }
Ejemplo n.º 6
0
/**
 * Prune the "find contributions" and "advanced contact search" forms
 * by removing the fields that don't make sense or don't work
 */
function donrec_civicrm_buildForm($formName, &$form)
{
    if ($formName == 'CRM_Contribute_Form_Search') {
        $item_fields = CRM_Donrec_Logic_ReceiptItem::getCustomFields();
        // remove unwanted fields
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'financial_type_id');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'total_amount');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'non_deductible_amount');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'currency');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'contribution_hash');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_on');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'receive_date');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_in');
        $form->assign('field_ids_to_remove', implode(',', $field_ids_to_remove));
        CRM_Core_Region::instance('page-body')->add(array('template' => 'CRM/Donrec/Form/Search/RemoveFields.snippet.tpl'));
        // DISABLED: date field search doesn't work
        // CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'issued_on', ts("Issed On - From", array('domain' => 'de.systopia.donrec')), ts("Issed On - To", array('domain' => 'de.systopia.donrec')));
        // CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'receive_date', ts("Received - From", array('domain' => 'de.systopia.donrec')), ts("Received - To", array('domain' => 'de.systopia.donrec')));
        // override the standard fields
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'status');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Status', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'original' => ts('original', array('domain' => 'de.systopia.donrec')), 'copy' => ts('copy', array('domain' => 'de.systopia.donrec')), 'withdrawn' => ts('withdrawn', array('domain' => 'de.systopia.donrec')), 'withdrawn_copy' => ts('withdrawn_copy', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'type');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Type', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'single' => ts('single receipt', array('domain' => 'de.systopia.donrec')), 'bulk' => ts('bulk receipt', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_in');
        if ($status_id) {
            $form->add('text', "custom_{$status_id}", ts('Receipt ID', array('domain' => 'de.systopia.donrec')));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_by');
        if ($status_id) {
            $form->add('text', "custom_{$status_id}", ts('Issued by contact', array('domain' => 'de.systopia.donrec')));
        }
    } elseif ($formName == 'CRM_Contact_Form_Search_Advanced') {
        // remove unwanted fields
        $item_fields = CRM_Donrec_Logic_Receipt::getCustomFields();
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_on');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'original_file');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'contact_type');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'gender');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'prefix');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'display_name');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'postal_greeting_display');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'email_greeting_display');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'addressee_display');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'street_address');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_1');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_2');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_3');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'postal_code');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'city');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'country');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_addressee_display');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_street_address');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_1');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_2');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_3');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_postal_code');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_city');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_country');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'date_from');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'date_to');
        // remove unwanted fields from receipt items (in contribution tab)
        $item_fields_receipt = CRM_Donrec_Logic_ReceiptItem::getCustomFields();
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'financial_type_id');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'total_amount');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'non_deductible_amount');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'currency');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'contribution_hash');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_on');
        $field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'receive_date');
        $form->assign('field_ids_to_remove', implode(',', $field_ids_to_remove));
        CRM_Core_Region::instance('page-body')->add(array('template' => 'CRM/Donrec/Form/Search/RemoveFields.snippet.tpl'));
        // DISABLED: date field search doesn't work
        //CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'issued_on', ts("Issed On - From", array('domain' => 'de.systopia.donrec')), ts("Issed On - To", array('domain' => 'de.systopia.donrec')));
        // override the standard fields
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'status');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Status', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'original' => ts('original', array('domain' => 'de.systopia.donrec')), 'copy' => ts('copy', array('domain' => 'de.systopia.donrec')), 'withdrawn' => ts('withdrawn', array('domain' => 'de.systopia.donrec')), 'withdrawn_copy' => ts('withdrawn_copy', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'type');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Type', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'single' => ts('single receipt', array('domain' => 'de.systopia.donrec')), 'bulk' => ts('bulk receipt', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_by');
        if ($status_id) {
            $form->add('text', "custom_{$status_id}", ts('Issued by contact', array('domain' => 'de.systopia.donrec')));
        }
        // override the receipt_item standard fields
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'status');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Status', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'original' => ts('original', array('domain' => 'de.systopia.donrec')), 'copy' => ts('copy', array('domain' => 'de.systopia.donrec')), 'withdrawn' => ts('withdrawn', array('domain' => 'de.systopia.donrec')), 'withdrawn_copy' => ts('withdrawn_copy', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'type');
        if ($status_id) {
            $form->add('select', "custom_{$status_id}", ts('Type', array('domain' => 'de.systopia.donrec')), array('' => ts('- any -', array('domain' => 'de.systopia.donrec')), 'single' => ts('single receipt', array('domain' => 'de.systopia.donrec')), 'bulk' => ts('bulk receipt', array('domain' => 'de.systopia.donrec'))));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_in');
        if ($status_id) {
            $form->add('text', "custom_{$status_id}", ts('Receipt ID', array('domain' => 'de.systopia.donrec')));
        }
        $status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_by');
        if ($status_id) {
            $form->add('text', "custom_{$status_id}", ts('Issued by contact', array('domain' => 'de.systopia.donrec')));
        }
    }
}
Ejemplo n.º 7
0
 /**
  * execute the next step of a donation receipt run
  *
  * @return array of stats:
  */
 public function nextStep()
 {
     // some containers
     $exporter_results = array();
     $files = array();
     $profile = $this->snapshot->getProfile();
     // Synchronize this step
     $lock = CRM_Utils_DonrecHelper::getLock('CRM_Donrec_Logic_Engine', 'nextStep');
     if (!$lock->isAcquired()) {
         // lock timed out
         CRM_Core_Error::debug_log_message("de.systopia.donrec - couldn't acquire lock. Timeout is " . $lock->_timeout);
         // compile and return "state of affairs" report
         $stats = $this->createStats();
         $stats['log'] = array();
         $stats['files'] = $files;
         $stats['chunk_size'] = 0;
         CRM_Donrec_Logic_Exporter::addLogEntry($stats, "Couldn't acquire lock. Parallel processing denied. Lock timeout is {$lock->_timeout}s.");
         return $stats;
     }
     // check status
     $is_bulk = !empty($this->parameters['bulk']);
     $is_test = !empty($this->parameters['test']);
     // initialize stuff
     $chunk = $this->snapshot->getNextChunk($is_bulk, $is_test);
     $exporters = $this->getExporters();
     // loop over receipts
     foreach ($chunk as $chunk_id => $chunk_items) {
         // Setup some parameters
         //**********************************
         // Prepare chunk_items:
         // #FIXME: It is more convenient to have a simalar array-structure for bulk-
         // and single-processing. In future the getNextChunk-method might be
         // refactored and build up the arrays correspondingly.
         $chunk_items = $is_bulk ? $chunk_items : array($chunk_items);
         $contact_id = $chunk_items[0]['contact_id'];
         $line_ids = array();
         foreach ($chunk_items as $chunk_item) {
             $line_ids[] = $chunk_item['id'];
         }
         // create a SnapshotReceipt
         $snapshot_receipt = $this->snapshot->getSnapshotReceipt($line_ids, $is_test);
         // call exporters
         //**********************************
         foreach ($exporters as $exporter) {
             $exporter_id = $exporter->getID();
             if ($is_bulk) {
                 $result = $exporter->exportBulk($snapshot_receipt, $is_test);
             } else {
                 $result = $exporter->exportSingle($snapshot_receipt, $is_test);
             }
             if (!isset($exporter_results[$exporter_id])) {
                 $exporter_results[$exporter_id] = array();
                 $exporter_results[$exporter_id]['success'] = 0;
                 $exporter_results[$exporter_id]['failure'] = 0;
             }
             if ($result) {
                 $exporter_results[$exporter_id]['success']++;
             } else {
                 $exporter_results[$exporter_id]['failure']++;
             }
         }
         // save original pdfs and create receipt for non-test-runs
         //**********************************
         if (!$is_test) {
             $receipt_params = array();
             $receipt_params['type'] = $is_bulk ? 'BULK' : 'SINGLE';
             if ($profile->saveOriginalPDF()) {
                 $pdf_file = $this->getPDF($line_ids);
                 $file = CRM_Donrec_Logic_File::createPermanentFile($pdf_file, basename($pdf_file), $contact_id);
                 if (!empty($file)) {
                     $receipt_params['original_file'] = $file['id'];
                 }
             }
             CRM_Donrec_Logic_Receipt::createFromSnapshotReceipt($snapshot_receipt, $receipt_params);
         }
     }
     // The last chunk is empty.
     // If it is the last do some wrap-up.
     // Otherwise mark the chunk as processed.
     //**********************************
     if (!$chunk) {
         foreach ($exporters as $exporter) {
             $result = $exporter->wrapUp($this->snapshot->getId(), $is_test, $is_bulk);
             if (!empty($result['download_name']) && !empty($result['download_url'])) {
                 $files[$exporter->getID()] = array($result['download_name'], $result['download_url']);
             }
         }
     } else {
         $this->snapshot->markChunkProcessed($chunk, $is_test, $is_bulk);
     }
     // compile stats
     //**********************************
     $stats = $this->createStats();
     // create log-messages
     foreach ($exporter_results as $exporter_id => $result) {
         $msg = sprintf('%s processed %d items - %d succeeded, %d failed', $exporter_id, count($chunk), $result['success'], $result['failure']);
         $type = $result['failure'] ? 'ERROR' : 'INFO';
         CRM_Donrec_Logic_Exporter::addLogEntry($stats, $msg, $type);
     }
     $stats['files'] = $files;
     if ($chunk == NULL) {
         $stats['progress'] = 100.0;
     } else {
         $stats['chunk_size'] = count($chunk);
     }
     // release our lock
     $lock->release();
     return $stats;
 }