/**
  * @param array  $params         (reference ) an assoc array of name/value pairs
  *
  * @return object       CRM_Core_BAO_SEPAMandate object on success, null otherwise
  * @access public
  * @static (I do apologize, I don't want to)
  */
 static function add(&$params)
 {
     // handle 'normal' creation process inlcuding hooks
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'SepaMandate', CRM_Utils_Array::value('id', $params), $params);
     // set default date to today
     if (!array_key_exists("date", $params)) {
         $params["date"] = date("YmdHis");
     }
     if (empty($params['id'])) {
         CRM_Utils_SepaCustomisationHooks::create_mandate($params);
         if (empty($params['reference'])) {
             // If no mandate reference was supplied by the caller nor the customisation hook, create a nice default one.
             $creditor = civicrm_api3('SepaCreditor', 'getsingle', array('id' => $params['creditor_id'], 'return' => 'mandate_prefix'));
             $dao = new CRM_Core_DAO();
             $database = $dao->database();
             $next_id = CRM_Core_DAO::singleValueQuery("SELECT auto_increment FROM information_schema.tables WHERE table_schema='{$database}' and table_name='civicrm_sdd_mandate';");
             $params['reference'] = $creditor['mandate_prefix'] . '-' . $params['creditor_id'] . '-' . $params['type'] . '-' . date("Y") . '-' . $next_id;
         }
     }
     // validate IBAN / BIC
     if (!empty($params['iban'])) {
         $params['iban'] = strtoupper($params['iban']);
         // create uppercase string
         $params['iban'] = str_replace(' ', '', $params['iban']);
         // strip spaces
         $iban_error = CRM_Sepa_Logic_Verification::verifyIBAN($params['iban']);
         if ($iban_error) {
             throw new CRM_Exception($iban_error . ':' . $params['iban']);
         }
     }
     if (!empty($params['bic'])) {
         $bic_error = CRM_Sepa_Logic_Verification::verifyBIC($params['bic']);
         if ($bic_error) {
             throw new CRM_Exception($bic_error . ':' . $params['bic']);
         }
     }
     // create the DAO object
     $dao = new CRM_Sepa_DAO_SEPAMandate();
     $dao->copyValues($params);
     if (self::is_active(CRM_Utils_Array::value('status', $params))) {
         if ($dao->validation_date == NULL) {
             $dao->validation_date = date("YmdHis");
         }
     }
     $dao->save();
     CRM_Utils_Hook::post($hook, 'SepaMandate', $dao->id, $dao);
     return $dao;
 }
 /**
  * returns the list of fields that can be exported
  *
  * @access public
  * return array
  * @static
  */
 static function &export($prefix = false)
 {
     if (!self::$_export) {
         self::$_export = array();
         $fields = self::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     self::$_export['sdd_mandate'] =& $fields[$name];
                 } else {
                     self::$_export[$name] =& $fields[$name];
                 }
             }
         }
     }
     return self::$_export;
 }