PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/extract.php

https://github.com/arnaudlimbourg/squirrel-helpers
PHP | 85 lines | 55 code | 11 blank | 19 comment | 12 complexity | 9203ff8b5a41ec67b42721f3d62a5698 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright (c) 2009, Arnaud Limbourg
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. */
  11. /*************************************************
  12. * Non destructive (read-only) script that
  13. * extracts transactions from a squirrel database
  14. * to a csv file.
  15. *
  16. * To be safe use on a copy of the database.
  17. *************************************************/
  18. if (!ini_get('register_argc_argv')) {
  19. bail('register_argc_argv must be set in your php.ini');
  20. }
  21. if (count($argv) === 1) {
  22. bail();
  23. }
  24. $options = getopt('d:la::f::o::');
  25. include 'squirrel.php';
  26. if (array_key_exists('d', $options) === false) {
  27. bail('Please indicate database with the -d flag');
  28. }
  29. try {
  30. $squirrel = new CMBSquirrel($options['d']);
  31. } catch (Exception $e) {
  32. bail('Cannot initialize: ' . $e->getMessage());
  33. }
  34. if (array_key_exists('l', $options)) {
  35. foreach ($squirrel->listAccounts() as $id => $name) {
  36. echo 'Account ID: ', $id, ' - ', $name, "\n";
  37. }
  38. exit(0);
  39. }
  40. if (array_key_exists('a', $options)
  41. && array_key_exists('f', $options)) {
  42. if (file_exists($options['f']) === false) {
  43. bail('The file containing your forecast expenses does not exists');
  44. }
  45. include $options['f'];
  46. $transactions = $squirrel->extractAndForecast($options['a'], $expenses_monthly);
  47. } else {
  48. $transactions = $squirrel->loadTransactions();
  49. }
  50. if (array_key_exists('o', $options)) {
  51. $squirrel->setOutputFile($options['o']);
  52. $squirrel->saveToFile($transactions);
  53. } else {
  54. bail('Please indicate the output file name with the -o flag');
  55. }
  56. function bail($msg = '')
  57. {
  58. if (empty($msg)) {
  59. echo "
  60. Usage:
  61. php extract.php -d/path/to/database
  62. php extract.php -l -d/path/to/database # list accounts
  63. php extract.php -l -oextract.csv -d/path/to/database
  64. php extract.php -l -aACCOUNTID -oextract.csv -d/path/to/database
  65. Examples:
  66. php extract.php -a3 -fexpenses.php -ofoo.csv -ddatabase.squirrel #extracts transactions for account 3, from the list accounts";
  67. } else {
  68. echo "$msg\n";
  69. }
  70. exit(1);
  71. }