/**
  * 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);
 }
/**
 * 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')));
        }
    }
}