Advertisement
lilcompanion

extract_transactions.php for Gnucash

May 11th, 2014
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This script will remove transactions from a given GNUcash book file. This was useful to me
  5.  * when I deleted a bunch of accounts and then was stuck with thousands of entries under
  6.  * the "Imbalance" category. AFAIK, there is no official way to perform this action.
  7.  *
  8.  * To use:
  9.  * 1. Make a copy of your gnucash file with a gzip extension: `cp my_file.gnucash my_file.gz`
  10.  * 2. Un-gzip it: `gzip -d my_file.gz`
  11.  * 3. Open it in a text editor and look for the <gnc:account> block for the account in question.
  12.  *    Usually you can just text search for the account name.
  13.  * 4. Copy down the GUID for the account, it will look something like:
  14.  *      <act:id type="guid">585c23271c91aa83b48179a21b12683b</act:id>
  15.  * 5. Modify the variables below based on the last few steps.
  16.  * 6. Run the PHP script in the same directory as the GNUcash file: `php extract_transactions.php`
  17.  * 7. Gzip the resulting file (it is the same name as the original with a '.out' extension on it)
  18.  *    and rename it to `whatever_you_want.gnucash`.
  19.  */
  20.  
  21. /* XXX ** Fill out these variables: **********/
  22. $filename = 'my_file';
  23. $guid = '';
  24. /*********************************************/
  25.  
  26. $file = file($filename);
  27. $file_out = fopen($filename.'.out', 'w');
  28.  
  29. // States:
  30. // 0 - outside transaction
  31. // 1 - inside transaction
  32. // 2 - inside bad transaction
  33. // 3 - exiting bad transaction
  34. // 4 - exiting good transaction
  35. $state = 0;
  36.  
  37. $str_trn_start = 'gnc:transaction';
  38. $str_trn_stop = '/gnc:transaction';
  39.  
  40. $buffer = '';
  41.  
  42. foreach ($file as $line_num => $line) {
  43.     if ($state == 0) {
  44.         if (strpos($line, $str_trn_start)) {
  45.             // We're entering a transaction
  46.             $state = 1;
  47.         } else {
  48.             // We left a normal good line, write it
  49.             fwrite($file_out, $line);
  50.         }
  51.     }
  52.  
  53.     // Buffer-building states
  54.     if ($state == 1) {
  55.         // if we're in a potentially good transaction, add the line to the buffer
  56.         $buffer .= $line;
  57.         // Check if this is a bad transaction
  58.         if (strpos($line, $guid)) {
  59.             $state = 2;
  60.         }
  61.  
  62.         // Check if we're leaving this transaction
  63.         if (strpos($line, $str_trn_stop)) {
  64.             $state = 4;
  65.         }
  66.     }
  67.     elseif ($state == 2) {
  68.         // inside bad transaction
  69.        
  70.         // Check if we're leaving this transaction
  71.         if (strpos($line, $str_trn_stop)) {
  72.             $state = 3;
  73.         }
  74.     }
  75.  
  76.     // Buffer writing states
  77.     if ($state == 3) {
  78.         // We left a bad transaction. Clear the buffer and move on.
  79.         $buffer = '';
  80.         $state = 0;
  81.     }
  82.     elseif ($state == 4) {
  83.         // We've left a good transaction, write the buffer
  84.         echo "writing good transaction to file\n";
  85.         fwrite($file_out, $buffer);
  86.         $buffer = '';
  87.         $state = 0;
  88.     }
  89.  
  90.     //echo $state;
  91. }
  92.  
  93. fclose($file_out);
  94.  
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement