From f89c86384ad8fea7be5cd2be9ce665cb5085f1b8 Mon Sep 17 00:00:00 2001 From: Jonathan Rosenbaum Date: Mon, 6 Apr 2015 07:31:42 +0000 Subject: [PATCH] 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! --- Connections/database_functions.php | 1 + js/transaction.js | 2 +- json/transaction.php | 86 +++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/Connections/database_functions.php b/Connections/database_functions.php index 2df2239..debae59 100644 --- a/Connections/database_functions.php +++ b/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 diff --git a/js/transaction.js b/js/transaction.js index 1f17506..8d17ac2 100644 --- a/js/transaction.js +++ b/js/transaction.js @@ -403,7 +403,7 @@ $(function() { $.post("json/transaction.php",{ gnucash_account_type: v, transaction_range: transaction_range}); }); } - }); + }); } diff --git a/json/transaction.php b/json/transaction.php index 246bf1e..c9a6c2c 100644 --- a/json/transaction.php +++ b/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'])) {