CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Invoice ID and Transaction ID on offline Contribution
Pages: [1]

Author Topic: Invoice ID and Transaction ID on offline Contribution  (Read 1311 times)

barto

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5.1
  • MySQL version: 5.5.29
  • PHP version: 5.3.19
Invoice ID and Transaction ID on offline Contribution
March 04, 2013, 12:41:26 am
Hi to all,
I'm tring to figure out how to use hooks to prepopulate the fields Invoice ID and Transaction ID on Offline Contribution Page. I would like that every time an operator insert an offline contrib automatically Civicrm populate Invoice ID adn Transaction ID based on a particular sql query or value.
I read manuel and documentation but I can't understand how to apply that. I know that I have to use hooks to implement it, and I should use hook_buildForm(), but in wich file I have to put the code? In the main.php of the contrib?

I'm using the last version of Wordpress as CMS and the last version of civicrm

Thank you all for your support

Barto

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Invoice ID and Transaction ID on offline Contribution
March 04, 2013, 04:34:42 am
Hi,

The easiest is to create a custom/native extension
https://github.com/totten/civix is a wonderful tool to create the template.

say your extension is org.yourorg.offlineinvoice

civix generate:module org.yourorg.offlineinvoice

that will create (among other things) a offlineinvoice.php file

in that file, you put a function offlineinvoice_buildForm and you should be good.

If your aim is to generate the invoice and not let the user modify it, you should do that on the pre hook (that allows you to modify the contribution just before being saved)


-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

barto

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: WordPress 3.5.1
  • MySQL version: 5.5.29
  • PHP version: 5.3.19
Re: Invoice ID and Transaction ID on offline Contribution
March 04, 2013, 08:41:05 am
I have figured out!  :D

I have added civicrmHooks.php on custom php url with this code

Code: [Select]
<?php
function wordpress_civicrm_buildForm($formName, &$form) {


if ( is_a( $form, 'CRM_Contribute_Form_Contribution' ) ) {

 mysql_connect("HOST", "USER", "PASSWORD") or die(mysql_error()); 
 mysql_select_db("DATABASE") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM TABLE") 
 or die(mysql_error()); 
 $info = mysql_fetch_array( $data );
 $idda = $info['anno'];
 $idfa = $info['val'] + 1;
$valore = $idda . '-' . str_pad($idfa, 10, '0', STR_PAD_LEFT);
    
$defaults['invoice_id'] = $valore;
$defaults['trxn_id'] = $valore;
    
$form->setDefaults($defaults);
  }
  }
  
function 
wordpress_civicrm_post($op, $objectName, $objectId, &$objectRef) {
 
 
  if (
$op == 'create' && $objectName == 'Contribution') {
 mysql_connect("HOST", "USER", "PASSWORD") or die(mysql_error()); 
 mysql_select_db("DATABASE") or die(mysql_error());
 $data = mysql_query("SELECT * FROM TABLE") 
 or die(mysql_error()); 
 $info = mysql_fetch_array( $data );
 $idfa = $info['val'] + 1;
 mysql_query("UPDATE TABLE SET val='".$idfa."'");
  } 
 
}  
?>

In this way I can query a DB for invoice id, punt in the field and if the form is confirmed the db is updated back.
What do you think about?

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Invoice ID and Transaction ID on offline Contribution
March 05, 2013, 06:21:23 am
looks ok - you might want to consider using CiviCRM's CRM_Core_DAO::executeQuery() function to get the results you need.
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Invoice ID and Transaction ID on offline Contribution

This forum was archived on 2017-11-26.