Browse Source

Creates GnuCash csv transaction files!

1) Does not automatically download them the browser yet, but can be found in the designated directory.  Works great with GnuCash!  Victory!
devel
Jonathan Rosenbaum 10 years ago
parent
commit
f89c86384a
  1. 1
      Connections/database_functions.php
  2. 2
      js/transaction.js
  3. 86
      json/transaction.php

1
Connections/database_functions.php

@ -154,6 +154,7 @@ define("CSV_DIRECTORY","csv");
// chown www-data:www-data csv
// chmod 0700 csv
// Define array mapping for Accounts. Usually Asset Accounts since Income is generally the main type of transaction.
// Currently four types of accounts are supported: checking, credit, account_receivable, donation
// checking/credit = transaction has been 1) paid and is 2) cash & check [checking] or a credit card [credit] and 3) deposited

2
js/transaction.js

@ -403,7 +403,7 @@ $(function() {
$.post("json/transaction.php",{ gnucash_account_type: v, transaction_range: transaction_range});
});
}
});
});
}

86
json/transaction.php

@ -192,42 +192,88 @@ $csv_directory = CSV_DIRECTORY;
}
// Create csv file(s) for GnuCash with this format:
// Create csv file(s) for GnuCash
if(isset($_POST['gnucash_account_type'])) {
/* require_once('../php-console/src/PhpConsole/__autoload.php');
$handler = PhpConsole\Handler::getInstance();
$handler->start();*/
$transaction_range = $_POST['transaction_range'];
$account_type = $_POST['gnucash_account_type'];
$accounts_gnucash = array_flip($gnucash_accounts);
// Date (yyyy-mm-dd), Num, Description, Deposit, Account
// checking (check or cash) || credit
// transaction has been 1) paid and is 2) cash & check [checking] or credit and 3) deposited
if( $account_type === 'checking' ) {
$query = "SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, description, amount " .
"FROM transaction_log WHERE paid=1 AND date!='NULL' " .
"AND (payment_type='cash' OR payment_type='check') " .
"AND (transaction_id>" . $transaction_range[0] . " AND transaction_id<" . $transaction_range[1] . ");";
$sql = mysql_query($query, $YBDB) or die(mysql_error());
$gnucash_csv_file = "";
while ( $result = mysql_fetch_assoc($sql) ) {
$description = preg_replace('/\n/', ' \r ', $result['description']);
$description = preg_replace('/\r/', '\r', $description);
$description = preg_replace('/,/', ';', $description);
$gnucash_csv_file .= $result['date'] . ', ' . $result['transaction_id'] .
', (Income:' . $result['transaction_type'] . ') ' . $description . ', ' . $result['amount'] . ', ' .
$accounts_gnucash['checking'] . "\n";
}
$file_name = preg_replace('/ /', '_', $accounts_gnucash['checking']);
$file_name = preg_replace('/:/', '-', $file_name);
$file_name = $file_name . '-' . $transaction_range[0] . '-' . $transaction_range[1] . '.csv';
$file = '../' . $csv_directory . '/' . $file_name;
$csv_file = fopen($file, "w") or die("Unable to open file for writing.");
fwrite($csv_file, $gnucash_csv_file);
fclose($csv_file);
// download file to browser
/*
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
*/
}
if ( $account_type === 'credit' ) {
$query = "SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, description, amount " .
"FROM transaction_log WHERE paid=1 AND date!='NULL' " .
"AND payment_type='credit' " .
"AND (transaction_id>" . $transaction_range[0] . " AND transaction_id<" . $transaction_range[1] . ");";
$sql = mysql_query($query, $YBDB) or die(mysql_error());
$result = mysql_fetch_assoc($sql);
$gnucash_csv_file = "";
while ( $result = mysql_fetch_assoc($sql) ) {
$description = preg_replace('/\n/', ' \r ', $result['description']);
$description = preg_replace('/\r/', '\r', $description);
$description = preg_replace('/,/', ';', $description);
$gnucash_csv_file .= $result['date'] . ', ' . $result['transaction_id'] .
', (Income:' . $result['transaction_type'] . ') ' . $description . ', ' . $result['amount'] . ', ' .
$accounts_gnucash['credit'] . "\n";
}
$file_name = preg_replace('/ /', '_', $accounts_gnucash['credit']);
$file_name = preg_replace('/:/', '-', $file_name);
$file_name = $file_name . '-' . $transaction_range[0] . '-' . $transaction_range[1] . '.csv';
$file = '../' . $csv_directory . '/' . $file_name;
$csv_file = fopen($file, "w") or die("Unable to open file for writing.");
fwrite($csv_file, $gnucash_csv_file);
fclose($csv_file);
}
/*
SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, description, amount
FROM transaction_log WHERE paid=1 AND date!='NULL'
AND (payment_type="cash" OR payment_type="check")
AND (transaction_id>256 AND transaction_id<259);
*/
/*
require_once('../php-console/src/PhpConsole/__autoload.php');
$handler = PhpConsole\Handler::getInstance();
$handler->start();
$handler->debug($transaction_range[0]);
*/
}
} // Create csv file(s) for GnuCash
// Deposit Calculator
if (isset($_POST['deposit'])) {

Loading…
Cancel
Save