/**
  * Checks if there is a VALID donation receipt for the given contribution
  *
  * This method should be HIGHLY optimized
  *
  * @return TRUE if there is a VALID donation receipt, FALSE otherwise
  */
 public static function isContributionLocked($contribution_id)
 {
     if (empty($contribution_id)) {
         return TRUE;
     } else {
         return CRM_Donrec_Logic_ReceiptItem::hasValidReceiptItem($contribution_id);
     }
 }
function donrec_civicrm_pre($op, $objectName, $id, &$params)
{
    if ($objectName == 'Contribution' && ($op == 'edit' || $op == 'delete')) {
        $has_item = CRM_Donrec_Logic_ReceiptItem::hasValidReceiptItem($id);
        $in_snapshot = CRM_Donrec_Logic_Snapshot::isInOpenSnapshot($id);
        if ($has_item || $in_snapshot) {
            if ($op == 'edit') {
                // columns that must not be changed
                $forbidden = array('financial_type_id', 'total_amount', 'receive_date', 'currency', 'contribution_status_id', 'payment_instrument_id');
                // get the contribution
                $query = "\n          SELECT *\n          FROM civicrm_contribution\n          WHERE id = {$id}";
                $result = CRM_Core_DAO::executeQuery($query);
                $result->fetch();
                // check if forbidden values are going to be changed.
                foreach ($forbidden as $col) {
                    if (isset($params[$col]) && $result->{$col} != $params[$col]) {
                        // we need a extra-check for dates (which are not in the same format)
                        if (strpos($col, 'date')) {
                            if ($col == 'receive_date' && substr(preg_replace('/[-: ]/', '', $result->{$col}), 0, -2) . "00" == $params[$col]) {
                                continue;
                            }
                        }
                        $message = sprintf(ts("The column '%s' of this contribution [%d] must not be changed because it has a receipt or is going to be receipted!", array('domain' => 'de.systopia.donrec')), $col, $id);
                        CRM_Utils_DonrecHelper::exitWithMessage($message);
                    }
                }
            } elseif ($op == 'delete') {
                $message = sprintf(ts("This contribution [%d] must not be deleted because it has a receipt or is going to be receipted!", array('domain' => 'de.systopia.donrec')), $id);
                CRM_Utils_DonrecHelper::exitWithMessage($message);
            }
        }
    }
    return;
}